Debian Codegraph @

CorsixTH/Lua/entities/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 --! An `Entity` which occupies at least a single map tile and does not move.
24 class "Object" (Entity)
25
26 ---@type Object
27 local Object = _G["Object"]
28
29 local orient_mirror = {
30 north = "west",
31 west = "north",
32 east = "south",
33 south = "east",
34 }
35
36 function Object:Object(hospital, object_type, x, y, direction, etc)
37 assert(class.is(hospital, Hospital), "First argument is not a Hospital instance.")
38
39 local th = TH.animation()
40 self:Entity(th)
41
42 if etc == "map object" then
43 if direction % 2 == 0 then
44 direction = "north"
45 else
46 direction = "west"
47 end
48 end
49
50 self.ticks = object_type.ticks
51 self.object_type = object_type
52 self.hospital = hospital
53 self.world = hospital.world
54 self.user = false
55 self.times_used = 0
56 self:updateDynamicInfo()
57 self:initOrientation(direction)
58 self:setTile(x, y)
59 end
60
61 --! Initializes the footprint, finds out what to draw and checks for
62 -- split animations.
63 --!param direction The orientation in which the object is facing.
64 function Object:initOrientation(direction)
65 self.direction = direction
66 local object_type = self.object_type
67
68 -- Decide the animation and the draw flags for it.
69 local flags = 0
70 local anim = object_type.idle_animations[direction]
71 if not anim then
72 anim = object_type.idle_animations[orient_mirror[direction]]
73 flags = DrawFlags.FlipHorizontal
74 end
75 local footprint = object_type.orientations
76 footprint = footprint and footprint[direction]
77 if footprint and footprint.early_list then
78 flags = flags + DrawFlags.EarlyList
79 end
80
81 local rap = footprint and footprint.render_attach_position
82 if rap and rap[1] and type(rap[1]) == "table" then
83 self.split_anims = {self.th}
84 self.split_anim_positions = rap
85 self.th:setCrop(rap[1].column)
86 for i = 2, #rap do
87 local point = rap[i]
88 local th = TH.animation()
89 th:setCrop(point.column)
90 th:setHitTestResult(self)
91 th:setPosition(Map:WorldToScreen(1-point[1], 1-point[2]))
92 self.split_anims[i] = th
93 end
94 else
95 -- Make sure these variables aren't left behind. The object
96 -- might just have been moved and rotated.
97 self.split_anims = nil
98 self.split_anim_positions = nil
99 end
100 if footprint and footprint.animation_offset then
101 self:setPosition(unpack(footprint.animation_offset))
102 end
103 footprint = footprint and footprint.footprint
104 if footprint then
105 self.footprint = footprint
106 else
107 self.footprint = nil
108 end
109 self:setAnimation(anim, flags)
110 end
111
112 --! Add methods to a class for creating and controlling a slave object
113 function Object.slaveMixinClass(class_method_table)
114 local name = class.name(class_method_table)
115 local super = class.superclass(class_method_table)
116 local super_constructor = super[class.name(super)]
117
118 -- Constructor
119 class_method_table[name] = function(self, hospital, object_type, x, y, direction, ...)
120 super_constructor(self, hospital, object_type, x, y, direction, ...)
121 if object_type.slave_id then
122 local orientation = object_type.orientations
123 orientation = orientation and orientation[direction]
124 if orientation.slave_position then
125 x = x + orientation.slave_position[1]
126 y = y + orientation.slave_position[2]
127 end
128 self.slave = hospital.world:newObject(object_type.slave_id, x, y, direction, ...)
129 self.slave.master = self
130 end
131 end
132
133 -- Slave -> Master redirects
134 local function slave_to_master(method)
135 local super_method = super[method]
136 class_method_table[method] = function(self, ...)
137 local master = self.master
138 if master then
139 return master[method](master, ...)
140 else
141 return super_method(self, ...)
142 end
143 end
144 end
145 slave_to_master("onClick")
146 slave_to_master("updateDynamicInfo")
147 slave_to_master("getDynamicInfo")
148
149 -- Master -> Slave notifications
150 local function master_to_slave(method)
151 local super_method = super[method]
152 class_method_table[method] = function(self, ...)
153 local slave = self.slave
154 if slave then
155 slave[method](slave, ...)
156 end
157 return super_method(self, ...)
158 end
159 end
160 master_to_slave("initOrientation")
161 class_method_table.onDestroy = function(self, ...)
162 local slave = self.slave
163 if slave then
164 self.world:destroyEntity(slave)
165 end
166 return super.onDestroy(self, ...)
167 end
168 class_method_table.setTile = function(self, x, y)
169 if self.slave then
170 local dx, dy = 0, 0
171 local orientation = self.object_type.orientations
172 orientation = orientation and orientation[self.direction]
173 if orientation.slave_position then
174 dx = orientation.slave_position[1]
175 dy = orientation.slave_position[2]
176 end
177 if x then
178 self.slave:setTile(x + dx, y + dy)
179 else
180 self.slave:setTile(x, y)
181 end
182 end
183 return super.setTile(self, x, y)
184 end
185
186 return slave_to_master, master_to_slave
187 end
188
189 function Object:tick()
190 if self.split_anims then
191 if self.num_animation_ticks then
192 for i = 2, #self.split_anims do
193 local th = self.split_anims[i]
194 for _ = 1, self.num_animation_ticks do
195 th:tick()
196 end
197 end
198 else
199 for i = 2, #self.split_anims do
200 self.split_anims[i]:tick()
201 end
202 end
203 end
204 return Entity.tick(self)
205 end
206
207 function Object:setPosition(x, y)
208 if self.split_anims then
209 -- The given position is for the primary tile, so position non-primary
210 -- animations relative to the primary one.
211 local bx, by = unpack(self.split_anim_positions[1])
212 for i, th in ipairs(self.split_anims) do
213 local point = self.split_anim_positions[i]
214 local dx, dy = Map:WorldToScreen(1-point[1]+bx, 1-point[2]+by)
215 th:setPosition(x + dx, y + dy)
216 end
217 else
218 self.th:setPosition(x, y)
219 end
220 return self
221 end
222
223 function Object:setAnimation(animation, flags)
224 if self.split_anims then
225 flags = (flags or 0) + DrawFlags.Crop
226 if self.permanent_flags then
227 flags = flags + self.permanent_flags
228 end
229 if animation ~= self.animation_idx or flags ~= self.animation_flags then
230 self.animation_idx = animation
231 self.animation_flags = flags
232 local anims = self.world.anims
233 for _, th in ipairs(self.split_anims) do
234 th:setAnimation(anims, animation, flags)
235 end
236 end
237 return self
238 else
239 return Entity.setAnimation(self, animation, flags)
240 end
241 end
242
243 --! Get the primary tile which the object is attached to for rendering
244 --[[! For objects which attach to a single tile for rendering, this method will
245 return the X and Y Lua world coordinates of that tile. For objects which split
246 their rendering over multiple tiles, one of them is arbitrarily designated as
247 the primary tile, and its coordinates are returned.
248 ]]
249 function Object:getRenderAttachTile()
250 local x, y = self.tile_x, self.tile_y
251 local offset = self.object_type.orientations
252 if x and offset then
253 offset = offset[self.direction].render_attach_position
254 if self.split_anims then
255 offset = offset[1]
256 end
257 x = x + offset[1]
258 y = y + offset[2]
259 end
260 return x, y
261 end
262
263 --! Call on object used. After object used increment use values accordingly.
264 function Object:incrementUsedCount()
265 self.times_used = self.times_used + 1
266 end
267
268 --! Updates the object's dynamic info
269 function Object:updateDynamicInfo()
270 local object = self.object_type
271 if object.dynamic_info then
272 self:setDynamicInfo("text", {object.name, "", _S.dynamic_info.object.times_used:format(self.times_used)})
273 end
274 end
275
276 --! This function returns a tiles offset coordinates for
277 --! object use position
278 --! param usage_position_name (string) usage position name
279 --! return (table) table with {x, y, usage_position_name} table
280 function Object:_getUsageTile(usage_position_name)
281 local object_x, object_y = self.tile_x, self.tile_y
282 local orientations = self.object_type.orientations
283 if orientations then
284 local object_layout = orientations[self.direction]
285 return Object:getXYforUsePosition(object_x, object_y, object_layout, usage_position_name)
286 else
287 return nil
288 end
289 end
290
291 --! This function returns an absolute world coordinates for use position.
292 --! param object_x (integer) world tile x coordinate.
293 --! param object_y (integer) world tile y coordinate.
294 --! param object_layout (table) object orientation description with footprint.
295 --! param usage_position_name (string) usage position name.
296 --! return (array) array with '{x, y, usage_position_name}' tables inside.
297 function Object:getXYforUsePosition(object_x, object_y, object_layout, usage_position_name)
298 local usage_tile_offset = object_layout[usage_position_name]
299 if usage_tile_offset then
300 local first = usage_tile_offset[1]
301 local second = usage_tile_offset[2]
302 if type(first) == "table" then -- for 'handyman_position' in 'cast_remover' case as it store table
303 local result = {}
304 for _, xy in ipairs(usage_tile_offset) do
305 local x = object_x + xy[1]
306 local y = object_y + xy[2]
307 table.insert(result, {x, y, usage_position_name})
308 end
309 return result
310 else -- for other regular cases
311 local x = object_x + first
312 local y = object_y + second
313 return {{x, y, usage_position_name}}
314 end
315 else
316 return nil
317 end
318 end
319
320 --! This function returns an array with use positions names
321 --! return (array) use positions names
322 function Object:usePositionNames()
323 return {
324 "use_position",
325 "use_position_secondary",
326 "slave_position",
327 "finish_use_position",
328 "finish_use_position_secondary",
329 "handyman_position",
330 }
331 end
332
333 --! This function returns a table with tiles offset coordinates for
334 --! object use positions
335 --! return (table) use positions offsets
336 function Object:getAllUsageTiles()
337 local result_table = {}
338 local object_use_positions_names = Object:usePositionNames()
339 for key, use_position_name in pairs(object_use_positions_names) do
340 local usage_tile = self:_getUsageTile(use_position_name)
341 result_table = table_merge(result_table, usage_tile)
342 end
343 return result_table
344 end
345
346 --! This function returns tile offset coordinate for "use_position_secondary"
347 --! return (int, int) x, y tile offset
348 function Object:getSecondaryUsageTile()
349 local secondary_usage_tile = self:_getUsageTile("use_position_secondary")
350 if secondary_usage_tile then
351 local xy = secondary_usage_tile[1]
352 return xy[1], xy[2]
353 else
354 return nil, nil
355 end
356 end
357
358 -- This function returns a list of all "only_passable" tiles belonging to an object.
359 -- It must be overridden by objects which do not have a footprint, but walkable tiles (e.g. doors of any kind)
360 function Object:getWalkableTiles()
361 local tiles = {}
362 for _, xy in ipairs(self.footprint) do
363 if xy.only_passable then
364 tiles[#tiles + 1] = { self.tile_x + xy[1], self.tile_y + xy[2] }
365 end
366 end
367 return tiles
368 end
369
370 local opposite_flags = {
371 travelNorth = "travelSouth",
372 travelSouth = "travelNorth",
373 travelEast = "travelWest",
374 travelWest = "travelEast"
375 }
376
377 --! The function returns the direction opposite to the one passed as a parameter.
378 --! For North, it returns South, and so on.
379 --! param passable_flag (string) 'passable_flag' that we want to invert.
380 --! return (string) inverted 'passable_flag'.
381 function Object.getComplementaryPassableFlag(passable_flag)
382 return opposite_flags[passable_flag]
383 end
384
385 function Object.directionParameters()
386 return
387 {
388 north = { x = 0, y = -1, buildable_flag = "buildableNorth", passable_flag = "travelNorth", needed_side = "need_north_side"},
389 east = { x = 1, y = 0, buildable_flag = "buildableEast", passable_flag = "travelEast", needed_side = "need_east_side"},
390 south = { x = 0, y = 1, buildable_flag = "buildableSouth", passable_flag = "travelSouth", needed_side = "need_south_side"},
391 west = { x = -1, y = 0, buildable_flag = "buildableWest", passable_flag = "travelWest", needed_side = "need_west_side"}
392 }
393 end
394
395 function Object:setTile(x, y)
396 -- Are there any tiles previously occupied by this object?
397 if self.tile_x ~= nil then
398 -- Deoccupy object tiles
399 self:deoccupyTilesByObjectFootprintAt(x, y)
400 end
401
402 local entity_map = self.world.entity_map
403 -- Remove the reference to the object at it's previous coordinates
404 if entity_map then
405 entity_map:removeEntity(self.tile_x,self.tile_y, self)
406 end
407
408 -- Update the objects coordinates
409 self.tile_x = x
410 self.tile_y = y
411
412 -- Update the entity map for the new position
413 if entity_map then
414 entity_map:addEntity(x,y,self)
415 end
416
417 if x then
418 -- Occupy tiles by the object
419 self:occupyTilesByObjectFootprintAt(x, y)
420
421 if self.split_anims then
422 -- Draw corresponding object animations
423 local map = self.world.map.th
424 local pos = self.split_anim_positions
425 for i = 2, #self.split_anims do
426 self.split_anims[i]:setTile(map, x + pos[i][1], y + pos[i][2],
427 self:getDrawingLayer())
428 end
429 end
430 else
431 self.th:setTile(nil)
432
433 if self.split_anims then
434 -- Clear corresponding object animations
435 for i = 2, #self.split_anims do
436 self.split_anims[i]:setTile(nil)
437 end
438 end
439 end
440 self.world:clearCaches()
441 return self
442 end
443
444 local function coordinatesAreInFootprint(object_footprint, xpos, ypos)
445 for _, xy in ipairs(object_footprint) do
446 if xy[1] == xpos and xy[2] == ypos then
447 return true
448 end
449 end
450 return false
451 end
452
453 local function setPassableFlags(map, passable_flag, xpos, ypos, next_x, next_y, value)
454 local flags1 = {}
455 flags1[passable_flag] = value
456 map.th:setCellFlags(xpos, ypos, flags1)
457 local flags2 = {}
458 flags2[Object.getComplementaryPassableFlag(passable_flag)] = value
459 map.th:setCellFlags(next_x, next_y, flags2)
460 end
461
462 --! Set cell flags on the tiles that fall under this object's footprint as occupied.
463 --! param x (integer) target tile x coordinate.
464 --! param y (integer) target tile y coordinate.
465 function Object:occupyTilesByObjectFootprintAt(x, y)
466 local direction_parameters = Object.directionParameters()
467 local direction = self.direction
468 -- 50 is a trash bin thob
469 if self.object_type.thob == 50 and direction == "east" then
470 direction = "west"
471 end
472
473 local ra_x, ra_y = self:getRenderAttachTile()
474 self.th:setTile(self.world.map.th, ra_x, ra_y, self:getDrawingLayer())
475 self.world:addObjectToTile(self, x, y)
476 if self.footprint then
477 local map = self.world.map.th
478 local optional_found = false
479 local room = self.world:getRoom(x, y)
480 local roomId = room and room.id
481 local next_tile_x, next_tile_y = x,y
482 local passable_flag
483
484 for _, xy in ipairs(self.footprint) do
485 local change_flags = true
486 local flags_to_set = {}
487 local lx = x + xy[1]
488 local ly = y + xy[2]
489 local flags
490
491 if xy.optional then
492 if optional_found then
493 -- An optional tile has been accepted, we don't need anymore such tiles.
494 change_flags = false
495 else
496 flags = map:getCellFlags(lx, ly)
497 local is_object_allowed = true
498 if roomId and flags.roomId ~= roomId then
499 is_object_allowed = false
500 elseif xy.only_passable and not self.world.pathfinder:isReachableFromHospital(lx, ly) then
501 is_object_allowed = false
502 end
503
504 if is_object_allowed then
505 change_flags = true
506 optional_found = true
507 else
508 change_flags = false
509 end
510 end
511 end
512
513 flags = map:getCellFlags(lx, ly)
514 if xy.only_side then
515 local par = direction_parameters[direction]
516 flags_to_set[par["buildable_flag"]] = false
517 passable_flag, next_tile_x, next_tile_y = par["passable_flag"], x + par["x"], y + par["y"]
518 else
519 for _, value in pairs(direction_parameters) do
520 if coordinatesAreInFootprint(self.footprint, xy[1] + value["x"], xy[2] + value["y"]) or
521 xy.complete_cell or xy[value["needed_side"]] then
522 if map:getCellFlags(x, y, flags)[value["buildable_flag"]] == 0 then
523 change_flags = false
524 end
525 flags_to_set[value["buildable_flag"]] = false
526 end
527 end
528 end
529
530 if change_flags then
531 if not xy.only_side then
532 map:setCellFlags(lx, ly, {
533 buildable = false,
534 passable = not not xy.only_passable,
535 })
536 end
537 if not isTableEmpty(flags_to_set) then
538 map:setCellFlags(lx, ly, flags_to_set)
539 end
540 if xy.only_side then
541 if map:getCellFlags(lx, ly)[passable_flag] == true then
542 self.set_passable_flags = true
543 setPassableFlags(self.world.map, passable_flag, lx, ly, next_tile_x, next_tile_y, false)
544 end
545 end
546 end
547 end
548 end
549 end
550
551 --! Set cell flags on the tiles that fall under this object's footprint as unoccupied.
552 --! param x (integer) target tile x coordinate.
553 --! param y (integer) target tile y coordinate.
554 function Object:deoccupyTilesByObjectFootprintAt(x, y)
555 local direction_parameters = Object.directionParameters()
556 local direction = self.direction
557 -- 50 is a trash bin thob
558 if self.object_type.thob == 50 and direction == "east" then
559 direction = "west"
560 end
561
562 self.world:removeObjectFromTile(self, self.tile_x, self.tile_y)
563 if self.footprint then
564 local map = self.world.map.th
565 for _, xy in ipairs(self.footprint) do
566 local xpos, ypos = self.tile_x + xy[1], self.tile_y + xy[2]
567
568 if xy.only_side then
569 if self.set_passable_flags then
570 self.set_passable_flags = nil
571 local par = direction_parameters[direction]
572 local passableFlag, next_tile_x, next_tile_y = par.passable_flag, xpos + par.x, ypos + par.y
573 setPassableFlags(self.world.map, passableFlag, xpos, ypos, next_tile_x, next_tile_y, true)
574 end
575 local flags_to_set= {}
576 flags_to_set[direction_parameters[direction].buildable_flag] = true
577 map:setCellFlags(xpos, ypos, flags_to_set)
578 else
579 local flags_to_set = {}
580 for _, value in pairs(direction_parameters) do
581 if coordinatesAreInFootprint(self.footprint, xy[1] + value.x, xy[2] + value.y) or
582 xy.complete_cell or xy[value.needed_side] then
583 flags_to_set[value.buildable_flag] = true
584 end
585 end
586
587 if not isTableEmpty(flags_to_set) then
588 map:setCellFlags(xpos, ypos, flags_to_set)
589 end
590 if not map:getCellFlags(xpos, ypos).passable then
591 map:setCellFlags(xpos, ypos, {
592 buildable = true,
593 passable = true,
594 })
595 else
596 -- passable tiles can "belong" to multiple objects, so we have to check that
597 if not self.world:isTilePartOfNearbyObject(xpos, ypos, 10) then
598 -- assumption: no object defines a passable tile further than 10 tiles away from its origin
599 map:setCellFlags(xpos, ypos, {
600 buildable = true,
601 })
602 end
603 end
604 end
605 end
606 end
607 end
608
609 --! Set object invisible state.
610 --! param invisible (bool) 'true' to make the object invisible,
611 -- 'false' to make the object visible.
612 function Object:setInvisible(invisible)
613 if self.split_anims then
614 for _, th_anim in ipairs(self.split_anims) do
615 if invisible then
616 th_anim:makeInvisible()
617 else
618 th_anim:makeVisible()
619 end
620 end
621 else
622 if invisible then
623 self.th:makeInvisible()
624 else
625 self.th:makeVisible()
626 end
627 end
628 end
629
630 -- Sets the user of this object to the given user. Note that if multiple_users_allowed
631 -- is set to true for this object "user" will always be the last added user to this object.
632 -- Please do not modify object.user directly.
633 --!param user The user that is about to use this object.
634 function Object:setUser(user)
635 assert(user, "setUser: Expected a user, got nil") -- It makes no sense to add a nil value
636 self.user = user
637 if self.object_type.multiple_users_allowed then
638 if not self.user_list then
639 self.user_list = {}
640 end
641 table.insert(self.user_list, user)
642 end
643 self.th:makeInvisible()
644 self:removeReservedUser(user)
645 end
646
647 -- Removes a user from the set of users if multiple_users_allowed is set to true for this
648 -- object. Otherwise it sets "user" to nil. Please don't directly change the variable "user".
649 function Object:removeUser(user)
650 if self.object_type.multiple_users_allowed then
651 if not user or not self.user_list then
652 -- No user specified, empty the list; or the list didn't exist
653 self.user_list = {}
654 end
655 local found = false
656 for i, users in ipairs(self.user_list) do
657 if users == user then
658 table.remove(self.user_list, i)
659 found = true
660 break
661 end
662 end
663 if not found then
664 self.world:gameLog("Warning: Could not find a humanoid to remove from the user list")
665 end
666 if #self.user_list == 0 then
667 -- No users left, make the object visible again.
668 self.user = nil
669 self.th:makeVisible()
670 end
671 else
672 self.user = nil
673 self.th:makeVisible()
674 end
675 end
676
677 -- Adds a reserved user for this object. If multiple_users_allowed is set there can be
678 -- many users reserving the object. reserved_for will then be the user who most recently
679 -- reserved this object.
680 function Object:addReservedUser(user)
681 assert(user, "Expected a user, got nil") -- It makes no sense to add a nil value
682 if self.object_type.multiple_users_allowed then
683 if not self.reserved_for_list then
684 self.reserved_for_list = {}
685 end
686 table.insert(self.reserved_for_list, user)
687 else
688 assert(not self.reserved_for or user ~= self.reserved_for, "Object already reserved for another user")
689 end
690 self.reserved_for = user
691 end
692
693 -- If multiple_users_allowed is true: Removes the user specified from this object's list of reserved users.
694 -- If the argument is nil it is assumed that the list should be emptied.
695 -- Note that if there are many humanoids reserving this object reserved_for might still be set after a
696 -- call to this function.
697 -- Otherwise: sets reserved_for to nil.
698 function Object:removeReservedUser(user)
699 if self.object_type.multiple_users_allowed then
700 -- No user specified, delete the whole list; or no list found, make it.
701 if not user or not self.reserved_for_list then
702 self.reserved_for_list = {}
703 end
704 local found = false
705 for i, users in ipairs(self.reserved_for_list) do
706 if users == user then
707 table.remove(self.reserved_for_list, i)
708 found = true
709 break
710 end
711 end
712 if not found then
713 self.world:gameLog("Warning: Could not find a humanoid to remove from the reserved user list")
714 end
715 if #self.reserved_for_list == 0 then
716 -- No users left, set reserved_for to nil.
717 self.reserved_for = nil
718 end
719 else
720 self.reserved_for = nil
721 end
722 end
723
724 -- Checks whether the object is reserved for the specified user.
725 -- If the argument is nil a check for any reserved user is done.
726 function Object:isReservedFor(user)
727 if not user then
728 if self.reserved_for_list then
729 return #self.reserved_for_list > 0
730 else
731 return not not self.reserved_for
732 end
733 end
734 if self.reserved_for == user then -- "Normal" use
735 return true
736 end
737 if self.object_type.multiple_users_allowed then
738 if not self.reserved_for_list then
739 self.reserved_for_list = {}
740 end
741 for _, users in ipairs(self.reserved_for_list) do
742 if users == user then
743 return true
744 end
745 end
746 end
747 return false
748 end
749
750 --! This function is for overriding it in the inheriting Machine class.
751 function Object:isMachine()
752 return false
753 end
754
755 --[[ Called when the object is clicked
756 !param ui (UI) The active ui.
757 !param button (string) Which button was clicked.
758 !param data (table) If some data should be retained after moving an object it is in this table.
759 ]]
760 function Object:onClick(ui, button, data)
761 local window = ui:getWindow(UIEditRoom)
762 -- Flag 'in_pickup_mode' can be used if for example some things
763 -- should only happen as long as the object is not picked up.
764 if button == "right" or (button == "left" and window and window.in_pickup_mode) then
765 local room = self:getRoom()
766 window = window and window.visible and window
767
768 if (not room and window) then
769 return
770 end
771
772 if (not room and not self.object_type.corridor_object) then
773 -- Trying to pickup an object in corridor not intended for pickup (sliding doors of the hospital).
774 return
775 end
776
777 if (room and not (window and window.room == room) and not self.object_type.corridor_object) then
778 -- Trying to pickup an object key to a room (sofa, machine), but not the room we're editing.
779 return
780 end
781
782 local fullscreen = ui:getWindow(UIFullscreen)
783 if fullscreen then
784 fullscreen:close()
785 end
786
787 if self.object_type.class == "Plant" or self.object_type.class == "Machine" then
788 -- remove the task associated with this object
789 local taskType = "watering"
790 if self.object_type.class == "Machine" then
791 taskType = "repairing"
792 end
793 local task_index = self.hospital:getIndexOfTask(self.tile_x, self.tile_y, taskType)
794 local task_exist = task_index ~= -1
795 if task_exist then
796 self.hospital:removeHandymanTask(task_index, taskType)
797 end
798 end
799
800 local room_editing_mode = window ~= nil
801 local initial_orientation = self.direction
802 local object_list = {{object = self.object_type, qty = 1, existing_object = self}}
803
804 self.picked_up = true
805 if not room_editing_mode then
806 self:onPickUp()
807 self:setInvisible(true)
808 window = UIPlaceObjects(ui, object_list, false) -- don't pay for
809 ui:addWindow(window)
810 else
811 -- NB: the object has to be destroyed before updating/creating the window,
812 -- or the blueprint will be wrong
813 self.world:destroyEntity(self)
814 window:stopPickupItems()
815 window:addObjects(object_list, false) -- don't pay for
816 window:selectObjectType(self.object_type)
817 window:checkEnableConfirm() -- since we removed an object from the room, the requirements may not be met anymore
818 end
819
820 -- blueprint orientation should be as orientation was on object pickup
821 window:setOrientation(initial_orientation)
822 self.orientation_before = initial_orientation
823 ui:playSound("pickup.wav")
824 end
825 end
826
827 function Object:eraseObject()
828 self.world.map.th:eraseObjectTypes(self.tile_x, self.tile_y)
829 end
830
831 function Object:resetAnimation()
832 self.world.map.th:setCellFlags(self.tile_x, self.tile_y, {thob = self.object_type.thob})
833 local ra_x, ra_y = self:getRenderAttachTile()
834 self.th:setTile(self.world.map.th, ra_x, ra_y, self:getDrawingLayer())
835 end
836
837 function Object:onDestroy()
838 -- Remove from room
839 local room = self:getRoom()
840 if room then
841 room.objects[self] = nil
842 end
843
844 self:resetUsageAndReservaton()
845 Entity.onDestroy(self)
846
847 self:rebuildPassableCellFlags()
848 end
849
850 function Object:rebuildPassableCellFlags()
851 -- Issue 1105 - rebuild wall travel<dir> and pathfinding on side object removal
852 if self.object_type.class == "SideObject" then
853 self.world.map.th:updatePathfinding()
854 self.world:resetSideObjects()
855 end
856 end
857
858 function Object:onPickUp()
859 self:resetUsageAndReservaton()
860 Entity.onPickUp(self)
861 end
862
863 function Object:resetUsageAndReservaton()
864 self:cancelUsage()
865 self:denyReservation()
866 end
867
868 function Object:cancelUsage()
869 if self.user_list then
870 for _, user in ipairs(self.user_list) do
871 user:handleRemovedObject(self)
872 end
873 self.user_list = {}
874 elseif self.user then
875 self.user:handleRemovedObject(self)
876 end
877 self.user = nil
878 end
879
880 function Object:denyReservation()
881 if self.reserved_for_list then
882 for _, reserver in ipairs(self.reserved_for_list) do
883 reserver:handleRemovedObject(self)
884 end
885 self.reserved_for_list = {}
886 elseif self.reserved_for then
887 self.reserved_for:handleRemovedObject(self)
888 end
889 self.reserved_for = nil
890 end
891
892 function Object:afterLoad(old, new)
893 if old < 52 then
894 self.hospital = self.world:getLocalPlayerHospital()
895 end
896 if old < 57 then
897 if self.footprint and self.direction then
898 local object_type = self.object_type
899 local footprint = object_type.orientations
900 footprint = footprint and footprint[self.direction]
901 if footprint and footprint.animation_offset then
902 self:setPosition(unpack(footprint.animation_offset))
903 end
904 footprint = footprint and footprint.footprint
905 self.footprint = footprint
906 if object_type.class == "SideObject" then
907 local flags = {buildable = true}
908 self.world.map.th:setCellFlags(self.tile_x, self.tile_y, flags)
909 end
910 self:setTile(self.tile_x, self.tile_y)
911 end
912 end
913 if old < 173 then
914 -- Fix bug with couch not being fully passable
915 if self.object_type.id == "couch" then
916 self:initOrientation(self.direction)
917 self:setTile(self.tile_x, self.tile_y)
918 end
919 end
920 self:updateDynamicInfo()
921 return Entity.afterLoad(self, old, new)
922 end
923
924 local all_pathfind_dirs = {[0] = true, [1] = true, [2] = true, [3] = true}
925
926 function Object.processTypeDefinition(object_type)
927 if object_type.id == "extinguisher" or object_type.id == "radiator" or
928 object_type.id == "plant" or object_type.id == "reception_desk" or
929 object_type.id == "bench" then
930 object_type.count_category = object_type.id
931 elseif object_type.id ~= "bin" and not object_type.corridor_object and
932 not object_type.id:find("door") then
933 object_type.count_category = "general"
934 end
935 if object_type.orientations then
936 for _, details in pairs(object_type.orientations) do
937 -- Set default values
938 if not details.animation_offset then
939 details.animation_offset = {0, 0}
940 end
941 if not details.render_attach_position then
942 details.render_attach_position = {0, 0}
943 end
944 -- Set the usage position
945 if details.use_position == "passable" then
946 -- "passable" => the *first* passable tile in the footprint list
947 for _, point in pairs(details.footprint) do
948 if point.only_passable then
949 details.use_position = {point[1], point[2]}
950 break
951 end
952 end
953 elseif not details.use_position then
954 details.use_position = {0, 0}
955 end
956 -- Set handyman repair tile
957 if object_type.default_strength and not details.handyman_position then
958 details.handyman_position = details.use_position
959 end
960 -- Find the nearest solid tile in the footprint to the usage position
961 local use_position = details.use_position
962 local solid_near_use_position
963 local solid_near_use_position_d = 10000
964 for _, point in pairs(details.footprint) do repeat
965 if point.only_passable then
966 break -- continue
967 end
968 local d = (point[1] - use_position[1])^2 + (point[2] - use_position[2])^2
969 if d >= solid_near_use_position_d then
970 break -- continue
971 end
972 solid_near_use_position = point
973 solid_near_use_position_d = d
974 until true end
975 if solid_near_use_position_d ~= 1 then
976 details.pathfind_allowed_dirs = all_pathfind_dirs
977 else
978 if use_position[1] < solid_near_use_position[1] then
979 details.pathfind_allowed_dirs = {[1] = true}
980 elseif use_position[1] > solid_near_use_position[1] then
981 details.pathfind_allowed_dirs = {[3] = true}
982 elseif use_position[2] < solid_near_use_position[2] then
983 details.pathfind_allowed_dirs = {[2] = true}
984 else
985 details.pathfind_allowed_dirs = {[0] = true}
986 end
987 end
988 -- Adjust the footprint to make this tile the origin
989 local solid_points = {}
990 if solid_near_use_position then
991 local x, y = unpack(solid_near_use_position)
992 for _, point in pairs(details.footprint) do
993 point[1] = point[1] - x
994 point[2] = point[2] - y
995 if not point.only_passable then
996 solid_points[point[1] * 100 + point[2]] = point
997 end
998 end
999 for _, key in ipairs({"use_position_secondary",
1000 "finish_use_position",
1001 "finish_use_position_secondary",
1002 "smoke_position"}) do
1003 if details[key] then
1004 details[key][1] = details[key][1] - x
1005 details[key][2] = details[key][2] - y
1006 end
1007 end
1008 use_position[1] = use_position[1] - x
1009 use_position[2] = use_position[2] - y
1010 if details.slave_position then
1011 details.slave_position[1] = details.slave_position[1] - x
1012 details.slave_position[2] = details.slave_position[2] - y
1013 end
1014 local rx, ry = unpack(details.render_attach_position)
1015 if type(rx) == "table" then
1016 rx, ry = unpack(details.render_attach_position[1])
1017 for _, point in ipairs(details.render_attach_position) do
1018 point.column = point[1] - point[2]
1019 point[1] = point[1] - x
1020 point[2] = point[2] - y
1021 end
1022 else
1023 details.render_attach_position[1] = rx - x
1024 details.render_attach_position[2] = ry - y
1025 end
1026 x, y = Map:WorldToScreen(rx + 1, ry + 1)
1027 details.animation_offset[1] = details.animation_offset[1] - x
1028 details.animation_offset[2] = details.animation_offset[2] - y
1029 end
1030 -- Find the region around the solid part of the footprint
1031 local adjacent_set = {}
1032 local adjacent_list = {}
1033 details.adjacent_to_solid_footprint = adjacent_list
1034 for _, point in pairs(solid_points) do
1035 for _, delta in ipairs({{-1, 0}, {0, -1}, {0, 1}, {1, 0}}) do
1036 local x = point[1] + delta[1]
1037 local y = point[2] + delta[2]
1038 local k2 = x * 100 + y
1039 if not solid_points[k2] and not adjacent_set[k2] then
1040 adjacent_set[k2] = {x, y}
1041 adjacent_list[#adjacent_list + 1] = adjacent_set[k2]
1042 end
1043 end
1044 end
1045 end
1046 end
1047 end
1048
1049 --[[ Gets the state of an object
1050
1051 ! The state can be later used to set the state of this object. This is
1052 useful when we would destroy and create a new object that should represent
1053 the same object.
1054
1055 !return (table) state
1056 ]]
1057 function Object:getState()
1058 return {times_used = self.times_used}
1059 end
1060
1061 --[[ Sets the state of an object
1062
1063 ! This is a complement to a pair function. IT will use the generated state
1064 table to update it's state.
1065 !param state (table) table holding the state
1066 !return (void)
1067 ]]
1068 function Object:setState(state)
1069 if state then
1070 self.times_used = state.times_used
1071 end
1072 end
1073
1074 --! An object that lives on the edge of a tile.
1075 class "SideObject" (Object)
1076
1077 ---@type SideObject
1078 local SideObject = _G["SideObject"]
1079
1080 function SideObject:SideObject(...)
1081 self:Object(...)
1082 end
1083
1084 function SideObject:getDrawingLayer()
1085 if self.direction == "north" then
1086 return DrawingLayers.NorthSideObject
1087 elseif self.direction == "west" then
1088 return DrawingLayers.WestSideObject
1089 else
1090 if self.direction == "east" then
1091 -- 50 is a trash bin thob
1092 if self.object_type.thob == 50 then
1093 --[[ The bin has orientations north and east but they are displayed in
1094 the north and west part of the tile respectively. This could lead to a
1095 graphical glitch where a side object in the west part of the tile is
1096 displayed over a doctor in the middle of the tile. ]]
1097 return DrawingLayers.WestSideObject
1098 else
1099 return DrawingLayers.EastSideObject
1100 end
1101 else --south
1102 return DrawingLayers.SouthSideObject
1103 end
1104 end
1105 end