MCPcopy Create free account
hub / github.com/OpenDungeons/OpenDungeons / handleSleep

Method handleSleep

source/creatureaction/CreatureActionSleep.cpp:36–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34}
35
36bool CreatureActionSleep::handleSleep(Creature& creature, int32_t nbTurnsActive)
37{
38 if (creature.getHomeTile() == nullptr)
39 {
40 if(!creature.hasActionBeenTried(CreatureActionType::findHome))
41 {
42 creature.pushAction(Utils::make_unique<CreatureActionFindHome>(creature, false));
43 return true;
44 }
45
46 creature.popAction();
47 return false;
48 }
49
50 Tile* myTile = creature.getPositionTile();
51 if (myTile != creature.getHomeTile())
52 {
53 // Walk to the the home tile.
54 if (creature.setDestination(creature.getHomeTile()))
55 return false;
56 }
57 else
58 {
59 // We are at the home tile so sleep. If it is the first time we are sleeping,
60 // we send the animation
61 if(nbTurnsActive == 0)
62 {
63 Room* roomHomeTile = creature.getHomeTile()->getCoveringRoom();
64 if((roomHomeTile == nullptr) || (roomHomeTile->getType() != RoomType::dormitory))
65 {
66 OD_LOG_ERR("creature=" + creature.getName() + ", wrong sleep tile on " + Tile::displayAsString(creature.getHomeTile()));
67 creature.popAction();
68 return false;
69 }
70 RoomDormitory* dormitory = static_cast<RoomDormitory*>(roomHomeTile);
71 creature.setAnimationState(EntityAnimation::sleep_anim, false, dormitory->getSleepDirection(&creature), false);
72 }
73
74 // Improve wakefulness
75 creature.increaseWakefulness(1.5);
76 creature.setHP(creature.getHP() + creature.getDefinition()->getSleepHeal());
77
78 creature.computeCreatureOverlayHealthValue();
79
80 if (creature.getWakefulness() >= 100.0 && creature.getHP() >= creature.getMaxHp())
81 {
82 creature.popAction();
83 return false;
84 }
85 }
86 return false;
87}

Callers

nothing calls this directly

Calls 15

getHomeTileMethod · 0.80
hasActionBeenTriedMethod · 0.80
pushActionMethod · 0.80
popActionMethod · 0.80
getPositionTileMethod · 0.80
setDestinationMethod · 0.80
getCoveringRoomMethod · 0.80
increaseWakefulnessMethod · 0.80
setHPMethod · 0.80
getSleepHealMethod · 0.80
getDefinitionMethod · 0.80

Tested by

no test coverage detected