| 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 "MultiUseObjectAction" (HumanoidAction) |
| 22 |
|
| 23 |
---@type MultiUseObjectAction |
| 24 |
local MultiUseObjectAction = _G["MultiUseObjectAction"] |
| 25 |
|
| 26 |
--! Construct a multi-use object action. |
| 27 |
--!param object (Object) Object being used. |
| 28 |
--!param use_with (Humanoid) Fellow user of the object. |
| 29 |
function MultiUseObjectAction:MultiUseObjectAction(object, use_with) |
| 30 |
assert(class.is(object, Object), "Invalid value for parameter 'object'") |
| 31 |
assert(class.is(use_with, Humanoid), "Invalid value for parameter 'use_with'") |
| 32 |
|
| 33 |
self:HumanoidAction("multi_use_object") |
| 34 |
self.object = object |
| 35 |
self.use_with = use_with |
| 36 |
self.prolonged_usage = nil -- If true, the usage is prolonged. |
| 37 |
self.layer3 = nil |
| 38 |
end |
| 39 |
|
| 40 |
--! Set the invisibility span. |
| 41 |
--!param span (array) Span of invisibility, {from, to} |
| 42 |
--!return (action) self, for daisy-chaining. |
| 43 |
function MultiUseObjectAction:setInvisiblePhaseSpan(span) |
| 44 |
assert(type(span) == "table" and type(span[1]) == "number" and |
| 45 |
type(span[2] == "number") and span[1] <= span[2], |
| 46 |
"Invalid value for parameter 'span'") |
| 47 |
|
| 48 |
self.invisible_phase_span = span |
| 49 |
return self |
| 50 |
end |
| 51 |
|
| 52 |
--! Set prolonged usage of the object. |
| 53 |
--!param prolonged (bool) If set, enable prolonged usage of the object. |
| 54 |
--!return (action) self, for daisy-chaining. |
| 55 |
function MultiUseObjectAction:setProlongedUsage(prolonged) |
| 56 |
assert(type(prolonged), "boolean", "Invalid value for parameter 'prolonged'") |
| 57 |
|
| 58 |
self.prolonged_usage = prolonged |
| 59 |
return self |
| 60 |
end |
| 61 |
|
| 62 |
-- Set animation layer3 to the given value. |
| 63 |
--!param layer3 (int) Value to set for animation layer 3. |
| 64 |
--!return (action) self, for daisy-chaining. |
| 65 |
function MultiUseObjectAction:setLayer3(layer3) |
| 66 |
assert(type(layer3) == "number", "Invalid value for parameter 'layer3'") |
| 67 |
|
| 68 |
self.layer3 = layer3 |
| 69 |
return self |
| 70 |
end |
| 71 |
|
| 72 |
local TH = require("TH") |
| 73 |
|
| 74 |
local orient_mirror = { |
| 75 |
north = "west", |
| 76 |
west = "north", |
| 77 |
east = "south", |
| 78 |
south = "east", |
| 79 |
} |
| 80 |
|
| 81 |
local function action_multi_use_next_phase(action, phase) |
| 82 |
phase = phase + 1 |
| 83 |
if phase < -5 then |
| 84 |
phase = -5 |
| 85 |
end |
| 86 |
if phase == -5 and not action.anims.begin_use then |
| 87 |
phase = phase + 1 |
| 88 |
end |
| 89 |
if phase == -4 and not action.anims.begin_use_2 then |
| 90 |
phase = phase + 1 |
| 91 |
end |
| 92 |
if phase == -3 and not action.anims.begin_use_3 then |
| 93 |
phase = phase + 1 |
| 94 |
end |
| 95 |
if phase == -2 and not action.anims.begin_use_4 then |
| 96 |
phase = phase + 1 |
| 97 |
end |
| 98 |
if phase == -1 and not action.anims.begin_use_5 then |
| 99 |
phase = phase + 1 |
| 100 |
end |
| 101 |
if phase == 0 and not action.anims.in_use then |
| 102 |
phase = phase + 1 |
| 103 |
end |
| 104 |
if phase == 1 and not action.anims.finish_use then |
| 105 |
phase = phase + 1 |
| 106 |
end |
| 107 |
if phase == 2 and not action.anims.finish_use_2 then |
| 108 |
phase = phase + 1 |
| 109 |
end |
| 110 |
if phase == 3 and not action.anims.finish_use_3 then |
| 111 |
phase = phase + 1 |
| 112 |
end |
| 113 |
if phase == 4 and not action.anims.finish_use_4 then |
| 114 |
phase = phase + 1 |
| 115 |
end |
| 116 |
if phase == 5 and not action.anims.finish_use_5 then |
| 117 |
phase = phase + 1 |
| 118 |
end |
| 119 |
if phase > 5 then |
| 120 |
phase = 100 |
| 121 |
end |
| 122 |
return phase |
| 123 |
end |
| 124 |
|
| 125 |
local action_multi_use_object_tick |
| 126 |
|
| 127 |
local function action_multi_use_phase(action, humanoid, phase) |
| 128 |
local object = action.object |
| 129 |
humanoid.user_of = nil -- Temporary to avoid tile change warning |
| 130 |
action.phase = phase |
| 131 |
local anim_name = "in_use" |
| 132 |
if phase == -5 then |
| 133 |
anim_name = "begin_use" |
| 134 |
elseif phase == -4 then |
| 135 |
anim_name = "begin_use_2" |
| 136 |
elseif phase == -3 then |
| 137 |
anim_name = "begin_use_3" |
| 138 |
elseif phase == -2 then |
| 139 |
anim_name = "begin_use_4" |
| 140 |
elseif phase == -1 then |
| 141 |
anim_name = "begin_use_5" |
| 142 |
elseif phase == 1 then |
| 143 |
anim_name = "finish_use" |
| 144 |
elseif phase == 2 then |
| 145 |
anim_name = "finish_use_2" |
| 146 |
elseif phase == 3 then |
| 147 |
anim_name = "finish_use_3" |
| 148 |
elseif phase == 4 then |
| 149 |
anim_name = "finish_use_4" |
| 150 |
elseif phase == 5 then |
| 151 |
anim_name = "finish_use_5" |
| 152 |
end |
| 153 |
local anim = action.anims[anim_name] |
| 154 |
if type(anim) == "table" then |
| 155 |
-- If an animation list is provided rather than a single animation, then |
| 156 |
-- choose an animation from the list at random, or according to the previous |
| 157 |
-- phase. Look at general diagnosis for usage example. |
| 158 |
if action.random_anim then |
| 159 |
anim = anim[action.random_anim] |
| 160 |
else |
| 161 |
action.random_anim = math.random(1, #anim) |
| 162 |
anim = anim[action.random_anim] |
| 163 |
end |
| 164 |
end |
| 165 |
if object.split_anims then |
| 166 |
local anims = humanoid.world.anims |
| 167 |
for i = 2, #object.split_anims do |
| 168 |
local th = object.split_anims[i] |
| 169 |
th:setAnimation(anims, anim, action.mirror_flags) |
| 170 |
end |
| 171 |
end |
| 172 |
humanoid:setAnimation(anim, action.mirror_flags) |
| 173 |
|
| 174 |
-- Compute the position of the animated humanoid. |
| 175 |
local tx, ty |
| 176 |
local obj_orient = object.object_type.orientations[object.direction] |
| 177 |
if obj_orient.use_animate_from_use_position then |
| 178 |
tx = object.tile_x + obj_orient.use_position[1] |
| 179 |
ty = object.tile_y + obj_orient.use_position[2] |
| 180 |
else |
| 181 |
tx, ty = object:getRenderAttachTile() |
| 182 |
end |
| 183 |
local offset = obj_orient.animation_offset |
| 184 |
humanoid:setTilePositionSpeed(tx, ty, offset[1], offset[2]) |
| 185 |
|
| 186 |
humanoid.user_of = object |
| 187 |
local length = TheApp.animation_manager:getAnimLength(anim) |
| 188 |
local secondary_anim = action.anims.secondary and action.anims.secondary[anim_name] |
| 189 |
if action.secondary_anim then |
| 190 |
secondary_anim = action.secondary_anim |
| 191 |
action.secondary_anim = nil |
| 192 |
end |
| 193 |
local use_with = action.use_with |
| 194 |
if secondary_anim then |
| 195 |
if type(secondary_anim) == "table" and secondary_anim[1] == "morph" then |
| 196 |
use_with:setAnimation(secondary_anim[2], action.mirror_flags) |
| 197 |
local morph_target = TH.animation() |
| 198 |
morph_target:setAnimation(use_with.world.anims, secondary_anim[3], action.mirror_flags) |
| 199 |
for layer, id in pairs(use_with.layers) do |
| 200 |
morph_target:setLayer(layer, id) |
| 201 |
end |
| 202 |
if secondary_anim.layers then |
| 203 |
for layer, id in pairs(use_with[secondary_anim.layers]) do |
| 204 |
morph_target:setLayer(layer, id) |
| 205 |
end |
| 206 |
action.change_secondary_layers = use_with[secondary_anim.layers] |
| 207 |
end |
| 208 |
use_with.th:setMorph(morph_target) |
| 209 |
secondary_anim = secondary_anim[3] |
| 210 |
else |
| 211 |
use_with:setAnimation(secondary_anim, action.mirror_flags) |
| 212 |
end |
| 213 |
use_with.th:makeVisible() |
| 214 |
local secondary_length = TheApp.animation_manager:getAnimLength(secondary_anim) |
| 215 |
if secondary_length > length then |
| 216 |
length = secondary_length |
| 217 |
end |
| 218 |
else |
| 219 |
local span = action.invisible_phase_span |
| 220 |
if span then |
| 221 |
if span[1] <= phase and span[2] >= phase then |
| 222 |
use_with.th:makeInvisible() |
| 223 |
else |
| 224 |
use_with.th:makeVisible() |
| 225 |
end |
| 226 |
else |
| 227 |
use_with.th:makeInvisible() |
| 228 |
end |
| 229 |
end |
| 230 |
humanoid:setTimer(length, action_multi_use_object_tick) |
| 231 |
end |
| 232 |
|
| 233 |
local function copy_layers(dest, src) |
| 234 |
if class.is(dest, Staff) then |
| 235 |
dest:setLayer(0, src.layers[0]) |
| 236 |
dest:setLayer(1, src.layers[1]) |
| 237 |
dest:setLayer(2, src.layers[2]) |
| 238 |
dest:setLayer(3, src.layers[3]) |
| 239 |
dest:setLayer(4, src.layers[4]) |
| 240 |
elseif class.is(src, Staff) then |
| 241 |
dest:setLayer(5, src.layers[5]) |
| 242 |
end |
| 243 |
end |
| 244 |
|
| 245 |
action_multi_use_object_tick = permanent"action_multi_use_object_tick"( function(humanoid) |
| 246 |
local action = humanoid:getCurrentAction() |
| 247 |
local use_with = action.use_with |
| 248 |
local object = action.object |
| 249 |
local phase = action.phase |
| 250 |
local oldphase = phase |
| 251 |
if phase ~= 0 or not action.prolonged_usage then |
| 252 |
phase = action_multi_use_next_phase(action, phase) |
| 253 |
elseif action.loop_callback then |
| 254 |
action:loop_callback() |
| 255 |
end |
| 256 |
if action.change_secondary_layers then |
| 257 |
for layer, id in pairs(action.change_secondary_layers) do |
| 258 |
use_with:setLayer(layer, id) |
| 259 |
end |
| 260 |
action.change_secondary_layers = nil |
| 261 |
copy_layers(humanoid, use_with) |
| 262 |
end |
| 263 |
if oldphase <= 5 and phase > 5 then |
| 264 |
object:removeUser(humanoid) |
| 265 |
humanoid.user_of = nil |
| 266 |
if object.split_anims then |
| 267 |
local anims, anim, frame, flags = humanoid.world.anims, |
| 268 |
object.th:getAnimation(), object.th:getFrame(), object.th:getFlag() |
| 269 |
for i = 2, #object.split_anims do |
| 270 |
local th = object.split_anims[i] |
| 271 |
th:setLayersFrom(object.th) |
| 272 |
th:setHitTestResult(object) |
| 273 |
th:setAnimation(anims, anim, flags) |
| 274 |
th:setFrame(frame) |
| 275 |
end |
| 276 |
object.ticks = object.object_type.ticks |
| 277 |
end |
| 278 |
end |
| 279 |
if phase == 0 then |
| 280 |
-- Already now move the secondary user to his final position and orientation. |
| 281 |
-- This is needed if some end phases have the secondary user visible (e.g. jelly moulder) |
| 282 |
local spec = object.object_type.orientations[object.direction] |
| 283 |
local pos = spec.finish_use_position_secondary or spec.use_position_secondary |
| 284 |
local direction = spec.finish_use_orientation_secondary |
| 285 |
use_with:setTilePositionSpeed(object.tile_x + pos[1], object.tile_y + pos[2]) |
| 286 |
if direction then |
| 287 |
local anims = use_with.walk_anims |
| 288 |
local anim = (direction == "north" or direction == "west") and anims.idle_north or anims.idle_east |
| 289 |
local flags = (direction == "north" or direction == "east") and 0 or 1 |
| 290 |
use_with:setAnimation(anim, flags) |
| 291 |
end |
| 292 |
end |
| 293 |
if phase == 100 then |
| 294 |
if action.layer3 then |
| 295 |
humanoid:setLayer(3, action.old_layer3_humanoid) |
| 296 |
use_with:setLayer(3, action.old_layer3_use_with) |
| 297 |
end |
| 298 |
|
| 299 |
use_with.th:makeVisible() |
| 300 |
use_with:getCurrentAction().on_interrupt = action.idle_interrupt |
| 301 |
use_with:getCurrentAction().must_happen = action.idle_must_happen |
| 302 |
local spec = object.object_type.orientations[object.direction] |
| 303 |
local pos = spec.finish_use_position or spec.use_position |
| 304 |
humanoid:setTilePositionSpeed(object.tile_x + pos[1], object.tile_y + pos[2]) |
| 305 |
-- Check if the room is about to be destroyed |
| 306 |
local room_destroyed = false |
| 307 |
if object:isMachine() then |
| 308 |
room_destroyed = object:machineUsed(humanoid:getRoom()) |
| 309 |
end |
| 310 |
if not room_destroyed then |
| 311 |
-- Now call the after_use function if appropriate |
| 312 |
if action.after_use then |
| 313 |
action.after_use() |
| 314 |
end |
| 315 |
humanoid:finishAction(action) |
| 316 |
end |
| 317 |
else |
| 318 |
action_multi_use_phase(action, humanoid, phase) |
| 319 |
end |
| 320 |
end) |
| 321 |
|
| 322 |
local action_multi_use_object_interrupt = permanent"action_multi_use_object_interrupt"( function(action) |
| 323 |
if not action.loop_callback then |
| 324 |
action.prolonged_usage = false |
| 325 |
end |
| 326 |
end) |
| 327 |
|
| 328 |
local function action_multi_use_object_start(action, humanoid) |
| 329 |
local use_with = action.use_with |
| 330 |
if action.must_happen then |
| 331 |
-- Setting must_happen is slightly dangerous (though required in some |
| 332 |
-- situations), as the multi-usage cannot be sure to happen until the |
| 333 |
-- secondary user is present (at which point, must_happen is always set). |
| 334 |
if action.todo_interrupt and not action.no_truncate then |
| 335 |
humanoid:finishAction(action) |
| 336 |
return |
| 337 |
end |
| 338 |
end |
| 339 |
if use_with:getCurrentAction().name ~= "idle" then |
| 340 |
humanoid:queueAction(IdleAction():setCount(2), 0) |
| 341 |
return |
| 342 |
else |
| 343 |
action.idle_interrupt = use_with:getCurrentAction().on_interrupt |
| 344 |
action.idle_must_happen = use_with:getCurrentAction().must_happen |
| 345 |
use_with:getCurrentAction().on_interrupt = nil |
| 346 |
use_with:getCurrentAction().must_happen = true |
| 347 |
end |
| 348 |
action.must_happen = true |
| 349 |
if action.prolonged_usage then |
| 350 |
action.on_interrupt = action_multi_use_object_interrupt |
| 351 |
use_with:getCurrentAction().on_interrupt = --[[persistable:action_multi_use_object_use_with_interrupt]] function() |
| 352 |
if action.on_interrupt then |
| 353 |
action:on_interrupt() |
| 354 |
action.on_interrupt = nil |
| 355 |
end |
| 356 |
end |
| 357 |
end |
| 358 |
local object = action.object |
| 359 |
local orient = object.direction |
| 360 |
local flags = 0 |
| 361 |
local anim_set = humanoid.humanoid_class .. " - " .. use_with.humanoid_class |
| 362 |
if not object.object_type.multi_usage_animations[anim_set][orient] then |
| 363 |
orient = orient_mirror[orient] |
| 364 |
flags = flags + 1 |
| 365 |
end |
| 366 |
if object.split_anims then |
| 367 |
flags = flags + DrawFlags.Crop |
| 368 |
end |
| 369 |
local spec = object.object_type.orientations[object.direction] |
| 370 |
-- early_list_while_in_use (if defined) will take precedence over early_list |
| 371 |
if spec.early_list_while_in_use or (spec.early_list_while_in_use == nil and spec.early_list) then |
| 372 |
flags = flags + 1024 |
| 373 |
end |
| 374 |
local anims = object.object_type.multi_usage_animations[anim_set][orient] |
| 375 |
action.anims = anims |
| 376 |
action.mirror_flags = flags |
| 377 |
|
| 378 |
object:setUser(humanoid) |
| 379 |
humanoid.user_of = object |
| 380 |
copy_layers(humanoid, use_with) |
| 381 |
if action.layer3 then |
| 382 |
action.old_layer3_humanoid = humanoid.layers[3] |
| 383 |
action.old_layer3_use_with = use_with.layers[3] |
| 384 |
humanoid:setLayer(3, action.layer3) |
| 385 |
use_with:setLayer(3, action.layer3) |
| 386 |
end |
| 387 |
if object.split_anims then |
| 388 |
for i = 2, #object.split_anims do |
| 389 |
local th = object.split_anims[i] |
| 390 |
th:setLayersFrom(humanoid.th) |
| 391 |
th:setHitTestResult(humanoid) |
| 392 |
end |
| 393 |
object.ticks = true |
| 394 |
end |
| 395 |
|
| 396 |
action_multi_use_phase(action, humanoid, action_multi_use_next_phase(action, -100)) |
| 397 |
end |
| 398 |
|
| 399 |
return action_multi_use_object_start |