Debian Codegraph @

CorsixTH/Lua/humanoid_actions/meander.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 class "MeanderAction" (HumanoidAction)
22
23 ---@type MeanderAction
24 local MeanderAction = _G["MeanderAction"]
25
26 function MeanderAction:MeanderAction()
27 self:HumanoidAction("meander")
28 end
29
30 local function meander_action_start(action, humanoid)
31 local room = humanoid:getRoom()
32 -- Answering call queue
33 if class.is(humanoid, Staff) and humanoid:isIdle() and not room then
34 if class.is(humanoid, Handyman) then
35 if humanoid:searchForHandymanTask() == true then
36 return
37 end
38
39 -- If staff starts wandering around in Idle mode,
40 -- he's effectively not in any room and need not to comeback after
41 -- staff room visit
42 elseif humanoid.world.dispatcher:answerCall(humanoid) then
43 if action.must_happen then
44 humanoid:finishAction()
45 end
46 return
47
48 else
49 -- Nowhere to go, start going to the old room if it's still in
50 -- need of staff.
51 local last_room = humanoid.last_room
52 if last_room and last_room.is_active and
53 last_room:testStaffCriteria(last_room:getMaximumStaffCriteria(), humanoid) then
54 humanoid:queueAction(last_room:createEnterAction(humanoid))
55 humanoid:setDynamicInfoText(_S.dynamic_info.staff.actions.heading_for
56 :format(last_room.room_info.name))
57 humanoid:finishAction()
58 return
59 end
60 end
61 humanoid.last_room = nil
62 end
63
64 -- Just wandering around
65 if class.is(humanoid, Doctor) or class.is(humanoid, Nurse) then
66 if not room then
67 humanoid:setDynamicInfoText(_S.dynamic_info.staff.actions.wandering)
68 end
69 end
70
71 -- Handymen may have an assigned parcel, but they are free to visit any staff
72 -- room to rest, or wait inside a room to repair
73 local meander_parcel
74 if class.is(humanoid, Handyman) and humanoid.parcelNr > 0 and
75 not humanoid:getRoom() then
76 meander_parcel = humanoid.parcelNr
77 end
78
79 local x, y = humanoid.world.pathfinder:findIdleTile(humanoid.tile_x,
80 humanoid.tile_y, math.random(1, 24), meander_parcel)
81
82 if x == humanoid.tile_x and y == humanoid.tile_y then
83 -- Nowhere to walk to - go idle instead, or go onto the next action
84 if #humanoid.action_queue == 1 then
85 humanoid:queueAction(IdleAction())
86 end
87 humanoid:finishAction()
88 return
89 end
90 if action.todo_interrupt then
91 humanoid:finishAction()
92 return
93 end
94 if action.count then
95 if action.count == 0 then
96 humanoid:finishAction()
97 return
98 else
99 action.count = action.count - 1
100 end
101 elseif action.loop_callback then
102 action.loop_callback()
103 if action ~= humanoid:getCurrentAction() then
104 return
105 end
106 end
107
108 local procrastination
109 if action.can_idle then
110 action.can_idle = false
111 procrastination = IdleAction():setCount(math.random(5, 40)):setMustHappen(action.must_happen)
112 else
113 action.can_idle = true
114 procrastination = WalkAction(x, y):setMustHappen(action.must_happen)
115 end
116 humanoid:queueAction(procrastination, 0)
117 end
118
119 return meander_action_start