Debian Codegraph @

CorsixTH/Lua/entities/humanoids/staff/handyman.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 corsixth.require("announcer")
22 corsixth.require("entities.humanoids.staff")
23
24 local AnnouncementPriority = _G["AnnouncementPriority"]
25
26 --! A Doctor, Nurse, Receptionist, Handyman, or Surgeon
27 class "Handyman" (Staff)
28
29 ---@type Handyman
30 local Handyman = _G["Handyman"]
31
32 --!param ... Arguments to base class constructor.
33 function Handyman:Handyman(...)
34 self:Staff(...)
35 self.leave_sounds = {"sack006.wav"}
36 end
37
38 -- Set task type priority value
39 --!param task_type (string) task type
40 --!param priority (float) priority to set
41 function Handyman:setPriority(task_type, priority)
42 self.attributes[task_type] = priority
43 end
44
45 -- Get task type priority value
46 --!param task_type (string) task type
47 --!return (float) priority
48 function Handyman:getPriority(task_type)
49 return self:getAttribute(task_type)
50 end
51
52 function Handyman:dump()
53 print("-----------------------------------")
54 if self.on_call then
55 print("On call: ")
56 CallsDispatcher.dumpCall(self.on_call)
57 else
58 print('On call: no')
59 end
60 print("Busy: ", (self:isIdle() and "idle" or "busy") .. (self.pickup and " and picked up" or ''))
61 if self.going_to_staffroom then print("Going to staffroom") end
62 if self.last_room then
63 print("Last room: ", self.last_room.room_info.id .. '@' .. self.last_room.x ..','.. self.last_room.y)
64 end
65
66 print("Cleaning: " .. self:getPriority("cleaning"),
67 "Watering: " .. self:getPriority("watering"),
68 "Repairing: " .. self:getPriority("repairing"))
69
70 Humanoid.dump(self)
71 end
72
73 function Handyman:setProfile(profile)
74 Staff.setProfile(self, profile)
75
76 -- Set Handyman priorities by default
77 self:setPriority("cleaning", 0.333)
78 self:setPriority("watering", 0.333)
79 self:setPriority("repairing", 0.333)
80 end
81
82 function Handyman:goToStaffRoom()
83 self:unassignTask()
84 Staff.goToStaffRoom(self)
85 end
86
87 -- Helper function to decide if Handyman fulfills a criterion
88 -- (one of "Doctor", "Nurse", "Psychiatrist", "Surgeon", "Researcher" and "Handyman", "Receptionist", "Junior", "Consultant")
89 function Handyman:fulfillsCriterion(criterion)
90 return criterion == "Handyman"
91 end
92
93 function Handyman:onPickup()
94 self:releaseHandymanFromTask(false)
95 Staff.onPickup(self)
96 end
97
98 -- Detaches the handyman from the task and forces him to search for a new one.
99 --!param reset_action_queue (bool) Should the handyman interrupt
100 -- the current action to search for a new task
101 function Handyman:releaseHandymanFromTask(reset_action_queue)
102 self:setDynamicInfoText("")
103 self:unassignTask()
104 -- Lets find another task
105 if reset_action_queue then
106 self:setNextAction(AnswerCallAction())
107 else
108 self:queueAction(AnswerCallAction())
109 end
110 end
111
112 function Handyman:searchForHandymanTask()
113 self.task = nil
114 local nr = math.random()
115 local task_type_1, task_type_2, task_type_3
116 local task_found = false
117 -- Let's determine what type of tasks will be a priority for that handyman.
118 if nr < self:getPriority("cleaning") then
119 task_type_1, task_type_2, task_type_3 = "cleaning", "watering", "repairing"
120 elseif nr < self:getPriority("cleaning") + self:getPriority("watering") then
121 task_type_1, task_type_2, task_type_3 = "watering", "cleaning", "repairing"
122 else
123 task_type_1, task_type_2, task_type_3 = "repairing", "watering", "cleaning"
124 end
125 -- Let's search First priority type task.
126 local index = self.hospital:searchForHandymanTask(self, task_type_1)
127 if index ~= -1 then
128 -- Found a task for the handyman
129 self:assignHandymanTask(index, task_type_1)
130 task_found = true
131 else
132 -- Unable find any first type priority tasks for the handyman.
133 if self:getPriority(task_type_1) < 1 then
134 local sum = self:getPriority(task_type_2) + self:getPriority(task_type_3)
135 if math.random(0, math.floor(sum * 100)) > math.floor(self:getPriority(task_type_2) * 100) then
136 task_type_2, task_type_3 = task_type_3, task_type_2
137 end
138 -- Let's search again but for Second priority type tasks now.
139 index = self.hospital:searchForHandymanTask(self, task_type_2)
140 if index ~= -1 then
141 self:assignHandymanTask(index, task_type_2)
142 task_found = true
143 elseif self:getPriority(task_type_3) > 0 then
144 -- Let's search again but for Third priority type tasks now.
145 index = self.hospital:searchForHandymanTask(self, task_type_3)
146 if index ~= -1 then
147 self:assignHandymanTask(index, task_type_3)
148 task_found = true
149 end
150 end
151 end
152 end
153 if task_found == false then
154 -- Unable to find any task for the handyman. So let him meandering.
155 self:doMeandering()
156 end
157 return task_found
158 end
159
160 function Handyman:doMeandering()
161 -- Make sure that the handyman isn't meandering already.
162 if self:isMeandering() then
163 return
164 end
165 if self:getRoom() then
166 self:queueAction(self:getRoom():createLeaveAction())
167 end
168 self:queueAction(MeanderAction())
169 end
170
171 -- Assign a handyman to a task
172 --!param task_index (integer) task index
173 --!param task_type (string) task type
174 function Handyman:assignHandymanTask(task_index, task_type)
175 local task_object = self.hospital:assignHandymanToTask(self, task_index, task_type)
176 if task_object then
177 if task_type == "cleaning" then
178 self:processCleaningTask(task_object)
179 else
180 if task_object.call.dropped then
181 task_object.call.dropped = nil
182 end
183 task_object.call.dispatcher:executeCall(task_object.call, self)
184 end
185 end
186 end
187
188 function Handyman:processCleaningTask(task)
189 if self:getRoom() then
190 self:setNextAction(self:getRoom():createLeaveAction())
191 self:queueAction(WalkAction(task.tile_x, task.tile_y))
192 else
193 self:setNextAction(WalkAction(task.tile_x, task.tile_y))
194 end
195 self:queueAction(SweepFloorAction(task.object))
196 -- After cleaning find next task
197 self:queueAction(AnswerCallAction())
198 end
199
200 -- If the staff member had a task outstanding, unassigning them from that task.
201 -- Tasks with no handyman assigned will be eligible for reassignment by the hospital.
202 function Handyman:unassignTask()
203 self:unassignCall()
204 if self.task then
205 self.task.assignedHandyman = nil
206 self.task = nil
207 end
208 end
209
210 function Handyman:unassignCall()
211 if self.on_call then
212 CallsDispatcher.unassignCall(self.on_call, false)
213 end
214 end
215
216 function Handyman:die()
217 Staff.die(self)
218 self:unassignTask()
219 self.hospital:unassignHandymanTasks(self)
220 end
221
222 function Handyman:afterLoad(old, new)
223 if old < 163 then
224 self.leave_priority = AnnouncementPriority.High
225 self.leave_sounds = {"sack006.wav"}
226 end
227 Staff.afterLoad(self, old, new)
228 end
229