/
entity_functions.lua
641 lines (496 loc) · 20.6 KB
/
entity_functions.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
-- OPENTOMB ENTITY FUNCTIONS SCRIPT
-- By TeslaRus, Lwmte, 2014-2015
--------------------------------------------------------------------------------
-- This script contains routines for all entity functions. Note that it is not
-- just entity functions, but "function creation" routines, which usually
-- includes some events executed on level start-up. Function itself is placed
-- inside entity function array (entity_funcs).
--------------------------------------------------------------------------------
entity_funcs = {}; -- Initialize entity function array.
-- Load up some extra entity scripts.
dofile("scripts/entity/script_switch.lua"); -- Additional switch scripts.
-- Erase single entity function.
function efuncs_EraseEntity(index)
if(entity_funcs[index] ~= nil) then
entity_funcs[index].onActivate = nil;
entity_funcs[index].onDeactivate = nil;
entity_funcs[index].onCollide = nil;
entity_funcs[index].onStand = nil;
entity_funcs[index].onHit = nil;
entity_funcs[index].onLoop = nil;
entity_funcs[index] = nil;
end;
end;
-- Clear whole entity functions array. Must be called on each level loading.
function entfuncs_Clear()
for k,v in pairs(entity_funcs) do
efuncs_EraseEntity(k);
end;
print("Entity function table cleaned");
end;
--------------------------------------------------------------------------------
-- ENTITY FUNCTIONS IMPLEMENTATION
--------------------------------------------------------------------------------
function door_init(id) -- NORMAL doors only!
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
setEntityActivity(object_id, 1);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(object_id == nil) then return end;
swapEntityState(object_id, 0, 1);
end;
entity_funcs[id].onDeactivate = entity_funcs[id].onActivate; -- Same function.
entity_funcs[id].onLoop = function(object_id)
if(tickEntity(object_id) == TICK_STOPPED) then
swapEntityState(object_id, 0, 1);
setEntityEvent(object_id, 0);
end;
end
prepareEntity(id);
end
function keyhole_init(id) -- Key and puzzle holes
setEntityTypeFlag(id, ENTITY_TYPE_INTERACTIVE);
setEntityActivity(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(object_id == nil or getEntityActivity(object_id) >= 1 or canTriggerEntity(activator_id, object_id, 256.0, 0.0, 256.0, 0.0) ~= 1) then
return;
end
if((getEntityActivity(object_id) == 0) and (switch_activate(object_id, activator_id) == 1)) then
setEntityPos(activator_id, getEntityPos(object_id));
moveEntityLocal(activator_id, 0.0, 360.0, 0.0);
end
end;
end
function switch_init(id) -- Ordinary switches
setEntityTypeFlag(id, ENTITY_TYPE_INTERACTIVE);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(object_id == nil or canTriggerEntity(activator_id, object_id, 256.0, 0.0, 256.0, 0.0) ~= 1) then
return;
end
if(switch_activate(object_id, activator_id) == 1) then
setEntityPos(activator_id, getEntityPos(object_id)); -- Move activator right next to object.
moveEntityLocal(activator_id, 0.0, 360.0, 0.0); -- Shift activator back to proper distance.
end;
end;
entity_funcs[id].onLoop = function(object_id)
if(tickEntity(object_id) == TICK_STOPPED) then
setEntityState(object_id, 1);
setEntitySectorStatus(object_id, 1);
end;
end;
end
function anim_init(id) -- Ordinary animatings
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
entity_funcs[id].onActivate = function(object_id, activator_id)
setEntityActivity(object_id, 1);
setEntityState(object_id, 1);
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
setEntityActivity(object_id, 0);
setEntityState(object_id, 0);
end
entity_funcs[id].onLoop = function(object_id)
if(tickEntity(object_id) == TICK_STOPPED) then setEntityState(object_id, 0) end;
end
end
function pushable_init(id)
setEntityTypeFlag(id, ENTITY_TYPE_HEAVYTRIGGER_ACTIVATOR);
end
function venicebird_init(id) -- Venice singing birds (TR2)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
entity_funcs[id].onActivate = function(object_id, activator_id)
setEntityActivity(object_id, 1);
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
setEntityActivity(object_id, 0);
end
entity_funcs[id].onLoop = function(object_id)
if(tickEntity(object_id) == TICK_STOPPED) then setEntityActivity(object_id, 0) end;
if(getEntityDistance(player, object_id) < 8192.0) then
if(math.random(100000) > 99500) then playSound(316, object_id) end;
end;
end
prepareEntity(id);
end
function drips_init(id) -- Maria Doria drips (TR2)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
entity_funcs[id].onActivate = function(object_id, activator_id)
setEntityActivity(object_id, 1);
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
setEntityActivity(object_id, 0);
end
entity_funcs[id].onLoop = function(object_id)
if(tickEntity(object_id) == TICK_STOPPED) then setEntityActivity(object_id, 0) end;
if(getEntityDistance(player, object_id) < 8192.0) then
if(math.random(100000) > 99500) then playSound(329, object_id) end;
end;
end
prepareEntity(id);
end
function doorbell_init(id) -- Lara's Home doorbell (TR2)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
setEntityActivity(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
setEntityActivity(object_id, 1);
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
setEntityActivity(object_id, 0);
end
entity_funcs[id].onLoop = function(object_id)
if(getEntityDistance(player, object_id) < 4096.0) then
playSound(334, object_id);
setEntityActivity(object_id, 0);
end;
end
end
function alarm_TR2_init(id) -- Offshore Rig alarm (TR2)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
setEntityActivity(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(getEntityActivity(object_id) == 0) then setEntityActivity(object_id, 1) end;
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
if(getEntityActivity(object_id) == 1) then
setEntityActivity(object_id, 0);
stopSound(332, object_id);
end;
end
entity_funcs[id].onLoop = function(object_id)
playSound(332, object_id);
if(tickEntity(object_id) == TICK_STOPPED) then
setEntityActivity(object_id, 0)
stopSound(332, object_id);
end;
end
end
function alarmbell_init(id) -- Home Sweet Home alarm (TR2)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
setEntityActivity(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(getEntityActivity(object_id) == 0) then setEntityActivity(object_id, 1) end;
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
if(getEntityActivity(object_id) == 1) then
setEntityActivity(object_id, 0);
stopSound(335, object_id);
end;
end
entity_funcs[id].onLoop = function(object_id)
playSound(335, object_id);
if(tickEntity(object_id) == TICK_STOPPED) then
setEntityActivity(object_id, 0)
stopSound(335, object_id);
end;
end
end
function heli_TR2_init(id) -- Helicopter (TR2)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
entity_funcs[id].distance_passed = 0;
setEntityActivity(id, 0);
setEntityVisibility(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(getEntityActivity(object_id) == 0) then
setEntityActivity(object_id, 1);
setEntityVisibility(id, 1);
playSound(297, object_id);
end;
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
setEntityActivity(object_id, 0);
end
entity_funcs[id].onLoop = function(object_id)
entity_funcs[object_id].distance_passed = entity_funcs[object_id].distance_passed + 40.0;
moveEntityLocal(object_id, 0.0, 40.0, 0.0);
if(entity_funcs[object_id].distance_passed > 30720) then
stopSound(297, object_id);
disableEntity(object_id);
end;
end
prepareEntity(id);
end
function heli_rig_TR2_init(id) -- Helicopter in Offshore Rig (TR2)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
setEntityActivity(id, 0);
setEntityVisibility(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
setEntityActivity(object_id, 1);
setEntityVisibility(object_id, 1);
end
entity_funcs[id].onLoop = function(object_id)
if(getEntityLock(object_id) == 1) then
if(getEntityState(object_id) ~= 2) then
setEntityState(object_id, 2);
else
local anim, frame, count = getEntityAnim(object_id);
if(frame == count-1) then disableEntity(object_id) end;
end
end;
end
prepareEntity(id);
end
function swingblade_init(id) -- Swinging blades (TR1)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
setEntityActivity(id, 1);
entity_funcs[id].onActivate = function(object_id, activator_id)
setEntityState(object_id, 2);
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
setEntityState(object_id, 0);
end
entity_funcs[id].onLoop = function(object_id)
if(tickEntity(object_id) == TICK_STOPPED) then setEntityState(object_id, 0) end;
end
prepareEntity(id);
end
function tallblock_init(id) -- Tall moving block (TR1)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
entity_funcs[id].distance_passed = 0;
setEntityActivity(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(getEntityActivity(object_id) == 0) then
setEntityActivity(object_id, 1);
playSound(64, object_id);
end;
end
entity_funcs[id].onDeactivate = entity_funcs[id].onActivate;
entity_funcs[id].onLoop = function(object_id)
local move_speed = 32.0;
if(getEntityEvent(object_id) == 0) then move_speed = 0 - move_speed end;
entity_funcs[object_id].distance_passed = entity_funcs[object_id].distance_passed + move_speed;
moveEntityLocal(object_id, 0.0, move_speed, 0.0);
if(math.abs(entity_funcs[object_id].distance_passed) >= 2048.0) then
stopSound(64, object_id);
setEntityActivity(object_id, 0);
entity_funcs[object_id].distance_passed = 0;
end;
end
prepareEntity(id);
end
function slamdoor_init(id) -- Slamming doors (TR1-TR2)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
setEntityActivity(id, 1);
entity_funcs[id].onActivate = function(object_id, activator_id)
setEntityState(object_id, 1);
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
setEntityState(object_id, 0);
end
entity_funcs[id].onLoop = function(object_id)
if(tickEntity(object_id) == TICK_STOPPED) then setEntityState(object_id, 0) end;
end
prepareEntity(id);
end
function wallblade_init(id) -- Wall blade (TR1-TR3)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
entity_funcs[id].onActivate = function(object_id, activator_id)
setEntityActivity(object_id, 1);
end
entity_funcs[id].onDeactivate = function(object_id, activator_id)
setEntityActivity(object_id, 0);
end
entity_funcs[id].onLoop = function(object_id)
if(tickEntity(object_id) == TICK_STOPPED) then setEntityActivity(object_id, 0) end;
local anim_number = getEntityAnim(object_id);
if(anim_number == 2) then
setEntityAnim(object_id, 3);
elseif(anim_number == 1) then
setEntityAnim(object_id, 0);
end;
end
prepareEntity(id);
end
function boulder_init(id)
setEntityTypeFlag(id, ENTITY_TYPE_HEAVYTRIGGER_ACTIVATOR);
setEntityAnimFlag(id, ANIM_LOCK);
setEntityActivity(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(getEntityActivity(object_id == 0)) then
setEntityBodyMass(object_id, getEntityMeshCount(object_id), 2000.0);
setEntityActivity(object_id, 1);
if(getLevelVersion() < TR_IV) then
pushEntityBody(object_id, 0, math.random(150) + 2500.0, 10.0, true);
lockEntityBodyLinearFactor(object_id, 0);
end;
end;
end
end
function pickup_init(id, item_id) -- Pick-ups
setEntityTypeFlag(id, ENTITY_TYPE_PICKABLE);
setEntityActivationOffset(id, 0.0, 0.0, 0.0, 480.0);
setEntityActivity(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
if((item_id == nil) or (object_id == nil)) then
return;
end
local need_set_pos = true;
local curr_anim = getEntityAnim(activator_id);
if(curr_anim == 103) then -- Stay idle
local dx, dy, dz = getEntityVector(object_id, activator_id);
if(dz < -256.0) then
need_set_pos = false;
setEntityAnim(activator_id, 425); -- Standing pickup, test version
else
setEntityAnim(activator_id, 135); -- Stay pickup
end;
elseif(curr_anim == 222) then -- Crouch idle
setEntityAnim(activator_id, 291); -- Crouch pickup
elseif(curr_anim == 263) then -- Crawl idle
setEntityAnim(activator_id, 292); -- Crawl pickup
elseif(curr_anim == 108) then -- Swim idle
setEntityAnim(activator_id, 130); -- Swim pickup
else
return; -- Disable picking up, if Lara isn't idle.
end;
addTask(
function()
-- Position corrector
if(getEntityMoveType(activator_id) == MOVE_UNDERWATER) then
if(getEntityDistance(object_id, activator_id) > 128.0) then
moveEntityToEntity(activator_id, object_id, 25.0);
end;
else
if(getEntityDistance(object_id, activator_id) > 32.0) then
moveEntityToEntity(activator_id, object_id, 50.0);
end;
end;
local a, f, c = getEntityAnim(activator_id);
-- Standing pickup anim makes action on frame 40 in TR1-3, in TR4-5
-- it was generalized with all rest animations by frame 16.
if((a == 135) and (getLevelVersion() < TR_IV)) then
if(f < 40) then
return true;
end;
else
if(f < 16) then
return true;
end;
end;
addItem(activator_id, item_id);
disableEntity(object_id);
return false; -- Item successfully picked up, kill the task.
end);
end;
end
function crystal_TR3_init(id) -- "Savegame" crystal (TR3 version)
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
setEntityActivity(id, 1);
entity_funcs[id].onLoop = function(object_id)
if(getEntityDistance(player, object_id) < 512.0) then
playSound(SOUND_MEDIPACK);
changeCharacterParam(player, PARAM_HEALTH, 200);
disableEntity(object_id);
end;
end
end
function fallblock_init(id) -- Falling block (TR1-3)
local f1, f2, f3 = getEntityFlags(id); -- f1 - state flags, f2 - type flags, f3 - callback flags
setEntityFlags(id, nil, nil, bit32.bor(f3, ENTITY_CALLBACK_STAND));
setEntitySpeed(id, 0.0, 0.0, 0.0);
entity_funcs[id].onStand = function(object_id, activator_id)
if((object_id == nil) or (activator_id == nil)) then
return;
end
local anim = getEntityAnim(object_id);
if(anim == 0) then
setEntityAnim(object_id, 1);
-- print("you trapped to id = "..object_id);
local once = true;
addTask(
function()
local anim = getEntityAnim(object_id);
if(anim == 1) then
return true;
end;
if(once) then
setEntityCollision(object_id, 0);
once = false;
end;
if(dropEntity(object_id, frame_time)) then
setEntityAnim(object_id, 3);
return false;
end;
return true;
end);
end;
end;
end
function fallceiling_init(id) -- Falling ceiling (TR1-3)
setEntitySpeed(id, 0.0, 0.0, 0.0);
local level_version = getLevelVersion();
if((level_version < TR_II) or (level_version >= TR_III)) then setEntityVisibility(id, 0) end;
entity_funcs[id].onActivate = function(object_id, activator_id)
if((object_id == nil) or (activator_id == nil)) then
return;
end
local anim = getEntityAnim(object_id);
if(anim == 0) then
setEntityAnim(object_id, 1);
setEntityVisibility(object_id, 1);
addTask(
function()
if(dropEntity(object_id, frame_time)) then
setEntityAnim(object_id, 2);
setEntityCollision(object_id, 0);
return false;
end;
return true;
end);
end;
end;
end
function pushdoor_init(id) -- Pushdoors (TR4)
setEntityTypeFlag(id, ENTITY_TYPE_INTERACTIVE);
setEntityActivity(id, 0);
entity_funcs[id].onActivate = function(object_id, activator_id)
if((object_id == nil) or (activator_id == nil)) then
return;
end;
if((getEntityActivity(object_id) == 0) and (getEntityDirDot(object_id, activator_id) < -0.9)) then
setEntityActivity(object_id, 1);
local x, y, z, az, ax, ay = getEntityPos(object_id);
setEntityPos(activator_id, x, y, z, az + 180.0, ax, ay);
moveEntityLocal(activator_id, 0.0, 256.0, 0.0);
-- floor door 317 anim
-- vertical door 412 anim
setEntityAnim(activator_id, 412);
end;
end;
end
function midastouch_init(id) -- Midas gold touch
setEntityTypeFlag(id, ENTITY_TYPE_GENERIC);
entity_funcs[id].onLoop = function(object_id)
if(getEntityDistance(player, object_id) < 1024.0) then
local lara_anim, frame, count = getEntityAnim(player);
local lara_sector = getEntitySectorIndex(player);
local hand_sector = getEntitySectorIndex(object_id);
if((lara_sector == hand_sector) and (getEntityMoveType(player) == MOVE_ON_FLOOR) and (getEntityAnim(player) ~= 50)) then
setCharacterParam(player, PARAM_HEALTH, 0);
setEntityAnim(player, 1, 0, 5);
disableEntity(object_id);
end;
end;
end
prepareEntity(id);
end
function oldspike_init(id) -- Teeth spikes (INVALID)
setEntityActivity(id, 1);
local f1, f2, f3 = getEntityFlags(id);
setEntityFlags(id, nil, bit32.bor(f2, ENTITY_TYPE_GENERIC), bit32.bor(f3, ENTITY_CALLBACK_COLLISION));
entity_funcs[id].onCollision = function(object_id, activator_id)
if((object_id == nil) or (activator_id == nil)) then
return;
end;
if(getModelID(activator_id) == 0) then -- Lara
local s1, s2, s3 = getEntitySpeed(activator_id);
-- check if Lara and remove her HP!!!!
print("speed: " .. s1 .. " " .. s2 .. " " .. s3);
end;
end;
end
function baddie_init(id) -- INVALID!
setEntityTypeFlag(id, ENTITY_TYPE_ACTOR);
disableEntity(id);
entity_funcs[id].onActivate = function(object_id, activator_id)
if(getEntityActivity(object_id) == 0) then enableEntity(object_id) end;
end;
end