cosmetix
[k8-i-v-a-n.git] / src / game / char.cpp
blob006ec295675b3e380ebe28d7ef154eed3e738b24
1 /*
3 * Iter Vehemens ad Necem (IVAN)
4 * Copyright (C) Timo Kiviluoto
5 * Released under the GNU General
6 * Public License
8 * See LICENSING which should be included
9 * along with this file for more details
12 /* Compiled through charset.cpp */
13 #include <time.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <unistd.h>
20 * 0: up-left
21 * 1: up
22 * 2: up-right
23 * 3: left
24 * 4: right
25 * 5: down-left
26 * 6: down
27 * 7: down-right
28 * 8: stand still
30 enum {
31 MDIR_UP_LEFT,
32 MDIR_UP,
33 MDIR_UP_RIGHT,
34 MDIR_LEFT,
35 MDIR_RIGHT,
36 MDIR_DOWN_LEFT,
37 MDIR_DOWN,
38 MDIR_DOWN_RIGHT,
39 MDIR_STAND
43 /* These statedata structs contain functions and values used for handling
44 * states. Remember to update them. All normal states must have
45 * PrintBeginMessage and PrintEndMessage functions and a Description string.
46 * BeginHandler, EndHandler, Handler (called each tick) and IsAllowed are
47 * optional, enter zero if the state doesn't need one. If the SECRET flag
48 * is set, Description is not shown in the panel without magical means.
49 * You can also set some source (SRC_*) and duration (DUR_*) flags, which
50 * control whether the state can be randomly activated in certain situations.
51 * These flags can be found in ivandef.h. RANDOMIZABLE sets all source
52 * & duration flags at once. */
53 struct statedata {
54 const char *Description;
55 int Flags;
56 void (character::*PrintBeginMessage) () const;
57 void (character::*PrintEndMessage) () const;
58 void (character::*BeginHandler) ();
59 void (character::*EndHandler) ();
60 void (character::*Handler) ();
61 truth (character::*IsAllowed) () const;
62 void (character::*SituationDangerModifier) (double &) const;
66 //WARNING! state count and state order MUST be synced with "define.dat"
67 const statedata StateData[STATES] =
70 "Polymorphed",
71 NO_FLAGS,
75 &character::EndPolymorph,
79 }, {
80 "Hasted",
81 RANDOMIZABLE&~(SRC_MUSHROOM|SRC_EVIL),
82 &character::PrintBeginHasteMessage,
83 &character::PrintEndHasteMessage,
89 }, {
90 "Slowed",
91 RANDOMIZABLE&~SRC_GOOD,
92 &character::PrintBeginSlowMessage,
93 &character::PrintEndSlowMessage,
99 }, {
100 "PolyControl",
101 RANDOMIZABLE&~(SRC_MUSHROOM|SRC_EVIL|SRC_GOOD),
102 &character::PrintBeginPolymorphControlMessage,
103 &character::PrintEndPolymorphControlMessage,
109 }, {
110 "LifeSaved",
111 SECRET,
112 &character::PrintBeginLifeSaveMessage,
113 &character::PrintEndLifeSaveMessage,
119 }, {
120 "Lycanthropy",
121 SECRET|SRC_FOUNTAIN|SRC_CONFUSE_READ|DUR_FLAGS,
122 &character::PrintBeginLycanthropyMessage,
123 &character::PrintEndLycanthropyMessage,
126 &character::LycanthropyHandler,
128 &character::LycanthropySituationDangerModifier
129 }, {
130 "Invisible",
131 RANDOMIZABLE&~(SRC_MUSHROOM|SRC_EVIL),
132 &character::PrintBeginInvisibilityMessage,
133 &character::PrintEndInvisibilityMessage,
134 &character::BeginInvisibility,
135 &character::EndInvisibility,
139 }, {
140 "Infravision",
141 RANDOMIZABLE&~(SRC_MUSHROOM|SRC_EVIL),
142 &character::PrintBeginInfraVisionMessage,
143 &character::PrintEndInfraVisionMessage,
144 &character::BeginInfraVision,
145 &character::EndInfraVision,
149 }, {
150 "ESP",
151 RANDOMIZABLE&~SRC_EVIL,
152 &character::PrintBeginESPMessage,
153 &character::PrintEndESPMessage,
154 &character::BeginESP,
155 &character::EndESP,
159 }, {
160 "Poisoned",
161 DUR_TEMPORARY,
162 &character::PrintBeginPoisonedMessage,
163 &character::PrintEndPoisonedMessage,
166 &character::PoisonedHandler,
167 &character::CanBePoisoned,
168 &character::PoisonedSituationDangerModifier
169 }, {
170 "Teleporting",
171 SECRET|(RANDOMIZABLE&~(SRC_MUSHROOM|SRC_GOOD)),
172 &character::PrintBeginTeleportMessage,
173 &character::PrintEndTeleportMessage,
176 &character::TeleportHandler,
179 }, {
180 "Polymorphing",
181 SECRET|(RANDOMIZABLE&~(SRC_MUSHROOM|SRC_GOOD)),
182 &character::PrintBeginPolymorphMessage,
183 &character::PrintEndPolymorphMessage,
186 &character::PolymorphHandler,
188 &character::PolymorphingSituationDangerModifier
189 }, {
190 "TeleControl",
191 RANDOMIZABLE&~(SRC_MUSHROOM|SRC_EVIL),
192 &character::PrintBeginTeleportControlMessage,
193 &character::PrintEndTeleportControlMessage,
199 }, {
200 "Panicked",
201 NO_FLAGS,
202 &character::PrintBeginPanicMessage,
203 &character::PrintEndPanicMessage,
204 &character::BeginPanic,
205 &character::EndPanic,
207 &character::CanPanic,
208 &character::PanicSituationDangerModifier
209 }, {
210 "Confused",
211 SECRET|(RANDOMIZABLE&~(DUR_PERMANENT|SRC_GOOD)),
212 &character::PrintBeginConfuseMessage,
213 &character::PrintEndConfuseMessage,
217 &character::CanBeConfused,
218 &character::ConfusedSituationDangerModifier
219 }, {
220 "Parasitized",
221 SECRET|(RANDOMIZABLE&~DUR_TEMPORARY),
222 &character::PrintBeginParasitizedMessage,
223 &character::PrintEndParasitizedMessage,
226 &character::ParasitizedHandler,
227 &character::CanBeParasitized,
228 &character::ParasitizedSituationDangerModifier
229 }, {
230 "Searching",
231 NO_FLAGS,
232 &character::PrintBeginSearchingMessage,
233 &character::PrintEndSearchingMessage,
236 &character::SearchingHandler,
239 }, {
240 "GasImmunity",
241 SECRET|(RANDOMIZABLE&~(SRC_GOOD|SRC_EVIL)),
242 &character::PrintBeginGasImmunityMessage,
243 &character::PrintEndGasImmunityMessage,
249 }, {
250 "Levitating",
251 RANDOMIZABLE&~SRC_EVIL,
252 &character::PrintBeginLevitationMessage,
253 &character::PrintEndLevitationMessage,
255 &character::EndLevitation,
259 }, {
260 "Leprosy",
261 SECRET|(RANDOMIZABLE&~DUR_TEMPORARY),
262 &character::PrintBeginLeprosyMessage,
263 &character::PrintEndLeprosyMessage,
264 &character::BeginLeprosy,
265 &character::EndLeprosy,
266 &character::LeprosyHandler,
268 &character::LeprosySituationDangerModifier
269 }, {
270 "Hiccups",
271 SRC_FOUNTAIN|SRC_CONFUSE_READ|DUR_FLAGS,
272 &character::PrintBeginHiccupsMessage,
273 &character::PrintEndHiccupsMessage,
276 &character::HiccupsHandler,
278 &character::HiccupsSituationDangerModifier
279 }, {
280 "Vampirism",
281 DUR_FLAGS, //perhaps no fountain, no secret and no confuse read either: SECRET|SRC_FOUNTAIN|SRC_CONFUSE_READ|
282 &character::PrintBeginVampirismMessage,
283 &character::PrintEndVampirismMessage,
286 &character::VampirismHandler,
288 &character::VampirismSituationDangerModifier
289 }, {
290 "Swimming",
291 SECRET,
292 &character::PrintBeginSwimmingMessage,
293 &character::PrintEndSwimmingMessage,
294 &character::BeginSwimming, &character::EndSwimming,
298 }, {
299 "Detecting",
300 SECRET|(RANDOMIZABLE&~(SRC_MUSHROOM|SRC_EVIL)),
301 &character::PrintBeginDetectMessage,
302 &character::PrintEndDetectMessage,
305 &character::DetectHandler,
308 }, {
309 "Ethereal",
310 NO_FLAGS,
311 &character::PrintBeginEtherealityMessage,
312 &character::PrintEndEtherealityMessage,
313 &character::BeginEthereality, &character::EndEthereality,
317 }, {
318 "Fearless",
319 RANDOMIZABLE&~SRC_EVIL,
320 &character::PrintBeginFearlessMessage,
321 &character::PrintEndFearlessMessage,
322 &character::BeginFearless,
323 &character::EndFearless,
327 }, {
328 "PolymorphLocked",
329 SECRET,
330 &character::PrintBeginPolymorphLockMessage,
331 &character::PrintEndPolymorphLockMessage,
334 &character::PolymorphLockHandler,
337 }, {
338 "Regenerating",
339 SECRET|(RANDOMIZABLE&~SRC_EVIL),
340 &character::PrintBeginRegenerationMessage,
341 &character::PrintEndRegenerationMessage,
347 }, {
348 "DiseaseImmunity",
349 SECRET|(RANDOMIZABLE&~SRC_EVIL),
350 &character::PrintBeginDiseaseImmunityMessage,
351 &character::PrintEndDiseaseImmunityMessage,
357 }, {
358 "TeleportLocked",
359 SECRET,
360 &character::PrintBeginTeleportLockMessage,
361 &character::PrintEndTeleportLockMessage,
364 &character::TeleportLockHandler,
371 characterprototype::characterprototype (const characterprototype *Base, characterspawner Spawner,
372 charactercloner Cloner, cchar *ClassID)
373 : Base(Base),
374 Spawner(Spawner),
375 Cloner(Cloner),
376 ClassID(ClassID)
378 Index = protocontainer<character>::Add(this);
382 void character::CreateInitialEquipment (int SpecialFlags) { AddToInventory(DataBase->Inventory, SpecialFlags); }
383 void character::EditAP (sLong What) { AP = Limit<sLong>(AP+What, -12000, 1200); }
384 int character::GetRandomStepperBodyPart () const { return TORSO_INDEX; }
385 void character::GainIntrinsic (sLong What) { BeginTemporaryState(What, PERMANENT); }
386 truth character::IsUsingArms () const { return GetAttackStyle() & USE_ARMS; }
387 truth character::IsUsingLegs () const { return GetAttackStyle() & USE_LEGS; }
388 truth character::IsUsingHead () const { return GetAttackStyle() & USE_HEAD; }
389 void character::CalculateAllowedWeaponSkillCategories () { AllowedWeaponSkillCategories = MARTIAL_SKILL_CATEGORIES; }
390 festring character::GetBeVerb () const { return IsPlayer() ? CONST_S("are") : CONST_S("is"); }
391 void character::SetEndurance (int What) { BaseExperience[ENDURANCE] = What * EXP_MULTIPLIER; }
392 void character::SetPerception (int What) { BaseExperience[PERCEPTION] = What * EXP_MULTIPLIER; }
393 void character::SetIntelligence (int What) { BaseExperience[INTELLIGENCE] = What * EXP_MULTIPLIER; }
394 void character::SetWisdom (int What) { BaseExperience[WISDOM] = What * EXP_MULTIPLIER; }
395 void character::SetWillPower (int What) { BaseExperience[WILL_POWER] = What * EXP_MULTIPLIER; }
396 void character::SetCharisma (int What) { BaseExperience[CHARISMA] = What * EXP_MULTIPLIER; }
397 void character::SetMana (int What) { BaseExperience[MANA] = What * EXP_MULTIPLIER; }
398 truth character::IsOnGround () const { return MotherEntity && MotherEntity->IsOnGround(); }
399 truth character::LeftOversAreUnique () const { return GetArticleMode() || AssignedName.GetSize(); }
400 truth character::HomeDataIsValid () const { return (HomeData && HomeData->Level == GetLSquareUnder()->GetLevelIndex() && HomeData->Dungeon == GetLSquareUnder()->GetDungeonIndex()); }
401 void character::SetHomePos (v2 Pos) { HomeData->Pos = Pos; }
402 cchar *character::FirstPersonUnarmedHitVerb () const { return "hit"; }
403 cchar *character::FirstPersonCriticalUnarmedHitVerb () const { return "critically hit"; }
404 cchar *character::ThirdPersonUnarmedHitVerb () const { return "hits"; }
405 cchar *character::ThirdPersonCriticalUnarmedHitVerb () const { return "critically hits"; }
406 cchar *character::FirstPersonKickVerb () const { return "kick"; }
407 cchar *character::FirstPersonCriticalKickVerb () const { return "critically kick"; }
408 cchar *character::ThirdPersonKickVerb () const { return "kicks"; }
409 cchar *character::ThirdPersonCriticalKickVerb () const { return "critically kicks"; }
410 cchar *character::FirstPersonBiteVerb () const { return "bite"; }
411 cchar *character::FirstPersonCriticalBiteVerb () const { return "critically bite"; }
412 cchar *character::ThirdPersonBiteVerb () const { return "bites"; }
413 cchar *character::ThirdPersonCriticalBiteVerb () const { return "critically bites"; }
414 cchar *character::UnarmedHitNoun () const { return "attack"; }
415 cchar *character::KickNoun () const { return "kick"; }
416 cchar *character::BiteNoun () const { return "attack"; }
417 cchar *character::GetEquipmentName (int) const { return ""; }
418 const std::list<feuLong> &character::GetOriginalBodyPartID (int I) const { return OriginalBodyPartID[I]; }
419 square *character::GetNeighbourSquare (int I) const { return GetSquareUnder()->GetNeighbourSquare(I); }
420 lsquare *character::GetNeighbourLSquare (int I) const { return static_cast<lsquare *>(GetSquareUnder())->GetNeighbourLSquare(I); }
421 //wsquare *character::GetNeighbourWSquare (int I) const { return static_cast<wsquare *>(GetSquareUnder())->GetNeighbourWSquare(I); }
422 god *character::GetMasterGod () const { return game::GetGod(GetConfig()); }
423 col16 character::GetBodyPartColorA (int, truth) const { return GetSkinColor(); }
424 col16 character::GetBodyPartColorB (int, truth) const { return GetTorsoMainColor(); }
425 col16 character::GetBodyPartColorC (int, truth) const { return GetBeltColor(); } // sorry...
426 col16 character::GetBodyPartColorD (int, truth) const { return GetTorsoSpecialColor(); }
427 int character::GetRandomApplyBodyPart () const { return TORSO_INDEX; }
428 truth character::IsPet () const { return GetTeam()->GetID() == PLAYER_TEAM; }
429 character* character::GetLeader () const { return GetTeam()->GetLeader(); }
430 festring character::GetZombieDescription () const { return " of "+GetName(INDEFINITE); }
431 truth character::BodyPartCanBeSevered (int I) const { return I; }
432 truth character::HasBeenSeen () const { return DataBase->Flags & HAS_BEEN_SEEN; }
433 truth character::IsTemporary () const { return GetTorso()->GetLifeExpectancy(); }
434 cchar *character::GetNormalDeathMessage () const { return "killed @k"; }
437 truth character::IsHomeLevel (level *lvl) const {
438 if (!lvl) return false;
439 // check level homes
440 const fearray<festring> &hlist = DataBase->HomeLevel;
441 if (hlist.Size == 0) return false;
442 // has "*"?
443 for (uInt f = 0; f < hlist.Size; ++f) { if (hlist[f] == "*") return true; }
444 // taken from unique characters code
445 cfestring *tag = lvl->GetLevelScript()->GetTag();
446 if (!tag) return false; // not a possible home level
447 // check for home level
448 for (uInt f = 0; f < hlist.Size; ++f) { if (hlist[f] == *tag) return true; }
449 return false;
453 truth character::MustBeRemovedFromBone () const {
454 if (IsUnique() && !CanBeGenerated()) return true;
455 // check level homes
456 const fearray<festring> &hlist = DataBase->HomeLevel;
457 if (hlist.Size == 0) return false;
458 // has "*"?
459 for (uInt f = 0; f < hlist.Size; ++f) { if (hlist[f] == "*") return true; }
460 // taken from unique characters code
461 if (!IsEnabled() || GetTeam()->GetID() != DataBase->NaturalTeam) return true;
462 return IsHomeLevel(GetLevel());
466 int character::GetMoveType () const {// return (!StateIsActivated(LEVITATION) ? DataBase->MoveType : DataBase->MoveType | FLY); }
467 return
468 (!StateIsActivated(LEVITATION) ? DataBase->MoveType : DataBase->MoveType|FLY)|
469 (!StateIsActivated(ETHEREAL_MOVING) ? DataBase->MoveType : DataBase->MoveType|ETHEREAL)|
470 (!StateIsActivated(SWIMMING) ? DataBase->MoveType : DataBase->MoveType|WALK|SWIM)|
476 int characterdatabase::*ExpPtr[ATTRIBUTES] = {
477 &characterdatabase::DefaultEndurance,
478 &characterdatabase::DefaultPerception,
479 &characterdatabase::DefaultIntelligence,
480 &characterdatabase::DefaultWisdom,
481 &characterdatabase::DefaultWillPower,
482 &characterdatabase::DefaultCharisma,
483 &characterdatabase::DefaultMana,
484 &characterdatabase::DefaultArmStrength,
485 &characterdatabase::DefaultLegStrength,
486 &characterdatabase::DefaultDexterity,
487 &characterdatabase::DefaultAgility
491 contentscript<item> characterdatabase::*EquipmentDataPtr[EQUIPMENT_DATAS] = {
492 &characterdatabase::Helmet,
493 &characterdatabase::Amulet,
494 &characterdatabase::Cloak,
495 &characterdatabase::BodyArmor,
496 &characterdatabase::Belt,
497 &characterdatabase::RightWielded,
498 &characterdatabase::LeftWielded,
499 &characterdatabase::RightRing,
500 &characterdatabase::LeftRing,
501 &characterdatabase::RightGauntlet,
502 &characterdatabase::LeftGauntlet,
503 &characterdatabase::RightBoot,
504 &characterdatabase::LeftBoot
508 character::character (ccharacter &Char) :
509 entity(Char), id(Char), NP(Char.NP), AP(Char.AP),
510 TemporaryState(Char.TemporaryState&~POLYMORPHED),
511 Team(Char.Team), GoingTo(ERROR_V2), Money(0),
512 AssignedName(Char.AssignedName), Action(0),
513 DataBase(Char.DataBase), MotherEntity(0),
514 PolymorphBackup(0), EquipmentState(0), SquareUnder(0),
515 AllowedWeaponSkillCategories(Char.AllowedWeaponSkillCategories),
516 BodyParts(Char.BodyParts),
517 RegenerationCounter(Char.RegenerationCounter),
518 SquaresUnder(Char.SquaresUnder), LastAcidMsgMin(0),
519 Stamina(Char.Stamina), MaxStamina(Char.MaxStamina),
520 BlocksSinceLastTurn(0), GenerationDanger(Char.GenerationDanger),
521 CommandFlags(Char.CommandFlags), WarnFlags(0),
522 ScienceTalks(Char.ScienceTalks), TrapData(0), CounterToMindWormHatch(0)
524 int c;
525 Flags &= ~C_PLAYER;
526 Flags |= C_INITIALIZING|C_IN_NO_MSG_MODE;
527 Stack = new stack(0, this, HIDDEN);
528 for (c = 0; c < STATES; ++c) TemporaryStateCounter[c] = Char.TemporaryStateCounter[c];
529 if (Team) Team->Add(this);
530 for (c = 0; c < BASE_ATTRIBUTES; ++c) BaseExperience[c] = Char.BaseExperience[c];
531 BodyPartSlot = new bodypartslot[BodyParts];
532 OriginalBodyPartID = new std::list<feuLong>[BodyParts];
533 CWeaponSkill = new cweaponskill[AllowedWeaponSkillCategories];
534 SquareUnder = new square *[SquaresUnder];
535 if (SquaresUnder == 1) *SquareUnder = 0; else memset(SquareUnder, 0, SquaresUnder*sizeof(square *));
536 for (c = 0; c < BodyParts; ++c) {
537 BodyPartSlot[c].SetMaster(this);
538 bodypart *CharBodyPart = Char.GetBodyPart(c);
539 OriginalBodyPartID[c] = Char.OriginalBodyPartID[c];
540 if (CharBodyPart) {
541 bodypart *BodyPart = static_cast<bodypart *>(CharBodyPart->Duplicate());
542 SetBodyPart(c, BodyPart);
543 BodyPart->CalculateEmitation();
546 for (c = 0; c < AllowedWeaponSkillCategories; ++c) CWeaponSkill[c] = Char.CWeaponSkill[c];
547 HomeData = Char.HomeData ? new homedata(*Char.HomeData) : 0;
548 ID = game::CreateNewCharacterID(this);
552 character::character () :
553 entity(HAS_BE), NP(50000), AP(0), TemporaryState(0), Team(0),
554 GoingTo(ERROR_V2), Money(0), Action(0), MotherEntity(0),
555 PolymorphBackup(0), EquipmentState(0), SquareUnder(0),
556 RegenerationCounter(0), HomeData(0), LastAcidMsgMin(0),
557 BlocksSinceLastTurn(0), GenerationDanger(DEFAULT_GENERATION_DANGER),
558 WarnFlags(0), ScienceTalks(0), TrapData(0), CounterToMindWormHatch(0)
560 Stack = new stack(0, this, HIDDEN);
564 character::~character () {
565 if (Action) delete Action;
566 if (Team) Team->Remove(this);
567 delete Stack;
568 for (int c = 0; c < BodyParts; ++c) delete GetBodyPart(c);
569 delete [] BodyPartSlot;
570 delete [] OriginalBodyPartID;
571 delete PolymorphBackup;
572 delete [] SquareUnder;
573 delete [] CWeaponSkill;
574 delete HomeData;
575 deleteList(TrapData);
576 game::RemoveCharacterID(ID);
580 void character::Hunger () {
581 auto bst = GetBurdenState();
582 if (bst == OVER_LOADED || bst == STRESSED) {
583 EditNP(-8);
584 EditExperience(LEG_STRENGTH, 150, 1 << 2);
585 EditExperience(AGILITY, -50, 1 << 2);
586 } else if (bst == BURDENED) {
587 EditNP(-2);
588 EditExperience(LEG_STRENGTH, 75, 1 << 1);
589 EditExperience(AGILITY, -25, 1 << 1);
590 } else if (bst == UNBURDENED) {
591 EditNP(-1);
594 auto hst = GetHungerState();
595 if (hst == STARVING) {
596 EditExperience(ARM_STRENGTH, -75, 1 << 3);
597 EditExperience(LEG_STRENGTH, -75, 1 << 3);
598 } else if (hst == VERY_HUNGRY) {
599 EditExperience(ARM_STRENGTH, -50, 1 << 2);
600 EditExperience(LEG_STRENGTH, -50, 1 << 2);
601 } else if (hst == HUNGRY) {
602 EditExperience(ARM_STRENGTH, -25, 1 << 1);
603 EditExperience(LEG_STRENGTH, -25, 1 << 1);
604 } else if (hst == SATIATED) {
605 EditExperience(AGILITY, -25, 1 << 1);
606 } else if (hst == BLOATED) {
607 EditExperience(AGILITY, -50, 1 << 2);
608 } else if (hst == OVER_FED) {
609 EditExperience(AGILITY, -75, 1 << 3);
612 CheckStarvationDeath(CONST_S("starved to death"));
616 int character::TakeHit (character *Enemy, item *Weapon, bodypart *EnemyBodyPart, v2 HitPos, double Damage,
617 double ToHitValue, int Success, int Type, int GivenDir, truth Critical, truth ForceHit)
619 //FIXME: args
620 if (!game::RunCharEvent("before_take_hit", this, Enemy, Weapon)) return DID_NO_DAMAGE;
622 int Dir = (Type == BITE_ATTACK ? YOURSELF : GivenDir);
623 double DodgeValue = GetDodgeValue();
624 if (!Enemy->IsPlayer() && GetAttackWisdomLimit() != NO_LIMIT) Enemy->EditExperience(WISDOM, 75, 1 << 13);
625 if (!Enemy->CanBeSeenBy(this)) ToHitValue *= 2;
626 if (!CanBeSeenBy(Enemy)) DodgeValue *= 2;
627 if (Enemy->StateIsActivated(CONFUSED)) ToHitValue *= 0.75;
629 switch (Enemy->GetTirednessState()) {
630 case FAINTING:
631 ToHitValue *= 0.50;
632 case EXHAUSTED:
633 ToHitValue *= 0.75;
635 switch (GetTirednessState()) {
636 case FAINTING:
637 DodgeValue *= 0.50;
638 case EXHAUSTED:
639 DodgeValue *= 0.75;
642 if (!ForceHit) {
643 if (!IsRetreating()) SetGoingTo(Enemy->GetPos());
644 else SetGoingTo(GetPos()-((Enemy->GetPos()-GetPos())<<4));
645 if (!Enemy->IsRetreating()) Enemy->SetGoingTo(GetPos());
646 else Enemy->SetGoingTo(Enemy->GetPos()-((GetPos()-Enemy->GetPos())<<4));
649 /* Effectively, the average chance to hit is 100% / (DV/THV + 1). */
650 if (RAND() % int(100+ToHitValue/DodgeValue*(100+Success)) < 100 && !Critical && !ForceHit) {
651 Enemy->AddMissMessage(this);
652 EditExperience(AGILITY, 150, 1 << 7);
653 EditExperience(PERCEPTION, 75, 1 << 7);
654 if (Enemy->CanBeSeenByPlayer())
655 DeActivateVoluntaryAction(CONST_S("The attack of ")+Enemy->GetName(DEFINITE)+CONST_S(" interrupts you."));
656 else
657 DeActivateVoluntaryAction(CONST_S("The attack interrupts you."));
658 return HAS_DODGED;
661 int TrueDamage = int(Damage*(100+Success)/100)+(RAND()%3 ? 1 : 0);
662 if (Critical) {
663 TrueDamage += TrueDamage >> 1;
664 ++TrueDamage;
667 int BodyPart = ChooseBodyPartToReceiveHit(ToHitValue, DodgeValue);
668 if (Critical) {
669 if (Type == UNARMED_ATTACK) Enemy->AddPrimitiveHitMessage(this, Enemy->FirstPersonCriticalUnarmedHitVerb(), Enemy->ThirdPersonCriticalUnarmedHitVerb(), BodyPart);
670 else if (Type == WEAPON_ATTACK) Enemy->AddWeaponHitMessage(this, Weapon, BodyPart, true);
671 else if (Type == KICK_ATTACK) Enemy->AddPrimitiveHitMessage(this, Enemy->FirstPersonCriticalKickVerb(), Enemy->ThirdPersonCriticalKickVerb(), BodyPart);
672 else if (Type == BITE_ATTACK) Enemy->AddPrimitiveHitMessage(this, Enemy->FirstPersonCriticalBiteVerb(), Enemy->ThirdPersonCriticalBiteVerb(), BodyPart);
673 } else {
674 if (Type == UNARMED_ATTACK) Enemy->AddPrimitiveHitMessage(this, Enemy->FirstPersonUnarmedHitVerb(), Enemy->ThirdPersonUnarmedHitVerb(), BodyPart);
675 else if (Type == WEAPON_ATTACK) Enemy->AddWeaponHitMessage(this, Weapon, BodyPart, false);
676 else if (Type == KICK_ATTACK) Enemy->AddPrimitiveHitMessage(this, Enemy->FirstPersonKickVerb(), Enemy->ThirdPersonKickVerb(), BodyPart);
677 else if (Type == BITE_ATTACK) Enemy->AddPrimitiveHitMessage(this, Enemy->FirstPersonBiteVerb(), Enemy->ThirdPersonBiteVerb(), BodyPart);
680 if (!Critical && TrueDamage && Enemy->AttackIsBlockable(Type)) {
681 TrueDamage = CheckForBlock(Enemy, Weapon, ToHitValue, TrueDamage, Success, Type);
682 if (!TrueDamage || (Weapon && !Weapon->Exists())) {
683 if (Enemy->CanBeSeenByPlayer())
684 DeActivateVoluntaryAction(CONST_S("The attack of ")+Enemy->GetName(DEFINITE)+CONST_S(" interrupts you."));
685 else
686 DeActivateVoluntaryAction(CONST_S("The attack interrupts you."));
687 return HAS_BLOCKED;
691 int WeaponSkillHits = CalculateWeaponSkillHits(Enemy);
692 int DoneDamage = ReceiveBodyPartDamage(Enemy, TrueDamage, PHYSICAL_DAMAGE, BodyPart, Dir, false, Critical, true, Type == BITE_ATTACK && Enemy->BiteCapturesBodyPart());
693 truth Succeeded = (GetBodyPart(BodyPart) && HitEffect(Enemy, Weapon, HitPos, Type, BodyPart, Dir, !DoneDamage, Critical, DoneDamage)) || DoneDamage;
694 if (Succeeded) Enemy->WeaponSkillHit(Weapon, Type, WeaponSkillHits);
696 if (Weapon) {
697 if (Weapon->Exists() && DoneDamage < TrueDamage) Weapon->ReceiveDamage(Enemy, TrueDamage-DoneDamage, PHYSICAL_DAMAGE);
698 if (Weapon->Exists() && DoneDamage && SpillsBlood() && GetBodyPart(BodyPart) &&
699 (GetBodyPart(BodyPart)->IsAlive() || GetBodyPart(BodyPart)->GetMainMaterial()->IsLiquid()))
700 Weapon->SpillFluid(0, CreateBlood(15+RAND()%15));
703 if (Enemy->AttackIsBlockable(Type)) SpecialBodyDefenceEffect(Enemy, EnemyBodyPart, Type);
705 if (!Succeeded) {
706 if (Enemy->CanBeSeenByPlayer())
707 DeActivateVoluntaryAction(CONST_S("The attack of ")+Enemy->GetName(DEFINITE)+CONST_S(" interrupts you."));
708 else
709 DeActivateVoluntaryAction(CONST_S("The attack interrupts you."));
711 return DID_NO_DAMAGE;
714 if (CheckDeath(GetNormalDeathMessage(), Enemy, Enemy->IsPlayer() ? FORCE_MSG : 0)) return HAS_DIED;
716 if (Enemy->CanBeSeenByPlayer())
717 DeActivateVoluntaryAction(CONST_S("The attack of ")+Enemy->GetName(DEFINITE)+CONST_S(" interrupts you."));
718 else
719 DeActivateVoluntaryAction(CONST_S("The attack interrupts you."));
721 return HAS_HIT;
725 struct svpriorityelement {
726 svpriorityelement (int BodyPart, int StrengthValue) : BodyPart(BodyPart), StrengthValue(StrengthValue) {}
727 bool operator < (const svpriorityelement &AnotherPair) const { return StrengthValue > AnotherPair.StrengthValue; }
728 int BodyPart;
729 int StrengthValue;
733 int character::ChooseBodyPartToReceiveHit (double ToHitValue, double DodgeValue) {
734 if (BodyParts == 1) return 0;
735 std::priority_queue<svpriorityelement> SVQueue;
736 for (int c = 0; c < BodyParts; ++c) {
737 bodypart *BodyPart = GetBodyPart(c);
738 if (BodyPart && (BodyPart->GetHP() != 1 || BodyPart->CanBeSevered(PHYSICAL_DAMAGE)))
739 SVQueue.push(svpriorityelement(c, ModifyBodyPartHitPreference(c, BodyPart->GetStrengthValue()+BodyPart->GetHP())));
741 while (SVQueue.size()) {
742 svpriorityelement E = SVQueue.top();
743 int ToHitPercentage = int(GLOBAL_WEAK_BODYPART_HIT_MODIFIER*ToHitValue*GetBodyPart(E.BodyPart)->GetBodyPartVolume()/(DodgeValue*GetBodyVolume()));
744 ToHitPercentage = ModifyBodyPartToHitChance(E.BodyPart, ToHitPercentage);
745 if (ToHitPercentage < 1) ToHitPercentage = 1;
746 else if (ToHitPercentage > 95) ToHitPercentage = 95;
747 if (ToHitPercentage > RAND()%100) return E.BodyPart;
748 SVQueue.pop();
750 return 0;
754 void character::Be () {
755 if (game::ForceJumpToPlayerBe()) {
756 if (!IsPlayer()) return;
757 game::SetForceJumpToPlayerBe(false);
758 } else {
759 truth ForceBe = HP != MaxHP || AllowSpoil();
760 for (int c = 0; c < BodyParts; ++c) {
761 bodypart *BodyPart = GetBodyPart(c);
762 if (BodyPart && (ForceBe || BodyPart->NeedsBe())) BodyPart->Be();
764 HandleStates();
765 if (!IsEnabled()) return;
766 if (GetTeam() == PLAYER->GetTeam()) {
767 for (int c = 0; c < AllowedWeaponSkillCategories; ++c) {
768 if (CWeaponSkill[c].Tick() && IsPlayer()) CWeaponSkill[c].AddLevelDownMessage(c);
770 SWeaponSkillTick();
772 if (IsPlayer()) {
773 if (GetHungerState() == STARVING && !(RAND()%50)) LoseConsciousness(250+RAND_N(250), true);
774 if (!Action || Action->AllowFoodConsumption()) Hunger();
776 if (Stamina != MaxStamina) RegenerateStamina();
777 if (HP != MaxHP || StateIsActivated(REGENERATION)) Regenerate();
778 if (Action && AP >= 1000) ActionAutoTermination();
779 if (Action && AP >= 1000) {
780 Action->Handle();
781 if (!IsEnabled()) return;
782 } else {
783 EditAP(GetStateAPGain(100));
786 if (AP >= 1000) {
787 SpecialTurnHandler();
788 BlocksSinceLastTurn = 0;
789 if (IsPlayer()) {
790 static int Timer = 0;
792 if (ivanconfig::GetAutoSaveInterval() && !GetAction() && ++Timer >= ivanconfig::GetAutoSaveInterval()) {
793 //fprintf(stderr, "autosaving..."); fflush(stderr);
794 game::Save(game::GetAutoSaveFileName());
795 //fprintf(stderr, "done\n"); fflush(stderr);
796 Timer = 0;
798 game::CalculateNextDanger();
799 if (!StateIsActivated(POLYMORPHED)) game::UpdatePlayerAttributeAverage();
800 if (!game::IsInWilderness()) Search(GetAttribute(PERCEPTION));
801 if (!Action) {
802 GetPlayerCommand();
803 } else {
804 if (Action->ShowEnvironment()) {
805 static int Counter = 0;
806 if (++Counter == 10) {
807 game::DrawEverything();
808 Counter = 0;
811 msgsystem::ThyMessagesAreNowOld();
812 if (Action->IsVoluntary() && READ_KEY()) Action->Terminate(false);
814 } else {
815 if (!Action && !game::IsInWilderness()) GetAICommand();
821 void character::Move (v2 MoveTo, truth TeleportMove, truth Run) {
822 if (!IsEnabled()) return;
823 /* Test whether the player is stuck to something */
824 if (!TeleportMove && !TryToUnStickTraps(MoveTo-GetPos())) return;
825 if (Run && !IsPlayer() && TorsoIsAlive() &&
826 (Stamina <= 10000/Max(GetAttribute(LEG_STRENGTH), 1) || (!StateIsActivated(PANIC) && Stamina < MaxStamina>>2))) {
827 Run = false;
829 RemoveTraps();
830 if (GetBurdenState() != OVER_LOADED || TeleportMove) {
831 lsquare *OldSquareUnder[MAX_SQUARES_UNDER];
833 if (!game::IsInWilderness()) {
834 if (IsPlayer()) {
835 // idiotic code!
836 area *ca = GetSquareUnder()->GetArea();
838 for (int f = 0; f < MDIR_STAND; ++f) {
839 v2 np = GetPos()+game::GetMoveVector(f);
841 if (np.X >= 0 && np.Y >= 0 && np.X < ca->GetXSize() && np.Y < ca->GetYSize()) {
842 lsquare *sq = static_cast<lsquare *>(ca->GetSquare(np.X, np.Y));
844 sq->SetGoSeen(true);
849 for (int c = 0; c < GetSquaresUnder(); ++c) OldSquareUnder[c] = GetLSquareUnder(c);
851 Remove();
852 PutTo(MoveTo);
853 if (!TeleportMove) {
854 /* Multitiled creatures should behave differently, maybe? */
855 if (Run) {
856 int ED = GetSquareUnder()->GetEntryDifficulty(), Base = 10000;
858 EditAP(-GetMoveAPRequirement(ED)>>1);
859 EditNP(-24*ED);
860 EditExperience(AGILITY, 125, ED<<7);
861 if (IsPlayer()) {
862 auto hst = GetHungerState();
863 if (hst == SATIATED) Base = 11000;
864 else if (hst == BLOATED) Base = 12500;
865 else if (hst == OVER_FED) Base = 15000;
867 EditStamina(-Base/Max(GetAttribute(LEG_STRENGTH), 1), true);
868 } else {
869 int ED = GetSquareUnder()->GetEntryDifficulty();
871 EditAP(-GetMoveAPRequirement(ED));
872 EditNP(-12*ED);
873 EditExperience(AGILITY, 75, ED<<7);
876 if (IsPlayer()) ShowNewPosInfo();
877 if (IsPlayer() && !game::IsInWilderness()) GetStackUnder()->SetSteppedOn(true);
878 if (!game::IsInWilderness()) SignalStepFrom(OldSquareUnder);
879 } else {
880 if (IsPlayer()) {
881 cchar *CrawlVerb = StateIsActivated(LEVITATION) ? "float" : "crawl";
883 ADD_MESSAGE("You try very hard to %s forward. But your load is too heavy.", CrawlVerb);
885 EditAP(-1000);
890 void character::GetAICommand () {
891 SeekLeader(GetLeader());
892 if (FollowLeader(GetLeader())) return;
893 if (CheckForEnemies(true, true, true)) return;
894 if (CheckForUsefulItemsOnGround()) return;
895 if (CheckForDoors()) return;
896 if (CheckSadism()) return;
897 if (MoveRandomly()) return;
898 EditAP(-1000);
902 truth character::MoveTowardsTarget (truth Run) {
903 v2 MoveTo[3];
904 v2 TPos;
905 v2 Pos = GetPos();
907 if (!Route.empty()) {
908 TPos = Route.back();
909 Route.pop_back();
910 } else TPos = GoingTo;
912 MoveTo[0] = v2(0, 0);
913 MoveTo[1] = v2(0, 0);
914 MoveTo[2] = v2(0, 0);
916 if (TPos.X < Pos.X) {
917 if (TPos.Y < Pos.Y) {
918 MoveTo[0] = v2(-1, -1);
919 MoveTo[1] = v2(-1, 0);
920 MoveTo[2] = v2( 0, -1);
921 } else if (TPos.Y == Pos.Y) {
922 MoveTo[0] = v2(-1, 0);
923 MoveTo[1] = v2(-1, -1);
924 MoveTo[2] = v2(-1, 1);
925 } else if (TPos.Y > Pos.Y) {
926 MoveTo[0] = v2(-1, 1);
927 MoveTo[1] = v2(-1, 0);
928 MoveTo[2] = v2( 0, 1);
930 } else if (TPos.X == Pos.X) {
931 if (TPos.Y < Pos.Y) {
932 MoveTo[0] = v2( 0, -1);
933 MoveTo[1] = v2(-1, -1);
934 MoveTo[2] = v2( 1, -1);
935 } else if (TPos.Y == Pos.Y) {
936 TerminateGoingTo();
937 return false;
938 } else if (TPos.Y > Pos.Y) {
939 MoveTo[0] = v2( 0, 1);
940 MoveTo[1] = v2(-1, 1);
941 MoveTo[2] = v2( 1, 1);
943 } else if (TPos.X > Pos.X) {
944 if (TPos.Y < Pos.Y) {
945 MoveTo[0] = v2(1, -1);
946 MoveTo[1] = v2(1, 0);
947 MoveTo[2] = v2(0, -1);
948 } else if (TPos.Y == Pos.Y) {
949 MoveTo[0] = v2(1, 0);
950 MoveTo[1] = v2(1, -1);
951 MoveTo[2] = v2(1, 1);
952 } else if (TPos.Y > Pos.Y) {
953 MoveTo[0] = v2(1, 1);
954 MoveTo[1] = v2(1, 0);
955 MoveTo[2] = v2(0, 1);
959 v2 ModifiedMoveTo = ApplyStateModification(MoveTo[0]);
961 if (TryMove(ModifiedMoveTo, true, Run)) return true;
963 int L = (Pos-TPos).GetManhattanLength();
965 if (RAND()&1) Swap(MoveTo[1], MoveTo[2]);
967 if (Pos.IsAdjacent(TPos)) {
968 TerminateGoingTo();
969 return false;
972 if ((Pos+MoveTo[1]-TPos).GetManhattanLength() <= L && TryMove(ApplyStateModification(MoveTo[1]), true, Run)) return true;
973 if ((Pos+MoveTo[2]-TPos).GetManhattanLength() <= L && TryMove(ApplyStateModification(MoveTo[2]), true, Run)) return true;
974 Illegal.insert(Pos+ModifiedMoveTo);
975 if (CreateRoute()) return true;
976 return false;
980 int character::CalculateNewSquaresUnder (lsquare **NewSquare, v2 Pos) const {
981 if (GetLevel()->IsValidPos(Pos)) {
982 *NewSquare = GetNearLSquare(Pos);
983 return 1;
985 return 0;
989 truth character::TryMove (v2 MoveVector, truth Important, truth Run) {
990 lsquare *MoveToSquare[MAX_SQUARES_UNDER];
991 character *Pet[MAX_SQUARES_UNDER];
992 character *Neutral[MAX_SQUARES_UNDER];
993 character *Hostile[MAX_SQUARES_UNDER];
994 v2 PetPos[MAX_SQUARES_UNDER];
995 v2 NeutralPos[MAX_SQUARES_UNDER];
996 v2 HostilePos[MAX_SQUARES_UNDER];
997 v2 MoveTo = GetPos()+MoveVector;
998 int Direction = game::GetDirectionForVector(MoveVector);
999 if (Direction == DIR_ERROR) ABORT("Direction fault.");
1000 if (!game::IsInWilderness()) {
1001 int Squares = CalculateNewSquaresUnder(MoveToSquare, MoveTo);
1002 if (Squares) {
1003 int Pets = 0;
1004 int Neutrals = 0;
1005 int Hostiles = 0;
1006 for (int c = 0; c < Squares; ++c) {
1007 character* Char = MoveToSquare[c]->GetCharacter();
1008 if (Char && Char != this) {
1009 v2 Pos = MoveToSquare[c]->GetPos();
1010 if (IsAlly(Char)) {
1011 Pet[Pets] = Char;
1012 PetPos[Pets++] = Pos;
1013 } else if (Char->GetRelation(this) != HOSTILE) {
1014 Neutral[Neutrals] = Char;
1015 NeutralPos[Neutrals++] = Pos;
1016 } else {
1017 Hostile[Hostiles] = Char;
1018 HostilePos[Hostiles++] = Pos;
1023 if (Hostiles == 1) return Hit(Hostile[0], HostilePos[0], Direction);
1024 if (Hostiles) {
1025 int Index = RAND() % Hostiles;
1026 return Hit(Hostile[Index], HostilePos[Index], Direction);
1029 if (Neutrals == 1) {
1030 if (!IsPlayer() && !Pets && Important && CanMoveOn(MoveToSquare[0]))
1031 return HandleCharacterBlockingTheWay(Neutral[0], NeutralPos[0], Direction);
1032 else
1033 return IsPlayer() && Hit(Neutral[0], NeutralPos[0], Direction);
1034 } else if (Neutrals) {
1035 if (IsPlayer()) {
1036 int Index = RAND() % Neutrals;
1037 return Hit(Neutral[Index], NeutralPos[Index], Direction);
1039 return false;
1042 if (!IsPlayer()) {
1043 for (int c = 0; c < Squares; ++c) if (MoveToSquare[c]->IsScary(this)) return false;
1046 if (Pets == 1) {
1047 if (IsPlayer() && !ivanconfig::GetBeNice() && Pet[0]->IsMasochist() && HasSadistAttackMode() &&
1048 game::TruthQuestion("Do you want to punish " + Pet[0]->GetObjectPronoun() + "?"))
1049 return Hit(Pet[0], PetPos[0], Direction, SADIST_HIT);
1050 else
1051 return (Important && (CanMoveOn(MoveToSquare[0]) ||
1052 (IsPlayer() && game::GoThroughWallsCheatIsActive())) && Displace(Pet[0]));
1053 } else if (Pets) return false;
1055 if ((CanMove() && CanMoveOn(MoveToSquare[0])) || ((game::GoThroughWallsCheatIsActive() && IsPlayer()))) {
1056 Move(MoveTo, false, Run);
1057 if (IsEnabled() && GetPos() == GoingTo) TerminateGoingTo();
1058 return true;
1059 } else {
1060 for (int c = 0; c < Squares; ++c) {
1061 olterrain *Terrain = MoveToSquare[c]->GetOLTerrain();
1062 if (Terrain && Terrain->CanBeOpened()) {
1063 if (CanOpen()) {
1064 if (Terrain->IsLocked()) {
1065 if (IsPlayer()) {
1066 /*k8*/
1067 if (ivanconfig::GetKickDownDoors()) {
1068 if (game::TruthQuestion(CONST_S("Locked! Do you want to kick ")+Terrain->GetName(DEFINITE)+"?", true, game::GetMoveCommandKeyBetweenPoints(PLAYER->GetPos(), MoveToSquare[0]->GetPos()))) {
1069 Kick(MoveToSquare[c], Direction);
1070 return true;
1071 } else {
1072 return false;
1075 /*k8*/
1076 ADD_MESSAGE("The %s is locked.", Terrain->GetNameSingular().CStr()); /* not sure if this is better than "the door is locked", but I guess it _might_ be slighltly better */
1077 return false;
1078 } else if (Important && CheckKick()) {
1079 room *Room = MoveToSquare[c]->GetRoom();
1080 if (!Room || Room->AllowKick(this, MoveToSquare[c])) {
1081 int HP = Terrain->GetHP();
1082 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s kicks %s.", CHAR_NAME(DEFINITE), Terrain->CHAR_NAME(DEFINITE));
1083 Kick(MoveToSquare[c], Direction);
1084 olterrain *NewTerrain = MoveToSquare[c]->GetOLTerrain();
1085 if (NewTerrain == Terrain && Terrain->GetHP() == HP) { // BUG!
1086 Illegal.insert(MoveTo);
1087 CreateRoute();
1089 return true;
1092 } else { /* if (Terrain->IsLocked()) */
1093 /*if(!IsPlayer() || game::TruthQuestion(CONST_S("Do you want to open ")+Terrain->GetName(DEFINITE)+"?", false, game::GetMoveCommandKeyBetweenPoints(PLAYER->GetPos(), MoveToSquare[0]->GetPos()))) return MoveToSquare[c]->Open(this);*/
1094 /* Non-players always try to open it */
1095 if (!IsPlayer()) return MoveToSquare[c]->Open(this);
1096 if (game::TruthQuestion(CONST_S("Do you want to open ")+Terrain->GetName(DEFINITE)+"?", false, game::GetMoveCommandKeyBetweenPoints(PLAYER->GetPos(), MoveToSquare[0]->GetPos()))) {
1097 return MoveToSquare[c]->Open(this);
1099 return false;
1100 } /* if (Terrain->IsLocked()) */
1101 } else { /* if (CanOpen()) */
1102 if (IsPlayer()) {
1103 ADD_MESSAGE("This monster type cannot open doors.");
1104 return false;
1106 if (Important) {
1107 Illegal.insert(MoveTo);
1108 return CreateRoute();
1110 } /* if (CanOpen()) */
1111 } /* if (Terrain && Terrain->CanBeOpened()) */
1112 } /* for */
1113 } /* if */
1114 return false;
1115 } else {
1116 if (IsPlayer() && !IsStuck() && GetLevel()->IsOnGround() && game::TruthQuestion(CONST_S("Do you want to leave ")+game::GetCurrentDungeon()->GetLevelDescription(game::GetCurrentLevelIndex())+"?")) {
1117 if (HasPetrussNut() && !HasGoldenEagleShirt()) {
1118 game::TextScreen(CONST_S("An undead and sinister voice greets you as you leave the city behind:\n\n\"MoRtAl! ThOu HaSt SlAuGtHeReD pEtRuS aNd PlEaSeD mE!\nfRoM tHiS dAy On, ThOu ArT tHe DeArEsT sErVaNt Of AlL eViL!\"\n\nYou are victorious!"));
1119 game::GetCurrentArea()->SendNewDrawRequest();
1120 game::DrawEverything();
1121 ShowAdventureInfo();
1122 festring Msg = CONST_S("killed Petrus and became the Avatar of Chaos");
1123 PLAYER->AddScoreEntry(Msg, 3, false);
1124 game::End(Msg);
1125 return true;
1127 if (game::TryTravel(WORLD_MAP, WORLD_MAP, game::GetCurrentDungeonIndex())) return true;
1129 return false;
1131 } else {
1132 /** No multitile support */
1133 if (CanMove() && GetArea()->IsValidPos(MoveTo) && (CanMoveOn(GetNearWSquare(MoveTo)) || game::GoThroughWallsCheatIsActive())) {
1134 if (!game::GoThroughWallsCheatIsActive()) {
1135 charactervector &V = game::GetWorldMap()->GetPlayerGroup();
1136 truth Discard = false;
1137 for (uInt c = 0; c < V.size(); ++c) {
1138 if (!V[c]->CanMoveOn(GetNearWSquare(MoveTo))) {
1139 if (!Discard) {
1140 ADD_MESSAGE("One or more of your team members cannot cross this terrain.");
1141 if (!game::TruthQuestion("Discard them?")) return false;
1142 Discard = true;
1144 if (Discard) delete V[c];
1145 V.erase(V.begin() + c--);
1149 Move(MoveTo, false);
1150 return true;
1151 } else {
1152 return false;
1158 void character::CreateCorpse (lsquare *Square) {
1159 if (!BodyPartsDisappearWhenSevered() && !game::AllBodyPartsVanish()) {
1160 corpse *Corpse = corpse::Spawn(0, NO_MATERIALS);
1161 Corpse->SetDeceased(this);
1162 Square->AddItem(Corpse);
1163 Disable();
1164 } else {
1165 SendToHell();
1170 void character::Die (character *Killer, cfestring &Msg, feuLong DeathFlags) {
1171 /* Note: This function musn't delete any objects, since one of these may be
1172 the one currently processed by pool::Be()! */
1173 if (!IsEnabled()) return;
1175 if (!game::RunCharEvent("before_die", this, Killer)) return;
1177 RemoveTraps();
1178 if (IsPlayer()) {
1179 ADD_MESSAGE("You die.");
1180 game::DrawEverything();
1181 if (game::TruthQuestion(CONST_S("Do you want to save screenshot?"), REQUIRES_ANSWER)) {
1182 festring dir;
1183 #ifdef LOCAL_SAVES
1184 dir << ivanconfig::GetMyDir() << "/save";
1185 mkdir(dir.CStr(), 0755);
1186 #else
1187 dir << getenv("HOME") << "/.ivan-save";
1188 mkdir(dir.CStr(), 0755);
1189 #endif
1190 dir << "/deathshots";
1191 mkdir(dir.CStr(), 0755);
1192 festring timestr;
1193 time_t t = time(NULL);
1194 struct tm *ts = localtime(&t);
1195 if (ts) {
1196 timestr << (int)(ts->tm_year%100);
1197 int t = ts->tm_mon+1;
1198 if (t < 10) timestr << '0'; timestr << t;
1199 t = ts->tm_mday; if (t < 10) timestr << '0'; timestr << t;
1200 timestr << '_';
1201 t = ts->tm_hour; if (t < 10) timestr << '0'; timestr << t;
1202 t = ts->tm_min; if (t < 10) timestr << '0'; timestr << t;
1203 t = ts->tm_sec; if (t < 10) timestr << '0'; timestr << t;
1204 } else {
1205 timestr = "heh";
1207 #if defined(HAVE_IMLIB2) || defined(HAVE_LIBPNG)
1208 festring ext = ".png";
1209 #else
1210 festring ext = ".bmp";
1211 #endif
1212 festring fname = dir+"/deathshot_"+timestr;
1213 if (inputfile::fileExists(fname+ext)) {
1214 for (int f = 0; f < 1000; f++) {
1215 char buf[16];
1216 sprintf(buf, "%03d", f);
1217 festring fn = fname+buf;
1218 if (!inputfile::fileExists(fn+ext)) {
1219 fname = fn;
1220 break;
1224 fname << ext;
1225 fprintf(stderr, "deathshot: %s\n", fname.CStr());
1226 #if defined(HAVE_IMLIB2) || defined(HAVE_LIBPNG)
1227 DOUBLE_BUFFER->SavePNG(fname);
1228 #else
1229 DOUBLE_BUFFER->SaveBMP(fname);
1230 #endif
1232 if (game::WizardModeIsActive()) {
1233 game::DrawEverything();
1234 if (!game::TruthQuestion(CONST_S("Do you want to do this, cheater?"), REQUIRES_ANSWER)) {
1235 RestoreBodyParts();
1236 ResetSpoiling();
1237 RestoreHP();
1238 RestoreStamina();
1239 ResetStates();
1240 SetNP(SATIATED_LEVEL);
1241 SendNewDrawRequest();
1242 return;
1245 } else if (CanBeSeenByPlayer() && !(DeathFlags & DISALLOW_MSG)) {
1246 ProcessAndAddMessage(GetDeathMessage());
1247 } else if (DeathFlags & FORCE_MSG) {
1248 ADD_MESSAGE("You sense the death of something.");
1251 if (!(DeathFlags & FORBID_REINCARNATION)) {
1252 if (StateIsActivated(LIFE_SAVED) && CanMoveOn(!game::IsInWilderness() ? GetSquareUnder() : PLAYER->GetSquareUnder())) {
1253 SaveLife();
1254 return;
1256 if (SpecialSaveLife()) return;
1257 } else if (StateIsActivated(LIFE_SAVED)) {
1258 RemoveLifeSavers();
1261 Flags |= C_IN_NO_MSG_MODE;
1262 character *Ghost = 0;
1263 if (IsPlayer()) {
1264 game::RemoveSaves();
1265 if (!game::IsInWilderness()) {
1266 Ghost = game::CreateGhost();
1267 Ghost->Disable();
1271 square *SquareUnder[MAX_SQUARES_UNDER];
1272 memset(SquareUnder, 0, sizeof(SquareUnder));
1273 Disable();
1274 if (IsPlayer() || !game::IsInWilderness()) {
1275 for (int c = 0; c < SquaresUnder; ++c) SquareUnder[c] = GetSquareUnder(c);
1276 Remove();
1277 } else {
1278 charactervector& V = game::GetWorldMap()->GetPlayerGroup();
1279 V.erase(std::find(V.begin(), V.end(), this));
1281 //lsquare **LSquareUnder = reinterpret_cast<lsquare **>(SquareUnder); /* warning; wtf? */
1282 lsquare *LSquareUnder[MAX_SQUARES_UNDER];
1283 memmove(LSquareUnder, SquareUnder, sizeof(SquareUnder));
1285 if (!game::IsInWilderness()) {
1286 if (!StateIsActivated(POLYMORPHED)) {
1287 if (!IsPlayer() && !IsTemporary() && !Msg.IsEmpty()) game::SignalDeath(this, Killer, Msg);
1288 if (!(DeathFlags & DISALLOW_CORPSE)) CreateCorpse(LSquareUnder[0]); else SendToHell();
1289 } else {
1290 if (!IsPlayer() && !IsTemporary() && !Msg.IsEmpty()) game::SignalDeath(GetPolymorphBackup(), Killer, Msg);
1291 GetPolymorphBackup()->CreateCorpse(LSquareUnder[0]);
1292 GetPolymorphBackup()->Flags &= ~C_POLYMORPHED;
1293 SetPolymorphBackup(0);
1294 SendToHell();
1296 } else {
1297 if (!IsPlayer() && !IsTemporary() && !Msg.IsEmpty()) game::SignalDeath(this, Killer, Msg);
1298 SendToHell();
1301 if (IsPlayer()) {
1302 if (!game::IsInWilderness()) {
1303 for (int c = 0; c < GetSquaresUnder(); ++c) LSquareUnder[c]->SetTemporaryEmitation(GetEmitation());
1305 ShowAdventureInfo();
1306 if (!game::IsInWilderness()) {
1307 for(int c = 0; c < GetSquaresUnder(); ++c) LSquareUnder[c]->SetTemporaryEmitation(0);
1311 if (!game::IsInWilderness()) {
1312 if (GetSquaresUnder() == 1) {
1313 stack *StackUnder = LSquareUnder[0]->GetStack();
1314 GetStack()->MoveItemsTo(StackUnder);
1315 doforbodypartswithparam<stack*>()(this, &bodypart::DropEquipment, StackUnder);
1316 } else {
1317 while (GetStack()->GetItems()) GetStack()->GetBottom()->MoveTo(LSquareUnder[RAND_N(GetSquaresUnder())]->GetStack());
1318 for (int c = 0; c < BodyParts; ++c) {
1319 bodypart *BodyPart = GetBodyPart(c);
1320 if (BodyPart) BodyPart->DropEquipment(LSquareUnder[RAND_N(GetSquaresUnder())]->GetStack());
1325 if (GetTeam()->GetLeader() == this) GetTeam()->SetLeader(0);
1327 Flags &= ~C_IN_NO_MSG_MODE;
1329 if (IsPlayer()) {
1330 AddScoreEntry(Msg);
1331 if (!game::IsInWilderness()) {
1332 Ghost->PutTo(LSquareUnder[0]->GetPos());
1333 Ghost->Enable();
1334 game::CreateBone();
1336 game::TextScreen(CONST_S("Unfortunately you died."), ZERO_V2, WHITE, true, true, &game::ShowDeathSmiley);
1337 game::End(Msg);
1342 void character::AddMissMessage (ccharacter *Enemy) const {
1343 festring Msg;
1344 if (Enemy->IsPlayer()) Msg = GetDescription(DEFINITE)+" misses you!";
1345 else if (IsPlayer()) Msg = CONST_S("You miss ")+Enemy->GetDescription(DEFINITE)+'!';
1346 else if (CanBeSeenByPlayer() || Enemy->CanBeSeenByPlayer()) Msg = GetDescription(DEFINITE)+" misses "+Enemy->GetDescription(DEFINITE)+'!';
1347 else return;
1348 ADD_MESSAGE("%s", Msg.CStr());
1352 void character::AddBlockMessage (ccharacter *Enemy, citem *Blocker, cfestring &HitNoun, truth Partial) const {
1353 festring Msg;
1354 festring BlockVerb = (Partial ? " to partially block the " : " to block the ")+HitNoun;
1355 if (IsPlayer()) {
1356 Msg << "You manage" << BlockVerb << " with your " << Blocker->GetName(UNARTICLED) << '!';
1357 } else if (Enemy->IsPlayer() || Enemy->CanBeSeenByPlayer()) {
1358 if (CanBeSeenByPlayer())
1359 Msg << GetName(DEFINITE) << " manages" << BlockVerb << " with " << GetPossessivePronoun() << ' ' << Blocker->GetName(UNARTICLED) << '!';
1360 else
1361 Msg << "Something manages" << BlockVerb << " with something!";
1362 } else {
1363 return;
1365 ADD_MESSAGE("%s", Msg.CStr());
1369 void character::AddPrimitiveHitMessage (ccharacter *Enemy, cfestring &FirstPersonHitVerb,
1370 cfestring &ThirdPersonHitVerb, int BodyPart) const
1372 festring Msg;
1373 festring BodyPartDescription;
1374 if (BodyPart && (Enemy->CanBeSeenByPlayer() || Enemy->IsPlayer()))
1375 BodyPartDescription << " in the " << Enemy->GetBodyPartName(BodyPart);
1376 if (Enemy->IsPlayer())
1377 Msg << GetDescription(DEFINITE) << ' ' << ThirdPersonHitVerb << " you" << BodyPartDescription << '!';
1378 else if (IsPlayer())
1379 Msg << "You " << FirstPersonHitVerb << ' ' << Enemy->GetDescription(DEFINITE) << BodyPartDescription << '!';
1380 else if (CanBeSeenByPlayer() || Enemy->CanBeSeenByPlayer())
1381 Msg << GetDescription(DEFINITE) << ' ' << ThirdPersonHitVerb << ' ' << Enemy->GetDescription(DEFINITE) + BodyPartDescription << '!';
1382 else
1383 return;
1384 ADD_MESSAGE("%s", Msg.CStr());
1388 cchar *const HitVerb[] = { "strike", "slash", "stab" };
1389 cchar *const HitVerb3rdPersonEnd[] = { "s", "es", "s" };
1392 void character::AddWeaponHitMessage (ccharacter *Enemy, citem *Weapon, int BodyPart, truth Critical) const {
1393 festring Msg;
1394 festring BodyPartDescription;
1396 if (BodyPart && (Enemy->CanBeSeenByPlayer() || Enemy->IsPlayer()))
1397 BodyPartDescription << " in the " << Enemy->GetBodyPartName(BodyPart);
1399 int FittingTypes = 0;
1400 int DamageFlags = Weapon->GetDamageFlags();
1401 int DamageType = 0;
1403 for (int c = 0; c < DAMAGE_TYPES; ++c) {
1404 if (1 << c & DamageFlags) {
1405 if (!FittingTypes || !RAND_N(FittingTypes+1)) DamageType = c;
1406 ++FittingTypes;
1410 if (!FittingTypes) ABORT("No damage flags specified for %s!", Weapon->CHAR_NAME(UNARTICLED));
1412 festring NewHitVerb = Critical ? " critically " : " ";
1413 NewHitVerb << HitVerb[DamageType];
1414 cchar *const E = HitVerb3rdPersonEnd[DamageType];
1416 if (Enemy->IsPlayer()) {
1417 Msg << GetDescription(DEFINITE) << NewHitVerb << E << " you" << BodyPartDescription;
1418 if (CanBeSeenByPlayer()) Msg << " with " << GetPossessivePronoun() << ' ' << Weapon->GetName(UNARTICLED);
1419 Msg << '!';
1420 } else if (IsPlayer()) {
1421 Msg << "You" << NewHitVerb << ' ' << Enemy->GetDescription(DEFINITE) << BodyPartDescription << '!';
1422 } else if(CanBeSeenByPlayer() || Enemy->CanBeSeenByPlayer()) {
1423 Msg << GetDescription(DEFINITE) << NewHitVerb << E << ' ' << Enemy->GetDescription(DEFINITE) << BodyPartDescription;
1424 if (CanBeSeenByPlayer()) Msg << " with " << GetPossessivePronoun() << ' ' << Weapon->GetName(UNARTICLED);
1425 Msg << '!';
1426 } else {
1427 return;
1429 ADD_MESSAGE("%s", Msg.CStr());
1433 item *character::GeneralFindItem (ItemCheckerCB chk) const {
1434 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) {
1435 item *it = *i;
1436 if (it && chk(it)) return it;
1438 return 0;
1442 static truth isEncryptedScroll (item *i) { return i->IsEncryptedScroll(); }
1443 truth character::HasEncryptedScroll () const {
1444 if (GeneralFindItem(::isEncryptedScroll)) return true;
1445 return combineequipmentpredicates()(this, &item::IsEncryptedScroll, 1);
1449 static truth isElpuriHead (item *i) { return i->IsHeadOfElpuri(); }
1450 truth character::HasHeadOfElpuri () const {
1451 if (GeneralFindItem(::isElpuriHead)) return true;
1452 return combineequipmentpredicates()(this, &item::IsHeadOfElpuri, 1);
1456 static truth isPetrussNut (item *i) { return i->IsPetrussNut(); }
1457 truth character::HasPetrussNut () const {
1458 if (GeneralFindItem(::isPetrussNut)) return true;
1459 return combineequipmentpredicates()(this, &item::IsPetrussNut, 1);
1463 static truth isGoldenEagleShirt (item *i) { return i->IsGoldenEagleShirt(); }
1464 truth character::HasGoldenEagleShirt () const {
1465 if (GeneralFindItem(::isGoldenEagleShirt)) return true;
1466 return combineequipmentpredicates()(this, &item::IsGoldenEagleShirt, 1);
1470 static truth isShadowVeil (item *i) { return i->IsShadowVeil(); }
1471 truth character::HasShadowVeil () const {
1472 if (GeneralFindItem(::isShadowVeil)) return true;
1473 return combineequipmentpredicates()(this, &item::IsShadowVeil, 1);
1477 static truth isLostRubyFlamingSword (item *i) { return i->IsLostRubyFlamingSword(); }
1478 truth character::HasLostRubyFlamingSword () const {
1479 if (GeneralFindItem(::isLostRubyFlamingSword)) return true;
1480 return combineequipmentpredicates()(this, &item::IsLostRubyFlamingSword, 1);
1484 truth character::HasOmmelBlood () const {
1485 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i)
1486 if (i->IsKleinBottle() && i->GetSecondaryMaterial() && i->GetSecondaryMaterial()->GetConfig() == OMMEL_BLOOD) return true;
1488 for (int c = 0; c < GetEquipments(); ++c) {
1489 item *Item = GetEquipment(c);
1491 if (Item && Item->IsKleinBottle() && Item->GetSecondaryMaterial() && Item->GetSecondaryMaterial()->GetConfig() == OMMEL_BLOOD) return true;
1493 return false; //combineequipmentpredicates()(this, &item::IsKleinBottle, 1);
1497 truth character::HasCurdledBlood () const {
1498 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i)
1499 if (i->IsKleinBottle() && i->GetSecondaryMaterial() && i->GetSecondaryMaterial()->GetConfig() == CURDLED_OMMEL_BLOOD) return true;
1501 for (int c = 0; c < GetEquipments(); ++c) {
1502 item *Item = GetEquipment(c);
1504 if (Item && Item->IsKleinBottle() && Item->GetSecondaryMaterial() && Item->GetSecondaryMaterial()->GetConfig() == CURDLED_OMMEL_BLOOD) return true;
1506 return false; //combineequipmentpredicates()(this, &item::IsKleinBottle, 1);
1510 truth character::CurdleOmmelBlood () const {
1511 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) {
1512 if (i->IsKleinBottle() && i->GetSecondaryMaterial() && i->GetSecondaryMaterial()->GetConfig() == OMMEL_BLOOD) {
1513 i->ChangeSecondaryMaterial(MAKE_MATERIAL(CURDLED_OMMEL_BLOOD));
1514 return true;
1518 for (int c = 0; c < GetEquipments(); ++c) {
1519 item *Item = GetEquipment(c);
1521 if (Item && Item->IsKleinBottle() && Item->GetSecondaryMaterial() && Item->GetSecondaryMaterial()->GetConfig() == OMMEL_BLOOD) {
1522 Item->ChangeSecondaryMaterial(MAKE_MATERIAL(CURDLED_OMMEL_BLOOD));
1523 return true;
1526 return false; //combineequipmentpredicates()(this, &item::IsKleinBottle, 1);
1530 truth character::RemoveCurdledOmmelBlood () {
1531 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) {
1532 if (i->IsKleinBottle() && i->GetSecondaryMaterial() && i->GetSecondaryMaterial()->GetConfig() == CURDLED_OMMEL_BLOOD) {
1533 (*i)->RemoveFromSlot();
1534 (*i)->SendToHell();
1535 return true;
1539 for (int c = 0; c < GetEquipments(); ++c) {
1540 item *Item = GetEquipment(c);
1542 if (Item && Item->IsKleinBottle() && Item->GetSecondaryMaterial() && Item->GetSecondaryMaterial()->GetConfig() == CURDLED_OMMEL_BLOOD) {
1543 Item->RemoveFromSlot();
1544 Item->SendToHell();
1545 return true;
1548 return false;
1552 int character::GeneralRemoveItem (ItemCheckerCB chk, truth allItems) {
1553 truth done;
1554 int cnt = 0;
1555 // inventory
1556 do {
1557 done = true;
1558 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) {
1559 item *Item = *i;
1560 if (Item && chk(Item)) {
1561 Item->RemoveFromSlot();
1562 Item->SendToHell();
1563 cnt++;
1564 if (!allItems) return cnt;
1565 done = false;
1566 break;
1569 } while (!done);
1570 // equipments
1571 do {
1572 done = true;
1573 for (int c = 0; c < GetEquipments(); ++c) {
1574 item *Item = GetEquipment(c);
1575 if (Item && chk(Item)) {
1576 Item->RemoveFromSlot();
1577 Item->SendToHell();
1578 cnt++;
1579 if (!allItems) return cnt;
1580 done = false;
1581 break;
1584 } while (!done);
1585 return cnt;
1589 //static truth isEncryptedScroll (item *i) { return i->IsEncryptedScroll(); }
1590 truth character::RemoveEncryptedScroll () { return GeneralRemoveItem(::isEncryptedScroll) != 0; }
1593 static truth isMondedrPass (item *i) { return i->IsMondedrPass(); }
1594 truth character::RemoveMondedrPass () { return GeneralRemoveItem(::isMondedrPass) != 0; }
1597 static truth isRingOfThieves (item *i) { return i->IsRingOfThieves(); }
1598 truth character::RemoveRingOfThieves () { return GeneralRemoveItem(::isRingOfThieves) != 0; }
1601 truth character::RemoveShadowVeil () { return (GeneralRemoveItem(::isShadowVeil) != 0); }
1604 truth character::ReadItem (item *ToBeRead) {
1605 if (!ToBeRead->CanBeRead(this)) {
1606 if (IsPlayer()) ADD_MESSAGE("You can't read this.");
1607 return false;
1609 if (!GetLSquareUnder()->IsDark() || game::GetSeeWholeMapCheatMode()) {
1610 if (StateIsActivated(CONFUSED) && !(RAND()&7)) {
1611 if (!ToBeRead->IsDestroyable(this)) {
1612 ADD_MESSAGE("You read some words of %s and understand exactly nothing.", ToBeRead->CHAR_NAME(DEFINITE));
1613 } else {
1614 ADD_MESSAGE("%s is very confusing. Or perhaps you are just too confused?", ToBeRead->CHAR_NAME(DEFINITE));
1615 ActivateRandomState(SRC_CONFUSE_READ, 1000+RAND()%1500);
1616 ToBeRead->RemoveFromSlot();
1617 ToBeRead->SendToHell();
1619 EditAP(-1000);
1620 return true;
1622 if (ToBeRead->Read(this)) {
1623 if (!game::WizardModeIsActive()) {
1624 /* This AP is used to take the stuff out of backpack */
1625 DexterityAction(5);
1627 return true;
1629 return false;
1631 if (IsPlayer()) ADD_MESSAGE("It's too dark here to read.");
1632 return false;
1636 void character::CalculateBurdenState () {
1637 int OldBurdenState = BurdenState;
1638 sLong SumOfMasses = GetCarriedWeight();
1639 sLong CarryingStrengthUnits = sLong(GetCarryingStrength())*2500;
1640 if (SumOfMasses > (CarryingStrengthUnits << 1) + CarryingStrengthUnits) BurdenState = OVER_LOADED;
1641 else if (SumOfMasses > CarryingStrengthUnits << 1) BurdenState = STRESSED;
1642 else if (SumOfMasses > CarryingStrengthUnits) BurdenState = BURDENED;
1643 else BurdenState = UNBURDENED;
1644 if (!IsInitializing() && BurdenState != OldBurdenState) CalculateBattleInfo();
1648 void character::Save (outputfile &SaveFile) const {
1649 SaveFile << (uShort)GetType();
1650 Stack->Save(SaveFile);
1651 SaveFile << ID;
1652 for (int c = 0; c < BASE_ATTRIBUTES; ++c) SaveFile << BaseExperience[c];
1654 SaveFile << ExpModifierMap;
1655 SaveFile << NP << AP << Stamina << GenerationDanger << ScienceTalks << CounterToMindWormHatch;
1656 SaveFile << TemporaryState << EquipmentState << Money << GoingTo << RegenerationCounter << Route << Illegal;
1657 SaveFile << CurrentSweatMaterial;
1658 SaveFile.Put(!!IsEnabled());
1659 SaveFile << HomeData << BlocksSinceLastTurn << CommandFlags;
1660 SaveFile << WarnFlags << (uShort)Flags;
1662 for (int c = 0; c < BodyParts; ++c) SaveFile << BodyPartSlot[c] << OriginalBodyPartID[c];
1664 SaveLinkedList(SaveFile, TrapData);
1665 SaveFile << Action;
1667 for (int c = 0; c < STATES; ++c) SaveFile << TemporaryStateCounter[c];
1669 if (GetTeam()) {
1670 SaveFile.Put(true);
1671 SaveFile << Team->GetID(); // feuLong
1672 } else {
1673 SaveFile.Put(false);
1676 if (GetTeam() && GetTeam()->GetLeader() == this) SaveFile.Put(true); else SaveFile.Put(false);
1678 SaveFile << AssignedName << PolymorphBackup;
1680 for (int c = 0; c < AllowedWeaponSkillCategories; ++c) SaveFile << CWeaponSkill[c];
1682 SaveFile << (uShort)GetConfig();
1686 void character::Load (inputfile &SaveFile) {
1687 LoadSquaresUnder();
1688 Stack->Load(SaveFile);
1689 SaveFile >> ID;
1690 game::AddCharacterID(this, ID);
1692 for (int c = 0; c < BASE_ATTRIBUTES; ++c) SaveFile >> BaseExperience[c];
1694 SaveFile >> ExpModifierMap;
1695 SaveFile >> NP >> AP >> Stamina >> GenerationDanger >> ScienceTalks >> CounterToMindWormHatch;
1696 SaveFile >> TemporaryState >> EquipmentState >> Money >> GoingTo >> RegenerationCounter >> Route >> Illegal;
1697 SaveFile >> CurrentSweatMaterial;
1699 if (!SaveFile.Get()) Disable();
1701 SaveFile >> HomeData >> BlocksSinceLastTurn >> CommandFlags;
1702 SaveFile >> WarnFlags;
1703 WarnFlags &= ~WARNED;
1704 Flags |= ReadType(uShort, SaveFile) & ~ENTITY_FLAGS;
1706 for (int c = 0; c < BodyParts; ++c) {
1707 SaveFile >> BodyPartSlot[c] >> OriginalBodyPartID[c];
1708 item *BodyPart = *BodyPartSlot[c];
1709 if (BodyPart) BodyPart->Disable();
1712 LoadLinkedList(SaveFile, TrapData);
1713 SaveFile >> Action;
1715 if (Action) Action->SetActor(this);
1717 for (int c = 0; c < STATES; ++c) SaveFile >> TemporaryStateCounter[c];
1719 if (SaveFile.Get()) SetTeam(game::GetTeam(ReadType(feuLong, SaveFile)));
1721 if (SaveFile.Get()) GetTeam()->SetLeader(this);
1723 SaveFile >> AssignedName >> PolymorphBackup;
1725 for (int c = 0; c < AllowedWeaponSkillCategories; ++c) SaveFile >> CWeaponSkill[c];
1727 databasecreator<character>::InstallDataBase(this, ReadType(uShort, SaveFile));
1729 if (IsEnabled() && !game::IsInWilderness()) {
1730 for (int c = 1; c < GetSquaresUnder(); ++c) GetSquareUnder(c)->SetCharacter(this);
1734 const fearray<festring> &lt = GetLevelTags();
1735 if (lt.Size > 1) {
1736 fprintf(stderr, "====\n");
1737 for (uInt f = 0; f < lt.Size; ++f) fprintf(stderr, " %u: [%s]\n", f, lt[f].CStr());
1743 truth character::Engrave (cfestring &What) {
1744 GetLSquareUnder()->Engrave(What);
1745 return true;
1748 truth character::MoveRandomly () {
1749 if (!IsEnabled()) return false;
1750 for (int c = 0; c < 10; ++c) {
1751 v2 ToTry = game::GetMoveVector(RAND()&7);
1752 if (GetLevel()->IsValidPos(GetPos()+ToTry)) {
1753 lsquare *Square = GetNearLSquare(GetPos()+ToTry);
1754 if (!Square->IsDangerous(this) && !Square->IsScary(this) && TryMove(ToTry, false, false)) return true;
1757 return false;
1761 truth character::TestForPickup (item *ToBeTested) const {
1762 if (MakesBurdened(ToBeTested->GetWeight()+GetCarriedWeight())) return false;
1763 return true;
1767 void character::AddScoreEntry (cfestring &Description, double Multiplier, truth AddEndLevel) const {
1768 if (!game::WizardModeIsReallyActive()) {
1769 highscore HScore;
1770 if (!HScore.CheckVersion()) {
1771 if (game::Menu(0, v2(RES.X >> 1, RES.Y >> 1), CONST_S("The highscore version doesn't match.\rDo you want to erase previous records and start a new file?\rNote, if you answer no, the score of your current game will be lost!\r"), CONST_S("Yes\rNo\r"), LIGHT_GRAY)) return;
1772 HScore.Clear();
1774 festring Desc = game::GetPlayerName();
1775 Desc << ", " << Description;
1776 if (AddEndLevel) Desc << " in "+(game::IsInWilderness() ? "the world map" : game::GetCurrentDungeon()->GetLevelDescription(game::GetCurrentLevelIndex()));
1777 HScore.Add(sLong(game::GetScore()*Multiplier), Desc);
1778 HScore.Save();
1783 truth character::CheckDeath (cfestring &Msg, character *Murderer, feuLong DeathFlags) {
1784 if (!IsEnabled()) return true;
1785 if (game::IsSumoWrestling() && IsDead()) {
1786 game::EndSumoWrestling(!!IsPlayer());
1787 return true;
1789 if (DeathFlags & FORCE_DEATH || IsDead()) {
1790 if (Murderer && Murderer->IsPlayer() && GetTeam()->GetKillEvilness()) game::DoEvilDeed(GetTeam()->GetKillEvilness());
1791 festring SpecifierMsg;
1792 int SpecifierParts = 0;
1793 if (GetPolymorphBackup()) {
1794 SpecifierMsg << " polymorphed into ";
1795 id::AddName(SpecifierMsg, INDEFINITE);
1796 ++SpecifierParts;
1798 if (!(DeathFlags & IGNORE_TRAPS) && IsStuck()) {
1799 if (SpecifierParts++) SpecifierMsg << " and";
1800 SpecifierMsg << " caught in " << GetTrapDescription();
1802 if (GetAction() && !(DeathFlags & IGNORE_UNCONSCIOUSNESS && GetAction()->IsUnconsciousness())) {
1803 festring ActionMsg = GetAction()->GetDeathExplanation();
1804 if (!ActionMsg.IsEmpty()) {
1805 if (SpecifierParts > 1) {
1806 SpecifierMsg = ActionMsg << ',' << SpecifierMsg;
1807 } else {
1808 if (SpecifierParts) SpecifierMsg << " and";
1809 SpecifierMsg << ActionMsg;
1811 ++SpecifierParts;
1814 festring NewMsg = Msg;
1815 if (Murderer == this) {
1816 SEARCH_N_REPLACE(NewMsg, "@bkp", CONST_S("by ") + GetPossessivePronoun(false) + " own");
1817 SEARCH_N_REPLACE(NewMsg, "@bk", CONST_S("by ") + GetObjectPronoun(false) + "self");
1818 SEARCH_N_REPLACE(NewMsg, "@k", GetObjectPronoun(false) + "self");
1819 } else {
1820 SEARCH_N_REPLACE(NewMsg, "@bkp", CONST_S("by ") + Murderer->GetName(INDEFINITE) + "'s");
1821 SEARCH_N_REPLACE(NewMsg, "@bk", CONST_S("by ") + Murderer->GetName(INDEFINITE));
1822 SEARCH_N_REPLACE(NewMsg, "@k", CONST_S("by ") + Murderer->GetName(INDEFINITE));
1824 if (SpecifierParts) NewMsg << " while" << SpecifierMsg;
1825 if (IsPlayer() && game::WizardModeIsActive()) ADD_MESSAGE("Death message: %s. Score: %d.", NewMsg.CStr(), game::GetScore());
1826 Die(Murderer, NewMsg, DeathFlags);
1827 return true;
1829 return false;
1833 truth character::CheckStarvationDeath (cfestring &Msg) {
1834 if (GetNP() < 1 && UsesNutrition()) return CheckDeath(Msg, 0, FORCE_DEATH);
1835 return false;
1839 void character::ThrowItem (int Direction, item *ToBeThrown) {
1840 if (Direction > 7) ABORT("Throw in TOO odd direction...");
1841 ToBeThrown->Fly(this, Direction, GetAttribute(ARM_STRENGTH));
1845 void character::HasBeenHitByItem (character *Thrower, item *Thingy, int Damage, double ToHitValue, int Direction) {
1846 if (IsPlayer()) ADD_MESSAGE("%s hits you.", Thingy->CHAR_NAME(DEFINITE));
1847 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s hits %s.", Thingy->CHAR_NAME(DEFINITE), CHAR_NAME(DEFINITE));
1848 int BodyPart = ChooseBodyPartToReceiveHit(ToHitValue, DodgeValue);
1849 int WeaponSkillHits = Thrower ? CalculateWeaponSkillHits(Thrower) : 0;
1850 int DoneDamage = ReceiveBodyPartDamage(Thrower, Damage, PHYSICAL_DAMAGE, BodyPart, Direction);
1851 truth Succeeded = (GetBodyPart(BodyPart) && HitEffect(Thrower, Thingy, Thingy->GetPos(), THROW_ATTACK, BodyPart, Direction, !DoneDamage, false, DoneDamage)) || DoneDamage;
1852 if (Succeeded && Thrower) Thrower->WeaponSkillHit(Thingy, THROW_ATTACK, WeaponSkillHits);
1853 festring DeathMsg = CONST_S("killed by a flying ")+Thingy->GetName(UNARTICLED);
1854 if (CheckDeath(DeathMsg, Thrower)) return;
1855 if (Thrower) {
1856 if (Thrower->CanBeSeenByPlayer())
1857 DeActivateVoluntaryAction(CONST_S("The attack of ")+Thrower->GetName(DEFINITE)+CONST_S(" interrupts you."));
1858 else
1859 DeActivateVoluntaryAction(CONST_S("The attack interrupts you."));
1860 } else {
1861 DeActivateVoluntaryAction(CONST_S("The hit interrupts you."));
1866 truth character::DodgesFlyingItem (item *Item, double ToHitValue) {
1867 return !Item->EffectIsGood() && RAND() % int(100+ToHitValue/DodgeValue*100) < 100;
1871 void character::GetPlayerCommand () {
1872 command *cmd;
1873 truth HasActed = false;
1874 while (!HasActed) {
1875 game::DrawEverything();
1876 if (game::GetDangerFound() && !StateIsActivated(FEARLESS)) {
1877 if (game::GetDangerFound() > 500.) {
1878 if (game::GetCausePanicFlag()) {
1879 game::SetCausePanicFlag(false);
1880 BeginTemporaryState(PANIC, 500+RAND_N(500));
1882 game::AskForEscPress(CONST_S("You are horrified by your situation!"));
1883 } else if (ivanconfig::GetWarnAboutDanger()) {
1884 if (game::GetDangerFound() > 50.) game::AskForEscPress(CONST_S("You sense great danger!"));
1885 else game::AskForEscPress(CONST_S("You sense danger!"));
1887 game::SetDangerFound(0);
1889 game::SetIsInGetCommand(true);
1890 int Key = GET_KEY();
1891 game::SetIsInGetCommand(false);
1892 if (Key != '+' && Key != '-' && Key != 'M') msgsystem::ThyMessagesAreNowOld(); // gum
1893 truth ValidKeyPressed = false;
1895 for (int c = 0; c < DIRECTION_COMMAND_KEYS; ++c) {
1896 if (Key == game::GetMoveCommandKey(c)) {
1897 HasActed = TryMove(ApplyStateModification(game::GetMoveVector(c)), true, game::PlayerIsRunning());
1898 ValidKeyPressed = true;
1902 if (!ValidKeyPressed) {
1903 for (int c = 0; (cmd = commandsystem::GetCommand(c)); ++c) {
1904 /* k8 */
1905 /* Numpad aliases for most commonly used commands */
1906 if (Key == KEY_DEL && cmd->GetName() == "Eat") Key = cmd->GetKey();
1907 if (Key == KEY_INS && cmd->GetName() == "PickUp") Key = cmd->GetKey();
1908 if (Key == KEY_PLUS && cmd->GetName() == "EquipmentScreen") Key = cmd->GetKey();
1909 /* k8 */
1910 if (Key == cmd->GetKey()) {
1911 if (game::IsInWilderness() && !commandsystem::GetCommand(c)->IsUsableInWilderness()) {
1912 ADD_MESSAGE("This function cannot be used while in wilderness.");
1913 } else if (!game::WizardModeIsActive() && commandsystem::GetCommand(c)->IsWizardModeFunction()) {
1914 ADD_MESSAGE("Activate wizardmode to use this function.");
1915 } else {
1916 HasActed = commandsystem::GetCommand(c)->GetLinkedFunction()(this);
1918 ValidKeyPressed = true;
1919 break;
1923 if (!ValidKeyPressed) ADD_MESSAGE("Unknown key. Press '?' for a list of commands.");
1925 game::IncreaseTurn();
1929 void character::Vomit (v2 Pos, int Amount, truth ShowMsg) {
1930 if (!CanVomit()) return;
1931 if (ShowMsg) {
1932 if (IsPlayer()) ADD_MESSAGE("You vomit.");
1933 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s vomits.", CHAR_NAME(DEFINITE));
1935 if (VomittingIsUnhealthy()) {
1936 EditExperience(ARM_STRENGTH, -75, 1 << 9);
1937 EditExperience(LEG_STRENGTH, -75, 1 << 9);
1939 if (IsPlayer()) {
1940 EditNP(-2500-RAND()%2501);
1941 CheckStarvationDeath(CONST_S("vomited himself to death"));
1943 if (StateIsActivated(PARASITIZED) && !(RAND() & 7)) {
1944 if (IsPlayer()) ADD_MESSAGE("You notice a dead broad tapeworm among your former stomach contents.");
1945 DeActivateTemporaryState(PARASITIZED);
1947 if (!game::IsInWilderness()) {
1948 GetNearLSquare(Pos)->ReceiveVomit(this, liquid::Spawn(GetVomitMaterial(), sLong(sqrt(GetBodyVolume())*Amount/1000)));
1953 truth character::Polymorph (character *NewForm, int Counter) {
1954 if (!IsPolymorphable() || (!IsPlayer() && game::IsInWilderness())) {
1955 delete NewForm;
1956 return false;
1958 RemoveTraps();
1959 if (GetAction()) GetAction()->Terminate(false);
1960 NewForm->SetAssignedName("");
1961 if (IsPlayer())
1962 ADD_MESSAGE("Your body glows in a crimson light. You transform into %s!", NewForm->CHAR_NAME(INDEFINITE));
1963 else if (CanBeSeenByPlayer())
1964 ADD_MESSAGE("%s glows in a crimson light and %s transforms into %s!", CHAR_NAME(DEFINITE), GetPersonalPronoun().CStr(), NewForm->CHAR_NAME(INDEFINITE));
1966 Flags |= C_IN_NO_MSG_MODE;
1967 NewForm->Flags |= C_IN_NO_MSG_MODE;
1968 NewForm->ChangeTeam(GetTeam());
1969 NewForm->GenerationDanger = GenerationDanger;
1970 NewForm->mOnEvents = this->mOnEvents;
1972 if (GetTeam()->GetLeader() == this) GetTeam()->SetLeader(NewForm);
1974 v2 Pos = GetPos();
1975 Remove();
1976 NewForm->PutToOrNear(Pos);
1977 NewForm->SetAssignedName(GetAssignedName());
1978 NewForm->ActivateTemporaryState(POLYMORPHED);
1979 NewForm->SetTemporaryStateCounter(POLYMORPHED, Counter);
1981 if (TemporaryStateIsActivated(POLYMORPHED)) {
1982 NewForm->SetPolymorphBackup(GetPolymorphBackup());
1983 SetPolymorphBackup(0);
1984 SendToHell();
1985 } else {
1986 NewForm->SetPolymorphBackup(this);
1987 Flags |= C_POLYMORPHED;
1988 Disable();
1991 GetStack()->MoveItemsTo(NewForm->GetStack());
1992 NewForm->SetMoney(GetMoney());
1993 DonateEquipmentTo(NewForm);
1994 Flags &= ~C_IN_NO_MSG_MODE;
1995 NewForm->Flags &= ~C_IN_NO_MSG_MODE;
1996 NewForm->CalculateAll();
1998 if (IsPlayer()) {
1999 Flags &= ~C_PLAYER;
2000 game::SetPlayer(NewForm);
2001 game::SendLOSUpdateRequest();
2002 UpdateESPLOS();
2005 NewForm->TestWalkability();
2006 return true;
2010 void character::BeKicked (character *Kicker, item *Boot, bodypart *Leg, v2 HitPos, double KickDamage,
2011 double ToHitValue, int Success, int Direction, truth Critical, truth ForceHit)
2013 //FIXME: other args
2014 if (!game::RunCharEvent("before_kicked_by", this, Kicker, Boot)) return;
2016 auto hitres = (TakeHit(Kicker, Boot, Leg, HitPos, KickDamage, ToHitValue, Success, KICK_ATTACK, Direction, Critical, ForceHit));
2017 if (hitres == HAS_HIT || hitres == HAS_BLOCKED || hitres == DID_NO_DAMAGE) {
2018 if (IsEnabled() && !CheckBalance(KickDamage)) {
2019 if (IsPlayer()) ADD_MESSAGE("The kick throws you off balance.");
2020 else if (Kicker->IsPlayer()) ADD_MESSAGE("The kick throws %s off balance.", CHAR_DESCRIPTION(DEFINITE));
2021 v2 FallToPos = GetPos()+game::GetMoveVector(Direction);
2022 FallTo(Kicker, FallToPos);
2028 /* Return true if still in balance */
2029 truth character::CheckBalance (double KickDamage) {
2030 return !CanMove() || IsStuck() || !KickDamage || (!IsFlying() && KickDamage*5 < RAND()%GetSize());
2034 void character::FallTo (character *GuiltyGuy, v2 Where) {
2035 EditAP(-500);
2036 lsquare *MoveToSquare[MAX_SQUARES_UNDER];
2037 int Squares = CalculateNewSquaresUnder(MoveToSquare, Where);
2038 if (Squares) {
2039 truth NoRoom = false;
2040 for (int c = 0; c < Squares; ++c) {
2041 olterrain *Terrain = MoveToSquare[c]->GetOLTerrain();
2042 if (Terrain && !CanMoveOn(Terrain)) { NoRoom = true; break; }
2044 if (NoRoom) {
2045 if (HasHead()) {
2046 if (IsPlayer()) ADD_MESSAGE("You hit your head on the wall.");
2047 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s hits %s head on the wall.", CHAR_NAME(DEFINITE), GetPossessivePronoun().CStr());
2049 ReceiveDamage(GuiltyGuy, 1+RAND()%5, PHYSICAL_DAMAGE, HEAD);
2050 CheckDeath(CONST_S("killed by hitting a wall due to being kicked @bk"), GuiltyGuy);
2051 } else {
2052 if (IsFreeForMe(MoveToSquare[0])) Move(Where, true);
2053 // Place code that handles characters bouncing to each other here
2059 truth character::CheckCannibalism (cmaterial *What) const {
2060 return GetTorso()->GetMainMaterial()->IsSameAs(What);
2064 void character::StandIdleAI () {
2065 SeekLeader(GetLeader());
2066 if (CheckForEnemies(true, true, true)) return;
2067 if (CheckForUsefulItemsOnGround()) return;
2068 if (FollowLeader(GetLeader())) return;
2069 if (CheckForDoors()) return;
2070 if (MoveTowardsHomePos()) return;
2071 if (CheckSadism()) return;
2072 EditAP(-1000);
2076 truth character::LoseConsciousness (int Counter, truth HungerFaint) {
2077 if (!AllowUnconsciousness()) return false;
2078 action *Action = GetAction();
2079 if (Action) {
2080 if (HungerFaint && !Action->AllowUnconsciousness()) return false;
2081 if (Action->IsUnconsciousness()) {
2082 static_cast<unconsciousness *>(Action)->RaiseCounterTo(Counter);
2083 return true;
2085 Action->Terminate(false);
2087 if (IsPlayer()) ADD_MESSAGE("You lose consciousness.");
2088 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s loses consciousness.", CHAR_NAME(DEFINITE));
2089 unconsciousness *Unconsciousness = unconsciousness::Spawn(this);
2090 Unconsciousness->SetCounter(Counter);
2091 SetAction(Unconsciousness);
2092 return true;
2096 void character::DeActivateVoluntaryAction (cfestring &Reason) {
2097 if (GetAction() && GetAction()->IsVoluntary()) {
2098 if (IsPlayer()) {
2099 if (Reason.GetSize()) ADD_MESSAGE("%s", Reason.CStr());
2100 if (game::TruthQuestion(CONST_S("Continue ")+GetAction()->GetDescription()+"?")) GetAction()->ActivateInDNDMode();
2101 else GetAction()->Terminate(false);
2102 } else {
2103 GetAction()->Terminate(false);
2109 void character::ActionAutoTermination () {
2110 if (!GetAction() || !GetAction()->IsVoluntary() || GetAction()->InDNDMode()) return;
2111 v2 Pos = GetPos();
2112 for (int c = 0; c < game::GetTeams(); ++c) {
2113 if (GetTeam()->GetRelation(game::GetTeam(c)) == HOSTILE) {
2114 for (std::list<character *>::const_iterator i = game::GetTeam(c)->GetMember().begin(); i != game::GetTeam(c)->GetMember().end(); ++i) {
2115 character *ch = *i;
2116 if (ch->IsEnabled() && ch->CanBeSeenBy(this, false, true) && (ch->CanMove() || ch->GetPos().IsAdjacent(Pos)) && ch->CanAttack()) {
2117 if (IsPlayer()) {
2118 ADD_MESSAGE("%s seems to be hostile.", ch->CHAR_NAME(DEFINITE));
2119 if (game::TruthQuestion(CONST_S("Continue ")+GetAction()->GetDescription()+"?")) GetAction()->ActivateInDNDMode();
2120 else GetAction()->Terminate(false);
2121 } else {
2122 GetAction()->Terminate(false);
2124 return;
2132 truth character::CheckForEnemies (truth CheckDoors, truth CheckGround, truth MayMoveRandomly, truth RunTowardsTarget) {
2133 if (!IsEnabled()) return false;
2134 truth HostileCharsNear = false;
2135 character *NearestChar = 0;
2136 sLong NearestDistance = 0x7FFFFFFF;
2137 v2 Pos = GetPos();
2138 for (int c = 0; c < game::GetTeams(); ++c) {
2139 if (GetTeam()->GetRelation(game::GetTeam(c)) == HOSTILE) {
2140 for (std::list<character*>::const_iterator i = game::GetTeam(c)->GetMember().begin(); i != game::GetTeam(c)->GetMember().end(); ++i) {
2141 character *ch = *i;
2142 if (ch->IsEnabled() && GetAttribute(WISDOM) < ch->GetAttackWisdomLimit()) {
2143 sLong ThisDistance = Max<sLong>(abs(ch->GetPos().X - Pos.X), abs(ch->GetPos().Y - Pos.Y));
2144 if (ThisDistance <= GetLOSRangeSquare()) HostileCharsNear = true;
2145 if ((ThisDistance < NearestDistance || (ThisDistance == NearestDistance && !(RAND() % 3))) &&
2146 ch->CanBeSeenBy(this, false, IsGoingSomeWhere()) &&
2147 (!IsGoingSomeWhere() || HasClearRouteTo(ch->GetPos()))) {
2148 NearestChar = ch;
2149 NearestDistance = ThisDistance;
2156 if (NearestChar) {
2157 if (GetAttribute(INTELLIGENCE) >= 10 || IsSpy()) game::CallForAttention(GetPos(), 100);
2158 if (SpecialEnemySightedReaction(NearestChar)) return true;
2159 if (IsExtraCoward() && !StateIsActivated(PANIC) && NearestChar->GetRelativeDanger(this) >= 0.5 && !StateIsActivated(FEARLESS)) {
2160 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s sees %s.", CHAR_NAME(DEFINITE), NearestChar->CHAR_DESCRIPTION(DEFINITE));
2161 BeginTemporaryState(PANIC, 500+RAND()%500);
2163 if (!IsRetreating()) {
2164 if (CheckGround && NearestDistance > 2 && CheckForUsefulItemsOnGround(false)) return true;
2165 SetGoingTo(NearestChar->GetPos());
2166 } else {
2167 SetGoingTo(Pos-((NearestChar->GetPos()-Pos)<<4));
2169 return MoveTowardsTarget(true);
2170 } else {
2171 character *Leader = GetLeader();
2172 if (Leader == this) Leader = 0;
2173 if (!Leader && IsGoingSomeWhere()) {
2174 if (!MoveTowardsTarget(RunTowardsTarget)) {
2175 TerminateGoingTo();
2176 return false;
2177 } else {
2178 if (!IsEnabled()) return true;
2179 if (GetPos() == GoingTo) TerminateGoingTo();
2180 return true;
2182 } else {
2183 if ((!Leader || (Leader && !IsGoingSomeWhere())) && HostileCharsNear) {
2184 if (CheckDoors && CheckForDoors()) return true;
2185 if (CheckGround && CheckForUsefulItemsOnGround()) return true;
2186 if (MayMoveRandomly && MoveRandomly()) return true; // one has heard that an enemy is near but doesn't know where
2188 return false;
2194 truth character::CheckForDoors () {
2195 if (!CanOpen() || !IsEnabled()) return false;
2196 for (int d = 0; d < GetNeighbourSquares(); ++d) {
2197 lsquare *Square = GetNeighbourLSquare(d);
2198 if (Square && Square->GetOLTerrain() && Square->GetOLTerrain()->Open(this)) return true;
2200 return false;
2204 truth character::CheckForUsefulItemsOnGround (truth CheckFood) {
2205 if (StateIsActivated(PANIC) || !IsEnabled()) return false;
2206 itemvector ItemVector;
2207 GetStackUnder()->FillItemVector(ItemVector);
2208 for (uInt c = 0; c < ItemVector.size(); ++c) {
2209 if (ItemVector[c]->CanBeSeenBy(this) && ItemVector[c]->IsPickable(this)) {
2210 if (!(CommandFlags & DONT_CHANGE_EQUIPMENT) && TryToEquip(ItemVector[c])) return true;
2211 if (CheckFood && UsesNutrition() && !CheckIfSatiated() && TryToConsume(ItemVector[c])) return true;
2212 if (IsRangedAttacker() && (ItemVector[c])->GetThrowItemTypes() && TryToAddToInventory(ItemVector[c])) return true;
2215 return false;
2219 truth character::TryToAddToInventory (item *Item) {
2220 if (!(GetBurdenState() > STRESSED) || !CanUseEquipment() || Item->GetSquaresUnder() != 1) return false;
2221 room *Room = GetRoom();
2222 if (!Room || Room->PickupItem(this, Item, 1)) {
2223 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s picks up %s from the ground.", CHAR_NAME(DEFINITE), Item->CHAR_NAME(INDEFINITE));
2224 Item->MoveTo(GetStack());
2225 DexterityAction(5);
2226 return true;
2228 return false;
2232 truth character::CheckInventoryForItemToThrow (item *ToBeChecked) {
2233 return (ToBeChecked->GetThrowItemTypes() & GetWhatThrowItemTypesToThrow()) ? true : false; //hehe
2237 truth character::CheckThrowItemOpportunity () {
2238 if (!IsRangedAttacker() || !CanThrow() || !IsHumanoid() || !IsSmall() || !IsEnabled()) return false; // total gum
2239 //fprintf(stderr, "character::CheckThrowItemOpportunity...\n");
2240 // Steps:
2241 // (1) - Acquire target as nearest enemy
2242 // (2) - Check that this enemy is in range, and is in appropriate direction; no friendly fire!
2243 // (3) - check inventory for throwing weapon, select this weapon
2244 // (4) - throw item in direction where the enemy is
2246 //Check the visible area for hostiles
2247 int ThrowDirection = 0;
2248 int TargetFound = 0;
2249 v2 Pos = GetPos();
2250 v2 TestPos;
2251 int RangeMax = GetLOSRange();
2252 int CandidateDirections[7] = {0, 0, 0, 0, 0, 0, 0};
2253 int HostileFound = 0;
2254 item *ToBeThrown = 0;
2255 level *Level = GetLevel();
2257 for (int r = 1; r <= RangeMax; ++r) {
2258 for (int dir = 0; dir < MDIR_STAND; ++dir) {
2260 switch (dir) {
2261 case 0: TestPos = v2(Pos.X-r, Pos.Y-r); break;
2262 case 1: TestPos = v2(Pos.X, Pos.Y-r); break;
2263 case 2: TestPos = v2(Pos.X+r, Pos.Y-r); break;
2264 case 3: TestPos = v2(Pos.X-r, Pos.Y); break;
2265 case 4: TestPos = v2(Pos.X+r, Pos.Y); break;
2266 case 5: TestPos = v2(Pos.X-r, Pos.Y+r); break;
2267 case 6: TestPos = v2(Pos.X, Pos.Y+r); break;
2268 case 7: TestPos = v2(Pos.X+r, Pos.Y+r); break;
2270 if (Level->IsValidPos(TestPos)) {
2271 square *TestSquare = GetNearSquare(TestPos);
2272 character *Dude = TestSquare->GetCharacter();
2274 if (Dude && Dude->IsEnabled() && Dude->CanBeSeenBy(this, false, true)) {
2275 if (GetRelation(Dude) != HOSTILE) CandidateDirections[dir] = BLOCKED;
2276 else if (GetRelation(Dude) == HOSTILE && CandidateDirections[dir] != BLOCKED) {
2277 //then load this candidate position direction into the vector of possible throw directions
2278 CandidateDirections[dir] = SUCCESS;
2279 HostileFound = 1;
2286 if (HostileFound) {
2287 for (int dir = 0; dir < MDIR_STAND; ++dir) {
2288 if (CandidateDirections[dir] == SUCCESS && !TargetFound) {
2289 ThrowDirection = dir;
2290 TargetFound = 1;
2291 break;
2294 if (!TargetFound) return false;
2295 } else {
2296 return false;
2298 //fprintf(stderr, "throw: has target.\n");
2299 // check inventory for throwing weapon
2300 itemvector ItemVector;
2301 GetStack()->FillItemVector(ItemVector);
2302 for (uInt c = 0; c < ItemVector.size(); ++c) {
2303 if (ItemVector[c]->IsThrowingWeapon()) {
2304 ToBeThrown = ItemVector[c];
2305 break;
2308 if (!ToBeThrown) return false;
2309 //fprintf(stderr, "throw: has throwing weapon.\n");
2310 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s throws %s.", CHAR_NAME(DEFINITE), ToBeThrown->CHAR_NAME(INDEFINITE));
2311 ThrowItem(ThrowDirection, ToBeThrown);
2312 EditExperience(ARM_STRENGTH, 75, 1<<8);
2313 EditExperience(DEXTERITY, 75, 1<<8);
2314 EditExperience(PERCEPTION, 75, 1<<8);
2315 EditNP(-50);
2316 DexterityAction(5);
2317 TerminateGoingTo();
2318 return true;
2322 truth character::CheckAIZapOpportunity () {
2323 if (/*!IsRangedAttacker() || */ !CanZap() || !IsHumanoid() || !IsSmall() || !IsEnabled()) return false; // total gum
2324 // Steps:
2325 // (1) - Acquire target as nearest enemy
2326 // (2) - Check that this enemy is in range, and is in appropriate direction; no friendly fire!
2327 // (3) - check inventory for zappable item
2328 // (4) - zap item in direction where the enemy is
2329 //Check the rest of the visible area for hostiles
2330 v2 Pos = GetPos();
2331 v2 TestPos;
2332 int SensibleRange = 5;
2333 int RangeMax = GetLOSRange();
2334 if (RangeMax < SensibleRange) SensibleRange = RangeMax;
2335 int CandidateDirections[7] = {0, 0, 0, 0, 0, 0, 0};
2336 int HostileFound = 0;
2337 int ZapDirection = 0;
2338 int TargetFound = 0;
2339 item *ToBeZapped = 0;
2340 level *Level = GetLevel();
2342 for (int r = 2; r <= SensibleRange; ++r) {
2343 for (int dir = 0; dir < MDIR_STAND; ++dir) {
2344 switch (dir) {
2345 case 0: TestPos = v2(Pos.X-r, Pos.Y-r); break;
2346 case 1: TestPos = v2(Pos.X, Pos.Y-r); break;
2347 case 2: TestPos = v2(Pos.X+r, Pos.Y-r); break;
2348 case 3: TestPos = v2(Pos.X-r, Pos.Y); break;
2349 case 4: TestPos = v2(Pos.X+r, Pos.Y); break;
2350 case 5: TestPos = v2(Pos.X-r, Pos.Y+r); break;
2351 case 6: TestPos = v2(Pos.X, Pos.Y+r); break;
2352 case 7: TestPos = v2(Pos.X+r, Pos.Y+r); break;
2354 if (Level->IsValidPos(TestPos)) {
2355 square *TestSquare = GetNearSquare(TestPos);
2356 character *Dude = TestSquare->GetCharacter();
2358 if (Dude && Dude->IsEnabled() && Dude->CanBeSeenBy(this, false, true)) {
2359 if (GetRelation(Dude) != HOSTILE) CandidateDirections[dir] = BLOCKED;
2360 else if (GetRelation(Dude) == HOSTILE && CandidateDirections[dir] != BLOCKED) {
2361 //then load this candidate position direction into the vector of possible zap directions
2362 CandidateDirections[dir] = SUCCESS;
2363 HostileFound = 1;
2370 if (HostileFound) {
2371 for (int dir = 0; dir < MDIR_STAND; ++dir) {
2372 if (CandidateDirections[dir] == SUCCESS && !TargetFound) {
2373 ZapDirection = dir;
2374 TargetFound = 1;
2375 break;
2378 if (!TargetFound) return false;
2379 } else {
2380 return false;
2382 // check inventory for zappable item
2383 itemvector ItemVector;
2384 GetStack()->FillItemVector(ItemVector);
2385 for (unsigned int c = 0; c < ItemVector.size(); ++c) {
2386 if (ItemVector[c]->GetMinCharges() > 0 && ItemVector[c]->GetPrice()) {
2387 // bald-faced gum solution for choosing zappables that have shots left.
2388 // MinCharges needs to be replaced. Empty wands have zero price!
2389 ToBeZapped = ItemVector[c];
2390 break;
2393 if (!ToBeZapped) return false;
2394 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s zaps %s.", CHAR_NAME(DEFINITE), ToBeZapped->CHAR_NAME(INDEFINITE));
2395 if (ToBeZapped->Zap(this, GetPos(), ZapDirection)) {
2396 EditAP(-100000/APBonus(GetAttribute(PERCEPTION)));
2397 return true;
2398 } else {
2399 return false;
2401 TerminateGoingTo();
2402 return true;
2406 truth character::FollowLeader (character *Leader) {
2407 if (!Leader || Leader == this || !IsEnabled()) return false;
2408 if ((CommandFlags&FOLLOW_LEADER) && Leader->CanBeSeenBy(this) && Leader->SquareUnderCanBeSeenBy(this, true)) {
2409 v2 Distance = GetPos()-GoingTo;
2410 if (abs(Distance.X) <= 2 && abs(Distance.Y) <= 2) return false;
2411 return MoveTowardsTarget(false);
2413 if (IsGoingSomeWhere()) {
2414 if (!MoveTowardsTarget(true)) {
2415 TerminateGoingTo();
2416 return false;
2418 return true;
2420 return false;
2424 void character::SeekLeader (ccharacter *Leader) {
2425 if (Leader && Leader != this) {
2426 if (Leader->CanBeSeenBy(this) && (Leader->SquareUnderCanBeSeenBy(this, true) || !IsGoingSomeWhere())) {
2427 if (CommandFlags&FOLLOW_LEADER) SetGoingTo(Leader->GetPos());
2428 } else if (!IsGoingSomeWhere()) {
2429 team *Team = GetTeam();
2430 for (std::list<character *>::const_iterator i = Team->GetMember().begin(); i != Team->GetMember().end(); ++i) {
2431 character *ch = *i;
2432 if (ch->IsEnabled() && ch->GetID() != GetID() &&
2433 (CommandFlags & FOLLOW_LEADER) == (ch->CommandFlags & FOLLOW_LEADER) && ch->CanBeSeenBy(this)) {
2434 v2 Pos = ch->GetPos();
2435 v2 Distance = GetPos()-Pos;
2436 if (abs(Distance.X) > 2 && abs(Distance.Y) > 2) {
2437 SetGoingTo(Pos);
2438 break;
2447 int character::GetMoveEase () const {
2448 if (BurdenState == OVER_LOADED || BurdenState == STRESSED) return 50;
2449 if (BurdenState == BURDENED) return 75;
2450 if (BurdenState == UNBURDENED) return 100;
2451 return 666;
2455 int character::GetLOSRange () const {
2456 if (!game::IsInWilderness()) return GetAttribute(PERCEPTION)*GetLevel()->GetLOSModifier()/48;
2457 return 3;
2461 truth character::Displace (character *Who, truth Forced) {
2462 if (GetBurdenState() == OVER_LOADED) {
2463 if (IsPlayer()) {
2464 cchar *CrawlVerb = StateIsActivated(LEVITATION) ? "float" : "crawl";
2465 ADD_MESSAGE("You try very hard to %s forward. But your load is too heavy.", CrawlVerb);
2466 EditAP(-1000);
2467 return true;
2469 return false;
2472 double Danger = GetRelativeDanger(Who);
2473 int PriorityDifference = Limit(GetDisplacePriority()-Who->GetDisplacePriority(), -31, 31);
2475 if (IsPlayer()) ++PriorityDifference;
2476 else if (Who->IsPlayer()) --PriorityDifference;
2478 if (PriorityDifference >= 0) Danger *= 1 << PriorityDifference;
2479 else Danger /= 1 << -PriorityDifference;
2481 if (IsSmall() && Who->IsSmall() &&
2482 (Forced || Danger > 1.0 || !(Who->IsPlayer() || Who->IsBadPath(GetPos()))) &&
2483 !IsStuck() && !Who->IsStuck() && (!Who->GetAction() || Who->GetAction()->TryDisplace()) &&
2484 CanMove() && Who->CanMove() && Who->CanMoveOn(GetLSquareUnder())) {
2485 if (IsPlayer()) ADD_MESSAGE("You displace %s!", Who->CHAR_DESCRIPTION(DEFINITE));
2486 else if (Who->IsPlayer()) ADD_MESSAGE("%s displaces you!", CHAR_DESCRIPTION(DEFINITE));
2487 else if (CanBeSeenByPlayer() || Who->CanBeSeenByPlayer()) ADD_MESSAGE("%s displaces %s!", CHAR_DESCRIPTION(DEFINITE), Who->CHAR_DESCRIPTION(DEFINITE));
2488 lsquare *OldSquareUnder1[MAX_SQUARES_UNDER];
2489 lsquare *OldSquareUnder2[MAX_SQUARES_UNDER];
2490 for (int c = 0; c < GetSquaresUnder(); ++c) OldSquareUnder1[c] = GetLSquareUnder(c);
2491 for (int c = 0; c < Who->GetSquaresUnder(); ++c) OldSquareUnder2[c] = Who->GetLSquareUnder(c);
2492 v2 Pos = GetPos();
2493 v2 WhoPos = Who->GetPos();
2494 Remove();
2495 Who->Remove();
2496 PutTo(WhoPos);
2497 Who->PutTo(Pos);
2498 EditAP(-GetMoveAPRequirement(GetSquareUnder()->GetEntryDifficulty()) - 500);
2499 EditNP(-12*GetSquareUnder()->GetEntryDifficulty());
2500 EditExperience(AGILITY, 75, GetSquareUnder()->GetEntryDifficulty() << 7);
2501 if (IsPlayer()) ShowNewPosInfo();
2502 if (Who->IsPlayer()) Who->ShowNewPosInfo();
2503 SignalStepFrom(OldSquareUnder1);
2504 Who->SignalStepFrom(OldSquareUnder2);
2505 return true;
2506 } else {
2507 if (IsPlayer()) {
2508 ADD_MESSAGE("%s resists!", Who->CHAR_DESCRIPTION(DEFINITE));
2509 EditAP(-1000);
2510 return true;
2512 return false;
2517 void character::SetNP (sLong What) {
2518 int OldState = GetHungerState();
2519 NP = What;
2520 if (IsPlayer()) {
2521 int NewState = GetHungerState();
2522 if (NewState == STARVING && OldState > STARVING) DeActivateVoluntaryAction(CONST_S("You are getting really hungry."));
2523 else if (NewState == VERY_HUNGRY && OldState > VERY_HUNGRY) DeActivateVoluntaryAction(CONST_S("You are getting very hungry."));
2524 else if (NewState == HUNGRY && OldState > HUNGRY) DeActivateVoluntaryAction(CONST_S("You are getting hungry."));
2529 void character::ShowNewPosInfo () const {
2530 msgsystem::EnterBigMessageMode();
2531 v2 Pos = GetPos();
2533 if (ivanconfig::GetAutoCenterMap()) {
2534 game::UpdateCameraX();
2535 game::UpdateCameraY();
2536 } else {
2537 if (Pos.X < game::GetCamera().X+3 || Pos.X >= game::GetCamera().X+game::GetScreenXSize()-3) game::UpdateCameraX();
2538 if (Pos.Y < game::GetCamera().Y+3 || Pos.Y >= game::GetCamera().Y+game::GetScreenYSize()-3) game::UpdateCameraY();
2541 game::SendLOSUpdateRequest();
2542 game::DrawEverythingNoBlit();
2543 UpdateESPLOS();
2545 if (!game::IsInWilderness()) {
2546 if (GetLSquareUnder()->IsDark() && !game::GetSeeWholeMapCheatMode()) ADD_MESSAGE("It's dark in here!");
2548 GetLSquareUnder()->ShowSmokeMessage();
2549 itemvectorvector PileVector;
2550 GetStackUnder()->Pile(PileVector, this, CENTER);
2552 if (PileVector.size()) {
2553 truth Feel = !GetLSquareUnder()->IsTransparent() || GetLSquareUnder()->IsDark();
2555 if (PileVector.size() == 1) {
2556 if (Feel) {
2557 ADD_MESSAGE("You feel %s lying here.", PileVector[0][0]->GetName(INDEFINITE, PileVector[0].size()).CStr());
2558 } else {
2559 if (ivanconfig::GetShowFullItemDesc() && PileVector[0][0]->AllowDetailedDescription()) {
2560 festring text;
2562 PileVector[0][0]->AddInventoryEntry(PLAYER, text, PileVector[0].size(), true);
2563 //fprintf(stderr, "invdsc : [%s]\n", text.CStr());
2564 ADD_MESSAGE("%s %s lying here.", text.CStr(), PileVector[0].size() == 1 ? "is" : "are");
2565 } else {
2566 ADD_MESSAGE("%s %s lying here.", PileVector[0][0]->GetName(INDEFINITE, PileVector[0].size()).CStr(), PileVector[0].size() == 1 ? "is" : "are");
2569 fprintf(stderr, "description: [%s]\n", PileVector[0][0]->GetDescription(INDEFINITE).CStr());
2570 fprintf(stderr, "strength : [%s]\n", PileVector[0][0]->GetStrengthValueDescription());
2571 fprintf(stderr, "basetohit : [%s]\n", PileVector[0][0]->GetBaseToHitValueDescription());
2572 fprintf(stderr, "baseblock : [%s]\n", PileVector[0][0]->GetBaseBlockValueDescription());
2573 fprintf(stderr, "extdsc : [%s]\n", PileVector[0][0]->GetExtendedDescription().CStr());
2576 } else {
2577 int Items = 0;
2578 for (uInt c = 0; c < PileVector.size(); ++c) {
2579 if ((Items += PileVector[c].size()) > 3) break;
2581 if (Items > 3) {
2582 if (Feel) ADD_MESSAGE("You feel several items lying here.");
2583 else ADD_MESSAGE("Several items are lying here.");
2584 } else if (Items) {
2585 if (Feel) ADD_MESSAGE("You feel a few items lying here.");
2586 else ADD_MESSAGE("A few items are lying here.");
2591 festring SideItems;
2592 GetLSquareUnder()->GetSideItemDescription(SideItems);
2594 if (!SideItems.IsEmpty()) ADD_MESSAGE("There is %s.", SideItems.CStr());
2596 if (GetLSquareUnder()->HasEngravings()) {
2597 if (CanRead()) ADD_MESSAGE("Something has been engraved here: \"%s\"", GetLSquareUnder()->GetEngraved());
2598 else ADD_MESSAGE("Something has been engraved here.");
2602 msgsystem::LeaveBigMessageMode();
2606 void character::Hostility (character *Enemy) {
2607 if (Enemy == this || !Enemy || !Team || !Enemy->Team) return;
2608 if (Enemy->IsMasochist() && GetRelation(Enemy) == FRIEND) return;
2609 if (!IsAlly(Enemy)) {
2610 GetTeam()->Hostility(Enemy->GetTeam());
2611 } else if (IsPlayer() && !Enemy->IsPlayer()) {
2612 // I believe both may be players due to polymorph feature...
2613 if (Enemy->CanBeSeenByPlayer()) ADD_MESSAGE("%s becomes enraged.", Enemy->CHAR_NAME(DEFINITE));
2614 Enemy->ChangeTeam(game::GetTeam(BETRAYED_TEAM));
2619 stack *character::GetGiftStack () const {
2620 if (GetLSquareUnder()->GetRoomIndex() && !GetLSquareUnder()->GetRoom()->AllowDropGifts()) return GetStack();
2621 return GetStackUnder();
2625 truth character::MoveRandomlyInRoom () {
2626 for (int c = 0; c < 10; ++c) {
2627 v2 ToTry = game::GetMoveVector(RAND()&7);
2628 if (GetLevel()->IsValidPos(GetPos()+ToTry)) {
2629 lsquare *Square = GetNearLSquare(GetPos()+ToTry);
2630 if (!Square->IsDangerous(this) && !Square->IsScary(this) &&
2631 (!Square->GetOLTerrain() || !Square->GetOLTerrain()->IsDoor()) &&
2632 TryMove(ToTry, false, false)) return true;
2635 return false;
2639 //#define dirlogf(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
2640 #define dirlogf(...) ((void)0)
2643 static const int revDir[MDIR_STAND] = { MDIR_DOWN_RIGHT, MDIR_DOWN, MDIR_DOWN_LEFT, MDIR_RIGHT, MDIR_LEFT, MDIR_UP_RIGHT, MDIR_UP, MDIR_UP_LEFT };
2644 static const bool orthoDir[MDIR_STAND] = { false, true, false, true, true, false, true, false };
2647 // only for ortho moveDir
2648 static inline truth IsDirExcluded (int moveDir, int dir) {
2649 if (moveDir == dir) return true;
2650 switch (moveDir) {
2651 case MDIR_UP: return (dir == MDIR_UP_LEFT || dir == MDIR_UP_RIGHT);
2652 case MDIR_LEFT: return (dir == MDIR_UP_LEFT || dir == MDIR_DOWN_LEFT);
2653 case MDIR_RIGHT: return (dir == MDIR_UP_RIGHT || dir == MDIR_DOWN_RIGHT);
2654 case MDIR_DOWN: return (dir == MDIR_DOWN_LEFT || dir == MDIR_DOWN_RIGHT);
2656 return false;
2660 truth character::IsPassableSquare (int x, int y) const {
2661 if (x >= 0 && y >= 0) {
2662 area *ca = GetSquareUnder()->GetArea();
2663 lsquare *sq;
2665 if (x >= ca->GetXSize() || y >= ca->GetYSize()) return false;
2666 sq = static_cast<lsquare *>(ca->GetSquare(x, y));
2667 return sq && CanMoveOn(sq);
2669 return false;
2673 void character::CountPossibleMoveDirs (cv2 pos, int *odirs, int *ndirs, int exclideDir) const {
2674 if (odirs) *odirs = 0;
2675 if (ndirs) *ndirs = 0;
2676 for (int f = 0; f < MDIR_STAND; ++f) {
2677 if (!IsDirExcluded(exclideDir, f)) {
2678 if (IsPassableSquare(pos+game::GetMoveVector(f))) {
2679 if (orthoDir[f]) {
2680 if (odirs) ++(*odirs);
2681 } else {
2682 if (ndirs) ++(*ndirs);
2691 * in corridor (for orto-dirs):
2692 * count dirs excluding ortho-dir we going:
2693 * if there is one or less ortho-dirs and one or less non-ortho-dirs, we are in corridor
2695 // only for ortho-dirs
2696 truth character::IsInCorridor (int x, int y, int moveDir) const {
2697 int od = 0, nd = 0;
2699 dirlogf("IsInCorridor(%d,%d,%d)\n", x, y, moveDir);
2700 // reverse moveDir
2701 moveDir = (moveDir >= 0 && moveDir < MDIR_STAND ? revDir[moveDir] : -1);
2702 dirlogf(" reversedDir: %d\n", moveDir);
2703 CountPossibleMoveDirs(v2(x, y), &od, &nd, moveDir);
2704 dirlogf(" possibleDirs: (%d:%d)\n", od, nd);
2705 dirlogf(" IsInCorridor: %s\n", ((od <= 1 && nd <= 1) ? "yes" : "no"));
2706 return (od <= 1 && nd <= 1);
2710 cv2 character::GetDiagonalForDirs (int moveDir, int newDir) const {
2711 switch (moveDir) {
2712 case MDIR_UP:
2713 switch (newDir) {
2714 case MDIR_LEFT: return game::GetMoveVector(MDIR_UP_LEFT);
2715 case MDIR_RIGHT: return game::GetMoveVector(MDIR_UP_RIGHT);
2717 break;
2718 case MDIR_DOWN:
2719 switch (newDir) {
2720 case MDIR_LEFT: return game::GetMoveVector(MDIR_DOWN_LEFT);
2721 case MDIR_RIGHT: return game::GetMoveVector(MDIR_DOWN_RIGHT);
2723 break;
2724 case MDIR_LEFT:
2725 switch (newDir) {
2726 case MDIR_UP: return game::GetMoveVector(MDIR_UP_LEFT);
2727 case MDIR_DOWN: return game::GetMoveVector(MDIR_DOWN_LEFT);
2729 break;
2730 case MDIR_RIGHT:
2731 switch (newDir) {
2732 case MDIR_UP: return game::GetMoveVector(MDIR_UP_RIGHT);
2733 case MDIR_DOWN: return game::GetMoveVector(MDIR_DOWN_RIGHT);
2735 break;
2737 ABORT("wtf in character::GetDiagonalForDirs()");
2741 truth character::IsInTunnelDeadEnd () const {
2742 int od, nd;
2744 CountPossibleMoveDirs(GetPos(), &od, &nd, -1);
2745 return (od <= 1 && nd == 0);
2750 * try to walk in the given dir
2751 * can do two steps without a turn and still in corridor?
2752 * yes:
2753 * just go
2754 * no:
2755 * go in non-ortho dir, set prevdir to last ortho-dir from corridor tracing
2757 // only for ortho-dirs; assume that the char is in corridor
2758 int character::CheckCorridorMove (v2 &moveVector, cv2 pos, int moveDir, truth *markAsTurn) const {
2759 v2 ps1(pos+(moveVector = game::GetMoveVector(moveDir)));
2761 if (markAsTurn) *markAsTurn = true;
2763 if (IsPassableSquare(ps1)) {
2764 // we can do first step in the given dir
2765 // check if we will be in corridor after it
2766 dirlogf("CheckCorridorMove: can do first step\n");
2767 if (IsInCorridor(ps1, moveDir)) {
2768 // check second step
2769 v2 ps2(ps1+moveVector);
2770 dirlogf("CheckCorridorMove: still in corridor after the first step\n");
2771 if (IsPassableSquare(ps2)) {
2772 // can do second step
2773 dirlogf("CheckCorridorMove: can do second step\n");
2774 return moveDir;
2775 } else {
2776 // can't do second step; but we still in corridor, so we should make a turn
2777 int newDir = -1; // direction to turn
2778 for (int f = 0; f < MDIR_STAND; ++f) {
2779 if (f != moveDir && orthoDir[f] && f != revDir[moveDir] && IsPassableSquare(ps1+game::GetMoveVector(f))) {
2780 newDir = f;
2781 break;
2784 dirlogf("CheckCorridorMove: can't do second step; moveDir=%d; newDir=%d\n", moveDir, newDir);
2785 if (newDir < 0) {
2786 // dead end, will stop
2787 //ABORT("wtd in character::CheckCorridorMove()");
2788 return moveDir;
2790 // we should do diagonal move
2791 moveVector = GetDiagonalForDirs(moveDir, newDir);
2792 // if this is 'one-tile-turn', we should not change the direction to newDir
2793 if (IsPassableSquare(ps1+game::GetMoveVector(newDir)+game::GetMoveVector(moveDir))) {
2794 // yes, this is 'one-tile-turn'
2795 dirlogf("CheckCorridorMove: one-tile-turn, don't change dir\n");
2796 /* 'g'o bug:
2798 * ####.######
2799 * ####*......
2800 * ..@..######
2801 * ######
2803 * 'g'o right: should stop at '*', but it just goes right
2805 if (markAsTurn) *markAsTurn = IsInCorridor(ps1+game::GetMoveVector(newDir), newDir);
2806 newDir = moveDir;
2808 return newDir;
2811 dirlogf("CheckCorridorMove: can do one or two steps; move forward\n");
2812 // can do one or two steps: check for T-junction
2813 // we should stop if we have more than two open dirs, or one of open dirs is not moveDir
2814 int dcount = 0;
2815 for (int f = 0; f < MDIR_STAND; ++f) {
2816 if (f == revDir[moveDir]) continue; // skip "reverse dir" check
2817 v2 ps2(pos+game::GetMoveVector(f));
2818 if (IsPassableSquare(ps2)) {
2819 ++dcount;
2820 if (dcount > 2) return -1; // more than two open dirs, stop
2821 if (f != moveDir) return -1; // one of open dirs is not moveDir
2824 // just move forward
2825 return moveDir;
2827 dirlogf("CheckCorridorMove: dead end\n");
2828 // can't go, assume invalid direction
2829 return -1;
2833 truth character::IsDangerousSquare (v2 pos) const {
2834 if (!IsPassableSquare(pos)) return false;
2835 lsquare *MoveToSquare[MAX_SQUARES_UNDER];
2836 auto Squares = CalculateNewSquaresUnder(MoveToSquare, pos);
2837 for (decltype(Squares) c = 0; c < Squares; ++c) {
2838 lsquare *Square = MoveToSquare[c];
2839 if (IsPlayer()) {
2840 if (!Square->HasBeenSeen()) continue;
2841 } else {
2842 if (!Square->CanBeSeenBy(this)) continue;
2844 // check if someone is standing at the square
2845 if (Square->GetCharacter() && GetTeam() != Square->GetCharacter()->GetTeam() && Square->GetCharacter()->CanBeSeenBy(this)) return true;
2846 if (Square->IsDangerous(this)) {
2847 if (IsPlayer() && Square->HasBeenSeen()) return true;
2848 if (Square->CanBeSeenBy(this)) return true;
2851 return false;
2855 void character::MarkAdjacentItemsAsSeen (v2 pos) {
2856 lsquare *sqlist[MAX_SQUARES_UNDER];
2857 for (int d = 0; d < MDIR_STAND; ++d) {
2858 auto np = pos+game::GetMoveVector(d);
2859 if (!IsPassableSquare(np)) continue;
2860 auto sqcount = CalculateNewSquaresUnder(sqlist, np);
2861 for (int n = 0; n < sqcount; ++n) {
2862 lsquare *sq = sqlist[n];
2863 if ((IsPlayer() && sq->HasBeenSeen()) || sq->CanBeSeenBy(this)) {
2864 sq->GetStack()->SetSteppedOn(true);
2871 void character::GoOn (go *Go, truth FirstStep) {
2872 dirlogf("=== character::GoOn; dir=%d; pos=(%d,%d) ===\n", Go->GetDirection(), GetPos().X, GetPos().Y);
2873 if (FirstStep) {
2874 dirlogf("FirstStep\n");
2875 mPrevMoveDir = Go->GetDirection();
2876 Go->SetIsWalkingInOpen(!IsInCorridor(Go->GetDirection()));
2879 v2 MoveVector = ApplyStateModification(game::GetMoveVector(Go->GetDirection()));
2880 lsquare *MoveToSquare[MAX_SQUARES_UNDER];
2881 lsquare *MoveToSquare2[MAX_SQUARES_UNDER];
2882 int Squares = CalculateNewSquaresUnder(MoveToSquare, GetPos()+MoveVector);
2883 int moveDir = game::MoveVectorToDirection(MoveVector);
2885 if (Squares == 0 || !CanMoveOn(MoveToSquare[0])) {
2886 dirlogf("just can't move\n");
2887 Go->Terminate(false);
2888 return;
2891 if (FirstStep) {
2892 // first step: mark all adjacent items as seen
2893 MarkAdjacentItemsAsSeen(GetPos());
2894 } else {
2895 // not a first step
2897 // check for corridor<->open place
2898 if (!Go->GetPrevWasTurn() && Go->IsWalkingInOpen() != !IsInCorridor(GetPos(), moveDir)) {
2899 dirlogf("moved to/from open place\n");
2900 Go->Terminate(false);
2901 return;
2904 // check for room change
2905 uInt OldRoomIndex = GetLSquareUnder()->GetRoomIndex();
2906 uInt CurrentRoomIndex = MoveToSquare[0]->GetRoomIndex();
2907 if (OldRoomIndex && (CurrentRoomIndex != OldRoomIndex)) {
2908 // room about to be changed, stop here
2909 dirlogf("room about to be changed\n");
2910 Go->Terminate(false);
2911 return;
2914 // stop near a dangerous square
2915 if (IsDangerousSquare(GetPos()+MoveVector)) {
2916 dirlogf("sense the danger\n");
2917 Go->Terminate(false);
2918 return;
2922 // if the state modified the direction, move and stop
2923 if (moveDir != Go->GetDirection()) {
2924 dirlogf("move affected by state\n");
2925 if (TryMove(MoveVector, true, game::PlayerIsRunning())) {
2926 game::DrawEverything();
2927 if (ivanconfig::GetGoingDelay()) DELAY(ivanconfig::GetGoingDelay());
2929 Go->Terminate(false);
2930 return;
2933 truth doStop = false, markAsTurn = false;
2934 if (!FirstStep) {
2935 // continuous walking
2936 if (Go->IsWalkingInOpen() || !orthoDir[moveDir]) {
2937 // walking in open space or diagonal walking
2938 v2 newPos(GetPos()+MoveVector);
2939 int ood, ond, nod, nnd;
2941 * open: stop if # of possible dirs in next step != # of possible dirs in current step
2942 * (or next step is in corridor)
2944 dirlogf("open walking\n");
2945 if (IsInCorridor(newPos, moveDir)) {
2946 // trying to enter the corridor, stop right here
2947 dirlogf("entering the corridor\n");
2948 Go->Terminate(false);
2949 return;
2951 CountPossibleMoveDirs(GetPos(), &ood, &ond);
2952 CountPossibleMoveDirs(newPos, &nod, &nnd);
2953 if (ood != nod || ond != nnd) {
2954 // # of directions to walk to changed, stop right here
2955 dirlogf("# of directions changed from (%d:%d) to (%d:%d)\n", ood, ond, nod, nnd);
2956 //Go->Terminate(false);
2957 //return;
2958 doStop = true;
2960 // ok, we can do this move
2961 } else {
2962 // ortho-walking thru the corridor
2963 int newDir = CheckCorridorMove(MoveVector, GetPos(), moveDir, &markAsTurn);
2964 if (newDir < 0) {
2965 // ah, something weird; stop right here
2966 Go->Terminate(false);
2967 return;
2969 Go->SetDirection(newDir); // perform possible turn
2972 // stop near the dangerous square
2973 for (int mdv = 0; mdv < MDIR_STAND; ++mdv) {
2974 if (IsDangerousSquare(GetPos()+MoveVector+game::GetMoveVector(mdv))) {
2975 dirlogf(" danger!\n");
2976 Go->Terminate(false);
2977 return;
2982 // now try to perform the move
2983 dirlogf("trying to make the move\n");
2985 square *BeginSquare = GetSquareUnder();
2986 uInt OldRoomIndex = GetLSquareUnder()->GetRoomIndex();
2987 uInt CurrentRoomIndex = MoveToSquare[0]->GetRoomIndex();
2989 // stop on the square with something interesting
2990 if (!doStop) {
2991 // idiotic code!
2992 area *ca = GetSquareUnder()->GetArea();
2993 v2 npos = GetPos()+MoveVector;
2994 for (int f = 0; f < MDIR_STAND; ++f) {
2995 v2 np = npos+game::GetMoveVector(f);
2996 if (np.X >= 0 && np.Y >= 0 && np.X < ca->GetXSize() && np.Y < ca->GetYSize()) {
2997 lsquare *sq = static_cast<lsquare *>(ca->GetSquare(np.X, np.Y));
2998 if (IsPlayer() && !sq->HasBeenSeen()) continue;
2999 //if (!sq->CanBeSeenBy(this)) continue;
3000 olterrain *terra = sq->GetOLTerrain();
3001 if (terra) {
3002 dirlogf("** OK terra at %d; door: %s; seen: %s\n", f, (terra->IsDoor() ? "yes" : "no"), (sq->IsGoSeen() ? "yes" : "no"));
3003 if (terra->IsDoor()) {
3004 if (ivanconfig::GetStopOnSeenDoors() || !sq->IsGoSeen()) {
3005 dirlogf(" *** stop near the door\n");
3006 doStop = true;
3007 break;
3014 // check items
3015 if (!doStop) {
3016 for (int c = 0; c < Squares; ++c) {
3017 lsquare *Square = MoveToSquare[c];
3018 if (IsPlayer()) {
3019 if (!Square->HasBeenSeen()) continue;
3020 } else {
3021 if (!Square->CanBeSeenBy(this)) continue;
3023 if (Square->GetStack()->HasSomethingFunny(this, ivanconfig::GetStopOnCorpses(), ivanconfig::GetStopOnSeenItems())) {
3024 dirlogf(" stepped near something interesting\n");
3025 doStop = true;
3026 break;
3031 // check items in adjacent squares too, so diagonal move won't miss any
3032 if (!doStop) {
3033 for (int f = 0; f < MDIR_STAND && !doStop; ++f) {
3034 v2 np = game::GetMoveVector(f);
3035 if (np == MoveVector) continue; // this will be checked on the next move
3036 if (!IsPassableSquare(GetPos()+np)) continue;
3037 int sq2 = CalculateNewSquaresUnder(MoveToSquare2, GetPos()+np);
3038 for (int c = 0; c < sq2; ++c) {
3039 lsquare *Square = MoveToSquare2[c];
3040 if (IsPlayer()) {
3041 if (!Square->HasBeenSeen()) continue;
3042 } else {
3043 if (!Square->CanBeSeenBy(this)) continue;
3045 if (Square->GetStack()->HasSomethingFunny(this, ivanconfig::GetStopOnCorpses(), ivanconfig::GetStopOnSeenItems())) {
3046 dirlogf(" stepped near something interesting\n");
3047 // stop
3048 Go->Terminate(false);
3049 return;
3056 Go->SetPrevWasTurn(markAsTurn && MoveVector.X && MoveVector.Y); // diagonal move?
3058 truth moveOk = TryMove(MoveVector, true, game::PlayerIsRunning());
3060 if (!moveOk || BeginSquare == GetSquareUnder() || (CurrentRoomIndex && (OldRoomIndex != CurrentRoomIndex))) {
3061 dirlogf(" stopped\n");
3062 if (moveOk) {
3063 game::DrawEverything();
3064 if (ivanconfig::GetGoingDelay()) DELAY(ivanconfig::GetGoingDelay());
3066 Go->Terminate(false);
3067 return;
3070 if (FirstStep) {
3071 mPrevMoveDir = Go->GetDirection();
3072 Go->SetIsWalkingInOpen(!IsInCorridor(moveDir));
3075 game::DrawEverything();
3076 if (ivanconfig::GetGoingDelay()) DELAY(ivanconfig::GetGoingDelay());
3077 if (doStop) Go->Terminate(false);
3081 void character::SetTeam (team *What) {
3082 Team = What;
3083 What->Add(this);
3087 void character::ChangeTeam (team *What) {
3088 if (Team) Team->Remove(this);
3089 Team = What;
3090 SendNewDrawRequest();
3091 if (Team) Team->Add(this);
3095 truth character::ChangeRandomAttribute (int HowMuch) {
3096 for (int c = 0; c < 50; ++c) {
3097 int AttribID = RAND()%ATTRIBUTES;
3098 if (EditAttribute(AttribID, HowMuch)) return true;
3100 return false;
3104 int character::RandomizeReply (sLong &Said, int Replies) {
3105 truth NotSaid = false;
3106 for (int c = 0; c < Replies; ++c) {
3107 if (!(Said & (1 << c))) {
3108 NotSaid = true;
3109 break;
3112 if (!NotSaid) Said = 0;
3113 sLong ToSay;
3114 while (Said & 1 << (ToSay = RAND() % Replies));
3115 Said |= 1 << ToSay;
3116 return ToSay;
3120 void character::DisplayInfo (festring &Msg) {
3121 if (IsPlayer()) {
3122 Msg << " You are " << GetStandVerb() << " here.";
3123 } else {
3124 Msg << ' ' << GetName(INDEFINITE).CapitalizeCopy() << " is " << GetStandVerb() << " here. " << GetPersonalPronoun().CapitalizeCopy();
3125 cchar *Separator1 = GetAction() ? "," : " and";
3126 cchar *Separator2 = " and";
3127 if (GetTeam() == PLAYER->GetTeam()) {
3128 Msg << " is tame";
3129 } else {
3130 int Relation = GetRelation(PLAYER);
3131 if (Relation == HOSTILE) Msg << " is hostile";
3132 else if (Relation == UNCARING) {
3133 Msg << " does not care about you";
3134 Separator1 = Separator2 = " and is";
3135 } else {
3136 Msg << " is friendly";
3139 if (StateIsActivated(PANIC)) {
3140 Msg << Separator1 << " panicked";
3141 Separator2 = " and";
3143 if (GetAction()) Msg << Separator2 << ' ' << GetAction()->GetDescription();
3144 Msg << '.';
3149 void character::TestWalkability () {
3150 if (!IsEnabled()) return;
3151 square *SquareUnder = !game::IsInWilderness() ? GetSquareUnder() : PLAYER->GetSquareUnder();
3152 if (SquareUnder->IsFatalToStay() && !CanMoveOn(SquareUnder)) {
3153 truth Alive = false;
3154 if (!game::IsInWilderness() || IsPlayer()) {
3155 for (int d = 0; d < GetNeighbourSquares(); ++d) {
3156 square *Square = GetNeighbourSquare(d);
3157 if (Square && CanMoveOn(Square) && IsFreeForMe(Square)) {
3158 if (IsPlayer()) ADD_MESSAGE("%s.", SquareUnder->SurviveMessage(this));
3159 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s %s.", CHAR_NAME(DEFINITE), SquareUnder->MonsterSurviveMessage(this));
3160 Move(Square->GetPos(), true); // actually, this shouldn't be a teleport move
3161 SquareUnder->SurviveEffect(this);
3162 Alive = true;
3163 break;
3167 if (!Alive) {
3168 if (IsPlayer()) {
3169 Remove();
3170 SendToHell();
3171 festring DeathMsg = festring(SquareUnder->DeathMessage(this));
3172 game::AskForEscPress(DeathMsg+".");
3173 festring Msg = SquareUnder->ScoreEntry(this);
3174 PLAYER->AddScoreEntry(Msg);
3175 game::End(Msg);
3176 } else {
3177 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s %s.", CHAR_NAME(DEFINITE), SquareUnder->MonsterDeathVerb(this));
3178 Die(0, SquareUnder->ScoreEntry(this), DISALLOW_MSG);
3185 int character::GetSize () const {
3186 if (GetTorso()->GetSize() < 1) {
3187 fprintf(stderr, "WARNING: character::GetSize() is %d for %s!\n", GetTorso()->GetSize(), GetNameSingular().CStr());
3188 return 1;
3190 return GetTorso()->GetSize();
3194 void character::SetMainMaterial (material *NewMaterial, int SpecialFlags) {
3195 NewMaterial->SetVolume(GetBodyPart(0)->GetMainMaterial()->GetVolume());
3196 GetBodyPart(0)->SetMainMaterial(NewMaterial, SpecialFlags);
3197 for (int c = 1; c < BodyParts; ++c) {
3198 NewMaterial = NewMaterial->SpawnMore(GetBodyPart(c)->GetMainMaterial()->GetVolume());
3199 GetBodyPart(c)->SetMainMaterial(NewMaterial, SpecialFlags);
3204 void character::ChangeMainMaterial (material *NewMaterial, int SpecialFlags) {
3205 NewMaterial->SetVolume(GetBodyPart(0)->GetMainMaterial()->GetVolume());
3206 GetBodyPart(0)->ChangeMainMaterial(NewMaterial, SpecialFlags);
3207 for (int c = 1; c < BodyParts; ++c) {
3208 NewMaterial = NewMaterial->SpawnMore(GetBodyPart(c)->GetMainMaterial()->GetVolume());
3209 GetBodyPart(c)->ChangeMainMaterial(NewMaterial, SpecialFlags);
3214 void character::SetSecondaryMaterial (material *, int) {
3215 ABORT("Illegal character::SetSecondaryMaterial call!");
3219 void character::ChangeSecondaryMaterial (material *, int) {
3220 ABORT("Illegal character::ChangeSecondaryMaterial call!");
3224 void character::TeleportRandomly (truth Intentional) {
3225 v2 TelePos = ERROR_V2;
3226 if (StateIsActivated(TELEPORT_LOCK)) { ADD_MESSAGE("You flicker for a second."); return; }
3227 if (StateIsActivated(TELEPORT_CONTROL)) {
3228 if (IsPlayer()) {
3229 v2 Input = game::PositionQuestion(CONST_S("Where do you wish to teleport? [direction keys move cursor, space accepts]"), GetPos(), &game::TeleportHandler, 0, false);
3230 if (Input == ERROR_V2) Input = GetPos(); // esc pressed
3231 lsquare *Square = GetNearLSquare(Input);
3232 if (CanMoveOn(Square) || game::GoThroughWallsCheatIsActive()) {
3233 if (Square->GetPos() == GetPos()) {
3234 ADD_MESSAGE("You disappear and reappear.");
3235 return;
3237 if (IsFreeForMe(Square)) {
3238 if ((Input-GetPos()).GetLengthSquare() <= GetTeleportRangeSquare()) {
3239 EditExperience(INTELLIGENCE, 100, 1 << 10);
3240 TelePos = Input;
3241 } else {
3242 ADD_MESSAGE("You cannot concentrate yourself enough to control a teleport that far.");
3244 } else {
3245 character *C = Square->GetCharacter();
3246 if (C) ADD_MESSAGE("For a moment you feel very much like %s.", C->CHAR_NAME(INDEFINITE));
3247 else ADD_MESSAGE("You feel that something weird has happened, but can't really tell what exactly.");
3249 } else {
3250 ADD_MESSAGE("You feel like having been hit by something really hard from the inside.");
3252 } else if (!Intentional) {
3253 if (IsGoingSomeWhere() && GetLevel()->IsValidPos(GoingTo)) {
3254 v2 Where = GetLevel()->GetNearestFreeSquare(this, GoingTo);
3255 if (Where != ERROR_V2 && (Where-GetPos()).GetLengthSquare() <= GetTeleportRangeSquare()) {
3256 EditExperience(INTELLIGENCE, 100, 1 << 10);
3257 Where = TelePos;
3263 if (IsPlayer()) {
3264 ADD_MESSAGE("A rainbow-colored whirlpool twists the existence around you. You are sucked through a tunnel piercing a myriad of surreal universes. Luckily you return to this dimension in one piece.");
3267 //if (TelePos != ERROR_V2) Move(TelePos, true);
3268 //else Move(GetLevel()->GetRandomSquare(this), true);
3269 //if (!IsPlayer() && CanBeSeenByPlayer()) ADD_MESSAGE("%s appears.", CHAR_NAME(INDEFINITE));
3270 //if (GetAction() && GetAction()->IsVoluntary()) GetAction()->Terminate(false);
3272 if (TelePos == ERROR_V2) TelePos = GetLevel()->GetRandomSquare(this);
3274 room *PossibleRoom = game::GetCurrentLevel()->GetLSquare(TelePos)->GetRoom();
3276 if (!PossibleRoom) {
3277 //if it's outside of a room
3278 if (TelePos != ERROR_V2) Move(TelePos, true); else Move(GetLevel()->GetRandomSquare(this), true);
3279 if (!IsPlayer() && CanBeSeenByPlayer()) ADD_MESSAGE("%s appears.", CHAR_NAME(INDEFINITE));
3280 if (GetAction() && GetAction()->IsVoluntary()) GetAction()->Terminate(false);
3281 } else if (PossibleRoom && PossibleRoom->IsOKToTeleportInto()) {
3282 // If it's inside of a room, check whether a ward is active that might impede the player
3283 if (TelePos != ERROR_V2) Move(TelePos, true); else Move(GetLevel()->GetRandomSquare(this), true);
3284 if (!IsPlayer() && CanBeSeenByPlayer()) ADD_MESSAGE("%s appears.", CHAR_NAME(INDEFINITE));
3285 if (GetAction() && GetAction()->IsVoluntary()) GetAction()->Terminate(false);
3286 } else {
3287 if (IsPlayer()){
3288 ADD_MESSAGE("A mighty force blasts you back to where you were standing. A ward prevents you from teleporting.");
3290 game::GetCurrentLevel()->Explosion(this, CONST_S("killed by an explosion triggered when attempting to teleport into room protected by a ward"), PLAYER->GetPos(), 300 >> 3, false);
3292 beamdata Beam
3294 this,
3295 CONST_S("killed by an explosion triggered when attempting to teleport into room protected by a ward"),
3296 YOURSELF,
3297 3 // or 0 ?
3299 lsquare* Square = GetNearLSquare(GetPos());
3300 Square->DrawParticles(RED);
3301 Square->FireBall(Beam);*/
3306 void character::DoDetecting () {
3307 material *TempMaterial;
3309 for (;;) {
3310 festring Temp = game::DefaultQuestion(CONST_S("What material do you want to detect?"), game::GetDefaultDetectMaterial());
3311 TempMaterial = protosystem::CreateMaterial(Temp);
3312 if (TempMaterial) break;
3313 game::DrawEverythingNoBlit();
3316 level *Level = GetLevel();
3317 int Squares = Level->DetectMaterial(TempMaterial);
3319 if (Squares > GetAttribute(INTELLIGENCE) * (25+RAND()%51)) {
3320 ADD_MESSAGE("An enormous burst of geographical information overwhelms your consciousness. Your mind cannot cope with it and your memories blur.");
3321 Level->BlurMemory();
3322 BeginTemporaryState(CONFUSED, 1000 + RAND() % 1000);
3323 EditExperience(INTELLIGENCE, -100, 1 << 12);
3324 } else if (!Squares) {
3325 ADD_MESSAGE("You feel a sudden urge to imagine the dark void of a starless night sky.");
3326 EditExperience(INTELLIGENCE, 200, 1 << 12);
3327 } else {
3328 ADD_MESSAGE("You feel attracted to all things made of %s.", TempMaterial->GetName(false, false).CStr());
3329 game::PositionQuestion(CONST_S("Detecting material [direction keys move cursor, space exits]"), GetPos(), 0, 0, false);
3330 EditExperience(INTELLIGENCE, 300, 1 << 12);
3333 delete TempMaterial;
3334 Level->CalculateLuminances();
3335 game::SendLOSUpdateRequest();
3339 void character::RestoreHP () {
3340 doforbodyparts()(this, &bodypart::FastRestoreHP);
3341 HP = MaxHP;
3345 void character::RestoreLivingHP () {
3346 HP = 0;
3347 for (int c = 0; c < BodyParts; ++c) {
3348 bodypart *BodyPart = GetBodyPart(c);
3349 if (BodyPart && BodyPart->CanRegenerate()) {
3350 BodyPart->FastRestoreHP();
3351 HP += BodyPart->GetHP();
3357 truth character::AllowDamageTypeBloodSpill (int Type) {
3358 if ((Type&0xFFF) == PHYSICAL_DAMAGE) return true;
3359 if ((Type&0xFFF) == SOUND) return true;
3360 if ((Type&0xFFF) == ENERGY) return true;
3362 if ((Type&0xFFF) == ACID) return false;
3363 if ((Type&0xFFF) == FIRE) return false;
3364 if ((Type&0xFFF) == DRAIN) return false;
3365 if ((Type&0xFFF) == POISON) return false;
3366 if ((Type&0xFFF) == ELECTRICITY) return false;
3367 if ((Type&0xFFF) == MUSTARD_GAS_DAMAGE) return false;
3368 if ((Type&0xFFF) == PSI) return false;
3370 ABORT("Unknown blood effect destroyed the dungeon!");
3371 return false;
3375 /* Returns truly done damage */
3376 int character::ReceiveBodyPartDamage (character *Damager, int Damage, int Type, int BodyPartIndex,
3377 int Direction, truth PenetrateResistance, truth Critical, truth ShowNoDamageMsg, truth CaptureBodyPart)
3379 bodypart *BodyPart = GetBodyPart(BodyPartIndex);
3380 if (!Damager || Damager->AttackMayDamageArmor()) BodyPart->DamageArmor(Damager, Damage, Type);
3381 if (!PenetrateResistance) {
3382 Damage -= (BodyPart->GetTotalResistance(Type)>>1)+RAND()%((BodyPart->GetTotalResistance(Type)>>1)+1);
3384 if (int(Damage) < 1) {
3385 if (Critical) {
3386 Damage = 1;
3387 } else {
3388 if (ShowNoDamageMsg) {
3389 if (IsPlayer()) ADD_MESSAGE("You are not hurt.");
3390 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s is not hurt.", GetPersonalPronoun().CStr());
3392 return 0;
3396 if (Critical && AllowDamageTypeBloodSpill(Type) && !game::IsInWilderness()) {
3397 BodyPart->SpillBlood(2+(RAND()&1));
3398 for (int d = 0; d < GetNeighbourSquares(); ++d) {
3399 lsquare *Square = GetNeighbourLSquare(d);
3400 if (Square && Square->IsFlyable()) BodyPart->SpillBlood(1, Square->GetPos());
3404 if (BodyPart->ReceiveDamage(Damager, Damage, Type, Direction) && BodyPartCanBeSevered(BodyPartIndex)) {
3405 if (DamageTypeDestroysBodyPart(Type)) {
3406 if (IsPlayer()) ADD_MESSAGE("Your %s is destroyed!", BodyPart->GetBodyPartName().CStr());
3407 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s %s is destroyed!", GetPossessivePronoun().CStr(), BodyPart->GetBodyPartName().CStr());
3408 GetBodyPart(BodyPartIndex)->DropEquipment();
3409 item *Severed = SevereBodyPart(BodyPartIndex);
3410 if (Severed) Severed->DestroyBodyPart(!game::IsInWilderness() ? GetStackUnder() : GetStack());
3411 SendNewDrawRequest();
3412 if (IsPlayer()) game::AskForEscPress(CONST_S("Bodypart destroyed!"));
3413 } else {
3414 if (IsPlayer()) ADD_MESSAGE("Your %s is severed off!", BodyPart->GetBodyPartName().CStr());
3415 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s %s is severed off!", GetPossessivePronoun().CStr(), BodyPart->GetBodyPartName().CStr());
3416 item *Severed = SevereBodyPart(BodyPartIndex);
3417 SendNewDrawRequest();
3418 if (Severed) {
3419 if (CaptureBodyPart) {
3420 Damager->GetLSquareUnder()->AddItem(Severed);
3421 } else if (!game::IsInWilderness()) {
3422 /** No multi-tile humanoid support! */
3423 GetStackUnder()->AddItem(Severed);
3424 if (Direction != YOURSELF) Severed->Fly(0, Direction, Damage);
3425 } else {
3426 GetStack()->AddItem(Severed);
3428 Severed->DropEquipment();
3429 } else if (IsPlayer() || CanBeSeenByPlayer()) {
3430 ADD_MESSAGE("It vanishes.");
3432 if (IsPlayer()) game::AskForEscPress(CONST_S("Bodypart severed!"));
3434 if (CanPanicFromSeveredBodyPart() && RAND()%100 < GetPanicLevel() && !StateIsActivated(PANIC) && !IsDead() && !StateIsActivated(FEARLESS)) {
3435 BeginTemporaryState(PANIC, 1000+RAND()%1001);
3437 SpecialBodyPartSeverReaction();
3440 if (!IsDead()) CheckPanic(500);
3442 return Damage;
3446 /* Returns 0 if bodypart disappears */
3447 item *character::SevereBodyPart (int BodyPartIndex, truth ForceDisappearance, stack *EquipmentDropStack) {
3448 bodypart *BodyPart = GetBodyPart(BodyPartIndex);
3449 if (StateIsActivated(LEPROSY)) BodyPart->GetMainMaterial()->SetIsInfectedByLeprosy(true);
3450 if (ForceDisappearance || BodyPartsDisappearWhenSevered() || StateIsActivated(POLYMORPHED) || game::AllBodyPartsVanish()) {
3451 BodyPart->DropEquipment(EquipmentDropStack);
3452 BodyPart->RemoveFromSlot();
3453 CalculateAttributeBonuses();
3454 CalculateBattleInfo();
3455 BodyPart->SendToHell();
3456 SignalPossibleTransparencyChange();
3457 RemoveTraps(BodyPartIndex);
3458 return 0;
3460 BodyPart->SetOwnerDescription("of " + GetName(INDEFINITE));
3461 BodyPart->SetIsUnique(LeftOversAreUnique());
3462 UpdateBodyPartPicture(BodyPartIndex, true);
3463 BodyPart->RemoveFromSlot();
3464 BodyPart->RandomizePosition();
3465 CalculateAttributeBonuses();
3466 CalculateBattleInfo();
3467 BodyPart->Enable();
3468 SignalPossibleTransparencyChange();
3469 RemoveTraps(BodyPartIndex);
3470 return BodyPart;
3474 /* The second int is actually TargetFlags, which is not used here, but seems to be used in humanoid::ReceiveDamage.
3475 * Returns true if the character really receives damage */
3476 truth character::ReceiveDamage (character *Damager, int Damage, int Type, int, int Direction,
3477 truth, truth PenetrateArmor, truth Critical, truth ShowMsg)
3479 truth Affected = ReceiveBodyPartDamage(Damager, Damage, Type, 0, Direction, PenetrateArmor, Critical, ShowMsg);
3480 if (DamageTypeAffectsInventory(Type)) {
3481 for (int c = 0; c < GetEquipments(); ++c) {
3482 item *Equipment = GetEquipment(c);
3483 if (Equipment) Equipment->ReceiveDamage(Damager, Damage, Type);
3485 GetStack()->ReceiveDamage(Damager, Damage, Type);
3487 return Affected;
3491 festring character::GetDescription (int Case) const {
3492 if (IsPlayer()) return CONST_S("you");
3493 if (CanBeSeenByPlayer()) return GetName(Case);
3494 return CONST_S("something");
3498 festring character::GetPersonalPronoun (truth PlayersView) const {
3499 if (IsPlayer() && PlayersView) return CONST_S("you");
3500 if (GetSex() == UNDEFINED || (PlayersView && !CanBeSeenByPlayer() && !game::GetSeeWholeMapCheatMode())) return CONST_S("it");
3501 if (GetSex() == MALE) return CONST_S("he");
3502 return CONST_S("she");
3506 festring character::GetPossessivePronoun (truth PlayersView) const {
3507 if (IsPlayer() && PlayersView) return CONST_S("your");
3508 if (GetSex() == UNDEFINED || (PlayersView && !CanBeSeenByPlayer() && !game::GetSeeWholeMapCheatMode())) return CONST_S("its");
3509 if (GetSex() == MALE) return CONST_S("his");
3510 return CONST_S("her");
3514 festring character::GetObjectPronoun (truth PlayersView) const {
3515 if (IsPlayer() && PlayersView) return CONST_S("you");
3516 if (GetSex() == UNDEFINED || (PlayersView && !CanBeSeenByPlayer() && !game::GetSeeWholeMapCheatMode())) return CONST_S("it");
3517 if (GetSex() == MALE) return CONST_S("him");
3518 return CONST_S("her");
3522 void character::AddName (festring &String, int Case) const {
3523 if (AssignedName.IsEmpty()) {
3524 id::AddName(String, Case);
3525 } else if (!(Case & PLURAL)) {
3526 if (!ShowClassDescription()) {
3527 String << AssignedName;
3528 } else {
3529 String << AssignedName << ' ';
3530 id::AddName(String, (Case|ARTICLE_BIT)&~INDEFINE_BIT);
3532 } else {
3533 id::AddName(String, Case);
3534 String << " named " << AssignedName;
3539 int character::GetHungerState () const {
3540 if (!UsesNutrition()) return NOT_HUNGRY;
3541 if (GetNP() > OVER_FED_LEVEL) return OVER_FED;
3542 if (GetNP() > BLOATED_LEVEL) return BLOATED;
3543 if (GetNP() > SATIATED_LEVEL) return SATIATED;
3544 if (GetNP() > NOT_HUNGER_LEVEL) return NOT_HUNGRY;
3545 if (GetNP() > HUNGER_LEVEL) return HUNGRY;
3546 if (GetNP() > VERY_HUNGER_LEVEL) return VERY_HUNGRY;
3547 return STARVING;
3551 truth character::CanConsume (material *Material) const {
3552 return GetConsumeFlags() & Material->GetConsumeType();
3556 void character::SetTemporaryStateCounter (sLong State, int What) {
3557 for (int c = 0; c < STATES; ++c) {
3558 if ((1 << c) & State) TemporaryStateCounter[c] = What;
3563 void character::EditTemporaryStateCounter (sLong State, int What) {
3564 for (int c = 0; c < STATES; ++c) {
3565 if ((1 << c) & State) TemporaryStateCounter[c] += What;
3570 int character::GetTemporaryStateCounter (sLong State) const {
3571 for (int c = 0; c < STATES; ++c) {
3572 if ((1 << c) & State) return TemporaryStateCounter[c];
3574 ABORT("Illegal GetTemporaryStateCounter request!");
3575 return 0;
3579 truth character::CheckKick () const {
3580 if (!CanKick()) {
3581 if (IsPlayer()) ADD_MESSAGE("This race can't kick.");
3582 return false;
3584 return true;
3588 int character::GetResistance (int Type) const {
3589 if ((Type&0xFFF) == PHYSICAL_DAMAGE) return 0;
3590 if ((Type&0xFFF) == DRAIN) return 0;
3591 if ((Type&0xFFF) == MUSTARD_GAS_DAMAGE) return 0;
3592 if ((Type&0xFFF) == PSI) return 0;
3594 if ((Type&0xFFF) == ENERGY) return GetEnergyResistance();
3595 if ((Type&0xFFF) == FIRE) return GetFireResistance();
3596 if ((Type&0xFFF) == POISON) return GetPoisonResistance();
3597 if ((Type&0xFFF) == ELECTRICITY) return GetElectricityResistance();
3598 if ((Type&0xFFF) == ACID) return GetAcidResistance();
3599 if ((Type&0xFFF) == SOUND) return GetSoundResistance();
3601 ABORT("Resistance lack detected!");
3602 return 0;
3606 void character::Regenerate () {
3607 if (HP == MaxHP) {
3608 if (StateIsActivated(REGENERATION) && !(RAND()%3000)) {
3609 bodypart *NewBodyPart = GenerateRandomBodyPart();
3610 if (!NewBodyPart) return;
3611 NewBodyPart->SetHP(1);
3612 if (IsPlayer()) ADD_MESSAGE("You grow a new %s.", NewBodyPart->GetBodyPartName().CStr());
3613 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s grows a new %s.", CHAR_NAME(DEFINITE), NewBodyPart->GetBodyPartName().CStr());
3615 return;
3617 sLong RegenerationBonus = 0;
3618 truth NoHealableBodyParts = true;
3619 for (int c = 0; c < BodyParts; ++c) {
3620 bodypart *BodyPart = GetBodyPart(c);
3621 if (BodyPart && BodyPart->CanRegenerate()) {
3622 RegenerationBonus += BodyPart->GetMaxHP();
3623 if (NoHealableBodyParts && BodyPart->GetHP() < BodyPart->GetMaxHP()) NoHealableBodyParts = false;
3626 if (!RegenerationBonus || NoHealableBodyParts) return;
3627 RegenerationBonus *= (50+GetAttribute(ENDURANCE));
3629 if (Action && Action->IsRest()) {
3630 if (SquaresUnder == 1) RegenerationBonus *= GetSquareUnder()->GetRestModifier() << 1;
3631 else {
3632 int Lowest = GetSquareUnder(0)->GetRestModifier();
3633 for (int c = 1; c < GetSquaresUnder(); ++c) {
3634 int Mod = GetSquareUnder(c)->GetRestModifier();
3635 if (Mod < Lowest) Lowest = Mod;
3637 RegenerationBonus *= Lowest << 1;
3641 RegenerationCounter += RegenerationBonus;
3643 while (RegenerationCounter > 1250000) {
3644 bodypart *BodyPart = HealHitPoint();
3645 if (!BodyPart) break;
3646 EditNP(-Max(7500/MaxHP, 1));
3647 RegenerationCounter -= 1250000;
3648 int HP = BodyPart->GetHP();
3649 EditExperience(ENDURANCE, Min(1000*BodyPart->GetMaxHP()/(HP*HP), 300), 1000);
3654 void character::PrintInfo () const {
3655 felist Info(CONST_S("Information about ")+GetName(DEFINITE));
3656 for (int c = 0; c < GetEquipments(); ++c) {
3657 item *Equipment = GetEquipment(c);
3658 if ((EquipmentEasilyRecognized(c) || game::WizardModeIsActive()) && Equipment) {
3659 int ImageKey = game::AddToItemDrawVector(itemvector(1, Equipment));
3660 Info.AddEntry(festring(GetEquipmentName(c))+": "+Equipment->GetName(INDEFINITE), LIGHT_GRAY, 0, ImageKey, true);
3663 if (Info.IsEmpty()) {
3664 ADD_MESSAGE("There's nothing special to tell about %s.", CHAR_NAME(DEFINITE));
3665 } else {
3666 game::SetStandardListAttributes(Info);
3667 Info.SetEntryDrawer(game::ItemEntryDrawer);
3668 Info.Draw();
3670 game::ClearItemDrawVector();
3674 truth character::TryToRiseFromTheDead () {
3675 for (int c = 0; c < BodyParts; ++c) {
3676 bodypart *BodyPart = GetBodyPart(c);
3677 if (BodyPart) {
3678 BodyPart->ResetSpoiling();
3679 if (BodyPart->CanRegenerate() || BodyPart->GetHP() < 1) BodyPart->SetHP(1);
3682 ResetStates();
3683 return true;
3687 truth character::RaiseTheDead (character *) {
3688 truth Useful = false;
3689 for (int c = 0; c < BodyParts; ++c) {
3690 bodypart *BodyPart = GetBodyPart(c);
3691 if (!BodyPart && CanCreateBodyPart(c)) {
3692 CreateBodyPart(c)->SetHP(1);
3693 if (IsPlayer()) ADD_MESSAGE("Suddenly you grow a new %s.", GetBodyPartName(c).CStr());
3694 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s grows a new %s.", CHAR_NAME(DEFINITE), GetBodyPartName(c).CStr());
3695 Useful = true;
3696 } else if (BodyPart && BodyPart->CanRegenerate() && BodyPart->GetHP() < 1) {
3697 BodyPart->SetHP(1);
3700 if (!Useful) {
3701 if (IsPlayer()) ADD_MESSAGE("You shudder.");
3702 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s shudders.", CHAR_NAME(DEFINITE));
3704 return Useful;
3708 void character::SetSize (int Size) {
3709 for (int c = 0; c < BodyParts; ++c) {
3710 bodypart *BodyPart = GetBodyPart(c);
3711 if (BodyPart) BodyPart->SetSize(GetBodyPartSize(c, Size));
3716 sLong character::GetBodyPartSize (int I, int TotalSize) const {
3717 if (I == TORSO_INDEX) return TotalSize;
3718 ABORT("Weird bodypart size request for a character!");
3719 return 0;
3723 sLong character::GetBodyPartVolume (int I) const {
3724 if (I == TORSO_INDEX) return GetTotalVolume();
3725 ABORT("Weird bodypart volume request for a character!");
3726 return 0;
3730 void character::CreateBodyParts (int SpecialFlags) {
3731 for (int c = 0; c < BodyParts; ++c) if (CanCreateBodyPart(c)) CreateBodyPart(c, SpecialFlags);
3735 void character::RestoreBodyParts () {
3736 for (int c = 0; c < BodyParts; ++c) if (!GetBodyPart(c) && CanCreateBodyPart(c)) CreateBodyPart(c);
3740 void character::UpdatePictures () {
3741 if (!PictureUpdatesAreForbidden()) for (int c = 0; c < BodyParts; ++c) UpdateBodyPartPicture(c, false);
3745 bodypart *character::MakeBodyPart (int I) const {
3746 if (I == TORSO_INDEX) return normaltorso::Spawn(0, NO_MATERIALS);
3747 ABORT("Weird bodypart to make for a character!");
3748 return 0;
3752 bodypart *character::CreateBodyPart (int I, int SpecialFlags) {
3753 bodypart *BodyPart = MakeBodyPart(I);
3754 material *Material = CreateBodyPartMaterial(I, GetBodyPartVolume(I));
3755 BodyPart->InitMaterials(Material, false);
3756 BodyPart->SetSize(GetBodyPartSize(I, GetTotalSize()));
3757 BodyPart->SetBloodMaterial(GetBloodMaterial());
3758 BodyPart->SetNormalMaterial(Material->GetConfig());
3759 BodyPart->SetHP(1);
3760 SetBodyPart(I, BodyPart);
3761 BodyPart->InitSpecialAttributes();
3762 if (!(SpecialFlags & NO_PIC_UPDATE)) UpdateBodyPartPicture(I, false);
3763 if (!IsInitializing()) {
3764 CalculateBattleInfo();
3765 SendNewDrawRequest();
3766 SignalPossibleTransparencyChange();
3768 return BodyPart;
3772 v2 character::GetBodyPartBitmapPos (int I, truth) const {
3773 if (I == TORSO_INDEX) return GetTorsoBitmapPos();
3774 ABORT("Weird bodypart BitmapPos request for a character!");
3775 return v2();
3779 void character::UpdateBodyPartPicture (int I, truth Severed) {
3780 bodypart *BP = GetBodyPart(I);
3781 if (BP) {
3782 BP->SetBitmapPos(GetBodyPartBitmapPos(I, Severed));
3783 BP->GetMainMaterial()->SetSkinColor(GetBodyPartColorA(I, Severed));
3784 BP->GetMainMaterial()->SetSkinColorIsSparkling(GetBodyPartSparkleFlags(I) & SPARKLING_A);
3785 BP->SetMaterialColorB(GetBodyPartColorB(I, Severed));
3786 BP->SetMaterialColorC(GetBodyPartColorC(I, Severed));
3787 BP->SetMaterialColorD(GetBodyPartColorD(I, Severed));
3788 BP->SetSparkleFlags(GetBodyPartSparkleFlags(I));
3789 BP->SetSpecialFlags(GetSpecialBodyPartFlags(I));
3790 BP->SetWobbleData(GetBodyPartWobbleData(I));
3791 BP->UpdatePictures();
3796 void character::LoadDataBaseStats () {
3797 for (int c = 0; c < BASE_ATTRIBUTES; ++c) {
3798 BaseExperience[c] = DataBase->NaturalExperience[c];
3799 if (BaseExperience[c]) LimitRef(BaseExperience[c], MIN_EXP, MAX_EXP);
3801 SetMoney(GetDefaultMoney());
3802 SetInitialSweatMaterial(GetSweatMaterial());
3803 const fearray<sLong> &Skills = GetKnownCWeaponSkills();
3804 if (Skills.Size) {
3805 const fearray<sLong> &Hits = GetCWeaponSkillHits();
3806 if (Hits.Size == 1) {
3807 for (uInt c = 0; c < Skills.Size; ++c) {
3808 if (Skills[c] < AllowedWeaponSkillCategories) CWeaponSkill[Skills[c]].AddHit(Hits[0]*100);
3810 } else if (Hits.Size == Skills.Size) {
3811 for (uInt c = 0; c < Skills.Size; ++c) {
3812 if (Skills[c] < AllowedWeaponSkillCategories) CWeaponSkill[Skills[c]].AddHit(Hits[c]*100);
3814 } else {
3815 ABORT("Illegal weapon skill hit array size detected!");
3821 character *characterprototype::SpawnAndLoad (inputfile &SaveFile) const {
3822 character *Char = Spawner(0, LOAD);
3823 Char->Load(SaveFile);
3824 Char->CalculateAll();
3825 return Char;
3829 void character::Initialize (int NewConfig, int SpecialFlags) {
3830 Flags |= C_INITIALIZING|C_IN_NO_MSG_MODE;
3831 CalculateBodyParts();
3832 CalculateAllowedWeaponSkillCategories();
3833 CalculateSquaresUnder();
3834 BodyPartSlot = new bodypartslot[BodyParts];
3835 OriginalBodyPartID = new std::list<feuLong>[BodyParts];
3836 CWeaponSkill = new cweaponskill[AllowedWeaponSkillCategories];
3837 SquareUnder = new square*[SquaresUnder];
3839 if (SquaresUnder == 1) *SquareUnder = 0; else memset(SquareUnder, 0, SquaresUnder*sizeof(square *));
3841 for (int c = 0; c < BodyParts; ++c) BodyPartSlot[c].SetMaster(this);
3843 if (!(SpecialFlags & LOAD)) {
3844 ID = game::CreateNewCharacterID(this);
3845 databasecreator<character>::InstallDataBase(this, NewConfig);
3846 LoadDataBaseStats();
3847 TemporaryState |= GetClassStates();
3848 if (TemporaryState) {
3849 for (int c = 0; c < STATES; ++c) if (TemporaryState & (1 << c)) TemporaryStateCounter[c] = PERMANENT;
3852 CreateBodyParts(SpecialFlags | NO_PIC_UPDATE);
3853 InitSpecialAttributes();
3854 CommandFlags = GetDefaultCommandFlags();
3856 if (GetAttribute(INTELLIGENCE, false) < 8) CommandFlags &= ~DONT_CONSUME_ANYTHING_VALUABLE; // gum
3857 if (!GetDefaultName().IsEmpty()) SetAssignedName(GetDefaultName());
3860 if (!(SpecialFlags & LOAD)) PostConstruct();
3862 if (!(SpecialFlags & LOAD)) {
3863 if (!(SpecialFlags & NO_EQUIPMENT)) CreateInitialEquipment((SpecialFlags & NO_EQUIPMENT_PIC_UPDATE) >> 1);
3864 if (!(SpecialFlags & NO_PIC_UPDATE)) UpdatePictures();
3865 CalculateAll();
3866 RestoreHP();
3867 RestoreStamina();
3870 Flags &= ~(C_INITIALIZING|C_IN_NO_MSG_MODE);
3874 truth character::TeleportNear (character *Caller) {
3875 v2 Where = GetLevel()->GetNearestFreeSquare(this, Caller->GetPos());
3876 if (Where == ERROR_V2) return false;
3877 Move(Where, true);
3878 return true;
3882 void character::ReceiveHeal (sLong Amount) {
3883 int c;
3884 for (c = 0; c < Amount / 10; ++c) if (!HealHitPoint()) break;
3885 Amount -= c*10;
3886 if (RAND()%10 < Amount) HealHitPoint();
3887 if (Amount >= 250 || RAND()%250 < Amount) {
3888 bodypart *NewBodyPart = GenerateRandomBodyPart();
3889 if (!NewBodyPart) return;
3890 NewBodyPart->SetHP(1);
3891 if (IsPlayer()) ADD_MESSAGE("You grow a new %s.", NewBodyPart->GetBodyPartName().CStr());
3892 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s grows a new %s.", CHAR_NAME(DEFINITE), NewBodyPart->GetBodyPartName().CStr());
3897 void character::AddHealingLiquidConsumeEndMessage () const {
3898 if (IsPlayer()) ADD_MESSAGE("You feel better.");
3899 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks healthier.", CHAR_NAME(DEFINITE));
3903 void character::ReceiveSchoolFood (sLong SizeOfEffect) {
3904 SizeOfEffect += RAND()%SizeOfEffect;
3905 if (SizeOfEffect >= 250) VomitAtRandomDirection(SizeOfEffect);
3906 if (!(RAND() % 3) && SizeOfEffect >= 500 && EditAttribute(ENDURANCE, SizeOfEffect/500)) {
3907 if (IsPlayer()) ADD_MESSAGE("You gain a little bit of toughness for surviving this stuff.");
3908 else if (CanBeSeenByPlayer()) ADD_MESSAGE("Suddenly %s looks tougher.", CHAR_NAME(DEFINITE));
3910 BeginTemporaryState(POISONED, (SizeOfEffect>>1));
3914 void character::AddSchoolFoodConsumeEndMessage () const {
3915 if (IsPlayer()) ADD_MESSAGE("Yuck! This stuff tasted like vomit and old mousepads.");
3919 void character::AddSchoolFoodHitMessage () const {
3920 if (IsPlayer()) ADD_MESSAGE("Yuck! This stuff feels like vomit and old mousepads.");
3924 void character::ReceiveNutrition (sLong SizeOfEffect) {
3925 EditNP(SizeOfEffect);
3929 void character::ReceiveOmmelUrine (sLong Amount) {
3930 EditExperience(ARM_STRENGTH, 500, Amount<<4);
3931 EditExperience(LEG_STRENGTH, 500, Amount<<4);
3932 if (IsPlayer()) game::DoEvilDeed(Amount/25);
3936 void character::ReceiveOmmelCerumen (sLong Amount) {
3937 EditExperience(INTELLIGENCE, 500, Amount << 5);
3938 EditExperience(WISDOM, 500, Amount << 5);
3939 if (IsPlayer()) game::DoEvilDeed(Amount / 25);
3943 void character::ReceiveOmmelSweat (sLong Amount) {
3944 EditExperience(AGILITY, 500, Amount << 4);
3945 EditExperience(DEXTERITY, 500, Amount << 4);
3946 RestoreStamina();
3947 if (IsPlayer()) game::DoEvilDeed(Amount / 25);
3951 void character::ReceiveOmmelTears (sLong Amount) {
3952 EditExperience(PERCEPTION, 500, Amount << 4);
3953 EditExperience(CHARISMA, 500, Amount << 4);
3954 if (IsPlayer()) game::DoEvilDeed(Amount / 25);
3958 void character::ReceiveOmmelSnot (sLong Amount) {
3959 EditExperience(ENDURANCE, 500, Amount << 5);
3960 RestoreLivingHP();
3961 if (IsPlayer()) game::DoEvilDeed(Amount / 25);
3965 void character::ReceiveOmmelBone (sLong Amount) {
3966 EditExperience(ARM_STRENGTH, 500, Amount << 6);
3967 EditExperience(LEG_STRENGTH, 500, Amount << 6);
3968 EditExperience(DEXTERITY, 500, Amount << 6);
3969 EditExperience(AGILITY, 500, Amount << 6);
3970 EditExperience(ENDURANCE, 500, Amount << 6);
3971 EditExperience(PERCEPTION, 500, Amount << 6);
3972 EditExperience(INTELLIGENCE, 500, Amount << 6);
3973 EditExperience(WISDOM, 500, Amount << 6);
3974 EditExperience(CHARISMA, 500, Amount << 6);
3975 RestoreLivingHP();
3976 RestoreStamina();
3977 if (IsPlayer()) game::DoEvilDeed(Amount / 25);
3981 void character::AddOmmelConsumeEndMessage () const {
3982 if (IsPlayer()) ADD_MESSAGE("You feel a primitive force coursing through your veins.");
3983 else if (CanBeSeenByPlayer()) ADD_MESSAGE("Suddenly %s looks more powerful.", CHAR_NAME(DEFINITE));
3987 void character::ReceivePepsi (sLong Amount) {
3988 ReceiveDamage(0, Amount / 100, POISON, TORSO);
3989 EditExperience(PERCEPTION, Amount, 1 << 14);
3990 if (CheckDeath(CONST_S("was poisoned by pepsi"), 0)) return;
3991 if (IsPlayer()) game::DoEvilDeed(Amount / 10);
3995 void character::AddPepsiConsumeEndMessage () const {
3996 if (IsPlayer()) ADD_MESSAGE("Urgh. You feel your guruism fading away.");
3997 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks very lame.", CHAR_NAME(DEFINITE));
4001 void character::ReceiveDarkness (sLong Amount) {
4002 EditExperience(INTELLIGENCE, -Amount / 5, 1 << 13);
4003 EditExperience(WISDOM, -Amount / 5, 1 << 13);
4004 EditExperience(CHARISMA, -Amount / 5, 1 << 13);
4005 if (IsPlayer()) game::DoEvilDeed(int(Amount / 50));
4009 void character::AddFrogFleshConsumeEndMessage () const {
4010 if (IsPlayer()) ADD_MESSAGE("Arg. You feel the fate of a navastater placed upon you...");
4011 else if (CanBeSeenByPlayer()) ADD_MESSAGE("Suddenly %s looks like a navastater.", CHAR_NAME(DEFINITE));
4015 void character::ReceiveKoboldFlesh (sLong) {
4016 /* As it is commonly known, the possibility of fainting per 500 cubic
4017 centimeters of kobold flesh is exactly 5%. */
4018 if (!(RAND() % 20)) {
4019 if (IsPlayer()) ADD_MESSAGE("You lose control of your legs and fall down.");
4020 LoseConsciousness(250 + RAND_N(250));
4025 void character::AddKoboldFleshConsumeEndMessage () const {
4026 if (IsPlayer()) ADD_MESSAGE("This stuff tasted really funny.");
4030 void character::AddKoboldFleshHitMessage () const {
4031 if (IsPlayer()) ADD_MESSAGE("You feel very funny.");
4035 void character::AddBoneConsumeEndMessage () const {
4036 if (IsPlayer()) ADD_MESSAGE("You feel like a hippie.");
4037 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s barks happily.", CHAR_NAME(DEFINITE)); // this suspects that nobody except dogs can eat bones
4040 truth character::RawEditAttribute (double &Experience, int Amount) const {
4041 /* Check if the attribute is disabled for creature */
4042 if (!Experience) return false;
4043 if ((Amount < 0 && Experience < 2 * EXP_MULTIPLIER) || (Amount > 0 && Experience > 999 * EXP_MULTIPLIER)) return false;
4044 Experience += Amount * EXP_MULTIPLIER;
4045 LimitRef<double>(Experience, MIN_EXP, MAX_EXP);
4046 return true;
4050 void character::DrawPanel (truth AnimationDraw) const {
4051 if (AnimationDraw) { DrawStats(true); return; }
4052 igraph::BlitBackGround(v2(19 + (game::GetScreenXSize() << 4), 0), v2(RES.X - 19 - (game::GetScreenXSize() << 4), RES.Y));
4053 igraph::BlitBackGround(v2(16, 45 + (game::GetScreenYSize() << 4)), v2(game::GetScreenXSize() << 4, 9));
4054 FONT->Printf(DOUBLE_BUFFER, v2(16, 45 + (game::GetScreenYSize() << 4)), WHITE, "%s", GetPanelName().CStr());
4055 game::UpdateAttributeMemory();
4056 int PanelPosX = RES.X - 96;
4057 int PanelPosY = DrawStats(false);
4058 PrintAttribute("End", ENDURANCE, PanelPosX, PanelPosY++);
4059 PrintAttribute("Per", PERCEPTION, PanelPosX, PanelPosY++);
4060 PrintAttribute("Int", INTELLIGENCE, PanelPosX, PanelPosY++);
4061 PrintAttribute("Wis", WISDOM, PanelPosX, PanelPosY++);
4062 PrintAttribute("Wil", WILL_POWER, PanelPosX, PanelPosY++);
4063 PrintAttribute("Cha", CHARISMA, PanelPosX, PanelPosY++);
4064 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Siz %d", GetSize());
4065 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), IsInBadCondition() ? RED : WHITE, "HP %d/%d", GetHP(), GetMaxHP());
4066 ++PanelPosY;
4067 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Gold: %d", GetMoney());
4068 ++PanelPosY;
4070 if (game::IsInWilderness())
4071 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Worldmap");
4072 else
4073 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "%s", game::GetCurrentDungeon()->GetShortLevelDescription(game::GetCurrentLevelIndex()).CapitalizeCopy().CStr());
4075 ivantime Time;
4076 game::GetTime(Time);
4077 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Day %d", Time.Day);
4078 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Time %d:%s%d", Time.Hour, Time.Min < 10 ? "0" : "", Time.Min);
4079 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Turn %d", game::GetTurn());
4081 ++PanelPosY;
4083 if (GetAction()) {
4084 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "%s", festring(GetAction()->GetDescription()).CapitalizeCopy().CStr());
4087 //printf("========= STATES =========\n");
4088 for (int c = 0; c < STATES; ++c) {
4089 //printf(" %d: %s (%s)\n", c, StateData[c].Description, (StateIsActivated(1<<c) ? "TAN" : "ona"));
4090 if (!(StateData[c].Flags & SECRET) && StateIsActivated(1 << c) && (1 << c != HASTE || !StateIsActivated(SLOW)) && (1 << c != SLOW || !StateIsActivated(HASTE))) {
4091 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), (1 << c) & EquipmentState || TemporaryStateCounter[c] >= PERMANENT ? BLUE : WHITE, "%s", StateData[c].Description);
4095 auto hst = GetHungerState();
4096 if (hst == STARVING) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), RED, "Starving");
4097 else if (hst == VERY_HUNGRY) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), RED, "Very hungry");
4098 else if (hst == HUNGRY) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), ORANGE, "Hungry");
4099 else if (hst == SATIATED) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Satiated");
4100 else if (hst == BLOATED) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Bloated");
4101 else if (hst == OVER_FED) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "Overfed!");
4103 auto bst = GetBurdenState();
4104 if (bst == OVER_LOADED) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), RED, "Overload!");
4105 else if (bst == STRESSED) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), ORANGE, "Stressed");
4106 else if (bst == BURDENED) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), BLUE, "Burdened");
4108 auto trst = GetTirednessState();
4109 if (trst == FAINTING) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), RED, "Fainting");
4110 else if (trst == EXHAUSTED) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), ORANGE, "Exhausted");
4112 if (game::PlayerIsRunning()) {
4113 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "%s", GetRunDescriptionLine(0));
4114 cchar *SecondLine = GetRunDescriptionLine(1);
4115 if (strlen(SecondLine)) FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY++ * 10), WHITE, "%s", SecondLine);
4120 void character::CalculateDodgeValue () {
4121 DodgeValue = 0.05 * GetMoveEase() * GetAttribute(AGILITY) / sqrt(GetSize());
4122 if (IsFlying()) DodgeValue *= 2;
4123 if (DodgeValue < 1) DodgeValue = 1;
4127 truth character::DamageTypeAffectsInventory (int Type) {
4128 if ((Type&0xFFF) == SOUND) return true;
4129 if ((Type&0xFFF) == ENERGY) return true;
4130 if ((Type&0xFFF) == ACID) return true;
4131 if ((Type&0xFFF) == FIRE) return true;
4132 if ((Type&0xFFF) == ELECTRICITY) return true;
4134 if ((Type&0xFFF) == PHYSICAL_DAMAGE) return false;
4135 if ((Type&0xFFF) == POISON) return false;
4136 if ((Type&0xFFF) == DRAIN) return false;
4137 if ((Type&0xFFF) == MUSTARD_GAS_DAMAGE) return false;
4138 if ((Type&0xFFF) == PSI) return false;
4140 ABORT("Unknown reaping effect destroyed dungeon!");
4141 return false;
4145 int character::CheckForBlockWithArm (character *Enemy, item *Weapon, arm *Arm,
4146 double WeaponToHitValue, int Damage, int Success, int Type)
4148 int BlockStrength = Arm->GetBlockCapability();
4149 double BlockValue = Arm->GetBlockValue();
4150 if (BlockStrength && BlockValue) {
4151 item *Blocker = Arm->GetWielded();
4152 if (RAND() % int(100+WeaponToHitValue/BlockValue/(1<<BlocksSinceLastTurn)*(100+Success)) < 100) {
4153 int NewDamage = BlockStrength < Damage ? Damage-BlockStrength : 0;
4154 if (Type == UNARMED_ATTACK) AddBlockMessage(Enemy, Blocker, Enemy->UnarmedHitNoun(), NewDamage);
4155 else if (Type == WEAPON_ATTACK) AddBlockMessage(Enemy, Blocker, "attack", NewDamage);
4156 else if (Type == KICK_ATTACK) AddBlockMessage(Enemy, Blocker, Enemy->KickNoun(), NewDamage);
4157 else if (Type == BITE_ATTACK) AddBlockMessage(Enemy, Blocker, Enemy->BiteNoun(), NewDamage);
4158 sLong Weight = Blocker->GetWeight();
4159 sLong StrExp = Limit(15 * Weight / 200, 75, 300);
4160 sLong DexExp = Weight ? Limit(75000 / Weight, 75, 300) : 300;
4161 Arm->EditExperience(ARM_STRENGTH, StrExp, 1 << 8);
4162 Arm->EditExperience(DEXTERITY, DexExp, 1 << 8);
4163 EditStamina(-10000 / GetAttribute(ARM_STRENGTH), false);
4164 if (Arm->TwoHandWieldIsActive()) {
4165 arm *PairArm = Arm->GetPairArm();
4166 PairArm->EditExperience(ARM_STRENGTH, StrExp, 1 << 8);
4167 PairArm->EditExperience(DEXTERITY, DexExp, 1 << 8);
4169 Blocker->WeaponSkillHit(Enemy->CalculateWeaponSkillHits(this));
4170 Blocker->ReceiveDamage(this, Damage, PHYSICAL_DAMAGE);
4171 Blocker->BlockEffect(this, Enemy, Weapon, Type);
4172 if (Weapon) Weapon->ReceiveDamage(Enemy, Damage - NewDamage, PHYSICAL_DAMAGE);
4173 if (BlocksSinceLastTurn < 16) ++BlocksSinceLastTurn;
4174 return NewDamage;
4177 return Damage;
4181 sLong character::GetStateAPGain (sLong BaseAPGain) const {
4182 if (!StateIsActivated(HASTE) == !StateIsActivated(SLOW)) return BaseAPGain;
4183 if (StateIsActivated(HASTE)) return (BaseAPGain * 5) >> 2;
4184 return (BaseAPGain << 2) / 5;
4188 void character::SignalEquipmentAdd (int EquipmentIndex) {
4189 item *Equipment = GetEquipment(EquipmentIndex);
4190 if (Equipment->IsInCorrectSlot(EquipmentIndex)) {
4191 sLong AddedStates = Equipment->GetGearStates();
4192 if (AddedStates) {
4193 for (int c = 0; c < STATES; ++c) {
4194 if (AddedStates & (1 << c)) {
4195 if (!StateIsActivated(1 << c)) {
4196 if (!IsInNoMsgMode()) (this->*StateData[c].PrintBeginMessage)();
4197 EquipmentState |= 1 << c;
4198 if (StateData[c].BeginHandler) (this->*StateData[c].BeginHandler)();
4199 } else {
4200 EquipmentState |= 1 << c;
4206 if (!IsInitializing() && Equipment->IsInCorrectSlot(EquipmentIndex)) ApplyEquipmentAttributeBonuses(Equipment);
4210 void character::SignalEquipmentRemoval (int, citem *Item) {
4211 CalculateEquipmentState();
4212 if (CalculateAttributeBonuses()) CheckDeath(festring("lost ")+GetPossessivePronoun(false)+" vital "+Item->GetName(INDEFINITE));
4216 void character::CalculateEquipmentState () {
4217 sLong Back = EquipmentState;
4218 EquipmentState = 0;
4219 for (int c = 0; c < GetEquipments(); ++c) {
4220 item *Equipment = GetEquipment(c);
4221 if (Equipment && Equipment->IsInCorrectSlot(c)) EquipmentState |= Equipment->GetGearStates();
4223 for (int c = 0; c < STATES; ++c) {
4224 if (Back & (1 << c) && !StateIsActivated(1 << c)) {
4225 if (StateData[c].EndHandler) {
4226 (this->*StateData[c].EndHandler)();
4227 if (!IsEnabled()) return;
4229 if (!IsInNoMsgMode()) (this->*StateData[c].PrintEndMessage)();
4235 /* Counter = duration in ticks */
4236 void character::BeginTemporaryState (sLong State, int Counter) {
4237 if (!Counter) return;
4238 int Index;
4239 if (State == POLYMORPHED) ABORT("No Polymorphing with BeginTemporaryState!");
4240 for (Index = 0; Index < STATES; ++Index) if (1 << Index == State) break;
4241 if (Index == STATES) ABORT("BeginTemporaryState works only when State == 2^n!");
4242 if (TemporaryStateIsActivated(State)) {
4243 int OldCounter = GetTemporaryStateCounter(State);
4244 if (OldCounter != PERMANENT) EditTemporaryStateCounter(State, Max(Counter, 50-OldCounter));
4245 } else if (StateData[Index].IsAllowed == 0 || (this->*StateData[Index].IsAllowed)()) {
4246 SetTemporaryStateCounter(State, Max(Counter, 50));
4247 if (!EquipmentStateIsActivated(State)) {
4248 if (!IsInNoMsgMode()) (this->*StateData[Index].PrintBeginMessage)();
4249 ActivateTemporaryState(State);
4250 if (StateData[Index].BeginHandler) (this->*StateData[Index].BeginHandler)();
4251 } else {
4252 ActivateTemporaryState(State);
4258 void character::HandleStates () {
4259 if (!TemporaryState && !EquipmentState) return;
4260 for (int c = 0; c < STATES; ++c) {
4261 if (TemporaryState & (1 << c) && TemporaryStateCounter[c] != PERMANENT) {
4262 if (!--TemporaryStateCounter[c]) {
4263 TemporaryState &= ~(1 << c);
4264 if (!(EquipmentState & (1 << c))) {
4265 if (StateData[c].EndHandler) {
4266 (this->*StateData[c].EndHandler)();
4267 if (!IsEnabled()) return;
4269 if (!TemporaryStateCounter[c]) (this->*StateData[c].PrintEndMessage)();
4273 if (StateIsActivated(1 << c)) {
4274 if (StateData[c].Handler) (this->*StateData[c].Handler)();
4276 if (!IsEnabled()) return;
4281 void character::PrintBeginPolymorphControlMessage () const {
4282 if (IsPlayer()) ADD_MESSAGE("You feel your mind has total control over your body.");
4286 void character::PrintEndPolymorphControlMessage () const {
4287 if (IsPlayer()) ADD_MESSAGE("You are somehow uncertain of your willpower.");
4291 void character::PrintBeginLifeSaveMessage () const {
4292 if (IsPlayer()) ADD_MESSAGE("You hear Hell's gates being locked just now.");
4296 void character::PrintEndLifeSaveMessage () const {
4297 if (IsPlayer()) ADD_MESSAGE("You feel the Afterlife is welcoming you once again.");
4301 void character::PrintBeginLycanthropyMessage () const {
4302 if (IsPlayer()) ADD_MESSAGE("You suddenly notice you've always loved full moons.");
4306 void character::PrintEndLycanthropyMessage () const {
4307 if (IsPlayer()) ADD_MESSAGE("You feel the wolf inside you has had enough of your bad habits.");
4311 void character::PrintBeginVampirismMessage () const {
4312 if (IsPlayer()) ADD_MESSAGE("You suddenly decide you have always hated garlic.");
4316 void character::PrintEndVampirismMessage () const {
4317 if (IsPlayer()) ADD_MESSAGE("You recall your delight of the morning sunshine back in New Attnam. You are a vampire no longer.");
4321 void character::PrintBeginInvisibilityMessage () const {
4322 if ((PLAYER->StateIsActivated(INFRA_VISION) && IsWarm()) || (PLAYER->StateIsActivated(ESP) && GetAttribute(INTELLIGENCE) >= 5)) {
4323 if (IsPlayer()) ADD_MESSAGE("You seem somehow transparent.");
4324 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s seems somehow transparent.", CHAR_NAME(DEFINITE));
4325 } else {
4326 if (IsPlayer()) ADD_MESSAGE("You fade away.");
4327 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s disappears!", CHAR_NAME(DEFINITE));
4332 void character::PrintEndInvisibilityMessage () const {
4333 if ((PLAYER->StateIsActivated(INFRA_VISION) && IsWarm()) || (PLAYER->StateIsActivated(ESP) && GetAttribute(INTELLIGENCE) >= 5)) {
4334 if (IsPlayer()) ADD_MESSAGE("Your notice your transparency has ended.");
4335 else if (CanBeSeenByPlayer()) ADD_MESSAGE("The appearance of %s seems far more solid now.", CHAR_NAME(INDEFINITE));
4336 } else {
4337 if (IsPlayer()) ADD_MESSAGE("You reappear.");
4338 else if (CanBeSeenByPlayer()) ADD_MESSAGE("Suddenly %s appears from nowhere!", CHAR_NAME(INDEFINITE));
4343 void character::PrintBeginInfraVisionMessage () const {
4344 if (IsPlayer()) {
4345 if (StateIsActivated(INVISIBLE) && IsWarm() && !(StateIsActivated(ESP) && GetAttribute(INTELLIGENCE) >= 5))
4346 ADD_MESSAGE("You reappear.");
4347 else
4348 ADD_MESSAGE("You feel your perception being magically altered.");
4353 void character::PrintEndInfraVisionMessage () const {
4354 if (IsPlayer()) {
4355 if (StateIsActivated(INVISIBLE) && IsWarm() && !(StateIsActivated(ESP) && GetAttribute(INTELLIGENCE) >= 5))
4356 ADD_MESSAGE("You disappear.");
4357 else
4358 ADD_MESSAGE("You feel your perception returning to normal.");
4363 void character::PrintBeginESPMessage () const {
4364 if (IsPlayer()) ADD_MESSAGE("You suddenly feel like being only a tiny part of a great network of intelligent minds.");
4368 void character::PrintEndESPMessage () const {
4369 if (IsPlayer()) ADD_MESSAGE("You are filled with desire to be just yourself from now on.");
4373 void character::PrintBeginHasteMessage () const {
4374 if (IsPlayer()) ADD_MESSAGE("Time slows down to a crawl.");
4375 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks faster!", CHAR_NAME(DEFINITE));
4379 void character::PrintEndHasteMessage () const {
4380 if (IsPlayer()) ADD_MESSAGE("Everything seems to move much faster now.");
4381 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks slower!", CHAR_NAME(DEFINITE));
4385 void character::PrintBeginSlowMessage () const {
4386 if (IsPlayer()) ADD_MESSAGE("Everything seems to move much faster now.");
4387 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks slower!", CHAR_NAME(DEFINITE));
4391 void character::PrintEndSlowMessage () const {
4392 if (IsPlayer()) ADD_MESSAGE("Time slows down to a crawl.");
4393 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks faster!", CHAR_NAME(DEFINITE));
4397 void character::EndPolymorph () {
4398 ForceEndPolymorph();
4402 character *character::ForceEndPolymorph () {
4403 if (IsPlayer()) {
4404 ADD_MESSAGE("You return to your true form.");
4405 } else if (game::IsInWilderness()) {
4406 ActivateTemporaryState(POLYMORPHED);
4407 SetTemporaryStateCounter(POLYMORPHED, 10);
4408 return this; // fast gum solution, state ends when the player enters a dungeon
4410 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s returns to %s true form.", CHAR_NAME(DEFINITE), GetPossessivePronoun().CStr());
4411 RemoveTraps();
4412 if (GetAction()) GetAction()->Terminate(false);
4413 v2 Pos = GetPos();
4414 SendToHell();
4415 Remove();
4416 character *Char = GetPolymorphBackup();
4417 Flags |= C_IN_NO_MSG_MODE;
4418 Char->Flags |= C_IN_NO_MSG_MODE;
4419 Char->PutToOrNear(Pos);
4420 Char->ChangeTeam(GetTeam());
4421 if (GetTeam()->GetLeader() == this) GetTeam()->SetLeader(Char);
4422 SetPolymorphBackup(0);
4423 Char->Enable();
4424 Char->Flags &= ~C_POLYMORPHED;
4425 GetStack()->MoveItemsTo(Char->GetStack());
4426 DonateEquipmentTo(Char);
4427 Char->SetMoney(GetMoney());
4428 Flags &= ~C_IN_NO_MSG_MODE;
4429 Char->Flags &= ~C_IN_NO_MSG_MODE;
4430 Char->CalculateAll();
4431 Char->SetAssignedName(GetAssignedName());
4432 if (IsPlayer()) {
4433 Flags &= ~C_PLAYER;
4434 game::SetPlayer(Char);
4435 game::SendLOSUpdateRequest();
4436 UpdateESPLOS();
4438 Char->TestWalkability();
4439 return Char;
4443 void character::LycanthropyHandler () {
4444 if (StateIsActivated(POLYMORPH_LOCK)) return;
4445 if (GetType() == werewolfwolf::ProtoType.GetIndex()) return;
4446 if (!(RAND() % 2000)) {
4447 if (StateIsActivated(POLYMORPH_CONTROL) && (IsPlayer() ? !game::TruthQuestion(CONST_S("Do you wish to change into a werewolf?")) : false)) return;
4448 Polymorph(werewolfwolf::Spawn(), 1000 + RAND() % 2000);
4453 void character::SaveLife () {
4454 if (TemporaryStateIsActivated(LIFE_SAVED)) {
4455 if (IsPlayer())
4456 ADD_MESSAGE("But wait! You glow briefly red and seem to be in a better shape!");
4457 else if (CanBeSeenByPlayer())
4458 ADD_MESSAGE("But wait, suddenly %s glows briefly red and seems to be in a better shape!", GetPersonalPronoun().CStr());
4459 DeActivateTemporaryState(LIFE_SAVED);
4460 } else {
4461 item *LifeSaver = 0;
4462 for (int c = 0; c < GetEquipments(); ++c) {
4463 item *Equipment = GetEquipment(c);
4464 if (Equipment && Equipment->IsInCorrectSlot(c) && Equipment->GetGearStates() & LIFE_SAVED) LifeSaver = Equipment;
4466 if (!LifeSaver) ABORT("The Universe can only kill you once!");
4467 if (IsPlayer())
4468 ADD_MESSAGE("But wait! Your %s glows briefly red and disappears and you seem to be in a better shape!", LifeSaver->CHAR_NAME(UNARTICLED));
4469 else if (CanBeSeenByPlayer())
4470 ADD_MESSAGE("But wait, suddenly %s %s glows briefly red and disappears and %s seems to be in a better shape!", GetPossessivePronoun().CStr(), LifeSaver->CHAR_NAME(UNARTICLED), GetPersonalPronoun().CStr());
4471 LifeSaver->RemoveFromSlot();
4472 LifeSaver->SendToHell();
4475 if (IsPlayer()) game::AskForEscPress(CONST_S("Life saved!"));
4477 RestoreBodyParts();
4478 ResetSpoiling();
4479 RestoreHP();
4480 RestoreStamina();
4481 ResetStates();
4483 if (GetNP() < SATIATED_LEVEL) SetNP(SATIATED_LEVEL);
4485 SendNewDrawRequest();
4487 if (GetAction()) GetAction()->Terminate(false);
4491 character *character::PolymorphRandomly (int MinDanger, int MaxDanger, int Time) {
4492 character *NewForm = 0;
4493 if (StateIsActivated(POLYMORPH_LOCK)) { ADD_MESSAGE("You feel uncertain about your body for a moment."); return NewForm; }
4494 if (StateIsActivated(POLYMORPH_CONTROL)) {
4495 if (IsPlayer()) {
4496 if (!GetNewFormForPolymorphWithControl(NewForm)) return NewForm;
4497 } else {
4498 NewForm = protosystem::CreateMonster(MinDanger*10, MaxDanger*10, NO_EQUIPMENT);
4500 } else {
4501 NewForm = protosystem::CreateMonster(MinDanger, MaxDanger, NO_EQUIPMENT);
4503 Polymorph(NewForm, Time);
4504 return NewForm;
4508 /* In reality, the reading takes Time / (Intelligence * 10) turns */
4509 void character::StartReading (item *Item, sLong Time) {
4510 study *Read = study::Spawn(this);
4511 Read->SetLiteratureID(Item->GetID());
4512 if (game::WizardModeIsActive()) Time = 1;
4513 Read->SetCounter(Time);
4514 SetAction(Read);
4515 if (IsPlayer()) ADD_MESSAGE("You start reading %s.", Item->CHAR_NAME(DEFINITE));
4516 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s starts reading %s.", CHAR_NAME(DEFINITE), Item->CHAR_NAME(DEFINITE));
4520 /* Call when one makes something with his/her/its hands.
4521 * Difficulty of 5 takes about one turn, so it's the most common to use. */
4522 void character::DexterityAction (int Difficulty) {
4523 EditAP(-20000 * Difficulty / APBonus(GetAttribute(DEXTERITY)));
4524 EditExperience(DEXTERITY, Difficulty * 15, 1 << 7);
4528 /* If Theoretically != false, range is not a factor. */
4529 truth character::CanBeSeenByPlayer (truth Theoretically, truth IgnoreESP) const {
4530 if (IsEnabled() && !game::IsGenerating() && (Theoretically || GetSquareUnder())) {
4531 truth MayBeESPSeen = PLAYER->IsEnabled() && !IgnoreESP && PLAYER->StateIsActivated(ESP) && GetAttribute(INTELLIGENCE) >= 5;
4532 truth MayBeInfraSeen = PLAYER->IsEnabled() && PLAYER->StateIsActivated(INFRA_VISION) && IsWarm();
4533 truth Visible = !StateIsActivated(INVISIBLE) || MayBeESPSeen || MayBeInfraSeen;
4534 if (game::IsInWilderness()) return Visible;
4535 if (MayBeESPSeen && (Theoretically || GetDistanceSquareFrom(PLAYER) <= PLAYER->GetESPRangeSquare())) return true;
4536 if (!Visible) return false;
4537 return (Theoretically || SquareUnderCanBeSeenByPlayer(MayBeInfraSeen));
4539 return false;
4543 truth character::CanBeSeenBy (ccharacter *Who, truth Theoretically, truth IgnoreESP) const {
4544 if (Who->IsPlayer()) return CanBeSeenByPlayer(Theoretically, IgnoreESP);
4545 if (IsEnabled() && !game::IsGenerating() && (Theoretically || GetSquareUnder())) {
4546 truth MayBeESPSeen = Who->IsEnabled() && !IgnoreESP && Who->StateIsActivated(ESP) && GetAttribute(INTELLIGENCE) >= 5;
4547 truth MayBeInfraSeen = Who->IsEnabled() && Who->StateIsActivated(INFRA_VISION) && IsWarm();
4548 truth Visible = !StateIsActivated(INVISIBLE) || MayBeESPSeen || MayBeInfraSeen;
4549 if (game::IsInWilderness()) return Visible;
4550 if (MayBeESPSeen && (Theoretically || GetDistanceSquareFrom(Who) <= Who->GetESPRangeSquare())) return true;
4551 if (!Visible) return false;
4552 return (Theoretically || SquareUnderCanBeSeenBy(Who, MayBeInfraSeen));
4554 return false;
4558 truth character::SquareUnderCanBeSeenByPlayer (truth IgnoreDarkness) const {
4559 if (!GetSquareUnder()) return false;
4560 int S1 = SquaresUnder, S2 = PLAYER->SquaresUnder;
4561 if (S1 == 1 && S2 == 1) {
4562 if (GetSquareUnder()->CanBeSeenByPlayer(IgnoreDarkness)) return true;
4563 if (IgnoreDarkness) {
4564 int LOSRangeSquare = PLAYER->GetLOSRangeSquare();
4565 if ((GetPos() - PLAYER->GetPos()).GetLengthSquare() <= LOSRangeSquare) {
4566 eyecontroller::Map = GetLevel()->GetMap();
4567 return mapmath<eyecontroller>::DoLine(PLAYER->GetPos().X, PLAYER->GetPos().Y, GetPos().X, GetPos().Y, SKIP_FIRST);
4570 return false;
4571 } else {
4572 for (int c1 = 0; c1 < S1; ++c1) {
4573 lsquare *Square = GetLSquareUnder(c1);
4574 if (Square->CanBeSeenByPlayer(IgnoreDarkness)) return true;
4575 else if (IgnoreDarkness) {
4576 v2 Pos = Square->GetPos();
4577 int LOSRangeSquare = PLAYER->GetLOSRangeSquare();
4578 for (int c2 = 0; c2 < S2; ++c2) {
4579 v2 PlayerPos = PLAYER->GetPos(c2);
4580 if ((Pos-PlayerPos).GetLengthSquare() <= LOSRangeSquare) {
4581 eyecontroller::Map = GetLevel()->GetMap();
4582 if (mapmath<eyecontroller>::DoLine(PlayerPos.X, PlayerPos.Y, Pos.X, Pos.Y, SKIP_FIRST)) return true;
4587 return false;
4592 truth character::SquareUnderCanBeSeenBy (ccharacter *Who, truth IgnoreDarkness) const {
4593 int S1 = SquaresUnder, S2 = Who->SquaresUnder;
4594 int LOSRangeSquare = Who->GetLOSRangeSquare();
4595 if (S1 == 1 && S2 == 1) return GetSquareUnder()->CanBeSeenFrom(Who->GetPos(), LOSRangeSquare, IgnoreDarkness);
4596 for (int c1 = 0; c1 < S1; ++c1) {
4597 lsquare *Square = GetLSquareUnder(c1);
4598 for (int c2 = 0; c2 < S2; ++c2) if (Square->CanBeSeenFrom(Who->GetPos(c2), LOSRangeSquare, IgnoreDarkness)) return true;
4600 return false;
4604 int character::GetDistanceSquareFrom (ccharacter *Who) const {
4605 int S1 = SquaresUnder, S2 = Who->SquaresUnder;
4606 if (S1 == 1 && S2 == 1) return (GetPos() - Who->GetPos()).GetLengthSquare();
4607 v2 MinDist(0x7FFF, 0x7FFF);
4608 int MinLength = 0xFFFF;
4609 for (int c1 = 0; c1 < S1; ++c1) {
4610 for (int c2 = 0; c2 < S2; ++c2) {
4611 v2 Dist = GetPos(c1)-Who->GetPos(c2);
4612 if (Dist.X < 0) Dist.X = -Dist.X;
4613 if (Dist.Y < 0) Dist.Y = -Dist.Y;
4614 if (Dist.X <= MinDist.X && Dist.Y <= MinDist.Y) {
4615 MinDist = Dist;
4616 MinLength = Dist.GetLengthSquare();
4617 } else if (Dist.X < MinDist.X || Dist.Y < MinDist.Y) {
4618 int Length = Dist.GetLengthSquare();
4619 if (Length < MinLength) {
4620 MinDist = Dist;
4621 MinLength = Length;
4626 return MinLength;
4630 void character::AttachBodyPart (bodypart *BodyPart) {
4631 SetBodyPart(BodyPart->GetBodyPartIndex(), BodyPart);
4632 if (!AllowSpoil()) BodyPart->ResetSpoiling();
4633 BodyPart->ResetPosition();
4634 BodyPart->UpdatePictures();
4635 CalculateAttributeBonuses();
4636 CalculateBattleInfo();
4637 SendNewDrawRequest();
4638 SignalPossibleTransparencyChange();
4642 /* Returns true if the character has all bodyparts, false if not. */
4643 truth character::HasAllBodyParts () const {
4644 for (int c = 0; c < BodyParts; ++c) if (!GetBodyPart(c) && CanCreateBodyPart(c)) return false;
4645 return true;
4649 bodypart *character::GenerateRandomBodyPart () {
4650 int NeededBodyPart[MAX_BODYPARTS];
4651 int Index = 0;
4652 for (int c = 0; c < BodyParts; ++c) if (!GetBodyPart(c) && CanCreateBodyPart(c)) NeededBodyPart[Index++] = c;
4653 return Index ? CreateBodyPart(NeededBodyPart[RAND() % Index]) : 0;
4657 /* Searches the character's Stack and if it find some bodyparts there that are the character's
4658 * old bodyparts returns a stackiterator to one of them (choosen in random).
4659 * If no fitting bodyparts are found the function returns 0 */
4660 bodypart *character::FindRandomOwnBodyPart (truth AllowNonLiving) const {
4661 itemvector LostAndFound;
4662 for (int c = 0; c < BodyParts; ++c) {
4663 if (!GetBodyPart(c)) {
4664 for (std::list<feuLong>::iterator i = OriginalBodyPartID[c].begin(); i != OriginalBodyPartID[c].end(); ++i) {
4665 bodypart *Found = static_cast<bodypart *>(SearchForItem(*i));
4666 if (Found && (AllowNonLiving || Found->CanRegenerate())) LostAndFound.push_back(Found);
4670 if (LostAndFound.empty()) return 0;
4671 return static_cast<bodypart *>(LostAndFound[RAND() % LostAndFound.size()]);
4675 void character::PrintBeginPoisonedMessage () const {
4676 if (IsPlayer()) ADD_MESSAGE("You seem to be very ill.");
4677 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks very ill.", CHAR_NAME(DEFINITE));
4681 void character::PrintEndPoisonedMessage () const {
4682 if (IsPlayer()) ADD_MESSAGE("You feel better again.");
4683 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks better.", CHAR_NAME(DEFINITE));
4687 void character::PoisonedHandler () {
4688 if (!(RAND() % 100)) VomitAtRandomDirection(500 + RAND_N(250));
4689 int Damage = 0;
4690 for (int Used = 0; Used < GetTemporaryStateCounter(POISONED); Used += 100) if (!(RAND() % 100)) ++Damage;
4691 if (Damage) {
4692 ReceiveDamage(0, Damage, POISON, ALL, 8, false, false, false, false);
4693 CheckDeath(CONST_S("died of acute poisoning"), 0);
4698 truth character::IsWarm () const {
4699 return combinebodypartpredicates()(this, &bodypart::IsWarm, 1);
4703 truth character::IsWarmBlooded() const
4705 return combinebodypartpredicates()(this, &bodypart::IsWarmBlooded, 1);
4709 void character::BeginInvisibility () {
4710 UpdatePictures();
4711 SendNewDrawRequest();
4712 SignalPossibleTransparencyChange();
4716 void character::BeginInfraVision () {
4717 if (IsPlayer()) GetArea()->SendNewDrawRequest();
4721 void character::BeginESP () {
4722 if (IsPlayer()) GetArea()->SendNewDrawRequest();
4726 void character::EndInvisibility () {
4727 UpdatePictures();
4728 SendNewDrawRequest();
4729 SignalPossibleTransparencyChange();
4733 void character::EndInfraVision () {
4734 if (IsPlayer() && IsEnabled()) GetArea()->SendNewDrawRequest();
4738 void character::EndESP () {
4739 if (IsPlayer() && IsEnabled()) GetArea()->SendNewDrawRequest();
4743 void character::Draw (blitdata &BlitData) const {
4744 col24 L = BlitData.Luminance;
4745 if (PLAYER->IsEnabled() &&
4746 ((PLAYER->StateIsActivated(ESP) && GetAttribute(INTELLIGENCE) >= 5 &&
4747 (PLAYER->GetPos() - GetPos()).GetLengthSquare() <= PLAYER->GetESPRangeSquare()) ||
4748 (PLAYER->StateIsActivated(INFRA_VISION) && IsWarm())))
4749 BlitData.Luminance = ivanconfig::GetContrastLuminance();
4751 DrawBodyParts(BlitData);
4752 BlitData.Luminance = ivanconfig::GetContrastLuminance();
4753 BlitData.Src.Y = 16;
4754 cint SquareIndex = BlitData.CustomData & SQUARE_INDEX_MASK;
4756 if (GetTeam() == PLAYER->GetTeam() && !IsPlayer() && SquareIndex == GetTameSymbolSquareIndex()) {
4757 BlitData.Src.X = 32;
4758 igraph::GetSymbolGraphic()->LuminanceMaskedBlit(BlitData);
4761 if (IsFlying() && SquareIndex == GetFlySymbolSquareIndex()) {
4762 BlitData.Src.X = 128;
4763 igraph::GetSymbolGraphic()->LuminanceMaskedBlit(BlitData);
4766 if (IsSwimming() && SquareIndex == GetSwimmingSymbolSquareIndex()) {
4767 BlitData.Src.X = 240;
4768 igraph::GetSymbolGraphic()->LuminanceMaskedBlit(BlitData);
4771 if (GetAction() && GetAction()->IsUnconsciousness() && SquareIndex == GetUnconsciousSymbolSquareIndex()) {
4772 BlitData.Src.X = 224;
4773 igraph::GetSymbolGraphic()->LuminanceMaskedBlit(BlitData);
4776 BlitData.Src.X = BlitData.Src.Y = 0;
4777 BlitData.Luminance = L;
4781 void character::DrawBodyParts (blitdata &BlitData) const {
4782 GetTorso()->Draw(BlitData);
4786 void character::PrintBeginTeleportMessage () const {
4787 if (IsPlayer()) ADD_MESSAGE("You feel jumpy.");
4791 void character::PrintEndTeleportMessage () const {
4792 if (IsPlayer()) ADD_MESSAGE("You suddenly realize you've always preferred walking to jumping.");
4796 void character::PrintBeginDetectMessage () const {
4797 if (IsPlayer()) ADD_MESSAGE("You feel curious about your surroundings.");
4801 void character::PrintEndDetectMessage () const {
4802 if (IsPlayer()) ADD_MESSAGE("You decide to rely on your intuition from now on.");
4806 void character::TeleportHandler () {
4807 if (!(RAND() % 1500) && !game::IsInWilderness()) {
4808 if (IsPlayer()) ADD_MESSAGE("You feel an urgent spatial relocation is now appropriate.");
4809 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s disappears.", CHAR_NAME(DEFINITE));
4810 TeleportRandomly();
4815 void character::DetectHandler () {
4816 if (IsPlayer()) {
4817 //the AI can't be asked position questions! So only the player can hav this state really :/ a bit daft of me
4818 if (!(RAND()%3000) && !game::IsInWilderness()) {
4819 ADD_MESSAGE("Your mind wanders in search of something.");
4820 DoDetecting(); //in fact, who knows what would happen if a dark frog had the detecting state?
4826 void character::PrintBeginPolymorphMessage () const {
4827 if (IsPlayer()) ADD_MESSAGE("An uncomfortable uncertainty of who you really are overwhelms you.");
4831 void character::PrintEndPolymorphMessage () const {
4832 if (IsPlayer()) ADD_MESSAGE("You feel you are you and no one else.");
4836 void character::PolymorphHandler () {
4837 if (!(RAND() % 1500)) PolymorphRandomly(1, 999999, 200 + RAND() % 800);
4840 void character::PrintBeginTeleportControlMessage () const {
4841 if (IsPlayer()) ADD_MESSAGE("You feel very controlled.");
4845 void character::PrintEndTeleportControlMessage () const {
4846 if (IsPlayer()) ADD_MESSAGE("You feel your control slipping.");
4850 void character::DisplayStethoscopeInfo (character *) const {
4851 felist Info(CONST_S("Information about ") + GetDescription(DEFINITE));
4852 AddSpecialStethoscopeInfo(Info);
4853 Info.AddEntry(CONST_S("Endurance: ") + GetAttribute(ENDURANCE), LIGHT_GRAY);
4854 Info.AddEntry(CONST_S("Perception: ") + GetAttribute(PERCEPTION), LIGHT_GRAY);
4855 Info.AddEntry(CONST_S("Intelligence: ") + GetAttribute(INTELLIGENCE), LIGHT_GRAY);
4856 Info.AddEntry(CONST_S("Wisdom: ") + GetAttribute(WISDOM), LIGHT_GRAY);
4857 //Info.AddEntry(CONST_S("Willpower: ") + GetAttribute(WILL_POWER), LIGHT_GRAY);
4858 Info.AddEntry(CONST_S("Charisma: ") + GetAttribute(CHARISMA), LIGHT_GRAY);
4859 Info.AddEntry(CONST_S("HP: ") + GetHP() + "/" + GetMaxHP(), IsInBadCondition() ? RED : LIGHT_GRAY);
4860 if (GetAction()) Info.AddEntry(festring(GetAction()->GetDescription()).CapitalizeCopy(), LIGHT_GRAY);
4861 for (int c = 0; c < STATES; ++c) {
4862 if (StateIsActivated(1 << c) && (1 << c != HASTE || !StateIsActivated(SLOW)) && (1 << c != SLOW || !StateIsActivated(HASTE)))
4863 Info.AddEntry(StateData[c].Description, LIGHT_GRAY);
4865 switch (GetTirednessState()) {
4866 case FAINTING: Info.AddEntry("Fainting", RED); break;
4867 case EXHAUSTED: Info.AddEntry("Exhausted", LIGHT_GRAY); break;
4869 game::SetStandardListAttributes(Info);
4870 Info.Draw();
4874 truth character::CanUseStethoscope (truth PrintReason) const {
4875 if (PrintReason) ADD_MESSAGE("This type of monster can't use a stethoscope.");
4876 return false;
4880 /* Effect used by at least Sophos.
4881 * NOTICE: Doesn't check for death! */
4882 void character::TeleportSomePartsAway (int NumberToTeleport) {
4883 if (StateIsActivated(TELEPORT_LOCK)) {
4884 if (IsPlayer()) ADD_MESSAGE("You feel very itchy for a moment.");
4885 return;
4887 for (int c = 0; c < NumberToTeleport; ++c) {
4888 int RandomBodyPart = GetRandomNonVitalBodyPart();
4889 if (RandomBodyPart == NONE_INDEX) {
4890 for (; c < NumberToTeleport; ++c) {
4891 GetTorso()->SetHP((GetTorso()->GetHP() << 2) / 5);
4892 sLong TorsosVolume = GetTorso()->GetMainMaterial()->GetVolume() / 10;
4893 if (!TorsosVolume) break;
4894 sLong Amount = (RAND() % TorsosVolume)+1;
4895 item *Lump = GetTorso()->GetMainMaterial()->CreateNaturalForm(Amount);
4896 GetTorso()->GetMainMaterial()->EditVolume(-Amount);
4897 Lump->MoveTo(GetNearLSquare(GetLevel()->GetRandomSquare())->GetStack());
4898 if (IsPlayer()) ADD_MESSAGE("Parts of you teleport away.");
4899 else if (CanBeSeenByPlayer()) ADD_MESSAGE("Parts of %s teleport away.", CHAR_NAME(DEFINITE));
4901 } else {
4902 item *SeveredBodyPart = SevereBodyPart(RandomBodyPart);
4903 if (SeveredBodyPart) {
4904 GetNearLSquare(GetLevel()->GetRandomSquare())->AddItem(SeveredBodyPart);
4905 SeveredBodyPart->DropEquipment();
4906 if (IsPlayer()) ADD_MESSAGE("Your %s teleports away.", GetBodyPartName(RandomBodyPart).CStr());
4907 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s %s teleports away.", GetPossessivePronoun().CStr(), GetBodyPartName(RandomBodyPart).CStr());
4908 } else {
4909 if (IsPlayer()) ADD_MESSAGE("Your %s disappears.", GetBodyPartName(RandomBodyPart).CStr());
4910 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s %s disappears.", GetPossessivePronoun().CStr(), GetBodyPartName(RandomBodyPart).CStr());
4917 /* Returns an index of a random bodypart that is not vital. If no non-vital bodypart is found returns NONE_INDEX */
4918 int character::GetRandomNonVitalBodyPart () const {
4919 int OKBodyPart[MAX_BODYPARTS];
4920 int OKBodyParts = 0;
4921 for (int c = 0; c < BodyParts; ++c) if (GetBodyPart(c) && !BodyPartIsVital(c)) OKBodyPart[OKBodyParts++] = c;
4922 return OKBodyParts ? OKBodyPart[RAND() % OKBodyParts] : NONE_INDEX;
4926 void character::CalculateVolumeAndWeight () {
4927 Volume = Stack->GetVolume();
4928 Weight = Stack->GetWeight();
4929 BodyVolume = 0;
4930 CarriedWeight = Weight;
4931 for (int c = 0; c < BodyParts; ++c) {
4932 bodypart *BodyPart = GetBodyPart(c);
4933 if (BodyPart) {
4934 BodyVolume += BodyPart->GetBodyPartVolume();
4935 Volume += BodyPart->GetVolume();
4936 CarriedWeight += BodyPart->GetCarriedWeight();
4937 Weight += BodyPart->GetWeight();
4943 void character::SignalVolumeAndWeightChange () {
4944 if (!IsInitializing()) {
4945 CalculateVolumeAndWeight();
4946 if (IsEnabled()) CalculateBurdenState();
4947 if (MotherEntity) MotherEntity->SignalVolumeAndWeightChange();
4952 void character::SignalEmitationIncrease (col24 EmitationUpdate) {
4953 if (game::CompareLights(EmitationUpdate, Emitation) > 0) {
4954 game::CombineLights(Emitation, EmitationUpdate);
4955 if (MotherEntity) MotherEntity->SignalEmitationIncrease(EmitationUpdate);
4956 else if (SquareUnder[0] && !game::IsInWilderness()) {
4957 for(int c = 0; c < GetSquaresUnder(); ++c) GetLSquareUnder()->SignalEmitationIncrease(EmitationUpdate);
4963 void character::SignalEmitationDecrease (col24 EmitationUpdate) {
4964 if (game::CompareLights(EmitationUpdate, Emitation) >= 0 && Emitation) {
4965 col24 Backup = Emitation;
4966 CalculateEmitation();
4967 if (Backup != Emitation) {
4968 if (MotherEntity) MotherEntity->SignalEmitationDecrease(EmitationUpdate);
4969 else if (SquareUnder[0] && !game::IsInWilderness()) {
4970 for (int c = 0; c < GetSquaresUnder(); ++c) GetLSquareUnder(c)->SignalEmitationDecrease(EmitationUpdate);
4977 void character::CalculateEmitation () {
4978 Emitation = GetBaseEmitation();
4979 for (int c = 0; c < BodyParts; ++c) {
4980 bodypart *BodyPart = GetBodyPart(c);
4981 if (BodyPart) game::CombineLights(Emitation, BodyPart->GetEmitation());
4983 game::CombineLights(Emitation, Stack->GetEmitation());
4987 void character::CalculateAll () {
4988 Flags |= C_INITIALIZING;
4989 CalculateAttributeBonuses();
4990 CalculateVolumeAndWeight();
4991 CalculateEmitation();
4992 CalculateBodyPartMaxHPs(0);
4993 CalculateMaxStamina();
4994 CalculateBurdenState();
4995 CalculateBattleInfo();
4996 Flags &= ~C_INITIALIZING;
5000 void character::CalculateHP () {
5001 HP = sumbodypartproperties()(this, &bodypart::GetHP);
5005 void character::CalculateMaxHP () {
5006 MaxHP = sumbodypartproperties()(this, &bodypart::GetMaxHP);
5010 void character::CalculateBodyPartMaxHPs (feuLong Flags) {
5011 doforbodypartswithparam<feuLong>()(this, &bodypart::CalculateMaxHP, Flags);
5012 CalculateMaxHP();
5013 CalculateHP();
5017 truth character::EditAttribute (int Identifier, int Value) {
5018 if (Identifier == ENDURANCE && UseMaterialAttributes()) return false;
5019 if (RawEditAttribute(BaseExperience[Identifier], Value)) {
5020 if (!IsInitializing()) {
5021 if (Identifier == LEG_STRENGTH) CalculateBurdenState();
5022 else if (Identifier == ENDURANCE) CalculateBodyPartMaxHPs();
5023 else if (IsPlayer() && Identifier == PERCEPTION) game::SendLOSUpdateRequest();
5024 else if (IsPlayerKind() && (Identifier == INTELLIGENCE || Identifier == WISDOM || Identifier == CHARISMA)) UpdatePictures();
5025 CalculateBattleInfo();
5027 return true;
5029 return false;
5033 truth character::ActivateRandomState (int Flags, int Time, sLong Seed) {
5034 femath::SaveSeed();
5035 if (Seed) femath::SetSeed(Seed);
5036 sLong ToBeActivated = GetRandomState(Flags|DUR_TEMPORARY);
5037 femath::LoadSeed();
5038 if (!ToBeActivated) return false;
5039 BeginTemporaryState(ToBeActivated, Time);
5040 return true;
5044 truth character::GainRandomIntrinsic (int Flags) {
5045 sLong ToBeActivated = GetRandomState(Flags|DUR_PERMANENT);
5046 if (!ToBeActivated) return false;
5047 GainIntrinsic(ToBeActivated);
5048 return true;
5052 /* Returns 0 if state not found */
5053 sLong character::GetRandomState (int Flags) const {
5054 sLong OKStates[STATES];
5055 int NumberOfOKStates = 0;
5056 for (int c = 0; c < STATES; ++c) {
5057 if (StateData[c].Flags & Flags & DUR_FLAGS && StateData[c].Flags & Flags & SRC_FLAGS) OKStates[NumberOfOKStates++] = 1 << c;
5059 return NumberOfOKStates ? OKStates[RAND() % NumberOfOKStates] : 0;
5063 int characterprototype::CreateSpecialConfigurations (characterdatabase **TempConfig, int Configs, int Level) {
5064 if (Level == 0 && TempConfig[0]->CreateDivineConfigurations) {
5065 Configs = databasecreator<character>::CreateDivineConfigurations(this, TempConfig, Configs);
5067 if (Level == 1 && TempConfig[0]->CreateUndeadConfigurations) {
5068 for (int c = 1; c < protocontainer<character>::GetSize(); ++c) {
5069 const character::prototype *Proto = protocontainer<character>::GetProto(c);
5070 if (!Proto) continue; // missing character
5071 const character::database *const *CharacterConfigData = Proto->GetConfigData();
5072 if (!CharacterConfigData) ABORT("No database entry for character <%s>!", Proto->GetClassID());
5073 const character::database*const* End = CharacterConfigData+Proto->GetConfigSize();
5074 for (++CharacterConfigData; CharacterConfigData != End; ++CharacterConfigData) {
5075 const character::database *CharacterDataBase = *CharacterConfigData;
5076 if (CharacterDataBase->UndeadVersions) {
5077 character::database* ConfigDataBase = new character::database(**TempConfig);
5078 festring ucfgname = "undead ";
5079 ucfgname << CharacterDataBase->CfgStrName;
5080 ConfigDataBase->InitDefaults(this, (c << 8) | CharacterDataBase->Config, ucfgname);
5081 ConfigDataBase->PostFix << "of ";
5082 if (CharacterDataBase->Adjective.GetSize()) {
5083 if (CharacterDataBase->UsesLongAdjectiveArticle) ConfigDataBase->PostFix << "an ";
5084 else ConfigDataBase->PostFix << "a ";
5085 ConfigDataBase->PostFix << CharacterDataBase->Adjective << ' ';
5086 } else {
5087 if (CharacterDataBase->UsesLongArticle) ConfigDataBase->PostFix << "an ";
5088 else ConfigDataBase->PostFix << "a ";
5090 ConfigDataBase->PostFix << CharacterDataBase->NameSingular;
5091 if (CharacterDataBase->PostFix.GetSize()) ConfigDataBase->PostFix << ' ' << CharacterDataBase->PostFix;
5092 int P1 = TempConfig[0]->UndeadAttributeModifier;
5093 int P2 = TempConfig[0]->UndeadVolumeModifier;
5094 int c2;
5095 for (c2 = 0; c2 < ATTRIBUTES; ++c2) ConfigDataBase->*ExpPtr[c2] = CharacterDataBase->*ExpPtr[c2] * P1 / 100;
5096 for (c2 = 0; c2 < EQUIPMENT_DATAS; ++c2) ConfigDataBase->*EquipmentDataPtr[c2] = contentscript<item>();
5097 ConfigDataBase->DefaultIntelligence = 5;
5098 ConfigDataBase->DefaultWisdom = 5;
5099 ConfigDataBase->DefaultCharisma = 5;
5100 ConfigDataBase->TotalSize = CharacterDataBase->TotalSize;
5101 ConfigDataBase->Sex = CharacterDataBase->Sex;
5102 ConfigDataBase->AttributeBonus = CharacterDataBase->AttributeBonus;
5103 ConfigDataBase->TotalVolume = CharacterDataBase->TotalVolume * P2 / 100;
5104 if (TempConfig[0]->UndeadCopyMaterials) {
5105 ConfigDataBase->HeadBitmapPos = CharacterDataBase->HeadBitmapPos;
5106 ConfigDataBase->HairColor = CharacterDataBase->HairColor;
5107 ConfigDataBase->EyeColor = CharacterDataBase->EyeColor;
5108 ConfigDataBase->CapColor = CharacterDataBase->CapColor;
5109 ConfigDataBase->FleshMaterial = CharacterDataBase->FleshMaterial;
5110 ConfigDataBase->BloodMaterial = CharacterDataBase->BloodMaterial;
5111 ConfigDataBase->VomitMaterial = CharacterDataBase->VomitMaterial;
5112 ConfigDataBase->SweatMaterial = CharacterDataBase->SweatMaterial;
5114 ConfigDataBase->KnownCWeaponSkills = CharacterDataBase->KnownCWeaponSkills;
5115 ConfigDataBase->CWeaponSkillHits = CharacterDataBase->CWeaponSkillHits;
5116 ConfigDataBase->PostProcess();
5117 TempConfig[Configs++] = ConfigDataBase;
5122 if (Level == 0 && TempConfig[0]->CreateGolemMaterialConfigurations) {
5123 for (int c = 1; c < protocontainer<material>::GetSize(); ++c) {
5124 const material::prototype* Proto = protocontainer<material>::GetProto(c);
5125 if (!Proto) continue; // missing matherial
5126 const material::database*const* MaterialConfigData = Proto->GetConfigData();
5127 const material::database*const* End = MaterialConfigData + Proto->GetConfigSize();
5128 for (++MaterialConfigData; MaterialConfigData != End; ++MaterialConfigData) {
5129 const material::database* MaterialDataBase = *MaterialConfigData;
5130 if (MaterialDataBase->CategoryFlags & IS_GOLEM_MATERIAL) {
5131 character::database* ConfigDataBase = new character::database(**TempConfig);
5132 festring gcfgname;
5133 gcfgname << MaterialDataBase->CfgStrName;
5134 gcfgname << " golem";
5135 ConfigDataBase->InitDefaults(this, MaterialDataBase->Config, gcfgname);
5136 ConfigDataBase->Adjective = MaterialDataBase->NameStem;
5137 ConfigDataBase->UsesLongAdjectiveArticle = MaterialDataBase->NameFlags & USE_AN;
5138 ConfigDataBase->AttachedGod = MaterialDataBase->AttachedGod;
5139 TempConfig[Configs++] = ConfigDataBase;
5144 return Configs;
5148 double character::GetTimeToDie (ccharacter *Enemy, int Damage, double ToHitValue, truth AttackIsBlockable, truth UseMaxHP) const {
5149 double DodgeValue = GetDodgeValue();
5150 if (!Enemy->CanBeSeenBy(this, true)) ToHitValue *= 2;
5151 if (!CanBeSeenBy(Enemy, true)) DodgeValue *= 2;
5152 double MinHits = 1000;
5153 truth First = true;
5154 for (int c = 0; c < BodyParts; ++c) {
5155 if (BodyPartIsVital(c) && GetBodyPart(c)) {
5156 double Hits = GetBodyPart(c)->GetTimeToDie(Damage, ToHitValue, DodgeValue, AttackIsBlockable, UseMaxHP);
5157 if (First) { MinHits = Hits; First = false; } else MinHits = 1/(1/MinHits+1/Hits);
5160 return MinHits;
5164 double character::GetRelativeDanger (ccharacter *Enemy, truth UseMaxHP) const {
5165 double Danger = Enemy->GetTimeToKill(this, UseMaxHP)/GetTimeToKill(Enemy, UseMaxHP);
5166 int EnemyAP = Enemy->GetMoveAPRequirement(1);
5167 int ThisAP = GetMoveAPRequirement(1);
5168 if (EnemyAP > ThisAP) Danger *= 1.25; else if (ThisAP > EnemyAP) Danger *= 0.80;
5169 if (!Enemy->CanBeSeenBy(this, true)) Danger *= (Enemy->IsPlayer() ? 0.2 : 0.5);
5170 if (!CanBeSeenBy(Enemy, true)) Danger *= (IsPlayer() ? 5.0 : 2.0);
5171 if (GetAttribute(INTELLIGENCE) < 10 && !IsPlayer()) Danger *= 0.80;
5172 if (Enemy->GetAttribute(INTELLIGENCE) < 10 && !Enemy->IsPlayer()) Danger *= 1.25;
5173 return Limit(Danger, 0.001, 1000.0);
5177 festring character::GetBodyPartName (int I, truth Articled) const {
5178 if (I == TORSO_INDEX) return Articled ? CONST_S("a torso") : CONST_S("torso");
5179 ABORT("Illegal character bodypart name request!");
5180 return "";
5184 item *character::SearchForItem (feuLong ID) const {
5185 item *Equipment = findequipment<feuLong>()(this, &item::HasID, ID);
5186 if (Equipment) return Equipment;
5187 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) if (i->GetID() == ID) return *i;
5188 return 0;
5192 truth character::ContentsCanBeSeenBy (ccharacter *Viewer) const {
5193 return (Viewer == this);
5197 truth character::HitEffect (character *Enemy, item* Weapon, v2 HitPos, int Type, int BodyPartIndex,
5198 int Direction, truth BlockedByArmour, truth Critical, int DoneDamage)
5200 if (Weapon) return Weapon->HitEffect(this, Enemy, HitPos, BodyPartIndex, Direction, BlockedByArmour);
5201 if (Type == UNARMED_ATTACK) return Enemy->SpecialUnarmedEffect(this, HitPos, BodyPartIndex, Direction, BlockedByArmour);
5202 if (Type == KICK_ATTACK) return Enemy->SpecialKickEffect(this, HitPos, BodyPartIndex, Direction, BlockedByArmour);
5203 if (Type == BITE_ATTACK) return Enemy->SpecialBiteEffect(this, HitPos, BodyPartIndex, Direction, BlockedByArmour, Critical, DoneDamage);
5204 return false;
5208 void character::WeaponSkillHit (item *Weapon, int Type, int Hits) {
5209 int Category;
5210 if (Type == UNARMED_ATTACK) Category = UNARMED;
5211 else if (Type == WEAPON_ATTACK) { Weapon->WeaponSkillHit(Hits); return; }
5212 else if (Type == KICK_ATTACK) Category = KICK;
5213 else if (Type == BITE_ATTACK) Category = BITE;
5214 else if (Type == THROW_ATTACK) { if (!IsHumanoid()) return; Category = Weapon->GetWeaponCategory(); }
5215 else { ABORT("Illegal Type %d passed to character::WeaponSkillHit()!", Type); return; }
5216 if (GetCWeaponSkill(Category)->AddHit(Hits)) {
5217 CalculateBattleInfo();
5218 if (IsPlayer()) GetCWeaponSkill(Category)->AddLevelUpMessage(Category);
5223 /* Returns 0 if character cannot be duplicated */
5224 character *character::Duplicate (feuLong Flags) {
5225 if (!(Flags & IGNORE_PROHIBITIONS) && !CanBeCloned()) return 0;
5226 character *Char = GetProtoType()->Clone(this);
5227 if (Flags & MIRROR_IMAGE) {
5228 DuplicateEquipment(Char, Flags & ~IGNORE_PROHIBITIONS);
5229 Char->SetLifeExpectancy(Flags >> LE_BASE_SHIFT & LE_BASE_RANGE, Flags >> LE_RAND_SHIFT & LE_RAND_RANGE);
5231 Char->CalculateAll();
5232 Char->CalculateEmitation();
5233 Char->UpdatePictures();
5234 Char->Flags &= ~(C_INITIALIZING|C_IN_NO_MSG_MODE);
5235 return Char;
5239 truth character::TryToEquip (item *Item) {
5240 if (!Item->AllowEquip() || !CanUseEquipment() || GetAttribute(WISDOM) >= Item->GetWearWisdomLimit() || Item->GetSquaresUnder() != 1) {
5241 return false;
5244 for (int e = 0; e < GetEquipments(); ++e) {
5245 if (GetBodyPartOfEquipment(e) && EquipmentIsAllowed(e)) {
5246 sorter Sorter = EquipmentSorter(e);
5247 if ((Sorter == 0 || (Item->*Sorter)(this)) &&
5248 ((e != RIGHT_WIELDED_INDEX && e != LEFT_WIELDED_INDEX) ||
5249 Item->IsWeapon(this) || Item->IsShield(this)) && AllowEquipment(Item, e))
5251 item *OldEquipment = GetEquipment(e);
5252 if (BoundToUse(OldEquipment, e)) continue;
5253 lsquare *LSquareUnder = GetLSquareUnder();
5254 stack *StackUnder = LSquareUnder->GetStack();
5255 msgsystem::DisableMessages();
5256 Flags |= C_PICTURE_UPDATES_FORBIDDEN;
5257 LSquareUnder->Freeze();
5258 StackUnder->Freeze();
5259 double Danger = GetRelativeDanger(PLAYER);
5260 if (OldEquipment) OldEquipment->RemoveFromSlot();
5261 Item->RemoveFromSlot();
5262 SetEquipment(e, Item);
5263 double NewDanger = GetRelativeDanger(PLAYER);
5264 Item->RemoveFromSlot();
5265 StackUnder->AddItem(Item);
5266 if (OldEquipment) SetEquipment(e, OldEquipment);
5267 msgsystem::EnableMessages();
5268 Flags &= ~C_PICTURE_UPDATES_FORBIDDEN;
5269 LSquareUnder->UnFreeze();
5270 StackUnder->UnFreeze();
5271 if (OldEquipment) {
5272 if (NewDanger > Danger || BoundToUse(Item, e)) {
5273 room *Room = GetRoom();
5274 if (!Room || Room->PickupItem(this, Item, 1)) {
5275 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s drops %s %s and equips %s instead.", CHAR_NAME(DEFINITE), CHAR_POSSESSIVE_PRONOUN, OldEquipment->CHAR_NAME(UNARTICLED), Item->CHAR_NAME(INDEFINITE));
5276 if (Room) Room->DropItem(this, OldEquipment, 1);
5277 OldEquipment->MoveTo(StackUnder);
5278 Item->RemoveFromSlot();
5279 SetEquipment(e, Item);
5280 DexterityAction(5);
5281 return true;
5284 } else {
5285 if (NewDanger > Danger || (NewDanger == Danger && e != RIGHT_WIELDED_INDEX && e != LEFT_WIELDED_INDEX) || BoundToUse(Item, e)) {
5286 room *Room = GetRoom();
5287 if (!Room || Room->PickupItem(this, Item, 1)) {
5288 if (CanBeSeenByPlayer()) ADD_MESSAGE("%s picks up and equips %s.", CHAR_NAME(DEFINITE), Item->CHAR_NAME(INDEFINITE));
5289 Item->RemoveFromSlot();
5290 SetEquipment(e, Item);
5291 DexterityAction(5);
5292 return true;
5299 return false;
5303 truth character::TryToConsume (item *Item) {
5304 return Item->CanBeEatenByAI(this) && ConsumeItem(Item, Item->GetConsumeMaterial(this)->GetConsumeVerb());
5308 void character::UpdateESPLOS () const {
5309 if (StateIsActivated(ESP) && !game::IsInWilderness()) {
5310 for (int c = 0; c < game::GetTeams(); ++c) {
5311 for (std::list<character *>::const_iterator i = game::GetTeam(c)->GetMember().begin(); i != game::GetTeam(c)->GetMember().end(); ++i) {
5312 const character *ch = *i;
5313 if (ch->IsEnabled()) ch->SendNewDrawRequest();
5320 int character::GetCWeaponSkillLevel (citem *Item) const {
5321 if (Item->GetWeaponCategory() < GetAllowedWeaponSkillCategories()) return GetCWeaponSkill(Item->GetWeaponCategory())->GetLevel();
5322 return 0;
5326 void character::PrintBeginPanicMessage () const {
5327 if (IsPlayer()) ADD_MESSAGE("You panic!");
5328 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s panics.", CHAR_NAME(DEFINITE));
5332 void character::PrintEndPanicMessage () const {
5333 if (IsPlayer()) ADD_MESSAGE("You finally calm down.");
5334 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s calms down.", CHAR_NAME(DEFINITE));
5338 void character::CheckPanic (int Ticks) {
5339 if (GetPanicLevel() > 1 && !StateIsActivated(PANIC) && GetHP()*100 < RAND()%(GetPanicLevel()*GetMaxHP()<<1) && !StateIsActivated(FEARLESS)) {
5340 BeginTemporaryState(PANIC, ((Ticks * 3) >> 2) + RAND() % ((Ticks >> 1) + 1)); // 25% randomness to ticks...
5345 /* returns 0 if fails else the newly created character */
5346 character *character::DuplicateToNearestSquare (character *Cloner, feuLong Flags) {
5347 character *NewlyCreated = Duplicate(Flags);
5348 if (!NewlyCreated) return 0;
5349 if (Flags & CHANGE_TEAM && Cloner) NewlyCreated->ChangeTeam(Cloner->GetTeam());
5350 NewlyCreated->PutNear(GetPos());
5351 return NewlyCreated;
5355 void character::SignalSpoil (material *m) {
5356 if (GetMotherEntity()) GetMotherEntity()->SignalSpoil(m);
5357 else Disappear(0, "spoil", &item::IsVeryCloseToSpoiling);
5361 truth character::CanHeal () const {
5362 for (int c = 0; c < BodyParts; ++c) {
5363 bodypart *BodyPart = GetBodyPart(c);
5364 if (BodyPart && BodyPart->CanRegenerate() && BodyPart->GetHP() < BodyPart->GetMaxHP()) return true;
5366 return false;
5370 int character::GetRelation (ccharacter *Who) const {
5371 return GetTeam()->GetRelation(Who->GetTeam());
5375 truth (item::*AffectTest[BASE_ATTRIBUTES])() const = {
5376 &item::AffectsEndurance,
5377 &item::AffectsPerception,
5378 &item::AffectsIntelligence,
5379 &item::AffectsWisdom,
5380 &item::AffectsWillPower,
5381 &item::AffectsCharisma,
5382 &item::AffectsMana
5386 /* Returns nonzero if endurance has decreased and death may occur */
5387 truth character::CalculateAttributeBonuses () {
5388 doforbodyparts()(this, &bodypart::CalculateAttributeBonuses);
5389 int BackupBonus[BASE_ATTRIBUTES];
5390 int BackupCarryingBonus = CarryingBonus;
5391 CarryingBonus = 0;
5392 int c1;
5393 for (c1 = 0; c1 < BASE_ATTRIBUTES; ++c1) {
5394 BackupBonus[c1] = AttributeBonus[c1];
5395 AttributeBonus[c1] = 0;
5397 for (c1 = 0; c1 < GetEquipments(); ++c1) {
5398 item *Equipment = GetEquipment(c1);
5399 if (!Equipment || !Equipment->IsInCorrectSlot(c1)) continue;
5400 for (int c2 = 0; c2 < BASE_ATTRIBUTES; ++c2) {
5401 if ((Equipment->*AffectTest[c2])()) AttributeBonus[c2] += Equipment->GetEnchantment();
5403 if (Equipment->AffectsCarryingCapacity()) CarryingBonus += Equipment->GetCarryingBonus();
5406 ApplySpecialAttributeBonuses();
5408 if (IsPlayer() && !IsInitializing() && AttributeBonus[PERCEPTION] != BackupBonus[PERCEPTION]) game::SendLOSUpdateRequest();
5409 if (IsPlayer() && !IsInitializing() && AttributeBonus[INTELLIGENCE] != BackupBonus[INTELLIGENCE]) UpdateESPLOS();
5411 if (!IsInitializing() && CarryingBonus != BackupCarryingBonus) CalculateBurdenState();
5413 if (!IsInitializing() && AttributeBonus[ENDURANCE] != BackupBonus[ENDURANCE]) {
5414 CalculateBodyPartMaxHPs();
5415 CalculateMaxStamina();
5416 return AttributeBonus[ENDURANCE] < BackupBonus[ENDURANCE];
5419 return false;
5423 void character::ApplyEquipmentAttributeBonuses (item *Equipment) {
5424 if (Equipment->AffectsEndurance()) {
5425 AttributeBonus[ENDURANCE] += Equipment->GetEnchantment();
5426 CalculateBodyPartMaxHPs();
5427 CalculateMaxStamina();
5429 if (Equipment->AffectsPerception()) {
5430 AttributeBonus[PERCEPTION] += Equipment->GetEnchantment();
5431 if (IsPlayer()) game::SendLOSUpdateRequest();
5433 if (Equipment->AffectsIntelligence()) {
5434 AttributeBonus[INTELLIGENCE] += Equipment->GetEnchantment();
5435 if (IsPlayer()) UpdateESPLOS();
5437 if (Equipment->AffectsWisdom()) AttributeBonus[WISDOM] += Equipment->GetEnchantment();
5438 if (Equipment->AffectsWillPower()) AttributeBonus[WILL_POWER] += Equipment->GetEnchantment();
5439 if (Equipment->AffectsCharisma()) AttributeBonus[CHARISMA] += Equipment->GetEnchantment();
5440 if (Equipment->AffectsMana()) AttributeBonus[MANA] += Equipment->GetEnchantment();
5441 if (Equipment->AffectsCarryingCapacity()) {
5442 CarryingBonus += Equipment->GetCarryingBonus();
5443 CalculateBurdenState();
5448 void character::ReceiveAntidote (sLong Amount) {
5449 if (StateIsActivated(POISONED)) {
5450 if (GetTemporaryStateCounter(POISONED) > Amount) {
5451 EditTemporaryStateCounter(POISONED, -Amount);
5452 Amount = 0;
5453 } else {
5454 if (IsPlayer()) ADD_MESSAGE("Aaaah... You feel much better.");
5455 Amount -= GetTemporaryStateCounter(POISONED);
5456 DeActivateTemporaryState(POISONED);
5459 if ((Amount >= 100 || RAND_N(100) < Amount) && StateIsActivated(PARASITIZED)) {
5460 if (IsPlayer()) ADD_MESSAGE("Something in your belly didn't seem to like this stuff.");
5461 DeActivateTemporaryState(PARASITIZED);
5462 Amount -= Min(100, Amount);
5464 if ((Amount >= 100 || RAND_N(100) < Amount) && StateIsActivated(LEPROSY)) {
5465 if (IsPlayer()) ADD_MESSAGE("You are not falling to pieces anymore.");
5466 DeActivateTemporaryState(LEPROSY);
5467 Amount -= Min(100, Amount);
5472 void character::AddAntidoteConsumeEndMessage () const {
5473 if (StateIsActivated(POISONED)) {
5474 // true only if the antidote didn't cure the poison completely
5475 if (IsPlayer()) ADD_MESSAGE("Your body processes the poison in your veins with rapid speed.");
5480 truth character::IsDead () const {
5481 for (int c = 0; c < BodyParts; ++c) {
5482 bodypart *BodyPart = GetBodyPart(c);
5483 if (BodyPartIsVital(c) && (!BodyPart || BodyPart->GetHP() < 1)) return true;
5485 return false;
5489 void character::SignalSpoilLevelChange (material *m) {
5490 if (GetMotherEntity()) GetMotherEntity()->SignalSpoilLevelChange(m); else UpdatePictures();
5494 void character::AddOriginalBodyPartID (int I, feuLong What) {
5495 if (std::find(OriginalBodyPartID[I].begin(), OriginalBodyPartID[I].end(), What) == OriginalBodyPartID[I].end()) {
5496 OriginalBodyPartID[I].push_back(What);
5497 if (OriginalBodyPartID[I].size() > 100) OriginalBodyPartID[I].erase(OriginalBodyPartID[I].begin());
5502 void character::AddToInventory (const fearray<contentscript<item> > &ItemArray, int SpecialFlags) {
5503 for (uInt c1 = 0; c1 < ItemArray.Size; ++c1) {
5504 if (ItemArray[c1].IsValid()) {
5505 const interval *TimesPtr = ItemArray[c1].GetTimes();
5506 int Times = TimesPtr ? TimesPtr->Randomize() : 1;
5507 for (int c2 = 0; c2 < Times; ++c2) {
5508 item *Item = ItemArray[c1].Instantiate(SpecialFlags);
5509 if (Item) {
5510 Stack->AddItem(Item);
5511 Item->SpecialGenerationHandler();
5519 truth character::HasHadBodyPart (citem *Item) const {
5520 for (int c = 0; c < BodyParts; ++c)
5521 if (std::find(OriginalBodyPartID[c].begin(), OriginalBodyPartID[c].end(), Item->GetID()) != OriginalBodyPartID[c].end())
5522 return true;
5523 return GetPolymorphBackup() && GetPolymorphBackup()->HasHadBodyPart(Item);
5527 festring &character::ProcessMessage (festring &Msg) const {
5528 SEARCH_N_REPLACE(Msg, "@nu", GetName(UNARTICLED));
5529 SEARCH_N_REPLACE(Msg, "@ni", GetName(INDEFINITE));
5530 SEARCH_N_REPLACE(Msg, "@nd", GetName(DEFINITE));
5531 SEARCH_N_REPLACE(Msg, "@du", GetDescription(UNARTICLED));
5532 SEARCH_N_REPLACE(Msg, "@di", GetDescription(INDEFINITE));
5533 SEARCH_N_REPLACE(Msg, "@dd", GetDescription(DEFINITE));
5534 SEARCH_N_REPLACE(Msg, "@pp", GetPersonalPronoun());
5535 SEARCH_N_REPLACE(Msg, "@sp", GetPossessivePronoun());
5536 SEARCH_N_REPLACE(Msg, "@op", GetObjectPronoun());
5537 SEARCH_N_REPLACE(Msg, "@Nu", GetName(UNARTICLED).CapitalizeCopy());
5538 SEARCH_N_REPLACE(Msg, "@Ni", GetName(INDEFINITE).CapitalizeCopy());
5539 SEARCH_N_REPLACE(Msg, "@Nd", GetName(DEFINITE).CapitalizeCopy());
5540 SEARCH_N_REPLACE(Msg, "@Du", GetDescription(UNARTICLED).CapitalizeCopy());
5541 SEARCH_N_REPLACE(Msg, "@Di", GetDescription(INDEFINITE).CapitalizeCopy());
5542 SEARCH_N_REPLACE(Msg, "@Dd", GetDescription(DEFINITE).CapitalizeCopy());
5543 SEARCH_N_REPLACE(Msg, "@Pp", GetPersonalPronoun().CapitalizeCopy());
5544 SEARCH_N_REPLACE(Msg, "@Sp", GetPossessivePronoun().CapitalizeCopy());
5545 SEARCH_N_REPLACE(Msg, "@Op", GetObjectPronoun().CapitalizeCopy());
5546 SEARCH_N_REPLACE(Msg, "@Gd", GetMasterGod()->GetName());
5547 return Msg;
5551 void character::ProcessAndAddMessage (festring Msg) const {
5552 ADD_MESSAGE("%s", ProcessMessage(Msg).CStr());
5556 void character::BeTalkedTo () {
5557 static sLong Said;
5558 if (GetRelation(PLAYER) == HOSTILE)
5559 ProcessAndAddMessage(GetHostileReplies()[RandomizeReply(Said, GetHostileReplies().Size)]);
5560 else
5561 ProcessAndAddMessage(GetFriendlyReplies()[RandomizeReply(Said, GetFriendlyReplies().Size)]);
5565 truth character::CheckZap () {
5566 if (!CanZap()) {
5567 ADD_MESSAGE("This monster type can't zap.");
5568 return false;
5570 return true;
5574 void character::DamageAllItems (character *Damager, int Damage, int Type) {
5575 GetStack()->ReceiveDamage(Damager, Damage, Type);
5576 for (int c = 0; c < GetEquipments(); ++c) {
5577 item *Equipment = GetEquipment(c);
5578 if (Equipment) Equipment->ReceiveDamage(Damager, Damage, Type);
5583 truth character::Equips (citem *Item) const {
5584 return combineequipmentpredicateswithparam<feuLong>()(this, &item::HasID, Item->GetID(), 1);
5588 void character::PrintBeginConfuseMessage () const {
5589 if (IsPlayer()) ADD_MESSAGE("You feel quite happy.");
5593 void character::PrintEndConfuseMessage () const {
5594 if (IsPlayer()) ADD_MESSAGE("The world is boring again.");
5598 v2 character::ApplyStateModification (v2 TryDirection) const {
5599 if (!StateIsActivated(CONFUSED) || RAND() & 15 || game::IsInWilderness()) return TryDirection;
5600 v2 To = GetLevel()->GetFreeAdjacentSquare(this, GetPos(), true);
5601 if (To == ERROR_V2) return TryDirection;
5602 To -= GetPos();
5603 if (To != TryDirection && IsPlayer()) ADD_MESSAGE("Whoa! You somehow don't manage to walk straight.");
5604 return To;
5608 void character::AddConfuseHitMessage () const {
5609 if (IsPlayer()) ADD_MESSAGE("This stuff is confusing.");
5613 item *character::SelectFromPossessions (cfestring &Topic, sorter Sorter) {
5614 itemvector ReturnVector;
5615 SelectFromPossessions(ReturnVector, Topic, NO_MULTI_SELECT, Sorter);
5616 return !ReturnVector.empty() ? ReturnVector[0] : 0;
5620 truth character::SelectFromPossessions (itemvector &ReturnVector, cfestring &Topic, int Flags, sorter Sorter) {
5621 felist List(Topic);
5622 truth InventoryPossible = GetStack()->SortedItems(this, Sorter);
5623 if (InventoryPossible) List.AddEntry(CONST_S("choose from inventory"), LIGHT_GRAY, 20, game::AddToItemDrawVector(itemvector()));
5624 truth Any = false;
5625 itemvector Item;
5626 festring Entry;
5627 int c;
5628 for (c = 0; c < BodyParts; ++c) {
5629 bodypart *BodyPart = GetBodyPart(c);
5630 if (BodyPart && (Sorter == 0 || (BodyPart->*Sorter)(this))) {
5631 Item.push_back(BodyPart);
5632 Entry.Empty();
5633 BodyPart->AddName(Entry, UNARTICLED);
5634 int ImageKey = game::AddToItemDrawVector(itemvector(1, BodyPart));
5635 List.AddEntry(Entry, LIGHT_GRAY, 20, ImageKey, true);
5636 Any = true;
5639 for (c = 0; c < GetEquipments(); ++c) {
5640 item *Equipment = GetEquipment(c);
5641 if (Equipment && (Sorter == 0 || (Equipment->*Sorter)(this))) {
5642 Item.push_back(Equipment);
5643 Entry = GetEquipmentName(c);
5644 Entry << ':';
5645 Entry.Resize(20);
5646 Equipment->AddInventoryEntry(this, Entry, 1, true);
5647 AddSpecialEquipmentInfo(Entry, c);
5648 int ImageKey = game::AddToItemDrawVector(itemvector(1, Equipment));
5649 List.AddEntry(Entry, LIGHT_GRAY, 20, ImageKey, true);
5650 Any = true;
5653 if (Any) {
5654 game::SetStandardListAttributes(List);
5655 List.SetFlags(SELECTABLE|DRAW_BACKGROUND_AFTERWARDS);
5656 List.SetEntryDrawer(game::ItemEntryDrawer);
5657 game::DrawEverythingNoBlit();
5658 int Chosen = List.Draw();
5659 game::ClearItemDrawVector();
5660 if (Chosen != ESCAPED) {
5661 if ((InventoryPossible && !Chosen) || Chosen & FELIST_ERROR_BIT) {
5662 GetStack()->DrawContents(ReturnVector, this, Topic, Flags, Sorter);
5663 } else {
5664 ReturnVector.push_back(Item[InventoryPossible ? Chosen - 1 : Chosen]);
5665 if (Flags & SELECT_PAIR && ReturnVector[0]->HandleInPairs()) {
5666 item *PairEquipment = GetPairEquipment(ReturnVector[0]->GetEquipmentIndex());
5667 if (PairEquipment && PairEquipment->CanBePiledWith(ReturnVector[0], this)) ReturnVector.push_back(PairEquipment);
5671 } else {
5672 if (!GetStack()->SortedItems(this, Sorter)) return false;
5673 game::ClearItemDrawVector();
5674 GetStack()->DrawContents(ReturnVector, this, Topic, Flags, Sorter);
5676 return true;
5680 truth character::EquipsSomething (sorter Sorter) {
5681 for (int c = 0; c < GetEquipments(); ++c) {
5682 item *Equipment = GetEquipment(c);
5683 if (Equipment && (Sorter == 0 || (Equipment->*Sorter)(this))) return true;
5685 return false;
5689 material *character::CreateBodyPartMaterial (int, sLong Volume) const {
5690 return MAKE_MATERIAL(GetFleshMaterial(), Volume);
5694 truth character::CheckTalk () {
5695 if (!CanTalk()) {
5696 ADD_MESSAGE("This monster does not know the art of talking.");
5697 return false;
5699 return true;
5703 truth character::MoveTowardsHomePos () {
5704 if (HomeDataIsValid() && IsEnabled()) {
5705 SetGoingTo(HomeData->Pos);
5706 return MoveTowardsTarget(false) || (!GetPos().IsAdjacent(HomeData->Pos) && MoveRandomly());
5708 return false;
5712 int character::TryToChangeEquipment (stack *MainStack, stack *SecStack, int Chosen) {
5713 if (!GetBodyPartOfEquipment(Chosen)) {
5714 ADD_MESSAGE("Bodypart missing!");
5715 return 0;
5718 item *OldEquipment = GetEquipment(Chosen);
5719 if (!IsPlayer() && BoundToUse(OldEquipment, Chosen)) {
5720 ADD_MESSAGE("%s refuses to unequip %s.", CHAR_DESCRIPTION(DEFINITE), OldEquipment->CHAR_NAME(DEFINITE));
5721 return 0;
5723 if (OldEquipment) OldEquipment->MoveTo(MainStack);
5725 sorter Sorter = EquipmentSorter(Chosen);
5726 if (!MainStack->SortedItems(this, Sorter) && (!SecStack || !SecStack->SortedItems(this, Sorter))) {
5727 ADD_MESSAGE("You haven't got any item that could be used for this purpose.");
5728 return 0;
5731 game::DrawEverythingNoBlit();
5732 itemvector ItemVector;
5733 int Return = MainStack->DrawContents(ItemVector, SecStack, this,
5734 CONST_S("Choose ")+GetEquipmentName(Chosen)+':',
5735 (SecStack ? CONST_S("Items in your inventory") : CONST_S("")),
5736 (SecStack ? festring(CONST_S("Items in ")+GetPossessivePronoun()+" inventory") : CONST_S("")),
5737 (SecStack ? festring(GetDescription(DEFINITE)+" is "+GetVerbalBurdenState()) : CONST_S("")),
5738 GetVerbalBurdenStateColor(),
5739 NONE_AS_CHOICE|NO_MULTI_SELECT|SELECT_PAIR|SKIP_FIRST_IF_NO_OLD|SELECT_MOST_RECENT,
5740 Sorter, OldEquipment);
5741 if (Return == ESCAPED) {
5742 if (OldEquipment) {
5743 OldEquipment->RemoveFromSlot();
5744 SetEquipment(Chosen, OldEquipment);
5746 return 0;
5749 item *Item = (ItemVector.empty() ? 0 : ItemVector[0]);
5750 int otherChosen = -1;
5752 if (Item) {
5753 if (!IsPlayer() && !AllowEquipment(Item, Chosen)) {
5754 ADD_MESSAGE("%s refuses to equip %s.", CHAR_DESCRIPTION(DEFINITE), Item->CHAR_NAME(DEFINITE));
5755 return 0;
5757 if (ItemVector[0]->HandleInPairs() && ItemVector.size() > 1) {
5758 switch (Chosen) {
5759 case RIGHT_GAUNTLET_INDEX: otherChosen = LEFT_GAUNTLET_INDEX; break;
5760 case LEFT_GAUNTLET_INDEX: otherChosen = RIGHT_GAUNTLET_INDEX; break;
5761 case RIGHT_BOOT_INDEX: otherChosen = LEFT_BOOT_INDEX; break;
5762 case LEFT_BOOT_INDEX: otherChosen = RIGHT_BOOT_INDEX; break;
5763 default: break;
5765 if (otherChosen != -1) {
5766 if (GetBodyPartOfEquipment(otherChosen)) {
5767 if (!game::TruthQuestion("Do you want to wear both items?", NO)) otherChosen = -1;
5768 } else {
5769 otherChosen = -1;
5773 // wear/wield first item
5774 Item->RemoveFromSlot();
5775 SetEquipment(Chosen, Item);
5776 if (CheckIfEquipmentIsNotUsable(Chosen)) { Item->MoveTo(MainStack); Item = 0; otherChosen = -1; } // small bug?
5777 // wear/wield possible second item
5778 if (Item && otherChosen != -1 && ItemVector[0]->HandleInPairs() && ItemVector.size() > 1 && GetBodyPartOfEquipment(otherChosen)) {
5779 item *otherOld = GetEquipment(otherChosen);
5780 if (otherOld && !IsPlayer() && BoundToUse(otherOld, otherChosen)) {
5781 ADD_MESSAGE("%s refuses to unequip %s.", CHAR_DESCRIPTION(DEFINITE), otherOld->CHAR_NAME(DEFINITE));
5782 otherChosen = -1;
5783 } else if (otherOld) {
5784 otherOld->MoveTo(MainStack);
5786 if (otherChosen != -1) {
5787 ItemVector[1]->RemoveFromSlot();
5788 SetEquipment(otherChosen, ItemVector[1]);
5789 if (CheckIfEquipmentIsNotUsable(otherChosen)) { ItemVector[1]->MoveTo(MainStack); otherChosen = -1; } // small bug?
5794 return (Item != OldEquipment ? (otherChosen != -1 ? 2 : 1) : 0);
5798 void character::PrintBeginParasitizedMessage () const {
5799 if (IsPlayer()) ADD_MESSAGE("You feel you are no longer alone.");
5803 void character::PrintEndParasitizedMessage () const {
5804 if (IsPlayer()) ADD_MESSAGE("A feeling of long welcome emptiness overwhelms you.");
5808 void character::ParasitizedHandler () {
5809 EditNP(-5);
5810 if (!(RAND() % 250)) {
5811 if (IsPlayer()) ADD_MESSAGE("Ugh. You feel something violently carving its way through your intestines.");
5812 ReceiveDamage(0, 1, POISON, TORSO, 8, false, false, false, false);
5813 CheckDeath(CONST_S("killed by a vile parasite"), 0);
5818 truth character::CanFollow () const {
5819 return CanMove() && !StateIsActivated(PANIC) && !IsStuck();
5823 festring character::GetKillName () const {
5824 if (!GetPolymorphBackup()) return GetName(INDEFINITE);
5825 festring KillName;
5826 GetPolymorphBackup()->AddName(KillName, INDEFINITE);
5827 KillName << " polymorphed into ";
5828 id::AddName(KillName, INDEFINITE);
5829 return KillName;
5833 festring character::GetPanelName () const {
5834 festring Name;
5835 Name << AssignedName << " the " << game::GetVerbalPlayerAlignment() << ' ';
5836 id::AddName(Name, UNARTICLED);
5837 return Name;
5841 sLong character::GetMoveAPRequirement (int Difficulty) const {
5842 return (!StateIsActivated(PANIC) ? 10000000 : 8000000) * Difficulty / (APBonus(GetAttribute(AGILITY)) * GetMoveEase());
5846 bodypart *character::HealHitPoint() {
5847 int NeedHeal = 0, NeedHealIndex[MAX_BODYPARTS];
5848 for (int c = 0; c < BodyParts; ++c) {
5849 bodypart *BodyPart = GetBodyPart(c);
5850 if (BodyPart && BodyPart->CanRegenerate() && BodyPart->GetHP() < BodyPart->GetMaxHP()) NeedHealIndex[NeedHeal++] = c;
5852 if (NeedHeal) {
5853 bodypart *BodyPart = GetBodyPart(NeedHealIndex[RAND() % NeedHeal]);
5854 BodyPart->IncreaseHP();
5855 ++HP;
5856 return BodyPart;
5858 return 0;
5862 void character::CreateHomeData () {
5863 HomeData = new homedata;
5864 lsquare *Square = GetLSquareUnder();
5865 HomeData->Pos = Square->GetPos();
5866 HomeData->Dungeon = Square->GetDungeonIndex();
5867 HomeData->Level = Square->GetLevelIndex();
5868 HomeData->Room = Square->GetRoomIndex();
5872 room *character::GetHomeRoom() const {
5873 if (HomeDataIsValid() && HomeData->Room) return GetLevel()->GetRoom(HomeData->Room);
5874 return 0;
5878 void character::RemoveHomeData () {
5879 delete HomeData;
5880 HomeData = 0;
5884 void character::AddESPConsumeMessage () const {
5885 if (IsPlayer()) ADD_MESSAGE("You feel a strange mental activity.");
5889 void character::SetBodyPart (int I, bodypart *What) {
5890 BodyPartSlot[I].PutInItem(What);
5891 if (What) {
5892 What->SignalPossibleUsabilityChange();
5893 What->Disable();
5894 AddOriginalBodyPartID(I, What->GetID());
5895 if (What->GetMainMaterial()->IsInfectedByLeprosy()) GainIntrinsic(LEPROSY);
5896 else if (StateIsActivated(LEPROSY)) What->GetMainMaterial()->SetIsInfectedByLeprosy(true);
5901 truth character::ConsumeItem (item *Item, cfestring &ConsumeVerb) {
5902 if (IsPlayer() && HasHadBodyPart(Item) && !game::TruthQuestion(CONST_S("Are you sure? You may be able to put it back..."))) {
5903 return false;
5905 if (Item->IsOnGround() && GetRoom() && !GetRoom()->ConsumeItem(this, Item, 1)) {
5906 return false;
5908 if (IsPlayer()) ADD_MESSAGE("You begin %s %s.", ConsumeVerb.CStr(), Item->CHAR_NAME(DEFINITE));
5909 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s begins %s %s.", CHAR_NAME(DEFINITE), ConsumeVerb.CStr(), Item->CHAR_NAME(DEFINITE));
5910 consume *Consume = consume::Spawn(this);
5911 Consume->SetDescription(ConsumeVerb);
5912 Consume->SetConsumingID(Item->GetID());
5913 SetAction(Consume);
5914 DexterityAction(5);
5915 return true;
5919 truth character::CheckThrow () const {
5920 if (!CanThrow()) {
5921 ADD_MESSAGE("This monster type cannot throw.");
5922 return false;
5924 return true;
5928 void character::GetHitByExplosion (const explosion *Explosion, int Damage) {
5929 int DamageDirection = GetPos() == Explosion->Pos ? RANDOM_DIR : game::CalculateRoughDirection(GetPos() - Explosion->Pos);
5930 if (!IsPet() && Explosion->Terrorist && Explosion->Terrorist->IsPet()) Explosion->Terrorist->Hostility(this);
5931 GetTorso()->SpillBlood((8 - Explosion->Size + RAND() % (8 - Explosion->Size)) >> 1);
5932 if (DamageDirection == RANDOM_DIR) DamageDirection = RAND()&7;
5933 v2 SpillPos = GetPos() + game::GetMoveVector(DamageDirection);
5934 if (SquareUnder[0] && GetArea()->IsValidPos(SpillPos)) GetTorso()->SpillBlood((8-Explosion->Size+RAND()%(8-Explosion->Size))>>1, SpillPos);
5935 if (IsPlayer()) ADD_MESSAGE("You are hit by the explosion!");
5936 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s is hit by the explosion.", CHAR_NAME(DEFINITE));
5937 truth WasUnconscious = GetAction() && GetAction()->IsUnconsciousness();
5938 ReceiveDamage(Explosion->Terrorist, Damage >> 1, FIRE, ALL, DamageDirection, true, false, false, false);
5939 if (IsEnabled()) {
5940 ReceiveDamage(Explosion->Terrorist, Damage >> 1, PHYSICAL_DAMAGE, ALL, DamageDirection, true, false, false, false);
5941 CheckDeath(Explosion->DeathMsg, Explosion->Terrorist, !WasUnconscious ? IGNORE_UNCONSCIOUSNESS : 0);
5946 void character::SortAllItems (const sortdata &SortData) {
5947 GetStack()->SortAllItems(SortData);
5948 doforequipmentswithparam<const sortdata&>()(this, &item::SortAllItems, SortData);
5952 void character::PrintBeginSearchingMessage () const {
5953 if (IsPlayer()) ADD_MESSAGE("You feel you can now notice even the very smallest details around you.");
5957 void character::PrintEndSearchingMessage () const {
5958 if (IsPlayer()) ADD_MESSAGE("You feel less perceptive.");
5962 void character::SearchingHandler () {
5963 if (!game::IsInWilderness()) Search(15);
5967 void character::Search (int Perception) {
5968 for (int d = 0; d < GetExtendedNeighbourSquares(); ++d) {
5969 lsquare *LSquare = GetNeighbourLSquare(d);
5970 if (LSquare) LSquare->GetStack()->Search(this, Min(Perception, 200));
5975 // surprisingly returns 0 if fails
5976 character *character::GetRandomNeighbour (int RelationFlags) const {
5977 character *Chars[MAX_NEIGHBOUR_SQUARES];
5978 int Index = 0;
5979 for (int d = 0; d < GetNeighbourSquares(); ++d) {
5980 lsquare *LSquare = GetNeighbourLSquare(d);
5981 if (LSquare) {
5982 character *Char = LSquare->GetCharacter();
5983 if (Char && (GetRelation(Char) & RelationFlags)) Chars[Index++] = Char;
5986 return Index ? Chars[RAND() % Index] : 0;
5990 void character::ResetStates () {
5991 for (int c = 0; c < STATES; ++c) {
5992 if (1 << c != POLYMORPHED && TemporaryStateIsActivated(1 << c) && TemporaryStateCounter[c] != PERMANENT) {
5993 TemporaryState &= ~(1 << c);
5994 if (StateData[c].EndHandler) {
5995 (this->*StateData[c].EndHandler)();
5996 if (!IsEnabled())return;
6003 void characterdatabase::InitDefaults (const characterprototype *NewProtoType, int NewConfig, cfestring &acfgstrname) {
6004 IsAbstract = false;
6005 ProtoType = NewProtoType;
6006 Config = NewConfig;
6007 CfgStrName = acfgstrname;
6008 Alias.Clear();
6012 void character::PrintBeginGasImmunityMessage () const {
6013 if (IsPlayer()) ADD_MESSAGE("All smells fade away.");
6017 void character::PrintEndGasImmunityMessage () const {
6018 if (IsPlayer()) ADD_MESSAGE("Yuck! The world smells bad again.");
6022 void character::ShowAdventureInfo () const {
6023 static const char *lists[4][4] = {
6024 { "Show massacre history",
6025 "Show inventory",
6026 "Show message history",
6027 NULL },
6028 { "Show inventory",
6029 "Show message history",
6030 NULL,
6031 NULL },
6032 { "Show message history",
6033 NULL,
6034 NULL,
6035 NULL },
6036 { "Show massacre history",
6037 "Show message history",
6038 NULL,
6039 NULL }
6041 // massacre, inventory, messages
6042 static const int nums[4][3] = {
6043 { 0, 1, 2},
6044 {-1, 0, 1},
6045 {-1,-1, 0},
6046 { 0,-1, 0}
6048 int idx = 0;
6049 if (GetStack()->GetItems()) {
6050 idx = game::MassacreListsEmpty() ? 1 : 0;
6051 } else {
6052 idx = game::MassacreListsEmpty() ? 2 : 3;
6054 int sel = -1;
6055 for (;;) {
6056 sel = game::ListSelectorArray(sel, CONST_S("Do you want to see some funny history?"), lists[idx]);
6057 if (sel < 0) break;
6058 if (sel == nums[idx][0] && !game::MassacreListsEmpty()) {
6059 game::DisplayMassacreLists();
6061 if (sel == nums[idx][1] && GetStack()->GetItems()) {
6062 GetStack()->DrawContents(this, CONST_S("Your inventory"), NO_SELECT);
6063 for(stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) i->DrawContents(this);
6064 doforequipmentswithparam<ccharacter *>()(this, &item::DrawContents, this);
6066 if (sel == nums[idx][2]) {
6067 msgsystem::DrawMessageHistory();
6073 truth character::EditAllAttributes (int Amount) {
6074 if (!Amount) return true;
6075 int c;
6076 truth MayEditMore = false;
6077 for (c = 0; c < BodyParts; ++c) {
6078 bodypart *BodyPart = GetBodyPart(c);
6079 if (BodyPart && BodyPart->EditAllAttributes(Amount)) MayEditMore = true;
6081 for (c = 0; c < BASE_ATTRIBUTES; ++c) {
6082 if (BaseExperience[c]) {
6083 BaseExperience[c] += Amount * EXP_MULTIPLIER;
6084 LimitRef(BaseExperience[c], MIN_EXP, MAX_EXP);
6085 if ((Amount < 0 && BaseExperience[c] != MIN_EXP) || (Amount > 0 && BaseExperience[c] != MAX_EXP)) MayEditMore = true;
6088 CalculateAll();
6089 RestoreHP();
6090 RestoreStamina();
6091 if (IsPlayer()) {
6092 game::SendLOSUpdateRequest();
6093 UpdateESPLOS();
6095 if (IsPlayerKind()) UpdatePictures();
6096 return MayEditMore;
6100 #ifdef WIZARD
6101 void character::AddAttributeInfo (festring &Entry) const {
6102 Entry.Resize(57);
6103 Entry << GetAttribute(ENDURANCE);
6104 Entry.Resize(60);
6105 Entry << GetAttribute(PERCEPTION);
6106 Entry.Resize(63);
6107 Entry << GetAttribute(INTELLIGENCE);
6108 Entry.Resize(66);
6109 Entry << GetAttribute(WISDOM);
6110 Entry.Resize(69);
6111 Entry << GetAttribute(CHARISMA);
6112 Entry.Resize(72);
6113 Entry << GetAttribute(MANA);
6117 void character::AddDefenceInfo (felist &List) const {
6118 festring Entry;
6119 for (int c = 0; c < BodyParts; ++c) {
6120 bodypart *BodyPart = GetBodyPart(c);
6121 if (BodyPart) {
6122 Entry = CONST_S(" ");
6123 BodyPart->AddName(Entry, UNARTICLED);
6124 Entry.Resize(60);
6125 Entry << BodyPart->GetMaxHP();
6126 Entry.Resize(70);
6127 Entry << BodyPart->GetTotalResistance(PHYSICAL_DAMAGE);
6128 List.AddEntry(Entry, LIGHT_GRAY);
6134 void character::DetachBodyPart () {
6135 ADD_MESSAGE("You haven't got any extra bodyparts.");
6137 #endif
6140 void character::ReceiveHolyBanana (sLong Amount) {
6141 Amount <<= 1;
6142 EditExperience(ARM_STRENGTH, Amount, 1 << 13);
6143 EditExperience(LEG_STRENGTH, Amount, 1 << 13);
6144 EditExperience(DEXTERITY, Amount, 1 << 13);
6145 EditExperience(AGILITY, Amount, 1 << 13);
6146 EditExperience(ENDURANCE, Amount, 1 << 13);
6147 EditExperience(PERCEPTION, Amount, 1 << 13);
6148 EditExperience(INTELLIGENCE, Amount, 1 << 13);
6149 EditExperience(WISDOM, Amount, 1 << 13);
6150 EditExperience(CHARISMA, Amount, 1 << 13);
6151 RestoreLivingHP();
6155 void character::AddHolyBananaConsumeEndMessage () const {
6156 if (IsPlayer()) ADD_MESSAGE("You feel a mysterious strengthening fire coursing through your body.");
6157 else if (CanBeSeenByPlayer()) ADD_MESSAGE("For a moment %s is surrounded by a swirling fire aura.", CHAR_NAME(DEFINITE));
6161 void character::ReceiveHolyMango (sLong Amount) {
6162 Amount <<= 1;
6163 EditExperience(ARM_STRENGTH, Amount, 1 << 13);
6164 EditExperience(LEG_STRENGTH, Amount, 1 << 13);
6165 EditExperience(DEXTERITY, Amount, 1 << 13);
6166 EditExperience(AGILITY, Amount, 1 << 13);
6167 EditExperience(ENDURANCE, Amount, 1 << 13);
6168 EditExperience(PERCEPTION, Amount, 1 << 13);
6169 EditExperience(INTELLIGENCE, Amount, 1 << 13);
6170 EditExperience(WISDOM, Amount, 1 << 13);
6171 EditExperience(CHARISMA, Amount, 1 << 13);
6172 RestoreLivingHP();
6176 void character::AddHolyMangoConsumeEndMessage () const {
6177 if (IsPlayer()) ADD_MESSAGE("You feel a mysterious strengthening fire coursing through your body.");
6178 else if (CanBeSeenByPlayer()) ADD_MESSAGE("For a moment %s is surrounded by a swirling fire aura.", CHAR_NAME(DEFINITE));
6182 truth character::PreProcessForBone () {
6183 if (IsPet() && IsEnabled()) {
6184 Die(0, CONST_S(""), FORBID_REINCARNATION);
6185 return true;
6187 if (GetAction()) GetAction()->Terminate(false);
6188 if (TemporaryStateIsActivated(POLYMORPHED)) {
6189 character *PolymorphBackup = GetPolymorphBackup();
6190 EndPolymorph();
6191 PolymorphBackup->PreProcessForBone();
6192 return true;
6194 if (MustBeRemovedFromBone()) return false;
6195 if (IsUnique() && !CanBeGenerated()) game::SignalQuestMonsterFound();
6196 RestoreLivingHP();
6197 ResetStates();
6198 RemoveTraps();
6199 GetStack()->PreProcessForBone();
6200 doforequipments()(this, &item::PreProcessForBone);
6201 doforbodyparts()(this, &bodypart::PreProcessForBone);
6202 game::RemoveCharacterID(ID);
6203 ID = -ID;
6204 game::AddCharacterID(this, ID);
6205 return true;
6209 truth character::PostProcessForBone (double &DangerSum, int& Enemies) {
6210 if (PostProcessForBone()) {
6211 if (GetRelation(PLAYER) == HOSTILE) {
6212 double Danger = GetRelativeDanger(PLAYER, true);
6213 if (Danger > 99.0) game::SetTooGreatDangerFound(true);
6214 else if (!IsUnique() && !IgnoreDanger()) {
6215 DangerSum += Danger;
6216 ++Enemies;
6219 return true;
6221 return false;
6225 truth character::PostProcessForBone () {
6226 feuLong NewID = game::CreateNewCharacterID(this);
6227 game::GetBoneCharacterIDMap().insert(std::make_pair(-ID, NewID));
6228 game::RemoveCharacterID(ID);
6229 ID = NewID;
6230 if (IsUnique() && CanBeGenerated()) {
6231 if (DataBase->Flags & HAS_BEEN_GENERATED) return false;
6232 SignalGeneration();
6234 GetStack()->PostProcessForBone();
6235 doforequipments()(this, &item::PostProcessForBone);
6236 doforbodyparts()(this, &bodypart::PostProcessForBone);
6237 return true;
6241 void character::FinalProcessForBone () {
6242 Flags &= ~C_PLAYER;
6243 GetStack()->FinalProcessForBone();
6244 doforequipments()(this, &item::FinalProcessForBone);
6245 int c;
6246 for (c = 0; c < BodyParts; ++c) {
6247 for (std::list<feuLong>::iterator i = OriginalBodyPartID[c].begin(); i != OriginalBodyPartID[c].end();) {
6248 boneidmap::iterator BI = game::GetBoneItemIDMap().find(*i);
6249 if (BI == game::GetBoneItemIDMap().end()) {
6250 std::list<feuLong>::iterator Dirt = i++;
6251 OriginalBodyPartID[c].erase(Dirt);
6252 } else {
6253 *i = BI->second;
6254 ++i;
6261 void character::SetSoulID (feuLong What) {
6262 if (GetPolymorphBackup()) GetPolymorphBackup()->SetSoulID(What);
6266 truth character::SearchForItem (citem *Item) const {
6267 if (combineequipmentpredicateswithparam<feuLong>()(this, &item::HasID, Item->GetID(), 1)) return true;
6268 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) if (*i == Item) return true;
6269 return false;
6273 item *character::SearchForItem (const sweaponskill *SWeaponSkill) const {
6274 for (int c = 0; c < GetEquipments(); ++c) {
6275 item *Equipment = GetEquipment(c);
6276 if (Equipment && SWeaponSkill->IsSkillOf(Equipment)) return Equipment;
6278 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) if (SWeaponSkill->IsSkillOf(*i)) return *i;
6279 return 0;
6283 void character::PutNear (v2 Pos) {
6284 v2 NewPos = game::GetCurrentLevel()->GetNearestFreeSquare(this, Pos, false);
6285 if (NewPos == ERROR_V2) {
6286 do { NewPos = game::GetCurrentLevel()->GetRandomSquare(this); } while(NewPos == Pos);
6288 PutTo(NewPos);
6292 void character::PutToOrNear (v2 Pos) {
6293 if (game::IsInWilderness() || (CanMoveOn(game::GetCurrentLevel()->GetLSquare(Pos)) && IsFreeForMe(game::GetCurrentLevel()->GetLSquare(Pos))))
6294 PutTo(Pos);
6295 else
6296 PutNear(Pos);
6300 void character::PutTo (v2 Pos) {
6301 SquareUnder[0] = game::GetCurrentArea()->GetSquare(Pos);
6302 SquareUnder[0]->AddCharacter(this);
6306 void character::Remove () {
6307 SquareUnder[0]->RemoveCharacter();
6308 SquareUnder[0] = 0;
6312 void character::SendNewDrawRequest () const {
6313 for (int c = 0; c < SquaresUnder; ++c) {
6314 square *Square = GetSquareUnder(c);
6315 if (Square) Square->SendNewDrawRequest();
6320 truth character::IsOver (v2 Pos) const {
6321 for (int c = 0; c < SquaresUnder; ++c) {
6322 square *Square = GetSquareUnder(c);
6323 if (Square && Square->GetPos() == Pos) return true;
6325 return false;
6329 truth character::CanTheoreticallyMoveOn (const lsquare *LSquare) const { return GetMoveType() & LSquare->GetTheoreticalWalkability(); }
6330 truth character::CanMoveOn (const lsquare *LSquare) const { return GetMoveType() & LSquare->GetWalkability(); }
6331 truth character::CanMoveOn (const square *Square) const { return GetMoveType() & Square->GetSquareWalkability(); }
6332 truth character::CanMoveOn (const olterrain *OLTerrain) const { return GetMoveType() & OLTerrain->GetWalkability(); }
6333 truth character::CanMoveOn (const oterrain *OTerrain) const { return GetMoveType() & OTerrain->GetWalkability(); }
6334 truth character::IsFreeForMe(square *Square) const { return !Square->GetCharacter() || Square->GetCharacter() == this; }
6335 void character::LoadSquaresUnder () { SquareUnder[0] = game::GetSquareInLoad(); }
6337 truth character::AttackAdjacentEnemyAI () {
6338 if (!IsEnabled()) return false;
6339 character *Char[MAX_NEIGHBOUR_SQUARES];
6340 v2 Pos[MAX_NEIGHBOUR_SQUARES];
6341 int Dir[MAX_NEIGHBOUR_SQUARES];
6342 int Index = 0;
6343 for (int d = 0; d < GetNeighbourSquares(); ++d) {
6344 square *Square = GetNeighbourSquare(d);
6345 if (Square) {
6346 character *Enemy = Square->GetCharacter();
6347 if (Enemy && (GetRelation(Enemy) == HOSTILE || StateIsActivated(CONFUSED))) {
6348 Dir[Index] = d;
6349 Pos[Index] = Square->GetPos();
6350 Char[Index++] = Enemy;
6354 if (Index) {
6355 int ChosenIndex = RAND() % Index;
6356 Hit(Char[ChosenIndex], Pos[ChosenIndex], Dir[ChosenIndex]);
6357 return true;
6359 return false;
6363 void character::SignalStepFrom (lsquare **OldSquareUnder) {
6364 int c;
6365 lsquare *NewSquareUnder[MAX_SQUARES_UNDER];
6366 for (c = 0; c < GetSquaresUnder(); ++c) NewSquareUnder[c] = GetLSquareUnder(c);
6367 for (c = 0; c < GetSquaresUnder(); ++c) {
6368 if (IsEnabled() && GetLSquareUnder(c) == NewSquareUnder[c]) NewSquareUnder[c]->StepOn(this, OldSquareUnder);
6373 int character::GetSumOfAttributes () const {
6374 return GetAttribute(ENDURANCE) + GetAttribute(PERCEPTION) + GetAttribute(INTELLIGENCE) + GetAttribute(WISDOM) + GetAttribute(CHARISMA) + GetAttribute(ARM_STRENGTH) + GetAttribute(AGILITY);
6378 void character::IntelligenceAction (int Difficulty) {
6379 EditAP(-20000 * Difficulty / APBonus(GetAttribute(INTELLIGENCE)));
6380 EditExperience(INTELLIGENCE, Difficulty * 15, 1 << 7);
6384 struct walkabilitycontroller {
6385 static truth Handler (int x, int y) {
6386 return x >= 0 && y >= 0 && x < LevelXSize && y < LevelYSize && Map[x][y]->GetTheoreticalWalkability() & MoveType;
6388 static lsquare ***Map;
6389 static int LevelXSize, LevelYSize;
6390 static int MoveType;
6394 lsquare ***walkabilitycontroller::Map;
6395 int walkabilitycontroller::LevelXSize, walkabilitycontroller::LevelYSize;
6396 int walkabilitycontroller::MoveType;
6399 truth character::CreateRoute () {
6400 Route.clear();
6401 if (GetAttribute(INTELLIGENCE) >= 10 && !StateIsActivated(CONFUSED)) {
6402 v2 Pos = GetPos();
6403 walkabilitycontroller::Map = GetLevel()->GetMap();
6404 walkabilitycontroller::LevelXSize = GetLevel()->GetXSize();
6405 walkabilitycontroller::LevelYSize = GetLevel()->GetYSize();
6406 walkabilitycontroller::MoveType = GetMoveType();
6407 node *Node;
6408 for (int c = 0; c < game::GetTeams(); ++c)
6409 for (std::list<character *>::const_iterator i = game::GetTeam(c)->GetMember().begin(); i != game::GetTeam(c)->GetMember().end(); ++i) {
6410 character *Char = *i;
6411 if (Char->IsEnabled() && !Char->Route.empty() && (Char->GetMoveType()&GetMoveType()) == Char->GetMoveType()) {
6412 v2 CharGoingTo = Char->Route[0];
6413 v2 iPos = Char->Route.back();
6414 if ((GoingTo-CharGoingTo).GetLengthSquare() <= 100 && (Pos - iPos).GetLengthSquare() <= 100 &&
6415 mapmath<walkabilitycontroller>::DoLine(CharGoingTo.X, CharGoingTo.Y, GoingTo.X, GoingTo.Y, SKIP_FIRST) &&
6416 mapmath<walkabilitycontroller>::DoLine(Pos.X, Pos.Y, iPos.X, iPos.Y, SKIP_FIRST)) {
6417 if (!Illegal.empty() && Illegal.find(Char->Route.back()) != Illegal.end()) continue;
6418 Node = GetLevel()->FindRoute(CharGoingTo, GoingTo, Illegal, GetMoveType());
6419 if (Node) { while(Node->Last) { Route.push_back(Node->Pos); Node = Node->Last; } }
6420 else { Route.clear(); continue; }
6421 Route.insert(Route.end(), Char->Route.begin(), Char->Route.end());
6422 Node = GetLevel()->FindRoute(Pos, iPos, Illegal, GetMoveType());
6423 if (Node) { while (Node->Last) { Route.push_back(Node->Pos); Node = Node->Last; } }
6424 else { Route.clear(); continue; }
6425 IntelligenceAction(1);
6426 return true;
6430 Node = GetLevel()->FindRoute(Pos, GoingTo, Illegal, GetMoveType());
6431 if (Node) { while(Node->Last) { Route.push_back(Node->Pos); Node = Node->Last; } }
6432 else TerminateGoingTo();
6433 IntelligenceAction(5);
6434 return true;
6436 return false;
6440 void character::SetGoingTo (v2 What) {
6441 if (GoingTo != What) {
6442 GoingTo = What;
6443 Route.clear();
6444 Illegal.clear();
6449 void character::TerminateGoingTo () {
6450 GoingTo = ERROR_V2;
6451 Route.clear();
6452 Illegal.clear();
6456 truth character::CheckForFood (int Radius) {
6457 if (StateIsActivated(PANIC) || !UsesNutrition() || !IsEnabled()) return false;
6458 v2 Pos = GetPos();
6459 int x, y;
6460 for (int r = 1; r <= Radius; ++r) {
6461 x = Pos.X-r;
6462 if (x >= 0) {
6463 for (y = Pos.Y-r; y <= Pos.Y+r; ++y) if (CheckForFoodInSquare(v2(x, y))) return true;
6465 x = Pos.X+r;
6466 if (x < GetLevel()->GetXSize()) {
6467 for (y = Pos.Y-r; y <= Pos.Y+r; ++y) if (CheckForFoodInSquare(v2(x, y))) return true;
6469 y = Pos.Y-r;
6470 if (y >= 0) {
6471 for (x = Pos.X-r; x <= Pos.X+r; ++x) if (CheckForFoodInSquare(v2(x, y))) return true;
6473 y = Pos.Y+r;
6474 if (y < GetLevel()->GetYSize()) {
6475 for (x = Pos.X-r; x <= Pos.X+r; ++x) if (CheckForFoodInSquare(v2(x, y))) return true;
6478 return false;
6482 truth character::CheckForFoodInSquare (v2 Pos) {
6483 level *Level = GetLevel();
6484 if (Level->IsValidPos(Pos)) {
6485 lsquare *Square = Level->GetLSquare(Pos);
6486 stack *Stack = Square->GetStack();
6487 if (Stack->GetItems()) {
6488 for (stackiterator i = Stack->GetBottom(); i.HasItem(); ++i) {
6489 if (i->IsPickable(this) && i->CanBeSeenBy(this) && i->CanBeEatenByAI(this) && (!Square->GetRoomIndex() || Square->GetRoom()->AllowFoodSearch())) {
6490 SetGoingTo(Pos);
6491 return MoveTowardsTarget(false);
6496 return false;
6500 void character::SetConfig (int NewConfig, int SpecialFlags) {
6501 databasecreator<character>::InstallDataBase(this, NewConfig);
6502 CalculateAll();
6503 CheckIfSeen();
6504 if (!(SpecialFlags & NO_PIC_UPDATE)) UpdatePictures();
6508 truth character::IsOver (citem *Item) const {
6509 for (int c1 = 0; c1 < Item->GetSquaresUnder(); ++c1)
6510 for (int c2 = 0; c2 < SquaresUnder; ++c2)
6511 if (Item->GetPos(c1) == GetPos(c2)) return true;
6512 return false;
6516 truth character::CheckConsume (cfestring &Verb) const {
6517 if (!UsesNutrition()) {
6518 if (IsPlayer()) ADD_MESSAGE("In this form you can't and don't need to %s.", Verb.CStr());
6519 return false;
6521 return true;
6525 void character::PutTo (lsquare *To) {
6526 PutTo(To->GetPos());
6530 double character::RandomizeBabyExperience (double SumE) {
6531 if (!SumE) return 0;
6532 double E = (SumE / 4) - (SumE / 32) + (double(RAND()) / MAX_RAND) * (SumE / 16 + 1);
6533 return Limit(E, MIN_EXP, MAX_EXP);
6537 liquid *character::CreateBlood (sLong Volume) const {
6538 return liquid::Spawn(GetBloodMaterial(), Volume);
6542 void character::SpillFluid (character *Spiller, liquid *Liquid, int SquareIndex) {
6543 sLong ReserveVolume = Liquid->GetVolume() >> 1;
6544 Liquid->EditVolume(-ReserveVolume);
6545 GetStack()->SpillFluid(Spiller, Liquid, sLong(Liquid->GetVolume() * sqrt(double(GetStack()->GetVolume()) / GetVolume())));
6546 Liquid->EditVolume(ReserveVolume);
6547 int c;
6548 sLong Modifier[MAX_BODYPARTS], ModifierSum = 0;
6549 for (c = 0; c < BodyParts; ++c) {
6550 if (GetBodyPart(c)) {
6551 Modifier[c] = sLong(sqrt(GetBodyPart(c)->GetVolume()));
6552 if (Modifier[c]) Modifier[c] *= 1 + (RAND() & 3);
6553 ModifierSum += Modifier[c];
6554 } else {
6555 Modifier[c] = 0;
6558 for (c = 1; c < GetBodyParts(); ++c) {
6559 if (GetBodyPart(c) && IsEnabled())
6560 GetBodyPart(c)->SpillFluid(Spiller, Liquid->SpawnMoreLiquid(Liquid->GetVolume() * Modifier[c] / ModifierSum), SquareIndex);
6562 if (IsEnabled()) {
6563 Liquid->SetVolume(Liquid->GetVolume() * Modifier[TORSO_INDEX] / ModifierSum);
6564 GetTorso()->SpillFluid(Spiller, Liquid, SquareIndex);
6569 void character::StayOn (liquid *Liquid) {
6570 Liquid->TouchEffect(this, TORSO_INDEX);
6574 truth character::IsAlly (ccharacter *Char) const {
6575 return Char->GetTeam()->GetID() == GetTeam()->GetID();
6579 void character::ResetSpoiling () {
6580 doforbodyparts()(this, &bodypart::ResetSpoiling);
6584 item *character::SearchForItem (ccharacter *Char, sorter Sorter) const {
6585 item *Equipment = findequipment<ccharacter *>()(this, Sorter, Char);
6586 if (Equipment) return Equipment;
6587 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) if (((*i)->*Sorter)(Char)) return *i;
6588 return 0;
6592 truth character::DetectMaterial (cmaterial *Material) const {
6593 return GetStack()->DetectMaterial(Material) ||
6594 combinebodypartpredicateswithparam<cmaterial*>()(this, &bodypart::DetectMaterial, Material, 1) ||
6595 combineequipmentpredicateswithparam<cmaterial*>()(this, &item::DetectMaterial, Material, 1);
6599 truth character::DamageTypeDestroysBodyPart (int Type) {
6600 return (Type&0xFFF) != PHYSICAL_DAMAGE;
6604 truth character::CheckIfTooScaredToHit (ccharacter *Enemy) const {
6605 if (IsPlayer() && StateIsActivated(PANIC)) {
6606 for (int d = 0; d < GetNeighbourSquares(); ++d) {
6607 square *Square = GetNeighbourSquare(d);
6608 if (Square) {
6609 if(CanMoveOn(Square) && (!Square->GetCharacter() || Square->GetCharacter()->IsPet())) {
6610 ADD_MESSAGE("You are too scared to attack %s.", Enemy->CHAR_DESCRIPTION(DEFINITE));
6611 return true;
6616 return false;
6620 void character::PrintBeginLevitationMessage () const {
6621 if (!IsFlying()) {
6622 if (IsPlayer()) ADD_MESSAGE("You rise into the air like a small hot-air balloon.");
6623 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s begins to float.", CHAR_NAME(DEFINITE));
6628 void character::PrintEndLevitationMessage () const {
6629 if (!IsFlying()) {
6630 if (IsPlayer()) ADD_MESSAGE("You descend gently onto the ground.");
6631 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s drops onto the ground.", CHAR_NAME(DEFINITE));
6636 truth character::IsLimbIndex (int I) {
6637 switch (I) {
6638 case RIGHT_ARM_INDEX:
6639 case LEFT_ARM_INDEX:
6640 case RIGHT_LEG_INDEX:
6641 case LEFT_LEG_INDEX:
6642 return true;
6644 return false;
6648 void character::EditExperience (int Identifier, double Value, double Speed) {
6649 if (!AllowExperience() || (Identifier == ENDURANCE && UseMaterialAttributes())) return;
6650 int Change = RawEditExperience(BaseExperience[Identifier], GetNaturalExperience(Identifier), Value, Speed);
6651 if (!Change) return;
6652 cchar *PlayerMsg = 0, *NPCMsg = 0;
6653 switch (Identifier) {
6654 case ENDURANCE:
6655 if (Change > 0) {
6656 PlayerMsg = "You feel tougher than anything!";
6657 if (IsPet()) NPCMsg = "Suddenly %s looks tougher.";
6658 } else {
6659 PlayerMsg = "You feel less healthy.";
6660 if (IsPet()) NPCMsg = "Suddenly %s looks less healthy.";
6662 CalculateBodyPartMaxHPs();
6663 CalculateMaxStamina();
6664 break;
6665 case PERCEPTION:
6666 if (IsPlayer()) {
6667 if (Change > 0) {
6668 PlayerMsg = "You now see the world in much better detail than before.";
6669 } else {
6670 PlayerMsg = "You feel very guru.";
6671 game::GetGod(VALPURUS)->AdjustRelation(100);
6673 game::SendLOSUpdateRequest();
6675 break;
6676 case INTELLIGENCE:
6677 if (IsPlayer()) {
6678 if (Change > 0) PlayerMsg = "Suddenly the inner structure of the Multiverse around you looks quite simple.";
6679 else PlayerMsg = "It surely is hard to think today.";
6680 UpdateESPLOS();
6682 if (IsPlayerKind()) UpdatePictures();
6683 break;
6684 case WISDOM:
6685 if (IsPlayer()) {
6686 if (Change > 0) PlayerMsg = "You feel your life experience increasing all the time.";
6687 else PlayerMsg = "You feel like having done something unwise.";
6689 if (IsPlayerKind()) UpdatePictures();
6690 break;
6691 case CHARISMA:
6692 if (Change > 0) {
6693 PlayerMsg = "You feel very confident of your social skills.";
6694 if (IsPet()) {
6695 if (GetAttribute(CHARISMA) <= 15) NPCMsg = "%s looks less ugly.";
6696 else NPCMsg = "%s looks more attractive.";
6698 } else {
6699 PlayerMsg = "You feel somehow disliked.";
6700 if (IsPet()) {
6701 if (GetAttribute(CHARISMA) < 15) NPCMsg = "%s looks more ugly.";
6702 else NPCMsg = "%s looks less attractive.";
6705 if (IsPlayerKind()) UpdatePictures();
6706 break;
6707 case MANA:
6708 if (Change > 0) {
6709 PlayerMsg = "You feel magical forces coursing through your body!";
6710 NPCMsg = "You notice an odd glow around %s.";
6711 } else {
6712 PlayerMsg = "You feel your magical abilities withering slowly.";
6713 NPCMsg = "You notice strange vibrations in the air around %s. But they disappear rapidly.";
6715 break;
6718 if (IsPlayer()) ADD_MESSAGE("%s", PlayerMsg);
6719 else if (NPCMsg && CanBeSeenByPlayer()) ADD_MESSAGE(NPCMsg, CHAR_NAME(DEFINITE));
6721 CalculateBattleInfo();
6725 int character::RawEditExperience (double &Exp, double NaturalExp, double Value, double Speed) const {
6726 double OldExp = Exp;
6727 if (Speed < 0) {
6728 Speed = -Speed;
6729 Value = -Value;
6731 if(!OldExp || !Value || (Value > 0 && OldExp >= NaturalExp * (100 + Value) / 100) ||
6732 (Value < 0 && OldExp <= NaturalExp * (100 + Value) / 100)) return 0;
6733 if (!IsPlayer()) Speed *= 1.5;
6734 Exp += (NaturalExp * (100 + Value) - 100 * OldExp) * Speed * EXP_DIVISOR;
6735 LimitRef(Exp, MIN_EXP, MAX_EXP);
6736 int NewA = int(Exp * EXP_DIVISOR);
6737 int OldA = int(OldExp * EXP_DIVISOR);
6738 int Delta = NewA - OldA;
6739 if (Delta > 0) Exp = Max(Exp, (NewA + 0.05) * EXP_MULTIPLIER);
6740 else if (Delta < 0) Exp = Min(Exp, (NewA + 0.95) * EXP_MULTIPLIER);
6741 LimitRef(Exp, MIN_EXP, MAX_EXP);
6742 return Delta;
6746 int character::GetAttribute (int Identifier, truth AllowBonus) const {
6747 int A = int(BaseExperience[Identifier] * EXP_DIVISOR);
6748 if (AllowBonus && Identifier == INTELLIGENCE && BrainsHurt()) return Max((A + AttributeBonus[INTELLIGENCE]) / 3, 1);
6749 return A && AllowBonus ? Max(A + AttributeBonus[Identifier], 1) : A;
6753 void characterdatabase::PostProcess () {
6754 double AM = (100 + AttributeBonus) * EXP_MULTIPLIER / 100;
6755 for (int c = 0; c < ATTRIBUTES; ++c) NaturalExperience[c] = this->*ExpPtr[c] * AM;
6759 void character::EditDealExperience (sLong Price) {
6760 EditExperience(CHARISMA, sqrt(Price) / 5, 1 << 9);
6764 void character::PrintBeginLeprosyMessage () const {
6765 if (IsPlayer()) ADD_MESSAGE("You feel you're falling in pieces.");
6769 void character::PrintEndLeprosyMessage () const {
6770 if (IsPlayer()) ADD_MESSAGE("You feel your limbs are stuck in place tightly."); // CHANGE OR DIE
6774 void character::TryToInfectWithLeprosy (ccharacter *Infector) {
6775 if (!IsImmuneToLeprosy() &&
6776 ((GetRelation(Infector) == HOSTILE && !RAND_N(50 * GetAttribute(ENDURANCE))) ||
6777 !RAND_N(500 * GetAttribute(ENDURANCE)))) GainIntrinsic(LEPROSY);
6781 void character::SignalGeneration () {
6782 const_cast<database *>(DataBase)->Flags |= HAS_BEEN_GENERATED;
6786 void character::CheckIfSeen () {
6787 if (IsPlayer() || CanBeSeenByPlayer()) SignalSeen();
6791 void character::SignalSeen () {
6792 if (!(WarnFlags & WARNED) && GetRelation(PLAYER) == HOSTILE && !StateIsActivated(FEARLESS)) {
6793 double Danger = GetRelativeDanger(PLAYER);
6794 if (Danger > 5.0) {
6795 game::SetDangerFound(Max(game::GetDangerFound(), Danger));
6796 if (Danger > 500.0 && !(WarnFlags & HAS_CAUSED_PANIC)) {
6797 WarnFlags |= HAS_CAUSED_PANIC;
6798 game::SetCausePanicFlag(true);
6800 WarnFlags |= WARNED;
6803 const_cast<database *>(DataBase)->Flags |= HAS_BEEN_SEEN;
6807 int character::GetPolymorphIntelligenceRequirement () const {
6808 if (DataBase->PolymorphIntelligenceRequirement == DEPENDS_ON_ATTRIBUTES) return Max(GetAttributeAverage() - 5, 0);
6809 return DataBase->PolymorphIntelligenceRequirement;
6813 void character::RemoveAllItems () {
6814 GetStack()->Clean();
6815 for (int c = 0; c < GetEquipments(); ++c) {
6816 item *Equipment = GetEquipment(c);
6817 if (Equipment) {
6818 Equipment->RemoveFromSlot();
6819 Equipment->SendToHell();
6825 int character::CalculateWeaponSkillHits (ccharacter *Enemy) const {
6826 if (Enemy->IsPlayer()) {
6827 configid ConfigID(GetType(), GetConfig());
6828 const dangerid& DangerID = game::GetDangerMap().find(ConfigID)->second;
6829 return Min(int(DangerID.EquippedDanger * 2000), 1000);
6831 return Min(int(GetRelativeDanger(Enemy, true) * 2000), 1000);
6835 truth character::CanUseEquipment (int I) const {
6836 return CanUseEquipment() && I < GetEquipments() && GetBodyPartOfEquipment(I) && EquipmentIsAllowed(I);
6840 /* Target mustn't have any equipment */
6841 void character::DonateEquipmentTo (character *Character) {
6842 if (IsPlayer()) {
6843 feuLong *EquipmentMemory = game::GetEquipmentMemory();
6844 for (int c = 0; c < MAX_EQUIPMENT_SLOTS; ++c) {
6845 item *Item = GetEquipment(c);
6846 if (Item) {
6847 if (Character->CanUseEquipment(c)) {
6848 Item->RemoveFromSlot();
6849 Character->SetEquipment(c, Item);
6850 } else {
6851 EquipmentMemory[c] = Item->GetID();
6852 Item->MoveTo(Character->GetStack());
6854 } else if (CanUseEquipment(c)) {
6855 EquipmentMemory[c] = 0;
6856 } else if (EquipmentMemory[c] && Character->CanUseEquipment(c)) {
6857 for (stackiterator i = Character->GetStack()->GetBottom(); i.HasItem(); ++i) {
6858 if (i->GetID() == EquipmentMemory[c]) {
6859 item *Item = *i;
6860 Item->RemoveFromSlot();
6861 Character->SetEquipment(c, Item);
6862 break;
6865 EquipmentMemory[c] = 0;
6868 } else {
6869 for (int c = 0; c < GetEquipments(); ++c) {
6870 item *Item = GetEquipment(c);
6871 if (Item) {
6872 if (Character->CanUseEquipment(c)) {
6873 Item->RemoveFromSlot();
6874 Character->SetEquipment(c, Item);
6875 } else {
6876 Item->MoveTo(Character->GetStackUnder());
6884 void character::ReceivePeaSoup (sLong) {
6885 if (!game::IsInWilderness()) {
6886 lsquare *Square = GetLSquareUnder();
6887 if (Square->IsFlyable()) Square->AddSmoke(gas::Spawn(FART, 250));
6892 void character::AddPeaSoupConsumeEndMessage () const {
6893 if (IsPlayer()) {
6894 if (CanHear()) ADD_MESSAGE("Mmmh! The soup is very tasty. You hear a small puff.");
6895 else ADD_MESSAGE("Mmmh! The soup is very tasty.");
6896 } else if (CanBeSeenByPlayer() && PLAYER->CanHear()) {
6897 // change someday
6898 ADD_MESSAGE("You hear a small puff.");
6903 void character::CalculateMaxStamina () {
6904 MaxStamina = TorsoIsAlive() ? GetAttribute(ENDURANCE) * 10000 : 0;
6908 void character::EditStamina (int Amount, truth CanCauseUnconsciousness) {
6909 if (!TorsoIsAlive()) return;
6910 int UnconsciousnessStamina = MaxStamina >> 3;
6911 if (!CanCauseUnconsciousness && Amount < 0) {
6912 if (Stamina > UnconsciousnessStamina) {
6913 Stamina += Amount;
6914 if (Stamina < UnconsciousnessStamina) Stamina = UnconsciousnessStamina;
6916 return;
6918 int OldStamina = Stamina;
6919 Stamina += Amount;
6920 if (Stamina > MaxStamina) {
6921 Stamina = MaxStamina;
6922 } else if (Stamina < 0) {
6923 Stamina = 0;
6924 LoseConsciousness(250 + RAND_N(250));
6925 } else if (IsPlayer()) {
6926 if (OldStamina >= MaxStamina >> 2 && Stamina < MaxStamina >> 2) {
6927 ADD_MESSAGE("You are getting a little tired.");
6928 } else if(OldStamina >= UnconsciousnessStamina && Stamina < UnconsciousnessStamina) {
6929 ADD_MESSAGE("You are seriously out of breath!");
6930 game::SetPlayerIsRunning(false);
6933 if (IsPlayer() && StateIsActivated(PANIC) && GetTirednessState() != FAINTING) game::SetPlayerIsRunning(true);
6937 void character::RegenerateStamina () {
6938 if (GetTirednessState() != UNTIRED) {
6939 EditExperience(ENDURANCE, 50, 1);
6940 if (Sweats() && TorsoIsAlive() && !RAND_N(30) && !game::IsInWilderness()) {
6941 // Sweat amount proportional to endurance also
6942 //sLong Volume = sLong(0.05 * sqrt(GetBodyVolume()));
6943 sLong Volume = long(0.05*sqrt(GetBodyVolume()*GetAttribute(ENDURANCE)/10));
6944 if (GetTirednessState() == FAINTING) Volume <<= 1;
6945 for (int c = 0; c < SquaresUnder; ++c) GetLSquareUnder(c)->SpillFluid(0, CreateSweat(Volume), false, false);
6948 int Bonus = 1;
6949 if (Action) {
6950 if (Action->IsRest()) {
6951 if (SquaresUnder == 1) {
6952 Bonus = GetSquareUnder()->GetRestModifier() << 1;
6953 } else {
6954 int Lowest = GetSquareUnder(0)->GetRestModifier();
6955 for (int c = 1; c < GetSquaresUnder(); ++c) {
6956 int Mod = GetSquareUnder(c)->GetRestModifier();
6957 if (Mod < Lowest) Lowest = Mod;
6959 Bonus = Lowest << 1;
6961 } else if (Action->IsUnconsciousness()) Bonus = 2;
6963 int Plus1 = 100;
6964 auto bst = GetBurdenState();
6965 if (bst == OVER_LOADED) Plus1 = 25;
6966 else if (bst == STRESSED) Plus1 = 50;
6967 else if (bst == BURDENED) Plus1 = 75;
6968 int Plus2 = 100;
6969 if (IsPlayer()) {
6970 auto hst = GetHungerState();
6971 if (hst == STARVING) Plus2 = 25;
6972 else if (hst == VERY_HUNGRY) Plus2 = 50;
6973 else if (hst == HUNGRY) Plus2 = 75;
6975 Stamina += Plus1 * Plus2 * Bonus / 1000;
6976 if (Stamina > MaxStamina) Stamina = MaxStamina;
6977 if (IsPlayer() && StateIsActivated(PANIC) && GetTirednessState() != FAINTING) game::SetPlayerIsRunning(true);
6981 void character::BeginPanic () {
6982 if (IsPlayer() && GetTirednessState() != FAINTING) game::SetPlayerIsRunning(true);
6983 DeActivateVoluntaryAction();
6987 void character::EndPanic () {
6988 if (IsPlayer()) game::SetPlayerIsRunning(false);
6992 int character::GetTirednessState () const {
6993 if (Stamina >= MaxStamina >> 2) return UNTIRED;
6994 if (Stamina >= MaxStamina >> 3) return EXHAUSTED;
6995 return FAINTING;
6999 void character::ReceiveBlackUnicorn (sLong Amount) {
7000 if (!(RAND() % 160)) game::DoEvilDeed(Amount / 50);
7001 BeginTemporaryState(TELEPORT, Amount / 100);
7002 for (int c = 0; c < STATES; ++c) {
7003 if (StateData[c].Flags & DUR_TEMPORARY) {
7004 BeginTemporaryState(1 << c, Amount / 100);
7005 if (!IsEnabled()) return;
7006 } else if (StateData[c].Flags & DUR_PERMANENT) {
7007 GainIntrinsic(1 << c);
7008 if (!IsEnabled()) return;
7014 void character::ReceiveGrayUnicorn (sLong Amount) {
7015 if (!(RAND() % 80)) game::DoEvilDeed(Amount / 50);
7016 BeginTemporaryState(TELEPORT, Amount / 100);
7017 for (int c = 0; c < STATES; ++c) {
7018 if (1 << c != TELEPORT) {
7019 DecreaseStateCounter(1 << c, -Amount / 100);
7020 if (!IsEnabled()) return;
7026 void character::ReceiveWhiteUnicorn (sLong Amount) {
7027 if (!(RAND() % 40)) game::DoEvilDeed(Amount / 50);
7028 BeginTemporaryState(TELEPORT, Amount / 100);
7029 DecreaseStateCounter(LYCANTHROPY, -Amount / 100);
7030 DecreaseStateCounter(POISONED, -Amount / 100);
7031 DecreaseStateCounter(PARASITIZED, -Amount / 100);
7032 DecreaseStateCounter(LEPROSY, -Amount / 100);
7033 DecreaseStateCounter(VAMPIRISM, -Amount / 100);
7037 /* Counter should be negative. Removes intrinsics. */
7038 void character::DecreaseStateCounter (sLong State, int Counter) {
7039 int Index;
7040 for (Index = 0; Index < STATES; ++Index) if (1 << Index == State) break;
7041 if (Index == STATES) ABORT("DecreaseTemporaryStateCounter works only when State == 2^n!");
7042 if (TemporaryState & State) {
7043 if (TemporaryStateCounter[Index] == PERMANENT || (TemporaryStateCounter[Index] += Counter) <= 0) {
7044 TemporaryState &= ~State;
7045 if (!(EquipmentState & State)) {
7046 if (StateData[Index].EndHandler) {
7047 (this->*StateData[Index].EndHandler)();
7048 if (!IsEnabled()) return;
7050 (this->*StateData[Index].PrintEndMessage)();
7057 truth character::IsImmuneToLeprosy () const {
7058 return (DataBase->IsImmuneToLeprosy || UseMaterialAttributes() || StateIsActivated(DISEASE_IMMUNITY));
7062 void character::LeprosyHandler () {
7063 EditExperience(ARM_STRENGTH, -25, 1 << 1);
7064 EditExperience(LEG_STRENGTH, -25, 1 << 1);
7065 EditExperience(DEXTERITY, -25, 1 << 1);
7066 EditExperience(AGILITY, -25, 1 << 1);
7067 EditExperience(ENDURANCE, -25, 1 << 1);
7068 EditExperience(CHARISMA, -25, 1 << 1);
7069 CheckDeath(CONST_S("killed by leprosy"));
7073 bodypart *character::SearchForOriginalBodyPart (int I) const {
7074 for (stackiterator i1 = GetStackUnder()->GetBottom(); i1.HasItem(); ++i1) {
7075 for (std::list<feuLong>::iterator i2 = OriginalBodyPartID[I].begin(); i2 != OriginalBodyPartID[I].end(); ++i2)
7076 if (i1->GetID() == *i2) return static_cast<bodypart*>(*i1);
7078 return 0;
7082 void character::SetLifeExpectancy (int Base, int RandPlus) {
7083 int c;
7084 for (c = 0; c < BodyParts; ++c) {
7085 bodypart *BodyPart = GetBodyPart(c);
7086 if (BodyPart) BodyPart->SetLifeExpectancy(Base, RandPlus);
7088 for (c = 0; c < GetEquipments(); ++c) {
7089 item *Equipment = GetEquipment(c);
7090 if (Equipment) Equipment->SetLifeExpectancy(Base, RandPlus);
7095 /* Receiver should be a fresh duplicate of this */
7096 void character::DuplicateEquipment (character *Receiver, feuLong Flags) {
7097 for (int c = 0; c < GetEquipments(); ++c) {
7098 item *Equipment = GetEquipment(c);
7099 if (Equipment) {
7100 item *Duplicate = Equipment->Duplicate(Flags);
7101 Receiver->SetEquipment(c, Duplicate);
7107 void character::Disappear (corpse *Corpse, cchar *Verb, truth (item::*ClosePredicate)() const) {
7108 truth TorsoDisappeared = false;
7109 truth CanBeSeen = Corpse ? Corpse->CanBeSeenByPlayer() : IsPlayer() || CanBeSeenByPlayer();
7110 int c;
7111 if ((GetTorso()->*ClosePredicate)()) {
7112 if (CanBeSeen) {
7113 if (Corpse) ADD_MESSAGE("%s %ss.", Corpse->CHAR_NAME(DEFINITE), Verb);
7114 else if (IsPlayer()) ADD_MESSAGE("You %s.", Verb);
7115 else ADD_MESSAGE("%s %ss.", CHAR_NAME(DEFINITE), Verb);
7117 TorsoDisappeared = true;
7118 for (c = 0; c < GetEquipments(); ++c) {
7119 item *Equipment = GetEquipment(c);
7120 if (Equipment && (Equipment->*ClosePredicate)()) {
7121 Equipment->RemoveFromSlot();
7122 Equipment->SendToHell();
7125 itemvector ItemVector;
7126 GetStack()->FillItemVector(ItemVector);
7127 for (uInt c = 0; c < ItemVector.size(); ++c) {
7128 if (ItemVector[c] && (ItemVector[c]->*ClosePredicate)()) {
7129 ItemVector[c]->RemoveFromSlot();
7130 ItemVector[c]->SendToHell();
7134 for (c = 1; c < GetBodyParts(); ++c) {
7135 bodypart *BodyPart = GetBodyPart(c);
7136 if (BodyPart) {
7137 if ((BodyPart->*ClosePredicate)()) {
7138 if (!TorsoDisappeared && CanBeSeen) {
7139 if(IsPlayer()) ADD_MESSAGE("Your %s %ss.", GetBodyPartName(c).CStr(), Verb);
7140 else ADD_MESSAGE("The %s of %s %ss.", GetBodyPartName(c).CStr(), CHAR_NAME(DEFINITE), Verb);
7142 BodyPart->DropEquipment();
7143 item *BodyPart = SevereBodyPart(c);
7144 if (BodyPart) BodyPart->SendToHell();
7145 } else if (TorsoDisappeared) {
7146 BodyPart->DropEquipment();
7147 item *BodyPart = SevereBodyPart(c);
7148 if (BodyPart) {
7149 if (Corpse) Corpse->GetSlot()->AddFriendItem(BodyPart);
7150 else if (!game::IsInWilderness()) GetStackUnder()->AddItem(BodyPart);
7151 else BodyPart->SendToHell();
7156 if (TorsoDisappeared) {
7157 if (Corpse) {
7158 Corpse->RemoveFromSlot();
7159 Corpse->SendToHell();
7160 } else {
7161 CheckDeath(festring(Verb) + "ed", 0, FORCE_DEATH|DISALLOW_CORPSE|DISALLOW_MSG);
7163 } else {
7164 CheckDeath(festring(Verb) + "ed", 0, DISALLOW_MSG);
7169 void character::SignalDisappearance () {
7170 if (GetMotherEntity()) GetMotherEntity()->SignalDisappearance();
7171 else Disappear(0, "disappear", &item::IsVeryCloseToDisappearance);
7175 truth character::HornOfFearWorks () const {
7176 return CanHear() && GetPanicLevel() > RAND()%33 && !StateIsActivated(FEARLESS);
7180 void character::BeginLeprosy () {
7181 doforbodypartswithparam<truth>()(this, &bodypart::SetIsInfectedByLeprosy, true);
7185 void character::EndLeprosy () {
7186 doforbodypartswithparam<truth>()(this, &bodypart::SetIsInfectedByLeprosy, false);
7190 truth character::IsSameAs (ccharacter *What) const {
7191 return What->GetType() == GetType() && What->GetConfig() == GetConfig();
7195 feuLong character::GetCommandFlags () const {
7196 return !StateIsActivated(PANIC) ? CommandFlags : CommandFlags|FLEE_FROM_ENEMIES;
7200 feuLong character::GetConstantCommandFlags () const {
7201 return !StateIsActivated(PANIC) ? DataBase->ConstantCommandFlags : DataBase->ConstantCommandFlags|FLEE_FROM_ENEMIES;
7205 feuLong character::GetPossibleCommandFlags () const {
7206 int Int = GetAttribute(INTELLIGENCE);
7207 feuLong Flags = ALL_COMMAND_FLAGS;
7208 if (!CanMove() || Int < 4) Flags &= ~FOLLOW_LEADER;
7209 if (!CanMove() || Int < 6) Flags &= ~FLEE_FROM_ENEMIES;
7210 if (!CanUseEquipment() || Int < 8) Flags &= ~DONT_CHANGE_EQUIPMENT;
7211 if (!UsesNutrition() || Int < 8) Flags &= ~DONT_CONSUME_ANYTHING_VALUABLE;
7212 return Flags;
7216 truth character::IsRetreating () const {
7217 return StateIsActivated(PANIC) || (CommandFlags & FLEE_FROM_ENEMIES && IsPet());
7221 truth character::ChatMenu () {
7222 if (GetAction() && !GetAction()->CanBeTalkedTo()) {
7223 ADD_MESSAGE("%s is silent.", CHAR_DESCRIPTION(DEFINITE));
7224 PLAYER->EditAP(-200);
7225 return true;
7227 feuLong ManagementFlags = GetManagementFlags();
7228 if (ManagementFlags == CHAT_IDLY || !IsPet()) return ChatIdly();
7229 static cchar *const ChatMenuEntry[CHAT_MENU_ENTRIES] = {
7230 "Change equipment",
7231 "Take items",
7232 "Give items",
7233 "Issue commands",
7234 "Chat idly",
7236 static const petmanagementfunction PMF[CHAT_MENU_ENTRIES] = {
7237 &character::ChangePetEquipment,
7238 &character::TakePetItems,
7239 &character::GivePetItems,
7240 &character::IssuePetCommands,
7241 &character::ChatIdly
7243 felist List(CONST_S("Choose action:"));
7244 game::SetStandardListAttributes(List);
7245 List.AddFlags(SELECTABLE);
7246 int c, i;
7247 for (c = 0; c < CHAT_MENU_ENTRIES; ++c) if (1 << c & ManagementFlags) List.AddEntry(ChatMenuEntry[c], LIGHT_GRAY);
7248 int Chosen = List.Draw();
7249 if (Chosen & FELIST_ERROR_BIT) return false;
7250 for (c = 0, i = 0; c < CHAT_MENU_ENTRIES; ++c) {
7251 if (1 << c & ManagementFlags && i++ == Chosen) return (this->*PMF[c])();
7253 return false; // dummy
7257 truth character::ChangePetEquipment () {
7258 if (EquipmentScreen(PLAYER->GetStack(), GetStack())) {
7259 DexterityAction(3);
7260 return true;
7262 return false;
7266 truth character::TakePetItems () {
7267 truth Success = false;
7268 stack::SetSelected(0);
7269 for (;;) {
7270 itemvector ToTake;
7271 game::DrawEverythingNoBlit();
7272 GetStack()->DrawContents(
7273 ToTake,
7275 PLAYER,
7276 CONST_S("What do you want to take from ") + CHAR_DESCRIPTION(DEFINITE) + '?',
7277 CONST_S(""),
7278 CONST_S(""),
7279 GetDescription(DEFINITE) + " is " + GetVerbalBurdenState(),
7280 GetVerbalBurdenStateColor(),
7281 REMEMBER_SELECTED);
7282 if (ToTake.empty()) break;
7283 for (uInt c = 0; c < ToTake.size(); ++c) ToTake[c]->MoveTo(PLAYER->GetStack());
7284 ADD_MESSAGE("You take %s.", ToTake[0]->GetName(DEFINITE, ToTake.size()).CStr());
7285 Success = true;
7287 if (Success) {
7288 DexterityAction(2);
7289 PLAYER->DexterityAction(2);
7291 return Success;
7295 truth character::GivePetItems () {
7296 truth Success = false;
7297 stack::SetSelected(0);
7298 for (;;) {
7299 itemvector ToGive;
7300 game::DrawEverythingNoBlit();
7301 PLAYER->GetStack()->DrawContents(
7302 ToGive,
7304 this,
7305 CONST_S("What do you want to give to ") + CHAR_DESCRIPTION(DEFINITE) + '?',
7306 CONST_S(""),
7307 CONST_S(""),
7308 GetDescription(DEFINITE) + " is " + GetVerbalBurdenState(),
7309 GetVerbalBurdenStateColor(),
7310 REMEMBER_SELECTED);
7311 if (ToGive.empty()) break;
7312 for (uInt c = 0; c < ToGive.size(); ++c) ToGive[c]->MoveTo(GetStack());
7313 ADD_MESSAGE("You give %s to %s.", ToGive[0]->GetName(DEFINITE, ToGive.size()).CStr(), CHAR_DESCRIPTION(DEFINITE));
7314 Success = true;
7316 if (Success) {
7317 DexterityAction(2);
7318 PLAYER->DexterityAction(2);
7320 return Success;
7324 truth character::IssuePetCommands () {
7325 if (!IsConscious()) {
7326 ADD_MESSAGE("%s is unconscious.", CHAR_DESCRIPTION(DEFINITE));
7327 return false;
7329 feuLong PossibleC = GetPossibleCommandFlags();
7330 if (!PossibleC) {
7331 ADD_MESSAGE("%s cannot be commanded.", CHAR_DESCRIPTION(DEFINITE));
7332 return false;
7334 feuLong OldC = GetCommandFlags();
7335 feuLong NewC = OldC, VaryFlags = 0;
7336 game::CommandScreen(CONST_S("Issue commands to ")+GetDescription(DEFINITE), PossibleC, GetConstantCommandFlags(), VaryFlags, NewC);
7337 if (NewC == OldC) return false;
7338 SetCommandFlags(NewC);
7339 PLAYER->EditAP(-500);
7340 PLAYER->EditExperience(CHARISMA, 25, 1 << 7);
7341 return true;
7345 truth character::ChatIdly () {
7346 if (!TryToTalkAboutScience()) {
7347 BeTalkedTo();
7348 PLAYER->EditExperience(CHARISMA, 75, 1 << 7);
7350 PLAYER->EditAP(-1000);
7351 return true;
7355 int character::HasSomethingToEquipAt (int chosen, truth equippedIsTrue) {
7356 if (!GetBodyPartOfEquipment(chosen)) return 0;
7358 item *oldEquipment = GetEquipment(chosen);
7359 if (!IsPlayer() && oldEquipment && BoundToUse(oldEquipment, chosen)) return 0;
7361 stack *mainStack = GetStack();
7362 sorter Sorter = EquipmentSorter(chosen);
7363 auto count = mainStack->SortedItemsCount(this, Sorter);
7365 if (equippedIsTrue && oldEquipment) ++count;
7367 return count;
7371 // returns 0, 1 or pickup time
7372 feuLong character::HasSomethingToEquipAtRecentTime (int chosen, truth equippedIsTrue) {
7373 if (!GetBodyPartOfEquipment(chosen)) return 0;
7375 item *oldEquipment = GetEquipment(chosen);
7376 if (!IsPlayer() && oldEquipment && BoundToUse(oldEquipment, chosen)) return 0;
7378 stack *mainStack = GetStack();
7379 sorter Sorter = EquipmentSorter(chosen);
7380 feuLong highestTime = mainStack->SortedItemsRecentTime(this, Sorter);
7382 if (equippedIsTrue && oldEquipment && oldEquipment->pickupTime > highestTime) highestTime = oldEquipment->pickupTime;
7384 return highestTime;
7388 truth character::EquipmentScreen (stack *MainStack, stack *SecStack) {
7389 if (!CanUseEquipment()) {
7390 ADD_MESSAGE("%s cannot use equipment.", CHAR_DESCRIPTION(DEFINITE));
7391 return false;
7393 int Chosen = 0;
7394 truth EquipmentChanged = false;
7395 felist List(CONST_S("Equipment menu [ESC exits]"));
7396 festring Entry;
7397 for (;;) {
7398 List.Empty();
7399 List.EmptyDescription();
7400 if (!IsPlayer()) {
7401 List.AddDescription(CONST_S(""));
7402 List.AddDescription(festring(GetDescription(DEFINITE) + " is " + GetVerbalBurdenState()).CapitalizeCopy(), GetVerbalBurdenStateColor());
7403 } else {
7404 int totalWeight = 0;
7405 for (int c = 0; c < GetEquipments(); ++c) {
7406 item *Equipment = GetEquipment(c);
7407 totalWeight += (Equipment ? Equipment->GetWeight() : 0);
7409 festring Total("Total weight: ");
7410 Total << totalWeight;
7411 Total << "g";
7412 List.AddDescription(CONST_S(""));
7413 List.AddDescription(Total);
7415 int firstEmpty = -1, firstNonEmpty = -1, selected = -1;
7416 feuLong selPickTime = 1;
7417 feuLong armPickTime = 0;
7418 int armFirst = -1;
7419 //truth selectedIsEmpty = false;
7420 for (int c = 0; c < GetEquipments(); ++c) {
7421 truth isArm = (c == 5 || c == 6);
7422 //int bpidx = (GetBodyPartOfEquipment(c) ? GetBodyPartOfEquipment(c)->GetBodyPartIndex() : -1);
7423 truth equippable = !!GetBodyPartOfEquipment(c);
7424 Entry = GetEquipmentName(c);
7425 Entry << ':';
7426 Entry.Resize(20);
7427 item *Equipment = GetEquipment(c);
7428 feuLong pickTm = (equippable ? HasSomethingToEquipAtRecentTime(c, false) : 0);
7429 int availEquipCount = (equippable ? HasSomethingToEquipAt(c, false) : 0);
7430 if (pickTm > 1 && game::GetTick()-pickTm > game::PickTimeout) pickTm = 0;
7431 //fprintf(stderr, "c=%d; equippable=%d; availcount=%d; pickTm=%u; tick=%u\n", c, (int)equippable, availEquipCount, pickTm, game::GetTick());
7432 if (Equipment) {
7433 Equipment->AddInventoryEntry(this, Entry, 1, true);
7434 AddSpecialEquipmentInfo(Entry, c);
7435 int ImageKey = game::AddToItemDrawVector(itemvector(1, Equipment));
7436 if (firstNonEmpty < 0 && equippable && !isArm && availEquipCount > 0) firstNonEmpty = c;
7437 if (equippable) {
7438 if (availEquipCount > 0 && isArm && armPickTime < pickTm) { armFirst = c; armPickTime = pickTm; }
7439 if (selPickTime < pickTm && equippable && !isArm) { selected = c; selPickTime = pickTm; }
7441 List.AddEntry(Entry, (availEquipCount ? ORANGE : LIGHT_GRAY), 20, ImageKey, true);
7442 } else {
7443 truth canUse = !!GetBodyPartOfEquipment(c);
7444 Entry << (canUse ? "-" : "can't use");
7445 col16 color = RED;
7446 if (canUse && availEquipCount > 0) {
7447 if (firstEmpty < 0 && equippable && !isArm && availEquipCount > 0) firstEmpty = c;
7448 if (equippable && isArm && armFirst < 0) armFirst = c;
7449 switch (availEquipCount) {
7450 case 0: color = RED; break;
7451 case 1: color = LIGHT_GRAY; break;
7452 default: color = ORANGE; break;
7455 if (color != RED && equippable) {
7456 if (pickTm > selPickTime && !isArm) { selected = c; selPickTime = pickTm; }
7458 List.AddEntry(Entry, color, 20, game::AddToItemDrawVector(itemvector()));
7461 game::DrawEverythingNoBlit();
7462 game::SetStandardListAttributes(List);
7464 //fprintf(stderr, " selected=%d; firstEmpty=%d; firstNonEmpty=%d; armFirst=%d; armPickTime=%u\n", selected, firstEmpty, firstNonEmpty, armFirst, armPickTime);
7465 if (selected < 0) {
7466 if (armPickTime > 0) selected = armFirst;
7467 else if (firstEmpty >= 0) selected = firstEmpty;
7468 else if (firstNonEmpty >= 0) selected = firstNonEmpty;
7470 if (selected >= 0) List.SetSelected(selected);
7472 List.SetFlags(SELECTABLE|DRAW_BACKGROUND_AFTERWARDS);
7473 List.SetEntryDrawer(game::ItemEntryDrawer);
7474 Chosen = List.Draw();
7475 game::ClearItemDrawVector();
7476 if (Chosen >= GetEquipments()) break;
7477 EquipmentChanged = TryToChangeEquipment(MainStack, SecStack, Chosen);
7479 if (EquipmentChanged) DexterityAction(5);
7480 return EquipmentChanged;
7484 feuLong character::GetManagementFlags () const {
7485 feuLong Flags = ALL_MANAGEMENT_FLAGS;
7486 if (!CanUseEquipment() || !AllowPlayerToChangeEquipment()) Flags &= ~CHANGE_EQUIPMENT;
7487 if (!GetStack()->GetItems()) Flags &= ~TAKE_ITEMS;
7488 if (!WillCarryItems()) Flags &= ~GIVE_ITEMS;
7489 if (!GetPossibleCommandFlags()) Flags &= ~ISSUE_COMMANDS;
7490 return Flags;
7494 cchar *VerbalBurdenState[] = { "overloaded", "stressed", "burdened", "unburdened" };
7495 col16 VerbalBurdenStateColor[] = { RED, BLUE, BLUE, WHITE };
7497 cchar *character::GetVerbalBurdenState () const { return VerbalBurdenState[BurdenState]; }
7498 col16 character::GetVerbalBurdenStateColor () const { return VerbalBurdenStateColor[BurdenState]; }
7499 int character::GetAttributeAverage () const { return GetSumOfAttributes()/7; }
7501 cfestring &character::GetStandVerb() const {
7502 if (ForceCustomStandVerb()) return DataBase->StandVerb;
7503 static festring Hovering = "hovering";
7504 static festring Swimming = "swimming";
7505 if (StateIsActivated(LEVITATION)) return Hovering;
7506 if (IsSwimming()) return Swimming;
7507 return DataBase->StandVerb;
7511 truth character::CheckApply () const {
7512 if (!CanApply()) {
7513 ADD_MESSAGE("This monster type cannot apply.");
7514 return false;
7516 return true;
7520 void character::EndLevitation () {
7521 if (!IsFlying() && GetSquareUnder()) {
7522 if (!game::IsInWilderness()) SignalStepFrom(0);
7523 if (game::IsInWilderness() || !GetLSquareUnder()->IsFreezed()) TestWalkability();
7528 truth character::CanMove () const {
7529 return !IsRooted() || StateIsActivated(LEVITATION);
7533 void character::CalculateEnchantments () {
7534 doforequipments()(this, &item::CalculateEnchantment);
7535 GetStack()->CalculateEnchantments();
7539 truth character::GetNewFormForPolymorphWithControl (character *&NewForm) {
7540 if (StateIsActivated(POLYMORPH_LOCK)) { ADD_MESSAGE("You feel uncertain about your body for a moment."); return false; }
7541 festring Topic, Temp;
7542 NewForm = 0;
7543 while (!NewForm) {
7544 festring Temp = game::DefaultQuestion(CONST_S("What do you want to become? [press '?' for a list]"), game::GetDefaultPolymorphTo(), &game::PolymorphControlKeyHandler);
7545 NewForm = protosystem::CreateMonster(Temp);
7546 if (NewForm) {
7547 if (NewForm->IsSameAs(this)) {
7548 delete NewForm;
7549 ADD_MESSAGE("You choose not to polymorph.");
7550 NewForm = this;
7551 return false;
7553 if (PolymorphBackup && NewForm->IsSameAs(PolymorphBackup)) {
7554 delete NewForm;
7555 NewForm = ForceEndPolymorph();
7556 return false;
7558 if (NewForm->GetPolymorphIntelligenceRequirement() > GetAttribute(INTELLIGENCE) && !game::WizardModeIsActive()) {
7559 ADD_MESSAGE("You feel your mind isn't yet powerful enough to call forth the form of %s.", NewForm->CHAR_NAME(INDEFINITE));
7560 delete NewForm;
7561 NewForm = 0;
7562 } else {
7563 NewForm->RemoveAllItems();
7567 return true;
7571 liquid *character::CreateSweat(sLong Volume) const {
7572 //return liquid::Spawn(GetSweatMaterial(), Volume);
7573 return liquid::Spawn(GetCurrentSweatMaterial(), Volume);
7577 truth character::TeleportRandomItem (truth TryToHinderVisibility) {
7578 if (IsImmuneToItemTeleport() || StateIsActivated(TELEPORT_LOCK)) return false;
7579 itemvector ItemVector;
7580 std::vector<sLong> PossibilityVector;
7581 int TotalPossibility = 0;
7582 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) {
7583 ItemVector.push_back(*i);
7584 int Possibility = i->GetTeleportPriority();
7585 if (TryToHinderVisibility) Possibility += i->GetHinderVisibilityBonus(this);
7586 PossibilityVector.push_back(Possibility);
7587 TotalPossibility += Possibility;
7589 for (int c = 0; c < GetEquipments(); ++c) {
7590 item *Equipment = GetEquipment(c);
7591 if (Equipment) {
7592 ItemVector.push_back(Equipment);
7593 int Possibility = Equipment->GetTeleportPriority();
7594 if (TryToHinderVisibility) Possibility += Equipment->GetHinderVisibilityBonus(this);
7595 PossibilityVector.push_back(Possibility <<= 1);
7596 TotalPossibility += Possibility;
7599 if (!TotalPossibility) return false;
7600 int Chosen = femath::WeightedRand(PossibilityVector, TotalPossibility);
7601 item *Item = ItemVector[Chosen];
7602 truth Equipped = PLAYER->Equips(Item);
7603 truth Seen = Item->CanBeSeenByPlayer();
7604 Item->RemoveFromSlot();
7605 if (Seen) ADD_MESSAGE("%s disappears.", Item->CHAR_NAME(DEFINITE));
7606 if (Equipped) game::AskForEscPress(CONST_S("Equipment lost!"));
7607 v2 Pos = GetPos();
7608 int Range = Item->GetEmitation() && TryToHinderVisibility ? 25 : 5;
7609 rect Border(Pos + v2(-Range, -Range), Pos + v2(Range, Range));
7610 Pos = GetLevel()->GetRandomSquare(this, 0, &Border);
7611 if (Pos == ERROR_V2) Pos = GetLevel()->GetRandomSquare();
7612 GetNearLSquare(Pos)->GetStack()->AddItem(Item);
7613 if (Item->CanBeSeenByPlayer()) ADD_MESSAGE("%s appears.", Item->CHAR_NAME(INDEFINITE));
7614 return true;
7618 truth character::HasClearRouteTo (v2 Pos) const {
7619 pathcontroller::Map = GetLevel()->GetMap();
7620 pathcontroller::Character = this;
7621 v2 ThisPos = GetPos();
7622 return mapmath<pathcontroller>::DoLine(ThisPos.X, ThisPos.Y, Pos.X, Pos.Y, SKIP_FIRST);
7626 truth character::IsTransparent () const {
7627 return !IsEnormous() || GetTorso()->GetMainMaterial()->IsTransparent() || StateIsActivated(INVISIBLE);
7631 void character::SignalPossibleTransparencyChange () {
7632 if (!game::IsInWilderness()) {
7633 for (int c = 0; c < SquaresUnder; ++c) {
7634 lsquare *Square = GetLSquareUnder(c);
7635 if (Square) Square->SignalPossibleTransparencyChange();
7641 int character::GetCursorData () const {
7642 int Bad = 0;
7643 int Color = (game::PlayerIsRunning() ? BLUE_CURSOR : DARK_CURSOR);
7644 for (int c = 0; c < BodyParts; ++c) {
7645 bodypart *BodyPart = GetBodyPart(c);
7646 if (BodyPart && BodyPart->IsUsable()) {
7647 int ConditionColorIndex = BodyPart->GetConditionColorIndex();
7648 if ((BodyPartIsVital(c) && !ConditionColorIndex) || (ConditionColorIndex <= 1 && ++Bad == 2)) return Color|CURSOR_FLASH;
7649 } else if (++Bad == 2) {
7650 return Color|CURSOR_FLASH;
7653 Color = (game::PlayerIsRunning() ? YELLOW_CURSOR : RED_CURSOR);
7654 return (Bad ? Color|CURSOR_FLASH : Color);
7658 void character::TryToName () {
7659 if (!IsPet()) ADD_MESSAGE("%s refuses to let YOU decide what %s's called.", CHAR_NAME(DEFINITE), CHAR_PERSONAL_PRONOUN);
7660 else if (IsPlayer()) ADD_MESSAGE("You can't rename yourself.");
7661 else if (!IsNameable()) ADD_MESSAGE("%s refuses to be called anything else but %s.", CHAR_NAME(DEFINITE), CHAR_NAME(DEFINITE));
7662 else {
7663 festring Topic = CONST_S("What name will you give to ")+GetName(DEFINITE)+'?';
7664 festring Name = game::StringQuestion(Topic, WHITE, 0, 80, true);
7665 if (Name.GetSize()) SetAssignedName(Name);
7670 double character::GetSituationDanger (ccharacter *Enemy, v2 ThisPos, v2 EnemyPos, truth SeesEnemy) const {
7671 double Danger;
7672 if (IgnoreDanger() && !IsPlayer()) {
7673 if (Enemy->IgnoreDanger() && !Enemy->IsPlayer()) {
7674 Danger = double(GetHP())*GetHPRequirementForGeneration()/(Enemy->GetHP()*Enemy->GetHPRequirementForGeneration());
7676 else {
7677 Danger = 0.25*GetHPRequirementForGeneration()/Enemy->GetHP();
7679 } else if (Enemy->IgnoreDanger() && !Enemy->IsPlayer()) {
7680 Danger = 4.0*GetHP()/Enemy->GetHPRequirementForGeneration();
7681 } else {
7682 Danger = GetRelativeDanger(Enemy);
7684 Danger *= 3.0/((EnemyPos-ThisPos).GetManhattanLength()+2);
7685 if (!SeesEnemy) Danger *= 0.2;
7686 if (StateIsActivated(PANIC)) Danger *= 0.2;
7687 Danger *= double(GetHP())*Enemy->GetMaxHP()/(Enemy->GetHP()*GetMaxHP());
7688 return Danger;
7692 void character::ModifySituationDanger (double &Danger) const {
7693 switch (GetTirednessState()) {
7694 case FAINTING: Danger *= 1.5;
7695 case EXHAUSTED: Danger *= 1.25;
7697 for (int c = 0; c < STATES; ++c) {
7698 if (StateIsActivated(1 << c) && StateData[c].SituationDangerModifier != 0) (this->*StateData[c].SituationDangerModifier)(Danger);
7703 void character::LycanthropySituationDangerModifier (double &Danger) const {
7704 character *Wolf = werewolfwolf::Spawn();
7705 double DangerToWolf = GetRelativeDanger(Wolf);
7706 Danger *= pow(DangerToWolf, 0.1);
7707 delete Wolf;
7711 void character::PoisonedSituationDangerModifier (double &Danger) const {
7712 int C = GetTemporaryStateCounter(POISONED);
7713 Danger *= (1+(C*C)/(GetHP()*10000.0*(GetGlobalResistance(POISON)+1)));
7717 void character::PolymorphingSituationDangerModifier (double &Danger) const {
7718 if ((!StateIsActivated(POLYMORPH_CONTROL)) && (!StateIsActivated(POLYMORPH_LOCK))) Danger *= 1.5;
7722 void character::PanicSituationDangerModifier (double &Danger) const {
7723 Danger *= 1.5;
7727 void character::ConfusedSituationDangerModifier (double &Danger) const {
7728 Danger *= 1.5;
7732 void character::ParasitizedSituationDangerModifier (double &Danger) const {
7733 Danger *= 1.25;
7737 void character::LeprosySituationDangerModifier (double &Danger) const {
7738 Danger *= 1.5;
7742 void character::AddRandomScienceName (festring &String) const {
7743 festring Science = GetScienceTalkName().GetRandomElement().CStr();
7744 if (Science[0] == '!') {
7745 String << Science.CStr()+1;
7746 return;
7748 festring Attribute = GetScienceTalkAdjectiveAttribute().GetRandomElement();
7749 festring Prefix;
7750 truth NoAttrib = Attribute.IsEmpty(), NoSecondAdjective = false;
7751 if (!Attribute.IsEmpty() && Attribute[0] == '!') {
7752 NoSecondAdjective = true;
7753 Attribute.Erase(0, 1);
7755 if (!Science.Find("the ")) {
7756 Science.Erase(0, 4);
7757 if (!Attribute.Find("the ", 0, 4)) Attribute << " the"; else Attribute.Insert(0, "the ", 4);
7759 if (islower(Science[0]) && Science.Find(' ') == festring::NPos && Science.Find('-') == festring::NPos &&
7760 Science.Find("phobia") == festring::NPos) {
7761 Prefix = GetScienceTalkPrefix().GetRandomElement();
7762 if (!Prefix.IsEmpty() && Science.Find(Prefix) != festring::NPos) Prefix.Empty();
7764 int L = Prefix.GetSize();
7765 if (L && Prefix[L-1] == Science[0]) Science.Erase(0, 1);
7766 if (!NoAttrib && !NoSecondAdjective == !RAND_GOOD(3)) {
7767 int S1 = NoSecondAdjective ? 0 : GetScienceTalkAdjectiveAttribute().Size;
7768 int S2 = GetScienceTalkSubstantiveAttribute().Size;
7769 festring OtherAttribute;
7770 int Chosen = RAND_GOOD(S1+S2);
7771 if (Chosen < S1) OtherAttribute = GetScienceTalkAdjectiveAttribute()[Chosen];
7772 else OtherAttribute = GetScienceTalkSubstantiveAttribute()[Chosen - S1];
7773 if (!OtherAttribute.IsEmpty() && OtherAttribute.Find("the ", 0, 4) && Attribute.Find(OtherAttribute) == festring::NPos) {
7774 String << Attribute << ' ' << OtherAttribute << ' ' << Prefix << Science;
7775 return;
7778 String << Attribute;
7779 if (!NoAttrib) String << ' ';
7780 String << Prefix << Science;
7784 truth character::TryToTalkAboutScience () {
7785 if (GetRelation(PLAYER) == HOSTILE ||
7786 GetScienceTalkPossibility() <= RAND_GOOD(100) ||
7787 PLAYER->GetAttribute(INTELLIGENCE) < GetScienceTalkIntelligenceRequirement() ||
7788 PLAYER->GetAttribute(WISDOM) < GetScienceTalkWisdomRequirement() ||
7789 PLAYER->GetAttribute(CHARISMA) < GetScienceTalkCharismaRequirement())
7790 return false;
7791 festring Science;
7792 if (RAND_GOOD(3)) {
7793 AddRandomScienceName(Science);
7794 } else {
7795 festring S1, S2;
7796 AddRandomScienceName(S1);
7797 AddRandomScienceName(S2);
7798 if (S1.Find(S2) == festring::NPos && S2.Find(S1) == festring::NPos) {
7799 switch (RAND_GOOD(3)) {
7800 case 0: Science = "the relation of "; break;
7801 case 1: Science = "the differences of "; break;
7802 case 2: Science = "the similarities of "; break;
7804 Science << S1 << " and " << S2;
7806 else {
7807 AddRandomScienceName(Science);
7810 switch ((RAND() + GET_TICK()) % 10) {
7811 case 0:
7812 ADD_MESSAGE("You have a rather pleasant chat about %s with %s.", Science.CStr(), CHAR_DESCRIPTION(DEFINITE));
7813 break;
7814 case 1:
7815 ADD_MESSAGE("%s explains a few of %s opinions regarding %s to you.", CHAR_DESCRIPTION(DEFINITE), CHAR_POSSESSIVE_PRONOUN, Science.CStr());
7816 break;
7817 case 2:
7818 ADD_MESSAGE("%s reveals a number of %s insightful views of %s to you.", CHAR_DESCRIPTION(DEFINITE), CHAR_POSSESSIVE_PRONOUN, Science.CStr());
7819 break;
7820 case 3:
7821 ADD_MESSAGE("You exchange some information pertaining to %s with %s.", Science.CStr(), CHAR_DESCRIPTION(DEFINITE));
7822 break;
7823 case 4:
7824 ADD_MESSAGE("You engage in a pretty intriguing conversation about %s with %s.", Science.CStr(), CHAR_DESCRIPTION(DEFINITE));
7825 break;
7826 case 5:
7827 ADD_MESSAGE("You discuss at length about %s with %s.", Science.CStr(), CHAR_DESCRIPTION(DEFINITE));
7828 break;
7829 case 6:
7830 ADD_MESSAGE("You have a somewhat boring talk concerning %s with %s.", Science.CStr(), CHAR_DESCRIPTION(DEFINITE));
7831 break;
7832 case 7:
7833 ADD_MESSAGE("You are drawn into a heated argument regarding %s with %s.", Science.CStr(), CHAR_DESCRIPTION(DEFINITE));
7834 break;
7835 case 8:
7836 ADD_MESSAGE("%s delivers a long monologue concerning eg. %s.", CHAR_DESCRIPTION(DEFINITE), Science.CStr());
7837 break;
7838 case 9:
7839 ADD_MESSAGE("You dive into a brief but thought-provoking debate over %s with %s", Science.CStr(), CHAR_DESCRIPTION(DEFINITE));
7840 break;
7842 PLAYER->EditExperience(INTELLIGENCE, 1000, 50. * GetScienceTalkIntelligenceModifier() / ++ScienceTalks);
7843 PLAYER->EditExperience(WISDOM, 1000, 50. * GetScienceTalkWisdomModifier() / ++ScienceTalks);
7844 PLAYER->EditExperience(CHARISMA, 1000, 50. * GetScienceTalkCharismaModifier() / ++ScienceTalks);
7845 return true;
7849 truth character::IsUsingWeaponOfCategory (int Category) const {
7850 return
7851 ((GetMainWielded() && GetMainWielded()->GetWeaponCategory() == Category) ||
7852 (GetSecondaryWielded() && GetSecondaryWielded()->GetWeaponCategory() == Category));
7856 truth character::TryToUnStickTraps (v2 Dir) {
7857 if (!TrapData) return true;
7858 std::vector<trapdata> TrapVector;
7859 for (const trapdata *T = TrapData; T; T = T->Next) TrapVector.push_back(*TrapData);
7860 for (uInt c = 0; c < TrapVector.size(); ++c) {
7861 if (IsEnabled()) {
7862 entity *Trap = game::SearchTrap(TrapVector[c].TrapID);
7863 /*k8:??? if(!Trap->Exists()) int esko = esko = 2; */
7864 if (!Trap->Exists()) continue; /*k8: ??? added by me; what this means? */
7865 if (Trap->GetVictimID() == GetID() && Trap->TryToUnStick(this, Dir)) break;
7868 return !TrapData && IsEnabled();
7872 struct trapidcomparer {
7873 trapidcomparer (feuLong ID) : ID(ID) {}
7874 truth operator () (const trapdata *T) const { return T->TrapID == ID; }
7875 feuLong ID;
7879 void character::RemoveTrap (feuLong ID) {
7880 trapdata *&T = ListFind(TrapData, trapidcomparer(ID));
7881 T = T->Next;
7882 doforbodyparts()(this, &bodypart::SignalPossibleUsabilityChange);
7886 void character::AddTrap (feuLong ID, feuLong BodyParts) {
7887 trapdata *&T = ListFind(TrapData, trapidcomparer(ID));
7888 if (T) T->BodyParts |= BodyParts;
7889 else T = new trapdata(ID, GetID(), BodyParts);
7890 doforbodyparts()(this, &bodypart::SignalPossibleUsabilityChange);
7894 truth character::IsStuckToTrap (feuLong ID) const {
7895 for (const trapdata *T = TrapData; T; T = T->Next) if (T->TrapID == ID) return true;
7896 return false;
7900 void character::RemoveTraps () {
7901 for (trapdata *T = TrapData; T; T = T->Next) {
7902 entity *Trap = game::SearchTrap(T->TrapID);
7903 if (Trap) Trap->UnStick();
7905 deleteList(TrapData);
7906 doforbodyparts()(this, &bodypart::SignalPossibleUsabilityChange);
7910 void character::RemoveTraps (int BodyPartIndex) {
7911 feuLong Flag = 1 << BodyPartIndex;
7912 for (trapdata **T = &TrapData; *T;) {
7913 if ((*T)->BodyParts & Flag) {
7914 entity *Trap = game::SearchTrap((*T)->TrapID);
7915 if (!((*T)->BodyParts &= ~Flag)) {
7916 if (Trap) Trap->UnStick();
7917 trapdata *ToDel = *T;
7918 *T = (*T)->Next;
7919 delete ToDel;
7920 } else {
7921 if (Trap) Trap->UnStick(BodyPartIndex);
7922 T = &(*T)->Next;
7925 else {
7926 T = &(*T)->Next;
7929 if (GetBodyPart(BodyPartIndex)) GetBodyPart(BodyPartIndex)->SignalPossibleUsabilityChange();
7933 festring character::GetTrapDescription () const {
7934 festring Desc;
7935 std::pair<entity *, int> TrapStack[3];
7936 int Index = 0;
7937 for (const trapdata *T = TrapData; T; T = T->Next) {
7938 if (Index < 3) {
7939 entity *Trap = game::SearchTrap(T->TrapID);
7940 if (Trap) {
7941 int c;
7942 for (c = 0; c < Index; ++c) if (TrapStack[c].first->GetTrapType() == Trap->GetTrapType()) ++TrapStack[c].second;
7943 if (c == Index) TrapStack[Index++] = std::make_pair(Trap, 1);
7945 } else {
7946 ++Index;
7947 break;
7950 if (Index <= 3) {
7951 TrapStack[0].first->AddTrapName(Desc, TrapStack[0].second);
7952 if (Index == 2) {
7953 Desc << " and ";
7954 TrapStack[1].first->AddTrapName(Desc, TrapStack[1].second);
7955 } else if (Index == 3) {
7956 Desc << ", ";
7957 TrapStack[1].first->AddTrapName(Desc, TrapStack[1].second);
7958 Desc << " and ";
7959 TrapStack[2].first->AddTrapName(Desc, TrapStack[2].second);
7961 } else {
7962 Desc << "lots of traps";
7964 return Desc;
7968 int character::RandomizeHurtBodyPart (feuLong BodyParts) const {
7969 int BodyPartIndex[MAX_BODYPARTS];
7970 int Index = 0;
7971 for (int c = 0; c < GetBodyParts(); ++c) {
7972 if (1 << c & BodyParts) {
7973 /*k8: ??? if(!GetBodyPart(c)) int esko = esko = 2; */
7974 if (!GetBodyPart(c)) continue;
7975 BodyPartIndex[Index++] = c;
7977 /*k8: ??? if(!Index) int esko = esko = 2;*/
7979 if (!Index) {
7980 fprintf(stderr, "FATAL: RandomizeHurtBodyPart -- Index==0\n");
7981 abort();
7983 return BodyPartIndex[RAND_N(Index)];
7987 truth character::BodyPartIsStuck (int I) const {
7988 for (const trapdata *T = TrapData; T; T = T->Next) if (1 << I & T->BodyParts) return true;
7989 return false;
7993 void character::PrintAttribute (cchar *Desc, int I, int PanelPosX, int PanelPosY) const {
7994 int Attribute = GetAttribute(I);
7995 int NoBonusAttribute = GetAttribute(I, false);
7996 col16 C = game::GetAttributeColor(I);
7997 festring String = Desc;
7998 String.Resize(5);
7999 String << Attribute;
8000 String.Resize(8);
8001 FONT->Printf(DOUBLE_BUFFER, v2(PanelPosX, PanelPosY * 10), C, "%s", String.CStr());
8002 if (Attribute != NoBonusAttribute) {
8003 int Where = PanelPosX + ((String.GetSize() + 1) << 3);
8004 FONT->Printf(DOUBLE_BUFFER, v2(Where, PanelPosY * 10), LIGHT_GRAY, "%d", NoBonusAttribute);
8009 truth character::AllowUnconsciousness () const {
8010 return DataBase->AllowUnconsciousness && TorsoIsAlive();
8014 truth character::CanPanic () const {
8015 return !Action || !Action->IsUnconsciousness() || !StateIsActivated(FEARLESS);
8019 int character::GetRandomBodyPart (feuLong Possible) const {
8020 int OKBodyPart[MAX_BODYPARTS];
8021 int OKBodyParts = 0;
8022 for (int c = 0; c < BodyParts; ++c) if (1 << c & Possible && GetBodyPart(c)) OKBodyPart[OKBodyParts++] = c;
8023 return OKBodyParts ? OKBodyPart[RAND_N(OKBodyParts)] : NONE_INDEX;
8027 void character::EditNP (sLong What) {
8028 int OldState = GetHungerState();
8029 NP += What;
8030 int NewState = GetHungerState();
8031 if (OldState > VERY_HUNGRY && NewState == VERY_HUNGRY) DeActivateVoluntaryAction(CONST_S("You are getting really hungry."));
8032 if (OldState > STARVING && NewState == STARVING) DeActivateVoluntaryAction(CONST_S("You are getting extremely hungry."));
8036 truth character::IsSwimming () const {
8037 return !IsFlying() && GetSquareUnder() && GetSquareUnder()->GetSquareWalkability() & SWIM;
8041 void character::AddBlackUnicornConsumeEndMessage () const {
8042 if (IsPlayer()) ADD_MESSAGE("You feel dirty and loathsome.");
8046 void character::AddGrayUnicornConsumeEndMessage () const {
8047 if (IsPlayer()) ADD_MESSAGE("You feel neutralized.");
8051 void character::AddWhiteUnicornConsumeEndMessage () const {
8052 if (IsPlayer()) ADD_MESSAGE("You feel purified.");
8056 void character::AddOmmelBoneConsumeEndMessage () const {
8057 if (IsPlayer()) ADD_MESSAGE("You feel the power of all your canine ancestors combining in your body.");
8058 else if (CanBeSeenByPlayer()) ADD_MESSAGE("For a moment %s looks extremely ferocious. You shudder.", CHAR_NAME(DEFINITE));
8062 void character::AddLiquidHorrorConsumeEndMessage () const {
8063 if (IsPlayer()) ADD_MESSAGE("Untold horrors flash before your eyes. The melancholy of the world is on your shoulders!");
8064 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks as if the melancholy of the world is on %s shoulders!.", CHAR_NAME(DEFINITE), GetPossessivePronoun().CStr());
8068 void character::AddAlienFleshConsumeEndMessage() const
8070 if (IsPlayer()) ADD_MESSAGE("You feel somehow sick by eating such acidic corpse...");
8071 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks like he eat something bad.", CHAR_NAME(DEFINITE));
8075 int character::GetBodyPartSparkleFlags (int) const {
8076 return
8077 ((GetNaturalSparkleFlags() & SKIN_COLOR ? SPARKLING_A : 0) |
8078 (GetNaturalSparkleFlags() & TORSO_MAIN_COLOR ? SPARKLING_B : 0) |
8079 (GetNaturalSparkleFlags() & TORSO_SPECIAL_COLOR ? SPARKLING_D : 0));
8083 truth character::IsAnimated () const {
8084 return combinebodypartpredicates()(this, &bodypart::IsAnimated, 1);
8088 double character::GetNaturalExperience (int Identifier) const {
8089 return DataBase->NaturalExperience[Identifier];
8093 truth character::HasBodyPart (sorter Sorter) const {
8094 if (Sorter == 0) return true;
8095 return combinebodypartpredicateswithparam<ccharacter*>()(this, Sorter, this, 1);
8099 truth character::PossessesItem (sorter Sorter) const {
8100 if (Sorter == 0) return true;
8101 return
8102 (GetStack()->SortedItems(this, Sorter) ||
8103 combinebodypartpredicateswithparam<ccharacter*>()(this, Sorter, this, 1) ||
8104 combineequipmentpredicateswithparam<ccharacter*>()(this, Sorter, this, 1));
8108 truth character::MoreThanOnePossessesItem (sorter Sorter) const {
8109 if (Sorter) {
8110 int count = 0;
8112 for (int c = 0; c < BodyParts; ++c) {
8113 bodypart *BodyPart = GetBodyPart(c);
8115 if (BodyPart && (Sorter == 0 || (BodyPart->*Sorter)(this))) {
8116 if (++count > 1) return true;
8119 for (int c = 0; c < GetEquipments(); ++c) {
8120 item *Equipment = GetEquipment(c);
8122 if (Equipment && (Sorter == 0 || (Equipment->*Sorter)(this))) {
8123 if (++count > 1) return true;
8126 for (int c = 0; c < GetStack()->GetItems(); ++c) {
8127 item *Stk = GetStack()->GetItem(c);
8129 if (Stk && (Sorter == 0 || (Stk->*Sorter)(this))) {
8130 if (++count > 1) return true;
8133 return false;
8135 return false;
8139 item *character::FirstPossessesItem (sorter Sorter) const {
8140 if (Sorter) {
8141 for (int c = 0; c < BodyParts; ++c) {
8142 bodypart *BodyPart = GetBodyPart(c);
8144 if (BodyPart && (Sorter == 0 || (BodyPart->*Sorter)(this))) return BodyPart;
8146 for (int c = 0; c < GetEquipments(); ++c) {
8147 item *Equipment = GetEquipment(c);
8149 if (Equipment && (Sorter == 0 || (Equipment->*Sorter)(this))) return Equipment;
8151 for (int c = 0; c < GetStack()->GetItems(); ++c) {
8152 item *Stk = GetStack()->GetItem(c);
8154 if (Stk && (Sorter == 0 || (Stk->*Sorter)(this))) return Stk;
8157 return 0;
8161 /* 0 <= I <= 1 */
8162 cchar *character::GetRunDescriptionLine (int I) const {
8163 if (!GetRunDescriptionLineOne().IsEmpty()) return !I ? GetRunDescriptionLineOne().CStr() : GetRunDescriptionLineTwo().CStr();
8164 if (IsFlying()) return !I ? "Flying" : "very fast";
8165 if (IsSwimming()) return !I ? "Swimming" : "very fast";
8166 return !I ? "Running" : "";
8170 void character::VomitAtRandomDirection (int Amount) {
8171 if (game::IsInWilderness()) return;
8172 /* Lacks support of multitile monsters */
8173 v2 Possible[9];
8174 int Index = 0;
8175 for (int d = 0; d < 9; ++d) {
8176 lsquare *Square = GetLSquareUnder()->GetNeighbourLSquare(d);
8177 if (Square && !Square->VomitingIsDangerous(this)) Possible[Index++] = Square->GetPos();
8179 if (Index) Vomit(Possible[RAND_N(Index)], Amount);
8180 else Vomit(GetPos(), Amount);
8184 void character::RemoveLifeSavers () {
8185 for (int c = 0; c < GetEquipments(); ++c) {
8186 item *Equipment = GetEquipment(c);
8187 if (Equipment && Equipment->IsInCorrectSlot(c) && Equipment->GetGearStates() & LIFE_SAVED) {
8188 Equipment->SendToHell();
8189 Equipment->RemoveFromSlot();
8195 ccharacter *character::FindCarrier () const {
8196 return this; //check
8200 void character::PrintBeginHiccupsMessage () const {
8201 if (IsPlayer()) ADD_MESSAGE("Your diaphragm is spasming vehemently.");
8205 void character::PrintEndHiccupsMessage () const {
8206 if (IsPlayer()) ADD_MESSAGE("You feel your annoying hiccoughs have finally subsided.");
8210 void character::HiccupsHandler () {
8212 if (!(RAND() % 2000)) {
8213 if (IsPlayer()) ADD_MESSAGE("");
8214 else if (CanBeSeenByPlayer()) ADD_MESSAGE("");
8215 else if ((PLAYER->GetPos()-GetPos()).GetLengthSquare() <= 400) ADD_MESSAGE("");
8216 game::CallForAttention(GetPos(), 400);
8222 void character::VampirismHandler () {
8223 //EditExperience(ARM_STRENGTH, -25, 1 << 1);
8224 //EditExperience(LEG_STRENGTH, -25, 1 << 1);
8225 //EditExperience(DEXTERITY, -25, 1 << 1);
8226 //EditExperience(AGILITY, -25, 1 << 1);
8227 //EditExperience(ENDURANCE, -25, 1 << 1);
8228 EditExperience(CHARISMA, -25, 1 << 1);
8229 EditExperience(WISDOM, -25, 1 << 1);
8230 EditExperience(INTELLIGENCE, -25, 1 << 1);
8231 CheckDeath(CONST_S("killed by vampirism"));
8235 void character::HiccupsSituationDangerModifier (double &Danger) const {
8236 Danger *= 1.25;
8240 void character::VampirismSituationDangerModifier (double &Danger) const {
8241 character *Vampire = vampire::Spawn();
8242 double DangerToVampire = GetRelativeDanger(Vampire);
8243 Danger *= pow(DangerToVampire, 0.1);
8244 delete Vampire;
8248 bool character::IsConscious () const {
8249 return !Action || !Action->IsUnconsciousness();
8253 wsquare *character::GetNearWSquare (v2 Pos) const {
8254 return static_cast<wsquare *>(GetSquareUnder()->GetArea()->GetSquare(Pos));
8258 wsquare *character::GetNearWSquare (int x, int y) const {
8259 return static_cast<wsquare *>(GetSquareUnder()->GetArea()->GetSquare(x, y));
8263 void character::ForcePutNear (v2 Pos) {
8264 /* GUM SOLUTION!!! */
8265 v2 NewPos = game::GetCurrentLevel()->GetNearestFreeSquare(PLAYER, Pos, false);
8266 if (NewPos == ERROR_V2) do { NewPos = game::GetCurrentLevel()->GetRandomSquare(this); } while(NewPos == Pos);
8267 PutTo(NewPos);
8271 void character::ReceiveMustardGas (int BodyPart, sLong Volume) {
8272 if (Volume) GetBodyPart(BodyPart)->AddFluid(liquid::Spawn(MUSTARD_GAS_LIQUID, Volume), CONST_S("skin"), 0, true);
8276 void character::ReceiveMustardGasLiquid (int BodyPartIndex, sLong Modifier) {
8277 bodypart *BodyPart = GetBodyPart(BodyPartIndex);
8278 if (BodyPart->GetMainMaterial()->GetInteractionFlags() & IS_AFFECTED_BY_MUSTARD_GAS) {
8279 sLong Tries = Modifier;
8280 Modifier -= Tries; //opt%?
8281 int Damage = 0;
8282 for (sLong c = 0; c < Tries; ++c) if (!(RAND() % 100)) ++Damage;
8283 if (Modifier && !(RAND() % 1000 / Modifier)) ++Damage;
8284 if (Damage) {
8285 feuLong Minute = game::GetTotalMinutes();
8286 if (GetLastAcidMsgMin() != Minute && (CanBeSeenByPlayer() || IsPlayer())) {
8287 SetLastAcidMsgMin(Minute);
8288 if (IsPlayer()) ADD_MESSAGE("Mustard gas dissolves the skin of your %s.", BodyPart->GetBodyPartName().CStr());
8289 else ADD_MESSAGE("Mustard gas dissolves %s.", CHAR_NAME(DEFINITE));
8291 ReceiveBodyPartDamage(0, Damage, MUSTARD_GAS_DAMAGE, BodyPartIndex, YOURSELF, false, false, false);
8292 CheckDeath(CONST_S("killed by a fatal exposure to mustard gas"));
8298 truth character::IsBadPath (v2 Pos) const {
8299 if (!IsGoingSomeWhere()) return false;
8300 v2 TPos = !Route.empty() ? Route.back() : GoingTo;
8301 return ((TPos - Pos).GetManhattanLength() > (TPos - GetPos()).GetManhattanLength());
8305 double &character::GetExpModifierRef (expid E) {
8306 return ExpModifierMap.insert(std::make_pair(E, 1.)).first->second;
8310 /* Should probably do more. Now only makes Player forget gods */
8311 truth character::ForgetRandomThing () {
8312 if (IsPlayer()) {
8313 /* hopefully this code isn't some where else */
8314 std::vector<god *> Known;
8315 for (int c = 1; c <= GODS; ++c) if (game::GetGod(c)->IsKnown()) Known.push_back(game::GetGod(c));
8316 if (Known.empty()) return false;
8317 int RandomGod = RAND_N(Known.size());
8318 Known.at(RAND_N(Known.size()))->SetIsKnown(false);
8319 ADD_MESSAGE("You forget how to pray to %s.", Known.at(RandomGod)->GetName());
8320 return true;
8322 return false;
8326 int character::CheckForBlock (character *Enemy, item *Weapon, double ToHitValue, int Damage, int Success, int Type) {
8327 return Damage;
8331 void character::ApplyAllGodsKnownBonus () {
8332 stack *AddPlace = GetStackUnder();
8333 if (game::IsInWilderness()) AddPlace = GetStack(); else AddPlace = GetStackUnder();
8334 pantheonbook *NewBook = pantheonbook::Spawn();
8335 AddPlace->AddItem(NewBook);
8336 ADD_MESSAGE("\"MORTAL! BEHOLD THE HOLY SAGA\"");
8337 ADD_MESSAGE("%s materializes near your feet.", NewBook->CHAR_NAME(INDEFINITE));
8341 void character::ReceiveSirenSong (character *Siren) {
8342 if (Siren->GetTeam() == GetTeam()) return;
8343 if (!RAND_N(4)) {
8344 if (IsPlayer()) ADD_MESSAGE("The beautiful melody of %s makes you feel sleepy.", Siren->CHAR_NAME(DEFINITE));
8345 else if (CanBeSeenByPlayer()) ADD_MESSAGE("The beautiful melody of %s makes %s look sleepy.", Siren->CHAR_NAME(DEFINITE), CHAR_NAME(DEFINITE)); /*k8*/
8346 Stamina -= (1 + RAND_N(4)) * 10000;
8347 return;
8349 if (!IsPlayer() && IsCharmable() && !RAND_N(5)) {
8350 ChangeTeam(Siren->GetTeam());
8351 ADD_MESSAGE("%s seems to be totally brainwashed by %s melodies.", CHAR_NAME(DEFINITE), Siren->CHAR_NAME(DEFINITE));
8352 return;
8354 if (!RAND_N(4)) {
8355 item *What = GiveMostExpensiveItem(Siren);
8356 if (What) {
8357 if (IsPlayer()) {
8358 ADD_MESSAGE("%s music persuades you to give %s to %s as a present.", Siren->CHAR_NAME(DEFINITE), What->CHAR_NAME(DEFINITE), Siren->CHAR_OBJECT_PRONOUN);
8359 } else {
8360 ADD_MESSAGE("%s is persuated to give %s to %s because of %s beautiful singing.", CHAR_NAME(DEFINITE), What->CHAR_NAME(INDEFINITE), Siren->CHAR_NAME(DEFINITE), Siren->CHAR_OBJECT_PRONOUN);
8362 } else {
8363 if (IsPlayer()) ADD_MESSAGE("You would like to give something to %s.", Siren->CHAR_NAME(DEFINITE));
8365 return;
8370 item *character::findFirstEquippedItem (cfestring &aclassname, int aconfig) const {
8371 for (int f = 0; f < GetEquipments(); ++f) {
8372 item *it = GetEquipment(f);
8373 if (!it) continue;
8374 if (aclassname.CompareIgnoreCase(it->GetClassID()) == 0) {
8375 if (aconfig == -1 || it->GetConfig() == aconfig) return it;
8378 for (uInt c = 0; c < it->GetDataBase()->Alias.Size; ++c) {
8379 if (s.CompareIgnoreCase(it->GetDataBase()->Alias[c]) == 0) return it;
8383 return nullptr;
8387 item *character::findFirstInventoryItem (cfestring &aclassname, int aconfig) const {
8388 itemvector items;
8389 GetStack()->FillItemVector(items);
8390 for (int f = 0; f < (int)items.size(); ++f) {
8391 item *it = items[f];
8392 if (!it) continue;
8393 if (aclassname.CompareIgnoreCase(it->GetClassID()) == 0) {
8394 if (aconfig == -1 || it->GetConfig() == aconfig) return it;
8397 return nullptr;
8401 item *character::findFirstItem (cfestring &aclassname, int aconfig) const {
8402 item *it = findFirstInventoryItem(aclassname, aconfig);
8403 if (!it) it = findFirstEquippedItem(aclassname, aconfig);
8404 return it;
8408 // return 0, if no item found
8409 item *character::FindMostExpensiveItem () const {
8410 int MaxPrice = -1;
8411 item *MostExpensive = 0;
8412 for (stackiterator i = GetStack()->GetBottom(); i.HasItem(); ++i) {
8413 if ((*i)->GetPrice() > MaxPrice) {
8414 MaxPrice = (*i)->GetPrice();
8415 MostExpensive = (*i);
8418 for (int c = 0; c < GetEquipments(); ++c) {
8419 item *Equipment = GetEquipment(c);
8420 if (Equipment && Equipment->GetPrice() > MaxPrice) {
8421 MaxPrice = Equipment->GetPrice();
8422 MostExpensive = Equipment;
8425 return MostExpensive;
8429 // returns 0 if no items available
8430 item *character::GiveMostExpensiveItem(character *ToWhom) {
8431 item *ToGive = FindMostExpensiveItem();
8432 if (!ToGive) return 0;
8433 truth Equipped = PLAYER->Equips(ToGive);
8434 ToGive->RemoveFromSlot();
8435 if (Equipped) game::AskForEscPress(CONST_S("Equipment lost!"));
8436 ToWhom->ReceiveItemAsPresent(ToGive);
8437 EditAP(-1000);
8438 return ToGive;
8442 void character::ReceiveItemAsPresent (item *Present) {
8443 if (TestForPickup(Present)) GetStack()->AddItem(Present); else GetStackUnder()->AddItem(Present);
8447 /* returns 0 if no enemies in sight */
8448 character *character::GetNearestEnemy () const {
8449 character *NearestEnemy = 0;
8450 sLong NearestEnemyDistance = 0x7FFFFFFF;
8451 v2 Pos = GetPos();
8452 for (int c = 0; c < game::GetTeams(); ++c) {
8453 if (GetTeam()->GetRelation(game::GetTeam(c)) == HOSTILE) {
8454 for (std::list<character*>::const_iterator i = game::GetTeam(c)->GetMember().begin(); i != game::GetTeam(c)->GetMember().end(); ++i) {
8455 if ((*i)->IsEnabled()) {
8456 sLong ThisDistance = Max<sLong>(abs((*i)->GetPos().X - Pos.X), abs((*i)->GetPos().Y - Pos.Y));
8457 if ((ThisDistance < NearestEnemyDistance || (ThisDistance == NearestEnemyDistance && !(RAND() % 3))) && (*i)->CanBeSeenBy(this)) {
8458 NearestEnemy = *i;
8459 NearestEnemyDistance = ThisDistance;
8465 return NearestEnemy;
8469 truth character::MindWormCanPenetrateSkull (mindworm *) const {
8470 return false;
8474 truth character::CanTameWithDulcis (const character *Tamer) const {
8475 int TamingDifficulty = GetTamingDifficulty();
8476 if (TamingDifficulty == NO_TAMING) return false;
8477 if (GetAttachedGod() == DULCIS) return true;
8478 int Modifier = Tamer->GetAttribute(WISDOM) + Tamer->GetAttribute(CHARISMA);
8479 if (Tamer->IsPlayer()) Modifier += game::GetGod(DULCIS)->GetRelation() / 20;
8480 else if (Tamer->GetAttachedGod() == DULCIS) Modifier += 50;
8481 if (TamingDifficulty == 0) {
8482 if (!IgnoreDanger()) TamingDifficulty = int(10 * GetRelativeDanger(Tamer));
8483 else TamingDifficulty = 10 * GetHPRequirementForGeneration()/Max(Tamer->GetHP(), 1);
8485 return Modifier >= TamingDifficulty * 3;
8489 truth character::CanTameWithLyre (const character *Tamer) const {
8490 int TamingDifficulty = GetTamingDifficulty();
8491 if (TamingDifficulty == NO_TAMING) return false;
8492 if (TamingDifficulty == 0) {
8493 if (!IgnoreDanger()) TamingDifficulty = int(10 * GetRelativeDanger(Tamer));
8494 else TamingDifficulty = 10*GetHPRequirementForGeneration()/Max(Tamer->GetHP(), 1);
8496 return Tamer->GetAttribute(CHARISMA) >= TamingDifficulty;
8500 truth character::CanTameWithScroll (const character *Tamer) const {
8501 int TamingDifficulty = GetTamingDifficulty();
8502 return
8503 (TamingDifficulty != NO_TAMING &&
8504 (TamingDifficulty == 0 ||
8505 Tamer->GetAttribute(INTELLIGENCE) * 4 + Tamer->GetAttribute(CHARISMA) >= TamingDifficulty * 5));
8509 truth character::CanTameWithResurrection (const character *Tamer) const {
8510 int TamingDifficulty = GetTamingDifficulty();
8511 if (TamingDifficulty == NO_TAMING) return false;
8512 if (TamingDifficulty == 0) return true;
8513 return (Tamer->GetAttribute(CHARISMA) >= TamingDifficulty/2);
8517 truth character::CheckSadism () {
8518 if (!IsSadist() || !HasSadistAttackMode() || !IsSmall()) return false; // gum
8519 if (!RAND_N(10)) {
8520 for (int d = 0; d < MDIR_STAND; ++d) {
8521 square *Square = GetNeighbourSquare(d);
8522 if (Square) {
8523 character *Char = Square->GetCharacter();
8524 if (Char && Char->IsMasochist() && GetRelation(Char) == FRIEND &&
8525 Char->GetHP() * 3 >= Char->GetMaxHP() * 2 && Hit(Char, Square->GetPos(), d, SADIST_HIT)) {
8526 TerminateGoingTo();
8527 return true;
8532 return false;
8536 truth character::CheckForBeverage () {
8537 if (StateIsActivated(PANIC) || !IsEnabled() || !UsesNutrition() || CheckIfSatiated()) return false;
8538 itemvector ItemVector;
8539 GetStack()->FillItemVector(ItemVector);
8540 for (uInt c = 0; c < ItemVector.size(); ++c) if (ItemVector[c]->IsBeverage(this) && TryToConsume(ItemVector[c])) return true;
8541 return false;
8545 void character::Haste () {
8546 doforbodyparts()(this, &bodypart::Haste);
8547 doforequipments()(this, &item::Haste);
8548 BeginTemporaryState(HASTE, 500 + RAND() % 1000);
8552 void character::Slow () {
8553 doforbodyparts()(this, &bodypart::Slow);
8554 doforequipments()(this, &item::Slow);
8555 //BeginTemporaryState(HASTE, 500 + RAND() % 1000); // this seems to be a bug
8556 BeginTemporaryState(SLOW, 500 + RAND() % 1000);
8560 void character::SurgicallyDetachBodyPart () {
8561 ADD_MESSAGE("You haven't got any extra bodyparts.");
8565 truth character::CanHear() const
8567 return DataBase->CanHear && HasHead();
8571 truth character::IsAllowedInDungeon (int dunIndex) {
8572 const fearray<int> &dlist = GetAllowedDungeons();
8574 for (uInt f = 0; f < dlist.Size; ++f) {
8575 if (dlist[f] == ALL_DUNGEONS || dlist[f] == dunIndex) {
8576 fprintf(stderr, "OK!\n");
8577 return true;
8580 fprintf(stderr, "NO!\n");
8581 return false;
8585 truth character::IsESPBlockedByEquipment () const {
8586 for (int c = 0; c < GetEquipments(); ++c) {
8587 item *Item = GetEquipment(c);
8588 if (Item && Item->IsHelmet(this) &&
8589 ((Item->GetMainMaterial() && Item->GetMainMaterial()->BlockESP()) ||
8590 (Item->GetSecondaryMaterial() && Item->GetSecondaryMaterial()->BlockESP()))) return true;
8592 return false;
8596 truth character::TemporaryStateIsActivated (sLong What) const {
8597 if ((What&ESP) && (TemporaryState&ESP) && IsESPBlockedByEquipment()) {
8598 return ((TemporaryState&What)&(~ESP));
8600 return (TemporaryState & What);
8604 truth character::StateIsActivated (sLong What) const {
8605 if ((What&ESP) && ((TemporaryState|EquipmentState)&ESP) && IsESPBlockedByEquipment()) {
8606 return ((TemporaryState&What)&(~ESP)) || ((EquipmentState&What)&(~ESP));
8608 return (TemporaryState & What) || (EquipmentState & What);
8612 void character::PrintBeginFearlessMessage () const {
8613 if (!StateIsActivated(FEARLESS)) {
8614 if (IsPlayer()) ADD_MESSAGE("You feel very comfortable.");
8615 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s seems very comfortable.", CHAR_NAME(DEFINITE));
8619 void character::PrintEndFearlessMessage () const {
8620 if (!StateIsActivated(FEARLESS)) {
8621 if (IsPlayer()) ADD_MESSAGE("Everything looks more dangerous now.");
8622 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s seems to have lost his confidence.", CHAR_NAME(DEFINITE));
8626 void character::BeginFearless () {
8627 DeActivateTemporaryState(PANIC);
8630 void character::EndFearless () {
8631 CheckPanic(500);
8635 void character::PrintBeginEtherealityMessage () const {
8636 if (IsPlayer()) ADD_MESSAGE("You feel like many miscible droplets of ether.");
8637 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s melds into the surroundings.", CHAR_NAME(DEFINITE));
8640 void character::PrintEndEtherealityMessage () const {
8641 if (IsPlayer()) ADD_MESSAGE("You drop out of the firmament, feeling suddenly quite dense.");
8642 else if (CanBeSeenByPlayer()) ADD_MESSAGE("Suddenly %s displaces the air with a puff.", CHAR_NAME(INDEFINITE));
8645 void character::BeginEthereality () {}
8647 void character::EndEthereality () {}
8650 void character::PrintBeginPolymorphLockMessage () const {
8651 if (IsPlayer()) ADD_MESSAGE("You feel incredibly stubborn about who you are.");
8654 void character::PrintEndPolymorphLockMessage () const {
8655 if (IsPlayer()) ADD_MESSAGE("You feel more open to new ideas.");
8658 void character::PolymorphLockHandler () {
8659 if (TemporaryStateIsActivated(POLYMORPHED)) {
8660 EditTemporaryStateCounter(POLYMORPHED, 1);
8661 if (GetTemporaryStateCounter(POLYMORPHED) < 1000) EditTemporaryStateCounter(POLYMORPHED, 1);
8665 void character::PrintBeginRegenerationMessage () const {
8666 if (IsPlayer()) ADD_MESSAGE("Your heart races.");
8669 void character::PrintEndRegenerationMessage () const {
8670 if (IsPlayer()) ADD_MESSAGE("Your rapid heartbeat calms down.");
8673 void character::PrintBeginDiseaseImmunityMessage () const {
8674 if (IsPlayer()) ADD_MESSAGE("You feel especially healthy.");
8677 void character::PrintEndDiseaseImmunityMessage () const {
8678 if (IsPlayer()) ADD_MESSAGE("You develop a sudden fear of germs.");
8681 void character::PrintBeginTeleportLockMessage () const {
8682 if (IsPlayer()) ADD_MESSAGE("You feel firmly planted in reality.");
8685 void character::PrintEndTeleportLockMessage () const {
8686 if (IsPlayer()) ADD_MESSAGE("Your mind soars far and wide.");
8689 void character::TeleportLockHandler () {
8690 if (StateIsActivated(TELEPORT_LOCK)) {
8691 EditTemporaryStateCounter(TELEPORT_LOCK, 1);
8692 if (GetTemporaryStateCounter(TELEPORT_LOCK) < 1000) EditTemporaryStateCounter(TELEPORT_LOCK, 1);
8697 void character::PrintBeginSwimmingMessage () const {
8698 if (IsPlayer()) ADD_MESSAGE("You fondly remember the sound of ocean waves.");
8699 else if (CanBeSeenByPlayer()) ADD_MESSAGE("%s looks wet.", CHAR_NAME(DEFINITE));
8702 void character::PrintEndSwimmingMessage () const {
8703 if (IsPlayer()) ADD_MESSAGE("You suddenly remember how you nearly drowned as a child.");
8704 else if (CanBeSeenByPlayer()) ADD_MESSAGE("Suddenly %s looks less wet.", CHAR_NAME(INDEFINITE));
8707 void character::BeginSwimming () {}
8708 void character::EndSwimming () {}