Debian Codegraph @

CorsixTH/Lua/humanoid_actions/use_object.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 TH = require("TH")
22
23
24 class "UseObjectAction" (HumanoidAction)
25
26 ---@type UseObjectAction
27 local UseObjectAction = _G["UseObjectAction"]
28
29 --! Construct a 'use object' action.
30 --!param object (Object) Object to use.
31 function UseObjectAction:UseObjectAction(object)
32 assert(class.is(object, Object), "Invalid value for parameter 'object'")
33
34 self:HumanoidAction("use_object")
35 self.object = object
36 self.watering_plant = false -- Whether the action is watering the plant.
37 self.prolonged_usage = nil -- If true, the usage is prolonged.
38 end
39
40 --! Set the 'watering plant' flag.
41 --!return (action) self, for daisy chaining.
42 function UseObjectAction:enableWateringPlant()
43 self.watering_plant = true
44 return self
45 end
46
47 --! Set prolonged usage of the object.
48 --!param prolonged (bool or nil) If set, enable prolonged usage of the object.
49 --!return (action) self, for daisy-chaining.
50 function UseObjectAction:setProlongedUsage(prolonged)
51 assert(prolonged == nil or type(prolonged) == "boolean",
52 "Invalid value for parameter 'prolonged'")
53
54 self.prolonged_usage = prolonged
55 return self
56 end
57
58 local orient_mirror = {
59 north = "west",
60 west = "north",
61 east = "south",
62 south = "east",
63 }
64
65 --! Find the next phase with an animation to show.
66 --!param action (UseObjectAction) The use_object action.
67 --!param phase The previous displayed phase or -100.
68 --!return The phase to show after the previous phase. Is 100 if there are no
69 -- phases with animations anymore.
70 local function action_use_next_phase(action, phase)
71 phase = phase + 1
72 if phase < -6 then
73 phase = -6
74 end
75 if phase == -6 and not action.do_walk then
76 phase = phase + 1
77 end
78 if phase == -5 and not action.anims.begin_use then
79 phase = phase + 1
80 end
81 if phase == -4 and not action.anims.begin_use_2 then
82 phase = phase + 1
83 end
84 if phase == -3 and not action.anims.begin_use_3 then
85 phase = phase + 1
86 end
87 if phase == -2 and not action.anims.begin_use_4 then
88 phase = phase + 1
89 end
90 if phase == -1 and not action.anims.begin_use_5 then
91 phase = phase + 1
92 end
93 if phase == 0 and not action.anims.in_use then
94 phase = phase + 1
95 end
96 if phase == 1 and not action.anims.finish_use then
97 phase = phase + 1
98 end
99 if phase == 2 and not action.anims.finish_use_2 then
100 phase = phase + 1
101 end
102 if phase == 3 and not action.anims.finish_use_3 then
103 phase = phase + 1
104 end
105 if phase == 4 and not action.anims.finish_use_4 then
106 phase = phase + 1
107 end
108 if phase == 5 and not action.anims.finish_use_5 then
109 phase = phase + 1
110 end
111 if phase == 6 and not action.do_walk or phase == 6 and action.destroy_user_after_use then
112 phase = phase + 1
113 end
114 if phase > 6 then
115 phase = 100
116 end
117 return phase
118 end
119
120 --! Compute and set the position of the animated humanoid from the footprint.
121 -- The humanoid does not move.
122 --!param action (UseObjectAction) Action being performed.
123 --!param humanoid (Humanoid) Person using the object.
124 local function setHumanoidTileSpeed(action, humanoid)
125 local object = action.object
126 local obj_orient = object.object_type.orientations[object.direction]
127
128 -- Decide the animation tile.
129 local tx, ty
130 if obj_orient.use_animate_from_use_position then
131 tx, ty = action.old_tile_x, action.old_tile_y
132 else
133 tx, ty = object:getRenderAttachTile()
134 end
135 if class.is(humanoid, Handyman) and
136 obj_orient.added_handyman_animate_offset_while_in_use then
137 tx = tx + obj_orient.added_handyman_animate_offset_while_in_use[1]
138 ty = ty + obj_orient.added_handyman_animate_offset_while_in_use[2]
139 end
140
141 -- Decide pixel offset.
142 local anim_offset = obj_orient.animation_offset
143 local added_offset = obj_orient.added_animation_offset_while_in_use or {0, 0}
144 local px = anim_offset[1] + added_offset[1]
145 local py = anim_offset[2] + added_offset[2]
146
147 humanoid:setTilePositionSpeed(tx, ty, px, py)
148 end
149
150 local action_use_object_tick
151
152 local function action_use_phase(action, humanoid, phase)
153 local object = action.object
154 humanoid.user_of = nil -- Temporary to avoid tile change warning
155 action.phase = phase
156
157 if phase == -6 then
158 HumanoidRawWalk(humanoid,
159 action.old_tile_x, action.old_tile_y,
160 action.new_tile_x, action.new_tile_y,
161 nil, action_use_object_tick)
162 return
163 elseif phase == 6 then
164 HumanoidRawWalk(humanoid,
165 action.new_tile_x, action.new_tile_y,
166 action.old_tile_x, action.old_tile_y,
167 nil, action_use_object_tick)
168 return
169 end
170
171 -- Get the animation table for the current phase.
172 local anim_table = action.anims.in_use -- phase 0
173 if phase == -5 then
174 anim_table = action.anims.begin_use
175 elseif phase == -4 then
176 anim_table = action.anims.begin_use_2
177 elseif phase == -3 then
178 anim_table = action.anims.begin_use_3
179 elseif phase == -2 then
180 anim_table = action.anims.begin_use_4
181 elseif phase == -1 then
182 anim_table = action.anims.begin_use_5
183 elseif phase == 1 then
184 anim_table = action.anims.finish_use
185 elseif phase == 2 then
186 anim_table = action.anims.finish_use_2
187 elseif phase == 3 then
188 anim_table = action.anims.finish_use_3
189 elseif phase == 4 then
190 anim_table = action.anims.finish_use_4
191 elseif phase == 5 then
192 anim_table = action.anims.finish_use_5
193 end
194
195 local anim = anim_table[humanoid.humanoid_class]
196 if not anim then
197 -- Handymen have their own number of animations.
198 if class.is(humanoid, Handyman) then
199 --action_use_phase(action, humanoid, action_use_next_phase(action, phase))
200 action_use_object_tick(humanoid)
201 return
202 else
203 error("No animation for " .. humanoid.humanoid_class .. " using " ..
204 object.object_type.id .. " facing " .. object.direction .. " phase " ..
205 phase)
206 end
207 end
208
209 -- Animation can be shown 'length' times rather than just once.
210 local anim_length = 1
211 if type(anim) == "table" and anim.length then
212 anim_length = anim.length
213 end
214
215 local is_list = false
216 if type(anim) == "table" and anim[1] ~= "morph" and #anim > 1 then
217 -- If an animation list is provided rather than a single animation, then
218 -- choose an animation from the list at random.
219 is_list = true
220 anim = anim[math.random(1, #anim)]
221 end
222
223 -- Handle various flags:
224 -- "mirror" to mirror a single animation (possibly for the second time).
225 -- "object_visible" whether the used object should be visible as well.
226 -- Default is to not show it.
227 local flags = action.mirror_flags
228 if type(anim) == "table" then
229 if anim.mirror then
230 -- a single animation may be (un-)mirrored, switch the mirror flag in that case
231 flags = flag_toggle(flags, DrawFlags.FlipHorizontal)
232 end
233 if anim.object_visible then
234 -- this flag may be set to make the (idle) object visible additionally to the usage animation
235 object.th:makeVisible()
236 else
237 object.th:makeInvisible()
238 end
239
240 -- If not morphing, grab the animation to show.
241 if anim[1] ~= "morph" then
242 anim = anim[1]
243 end
244 else
245 object.th:makeInvisible()
246 end
247
248 -- For split animations, add a Crop flag to the selected animations.
249 if object.split_anims then
250 flags = flags + DrawFlags.Crop
251 local anims = humanoid.world.anims
252 for i = 2, #object.split_anims do
253 local th = object.split_anims[i]
254 th:setAnimation(anims, anim, flags)
255 end
256 end
257
258 -- Morph from one animation to another.
259 if type(anim) == "table" and anim[1] == "morph" then
260 -- If a table with entries {"morph", A, B} is given rather than a single
261 -- animation, then display A first, then morph to B.
262 humanoid:setAnimation(anim[2], flags) -- A animation.
263 local morph_target = TH.animation()
264 local morph_flags = flags
265 if anim.mirror_morph then
266 morph_flags = flag_toggle(morph_flags, DrawFlags.FlipHorizontal)
267 end
268 morph_target:setAnimation(humanoid.world.anims, anim[3], morph_flags) -- B animation.
269 for layer, id in pairs(humanoid.layers) do
270 morph_target:setLayer(layer, id)
271 end
272 if anim.layers then
273 for layer, id in pairs(humanoid[anim.layers]) do
274 morph_target:setLayer(layer, id)
275 end
276 action.change_layers = humanoid[anim.layers] -- Rescue the layers for use afterwards.
277 end
278 humanoid.th:setMorph(morph_target, anim_length)
279 anim = anim[3]
280 else
281 humanoid:setAnimation(anim, flags)
282 end
283
284 -- Set position (and speed) of the humanoid, and make it the user of the object.
285 setHumanoidTileSpeed(action, humanoid)
286 humanoid.user_of = object
287
288 local frame_count = TheApp.animation_manager:getAnimLength(anim)
289 local action_anim_count = 1 -- Number of times to show 'in_use' animation for the action.
290 if action.min_length and phase == 0 then
291 -- 'action.min_length' is a frame count, convert to number of complete animations.
292 action_anim_count = math.floor((action.min_length + frame_count - 1) / frame_count)
293 end
294 -- Take the smallest number of of animations that satisfies both the object and
295 -- the action minimum count.
296 local length = math.max(action_anim_count, anim_length) * frame_count
297
298 -- A timer would be redundant in certain situations, so check it is needed
299 if phase ~= 0 or is_list or length ~= 1 or not action.prolonged_usage or
300 not action.on_interrupt or action.loop_callback then
301 humanoid:setTimer(length, action_use_object_tick)
302 end
303 end
304
305 --! Setup split animations by copying the humanoid layers and hit-test
306 -- to the other animations.
307 local function init_split_anims(object, humanoid)
308 if object.split_anims then
309 for i = 2, #object.split_anims do
310 local th = object.split_anims[i]
311 th:setLayersFrom(humanoid.th)
312 th:setHitTestResult(humanoid)
313 end
314 object.ticks = true
315 end
316 end
317
318 --! Cleanup function after usage.
319 local function finish_using(object, humanoid)
320 object:removeUser(humanoid) -- Disconnect the humanoid from the object.
321 humanoid.user_of = nil
322
323 -- Restore the layers and hit-test of the object, and set all animations
324 -- to the same frame.
325 if object.split_anims then
326 local anims = humanoid.world.anims
327 local anim = object.th:getAnimation()
328 local frame = object.th:getFrame()
329 local flags = object.th:getFlag()
330
331 for i = 2, #object.split_anims do
332 local th = object.split_anims[i]
333 th:setLayersFrom(object.th)
334 th:setHitTestResult(object)
335 th:setAnimation(anims, anim, flags)
336 th:setFrame(frame)
337 end
338
339 -- Restore ticks to default of the object.
340 object.ticks = object.object_type.ticks
341 end
342 end
343
344 --! Callback after an animation.
345 action_use_object_tick = permanent"action_use_object_tick"( function(humanoid)
346 local action = humanoid:getCurrentAction()
347 local object = action.object
348 local phase = action.phase
349 local oldphase = phase
350
351 -- walk_in phase done.
352 if oldphase == -6 then
353 object:setUser(humanoid)
354 humanoid.user_of = object
355 init_split_anims(object, humanoid)
356 if action.after_walk_in then
357 action:after_walk_in()
358 end
359 end
360
361 if phase ~= 0 or not action.prolonged_usage or not action.on_interrupt then
362 -- For the 'begin_use*', the 'finish_use*', and simple 'in_use' animations,
363 -- find to the next phase to animate.
364 phase = action_use_next_phase(action, phase)
365 elseif action.loop_callback then
366 action:loop_callback()
367 end
368
369 -- Apply the layers from before the morphed animation if available.
370 if action.change_layers then
371 for layer, id in pairs(action.change_layers) do
372 humanoid:setLayer(layer, id)
373 end
374 action.change_layers = nil
375 end
376
377 -- Except for possibly walking away from the object, all usage animations are done.
378 -- Perform cleanup.
379 if oldphase <= 5 and phase > 5 then
380 finish_using(object, humanoid)
381 end
382
383 if phase == 100 then
384 -- Any walking away from the object is done too.
385 humanoid:setTilePositionSpeed(action.old_tile_x, action.old_tile_y)
386
387 -- Check if the room is about to be destroyed
388 local room_destroyed = false
389 if object:isMachine() then
390 if not class.is(humanoid, Handyman) then
391 room_destroyed = object:machineUsed(humanoid:getRoom())
392 end
393 elseif object:getDynamicInfo() and not object.master then
394 -- Don't update if it is a slave object.
395 object:incrementUsedCount()
396 object:updateDynamicInfo()
397 end
398 if not room_destroyed then
399 -- Note that after_use is not called if the room has been destroyed!
400 -- In that case both the patient, staff member(s) and
401 -- the actual object are already dead.
402 if action.after_use then
403 action.after_use()
404 end
405
406 if action.destroy_user_after_use then
407 humanoid:despawn()
408 humanoid.world:destroyEntity(humanoid)
409 else
410 humanoid:finishAction(action)
411 end
412 end
413 else
414 -- Perform the next phase.
415 action_use_phase(action, humanoid, phase)
416 end
417 end)
418
419 local action_use_object_interrupt = permanent"action_use_object_interrupt"( function(action, humanoid, high_priority)
420 if not action.uninterruptible and high_priority then
421 local object = action.object
422 if humanoid.user_of then
423 finish_using(object, humanoid) -- Cleanup after usage.
424 elseif object:isReservedFor(humanoid) then
425 object:removeReservedUser(humanoid)
426 end
427 humanoid:setTimer(nil)
428 humanoid:setTilePositionSpeed(action.old_tile_x, action.old_tile_y)
429 humanoid:finishAction()
430 elseif not action.uninterruptible and not humanoid.timer_function then
431 humanoid:setTimer(1, action_use_object_tick)
432 end
433 -- Only patients can be vaccination candidates so no need to check
434 if humanoid.vaccination_candidate then
435 humanoid:removeVaccinationCandidateStatus()
436 end
437 end)
438
439 local function action_use_object_start(action, humanoid)
440 action.old_tile_x = humanoid.tile_x
441 action.old_tile_y = humanoid.tile_y
442 action.on_interrupt = action_use_object_interrupt
443 action.must_happen = true
444
445 local object = action.object
446 local orient = object.direction
447 local flags = 0
448 if not object.object_type.usage_animations[orient] then
449 orient = orient_mirror[orient]
450 flags = flags + DrawFlags.FlipHorizontal
451 end
452 local spec = object.object_type.orientations[object.direction]
453 -- early_list_while_in_use (if defined) will take precedence over early_list
454 if spec.early_list_while_in_use or (spec.early_list_while_in_use == nil and spec.early_list) then
455 flags = flags + DrawFlags.EarlyList
456 end
457
458 -- The handyman has his own place to be in
459 if spec.finish_use_position and not class.is(humanoid, Handyman) then
460 action.old_tile_x = object.tile_x + spec.finish_use_position[1]
461 action.old_tile_y = object.tile_y + spec.finish_use_position[2]
462 end
463
464 if spec.walk_in_tile then
465 action.new_tile_x = spec.walk_in_tile[1] + object.tile_x
466 action.new_tile_y = spec.walk_in_tile[2] + object.tile_y
467 else
468 action.new_tile_x = object.tile_x
469 action.new_tile_y = object.tile_y
470 end
471
472 local anims = object.object_type.usage_animations[orient]
473 action.anims = anims
474 action.mirror_flags = flags
475 if action.prolonged_usage == nil and anims.begin_use and
476 anims.in_use and anims.finish_use then
477 action.prolonged_usage = true
478 end
479
480 -- If walking in and walking out must be done, delay assigning the object to the humanoid.
481 if object.object_type.walk_in_to_use then
482 action.do_walk = true
483 else
484 object:setUser(humanoid)
485 humanoid.user_of = object
486 init_split_anims(object, humanoid)
487 end
488
489 if action.watering_plant then
490 -- Tell the plant to start restoring itself
491 object:restoreToFullHealth()
492 end
493
494 action_use_phase(action, humanoid, action_use_next_phase(action, -100))
495 end
496
497 return action_use_object_start