Debian Codegraph @

CorsixTH/Lua/objects/plant.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 local object = {}
22 object.id = "plant"
23 object.thob = 45
24 object.name = _S.object.plant
25 object.class = "Plant"
26 object.tooltip = _S.tooltip.objects.plant
27 object.ticks = false
28 object.corridor_object = 7
29 object.build_preview_animation = 934
30
31 object.idle_animations = {
32 north = 1950,
33 south = 1950,
34 east = 1950,
35 west = 1950,
36 }
37 object.usage_animations = {
38 north = {
39 begin_use = { ["Handyman"] = {1972, object_visible = true} },
40 in_use = { ["Handyman"] = {1980, object_visible = true} },
41 },
42 east = {
43 begin_use = { ["Handyman"] = {1974, object_visible = true} },
44 in_use = { ["Handyman"] = {1982, object_visible = true} },
45 },
46 }
47 object.orientations = {
48 north = {
49 footprint = { {0, 0, complete_cell = true} },
50 use_animate_from_use_position = true
51 },
52 east = {
53 footprint = { {0, 0, complete_cell = true} },
54 use_animate_from_use_position = true
55 },
56 south = {
57 footprint = { {0, 0, complete_cell = true} },
58 use_animate_from_use_position = true
59 },
60 west = {
61 footprint = { {0, 0, complete_cell = true} },
62 use_animate_from_use_position = true
63 },
64 }
65
66 local kf1, kf2 = {-1, 1, "px"}, {4, -1, "px"}
67 local anim_mgr = TheApp.animation_manager
68 anim_mgr:setStaffMarker(1972, 0, kf1, 12, kf1, 13, kf2)
69 local kf3, kf4 = {2, -1, "px"}, {6, 0, "px"}
70 anim_mgr:setStaffMarker(1974, 0, kf1, 19, kf1, 20, kf3, 21, kf4)
71
72
73 -- For litter: put broom back 356
74 -- take broom out: 1874
75 -- swoop: 1878
76 -- Frames for plant states are
77 -- * healthy: 1950
78 -- * drooping1: 1951
79 -- * drooping2: 1952
80 -- * dying: 1953
81 -- * dead: 1954
82
83 -- TH uses rough 50 days between state transitions
84 -- to approximate that same average rate at ideal temperatures
85 local days_between_states = 64
86
87 -- days before we reannouncing our watering status if we were unreachable
88 local days_unreachable = 10
89
90 --! An `Object` which needs watering now and then.
91 class "Plant" (Object)
92
93 ---@type Plant
94 local Plant = _G["Plant"]
95
96 function Plant:Plant(hospital, object_type, x, y, direction, etc)
97 -- It doesn't matter which direction the plant is facing. It will be rotated so that an approaching
98 -- handyman uses the correct usage animation when appropriate.
99 self:Object(hospital, object_type, x, y, direction, etc)
100 self.current_state = 0
101 self.base_frame = self.th:getFrame()
102 self.days_left = days_between_states
103 self.unreachable = false
104 self.unreachable_counter = days_unreachable
105 self.phases = 5
106 end
107
108 --! Goes one step forward (or backward) in the states of the plant.
109 --!param restoring (boolean) If true the plant improves its health instead of drooping.
110 function Plant:setNextState(restoring)
111 if restoring then
112 if self.current_state > 0 then
113 self.current_state = self.current_state - 1
114 end
115 elseif self.current_state < self.phases - 1 then
116 self.current_state = self.current_state + 1
117 end
118
119 self.th:setFrame(self.base_frame + self.current_state)
120 end
121
122 local plant_restoring; plant_restoring = permanent"plant_restoring"( function(plant)
123 local phase = plant.phase
124 plant:setNextState(true)
125 if phase > 0 then
126 plant.phase = phase - 1
127 plant:setTimer(math.floor(14 / plant.cycles), plant_restoring)
128 else
129 plant.ticks = false
130 end
131 end)
132
133 --! Restores the plant to its initial state. (i.e. healthy)
134 function Plant:restoreToFullHealth()
135 self.ticks = true
136 self.phase = self.current_state
137 self.cycles = self.current_state
138 self:setTimer((self.direction == "south" or self.direction == "east") and 35 or 20, plant_restoring)
139 self.days_left = days_between_states
140
141 local taskIndex = self.hospital:getIndexOfTask(self.tile_x, self.tile_y, "watering")
142 if taskIndex ~= -1 then
143 self.hospital:removeHandymanTask(taskIndex, "watering")
144 end
145 end
146
147 --! Overridden since the plant animates slowly over time
148 function Plant:tick()
149 local timer = self.timer_time
150 if timer then
151 timer = timer - 1
152 if timer == 0 then
153 self.timer_time = nil
154 local timer_function = self.timer_function
155 self.timer_function = nil
156 timer_function(self)
157 else
158 self.timer_time = timer
159 end
160 end
161 end
162
163 --! Returns whether the plant is in need of watering right now.
164 function Plant:needsWatering()
165 return self.current_state ~= 0
166 end
167
168 --! When the plant needs water it periodically calls for a nearby handyman.
169 function Plant:callForWatering()
170 -- If self.ticks is true it means that a handyman is currently watering the plant.
171 -- If there are no tiles to water from, just die.
172 if not self.ticks then
173 if not self.unreachable then
174 local index = self.hospital:getIndexOfTask(self.tile_x, self.tile_y, "watering")
175 if index == -1 then
176 local call = self.world.dispatcher:callForWatering(self)
177 self.hospital:addHandymanTask(self, "watering", self.current_state + 1, self.tile_x, self.tile_y, call)
178 else
179 self.hospital:modifyHandymanTaskPriority(index, self.current_state + 1, "watering")
180 end
181 end
182
183 -- If very thirsty, make user aware of it.
184 if self.current_state > 1 and not self.plant_announced then
185 self.hospital:giveAdvice({ _A.warnings.plants_thirsty })
186 self.plant_announced = true
187 end
188 end
189 end
190
191 --! When a handyman is about to be summoned this function queues the complete set of actions necessary,
192 -- including entering and leaving any room involved. It also queues a meander action at the end.
193 -- Note that if there are more plants that need watering inside the room he will continue to water
194 -- those too before leaving.
195 --!param handyman (Staff) The handyman that is about to get the actions.
196 function Plant:createHandymanActions(handyman)
197 local this_room = self:getRoom()
198 local handyman_room = handyman:getRoom()
199 local ux, uy = self:getBestUsageTileXY(handyman.tile_x, handyman.tile_y)
200 if not ux or not uy then
201 -- The plant cannot be reached.
202 self.unreachable = true
203 self.unreachable_counter = days_unreachable
204 local index = self.hospital:getIndexOfTask(self.tile_x, self.tile_y, "watering")
205 if index ~= -1 then
206 self.hospital:removeHandymanTask(index, "watering")
207 end
208 -- Release Handyman
209 handyman:setCallCompleted()
210 if handyman_room then
211 handyman:setNextAction(handyman_room:createLeaveAction())
212 handyman:queueAction(MeanderAction())
213 else
214 handyman:setNextAction(MeanderAction())
215 end
216 return
217 end
218 self.reserved_for = handyman
219 local walk_action = WalkAction(ux, uy):setIsEntering(this_room and true or false)
220 if handyman_room and handyman_room ~= this_room then
221 handyman:setNextAction(handyman_room:createLeaveAction())
222 handyman:queueAction(walk_action)
223 else
224 handyman:setNextAction(walk_action)
225 end
226 handyman:queueAction(UseObjectAction(self):enableWateringPlant())
227 CallsDispatcher.queueCallCheckpointAction(handyman)
228 handyman:queueAction(AnswerCallAction())
229 end
230
231 --! When a handyman should go to the plant he should approach it from the
232 -- closest reachable tile within hospital buildings.
233 --!param from_x (integer) The x coordinate of tile to calculate from.
234 --!param from_y (integer) The y coordinate of tile to calculate from.
235 function Plant:getBestUsageTileXY(from_x, from_y)
236 local access_points = {{dx = 0, dy = 1, direction = "north"},
237 {dx = 0, dy = -1, direction = "south"},
238 {dx = -1, dy = 0, direction = "east"},
239 {dx = 1, dy = 0, direction = "west"}}
240 local shortest
241 local best_point = nil
242 local room_here = self:getRoom()
243 for _, point in ipairs(access_points) do
244 local dest_x, dest_y = self.tile_x + point.dx, self.tile_y + point.dy
245 local room_there = self.world:getRoom(dest_x, dest_y)
246 if room_here == room_there and self.hospital:isInHospital(dest_x, dest_y) then
247 local distance = self.world:getPathDistance(from_x, from_y, dest_x, dest_y)
248 if distance and (not best_point or shortest > distance) then
249 best_point = point
250 shortest = distance
251 end
252 end
253 end
254
255 if best_point then
256 self.direction = best_point.direction
257 return self.tile_x + best_point.dx, self.tile_y + best_point.dy
258 else
259 self.direction = "north"
260 return
261 end
262 end
263
264 --! Counts down to eventually let the plant droop.
265 function Plant:tickDay()
266 if not self.picked_up then
267 -- The plant will need water a little more often if it is hot where it is.
268 local temp = self.world.map.th:getCellTemperature(self.tile_x, self.tile_y)
269 self.days_left = self.days_left - (1 + temp)
270 if self.days_left < 1 then
271 self.days_left = days_between_states
272 self:setNextState()
273 elseif not self.reserved_for and self:needsWatering() and not self.unreachable then
274 self:callForWatering()
275 end
276 if self.unreachable then
277 self.unreachable_counter = self.unreachable_counter - 1
278 if self.unreachable_counter == 0 then
279 self.unreachable = false
280 end
281 end
282 end
283 end
284
285 --! The plant needs to retain its animation and reset its unreachable flag when being moved
286 function Plant:onClick(ui, button)
287 if button == "right" then
288 self.unreachable = false
289 self.picked_up = true
290 self.current_frame = self.base_frame + self.current_state
291 end
292 Object.onClick(self, ui, button)
293 end
294
295 --! Plant health/state should be used on evaluations of pleasantness
296 --! returns (integer) score of this plants health, 1 to 5 (best)
297 function Plant:isPleasingFactor()
298 return self.phases - self.current_state
299 end
300
301 --! Check if a plant is dying or about to start dying
302 function Plant:isDying()
303 if self.current_state ~= 0 or self.days_left < 3 then
304 return true
305 end
306 return false
307 end
308
309 function Plant:resetUsageAndReservaton()
310 local index = self.hospital:getIndexOfTask(self.tile_x, self.tile_y, "watering")
311 if index ~= -1 then
312 self.hospital:removeHandymanTask(index, "watering")
313 end
314 Object.resetUsageAndReservaton(self)
315 end
316
317 function Plant:afterLoad(old, new)
318 if old < 52 then
319 self.hospital = self.world:getLocalPlayerHospital()
320 end
321
322 if old < 150 then
323 self.phases = 5
324 end
325
326 Object.afterLoad(self, old, new)
327 end
328
329
330 return object