forked from Athemis/mobs_redo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
4824 lines (3533 loc) · 106 KB
/
api.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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Load support for intllib.
local MP = minetest.get_modpath(minetest.get_current_modname())
local S = minetest.get_translator and minetest.get_translator("mobs_redo") or
dofile(MP .. "/intllib.lua")
-- CMI support check
local use_cmi = minetest.global_exists("cmi")
mobs = {
mod = "redo",
version = "20210801",
intllib = S,
invis = minetest.global_exists("invisibility") and invisibility or {}
}
-- localize common functions
local pi = math.pi
local square = math.sqrt
local sin = math.sin
local cos = math.cos
local abs = math.abs
local min = math.min
local max = math.max
local random = math.random
local floor = math.floor
local ceil = math.ceil
local rad = math.rad
local atann = math.atan
local atan = function(x)
if not x or x ~= x then
return 0 -- NaN
else
return atann(x)
end
end
local table_copy = table.copy
local table_remove = table.remove
local vadd = vector.add
local vdirection = vector.direction
local vmultiply = vector.multiply
local vsubtract = vector.subtract
local settings = minetest.settings
-- creative check
local creative_cache = minetest.settings:get_bool("creative_mode")
function mobs.is_creative(name)
return creative_cache or minetest.check_player_privs(name,
{creative = true})
end
-- Load settings
local damage_enabled = settings:get_bool("enable_damage")
local mobs_spawn = settings:get_bool("mobs_spawn") ~= false
local peaceful_only = settings:get_bool("only_peaceful_mobs")
local disable_blood = settings:get_bool("mobs_disable_blood")
local mobs_drop_items = settings:get_bool("mobs_drop_items") ~= false
local mobs_griefing = settings:get_bool("mobs_griefing") ~= false
local spawn_protected = settings:get_bool("mobs_spawn_protected") ~= false
local spawn_monster_protected = settings:get_bool("mobs_spawn_monster_protected") ~= false
local remove_far = settings:get_bool("remove_far_mobs") ~= false
local mob_area_spawn = settings:get_bool("mob_area_spawn")
local difficulty = tonumber(settings:get("mob_difficulty")) or 1.0
local show_health = settings:get_bool("mob_show_health") ~= false
local max_per_block = tonumber(settings:get("max_objects_per_block") or 99)
local mob_nospawn_range = tonumber(settings:get("mob_nospawn_range") or 12)
local active_limit = tonumber(settings:get("mob_active_limit") or 0)
local mob_chance_multiplier = tonumber(settings:get("mob_chance_multiplier") or 1)
local peaceful_player_enabled = settings:get_bool("enable_peaceful_player")
local mob_smooth_rotate = settings:get_bool("mob_smooth_rotate") ~= false
local active_mobs = 0
-- Peaceful mode message so players will know there are no monsters
if peaceful_only then
minetest.register_on_joinplayer(function(player)
minetest.chat_send_player(player:get_player_name(),
S("** Peaceful Mode Active - No Monsters Will Spawn"))
end)
end
-- calculate aoc range for mob count
local aoc_range = tonumber(settings:get("active_block_range")) * 16
-- pathfinding settings
local enable_pathfinding = true
local stuck_timeout = 3 -- how long before stuck mod starts searching
local stuck_path_timeout = 5 -- how long will mob follow path before giving up
-- default nodes
local node_fire = "fire:basic_flame"
local node_permanent_flame = "fire:permanent_flame"
local node_ice = "default:ice"
local node_snowblock = "default:snowblock"
local node_snow = "default:snow"
mobs.fallback_node = minetest.registered_aliases["mapgen_dirt"] or "default:dirt"
local mob_class = {
stepheight = 1.1,
fly_in = "air",
owner = "",
order = "",
jump_height = 4,
lifetimer = 180, -- 3 minutes
physical = true,
collisionbox = {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
visual_size = {x = 1, y = 1},
texture_mods = "",
makes_footstep_sound = false,
view_range = 5,
walk_velocity = 1,
run_velocity = 2,
light_damage = 0,
light_damage_min = 14,
light_damage_max = 15,
water_damage = 0,
lava_damage = 4,
fire_damage = 4,
air_damage = 0,
suffocation = 2,
fall_damage = 1,
fall_speed = -10, -- must be lower than -2 (default: -10)
drops = {},
armor = 100,
sounds = {},
jump = true,
knock_back = true,
walk_chance = 50,
stand_chance = 30,
attack_chance = 5,
passive = false,
blood_amount = 5,
blood_texture = "mobs_blood.png",
shoot_offset = 0,
floats = 1, -- floats in water by default
replace_offset = 0,
timer = 0,
env_damage_timer = 0, -- only used when state = "attack"
tamed = false,
pause_timer = 0,
horny = false,
hornytimer = 0,
child = false,
gotten = false,
health = 0,
reach = 3,
htimer = 0,
docile_by_day = false,
time_of_day = 0.5,
fear_height = 0,
runaway_timer = 0,
immune_to = {},
explosion_timer = 3,
allow_fuse_reset = true,
stop_to_explode = true,
dogshoot_count = 0,
dogshoot_count_max = 5,
dogshoot_count2_max = 5,
group_attack = false,
attack_monsters = false,
attack_animals = false,
attack_players = true,
attack_npcs = true,
facing_fence = false,
_cmi_is_mob = true
}
local mob_class_meta = {__index = mob_class}
-- play sound
function mob_class:mob_sound(sound)
local pitch = 1.0
-- higher pitch for a child
if self.child then pitch = pitch * 1.5 end
-- a little random pitch to be different
pitch = pitch + random(-10, 10) * 0.005
if sound then
minetest.sound_play(sound, {
object = self.object,
gain = 1.0,
max_hear_distance = self.sounds.distance,
pitch = pitch
}, true)
end
end
-- attack player/mob
function mob_class:do_attack(player)
if self.state == "attack" then
return
end
self.attack = player
self.state = "attack"
if random(0, 100) < 90 then
self:mob_sound(self.sounds.war_cry)
end
end
-- calculate distance
local get_distance = function(a, b)
if not a or not b then return 50 end -- nil check
local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z
return square(x * x + y * y + z * z)
end
-- collision function based on jordan4ibanez' open_ai mod
function mob_class:collision()
local pos = self.object:get_pos()
local x, z = 0, 0
local width = -self.collisionbox[1] + self.collisionbox[4] + 0.5
for _,object in ipairs(minetest.get_objects_inside_radius(pos, width)) do
if object:is_player() then
local pos2 = object:get_pos()
local vec = {x = pos.x - pos2.x, z = pos.z - pos2.z}
x = x + vec.x
z = z + vec.z
end
end
return({x, z})
end
-- check if string exists in another string or table
local check_for = function(look_for, look_inside)
if type(look_inside) == "string" and look_inside == look_for then
return true
elseif type(look_inside) == "table" then
for _, str in pairs(look_inside) do
if str == look_for then
return true
end
if str:find("group:") then
local group = str:split(":")[2]
if minetest.get_item_group(look_for, group) ~= 0 then
return true
end
end
end
end
return false
end
-- move mob in facing direction
function mob_class:set_velocity(v)
-- halt mob if it has been ordered to stay
if self.order == "stand" then
local vel = self.object:get_velocity() or {y = 0}
self.object:set_velocity({x = 0, y = vel.y, z = 0})
return
end
local c_x, c_y = 0, 0
-- can mob be pushed, if so calculate direction
if self.pushable then
c_x, c_y = unpack(self:collision())
end
local yaw = (self.object:get_yaw() or 0) + (self.rotate or 0)
-- nil check for velocity
v = v or 0.01
-- check if standing in liquid with max viscosity of 7
local visc = min(minetest.registered_nodes[self.standing_in].liquid_viscosity, 7)
-- only slow mob trying to move while inside a viscous fluid that
-- they aren't meant to be in (fish in water, spiders in cobweb etc)
if v > 0 and visc and visc > 0
and not check_for(self.standing_in, self.fly_in) then
v = v / (visc + 1)
end
-- set velocity
local vel = self.object:get_velocity() or 0
local new_vel = {
x = (sin(yaw) * -v) + c_x,
y = vel.y,
z = (cos(yaw) * v) + c_y}
self.object:set_velocity(new_vel)
end
-- global version of above function
function mobs:set_velocity(entity, v)
mob_class.set_velocity(entity, v)
end
-- calculate mob velocity
function mob_class:get_velocity()
local v = self.object:get_velocity()
if not v then return 0 end
return (v.x * v.x + v.z * v.z) ^ 0.5
end
-- set and return valid yaw
function mob_class:set_yaw(yaw, delay)
if not yaw or yaw ~= yaw then
yaw = 0
end
delay = mob_smooth_rotate and (delay or 0) or 0
if delay == 0 then
self.object:set_yaw(yaw)
return yaw
end
self.target_yaw = yaw
self.delay = delay
return self.target_yaw
end
-- global function to set mob yaw
function mobs:yaw(entity, yaw, delay)
mob_class.set_yaw(entity, yaw, delay)
end
-- set defined animation
function mob_class:set_animation(anim, force)
if not self.animation or not anim then return end
self.animation.current = self.animation.current or ""
-- only use different animation for attacks when using same set
if force ~= true and anim ~= "punch" and anim ~= "shoot"
and string.find(self.animation.current, anim) then
return
end
local num = 0
-- check for more than one animation (max 4)
for n = 1, 4 do
if self.animation[anim .. n .. "_start"]
and self.animation[anim .. n .. "_end"] then
num = n
end
end
-- choose random animation from set
if num > 0 then
num = random(0, num)
anim = anim .. (num ~= 0 and num or "")
end
if anim == self.animation.current
or not self.animation[anim .. "_start"]
or not self.animation[anim .. "_end"] then
return
end
self.animation.current = anim
self.object:set_animation({
x = self.animation[anim .. "_start"],
y = self.animation[anim .. "_end"]},
self.animation[anim .. "_speed"] or
self.animation.speed_normal or 15,
0, self.animation[anim .. "_loop"] ~= false)
end
function mobs:set_animation(entity, anim)
entity.set_animation(entity, anim)
end
-- check line of sight (BrunoMine)
local line_of_sight = function(self, pos1, pos2, stepsize)
stepsize = stepsize or 1
local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
-- normal walking and flying mobs can see you through air
if s == true then
return true
end
-- New pos1 to be analyzed
local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
-- Checks the return
if r == true then return true end
-- Nodename found
local nn = minetest.get_node(pos).name
-- Target Distance (td) to travel
local td = get_distance(pos1, pos2)
-- Actual Distance (ad) traveled
local ad = 0
-- It continues to advance in the line of sight in search of a real
-- obstruction which counts as 'walkable' nodebox.
while minetest.registered_nodes[nn]
and (minetest.registered_nodes[nn].walkable == false) do
-- Check if you can still move forward
if td < ad + stepsize then
return true -- Reached the target
end
-- Moves the analyzed pos
local d = get_distance(pos1, pos2)
npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
-- NaN checks
if d == 0
or npos1.x ~= npos1.x
or npos1.y ~= npos1.y
or npos1.z ~= npos1.z then
return false
end
ad = ad + stepsize
-- scan again
r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
if r == true then return true end
-- New Nodename found
nn = minetest.get_node(pos).name
end
return false
end
-- check line of sight (by BrunoMine, tweaked by Astrobe)
local new_line_of_sight = function(self, pos1, pos2, stepsize)
if not pos1 or not pos2 then return end
stepsize = stepsize or 1
local stepv = vmultiply(vdirection(pos1, pos2), stepsize)
local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
-- normal walking and flying mobs can see you through air
if s == true then return true end
-- New pos1 to be analyzed
local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
-- Checks the return
if r == true then return true end
-- Nodename found
local nn = minetest.get_node(pos).name
-- It continues to advance in the line of sight in search of a real
-- obstruction which counts as 'walkable' nodebox.
while minetest.registered_nodes[nn]
and (minetest.registered_nodes[nn].walkable == false) do
npos1 = vadd(npos1, stepv)
if get_distance(npos1, pos2) < stepsize then return true end
-- scan again
r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
if r == true then return true end
-- New Nodename found
nn = minetest.get_node(pos).name
end
return false
end
-- check line of sight using raycasting (thanks Astrobe)
local ray_line_of_sight = function(self, pos1, pos2)
local ray = minetest.raycast(pos1, pos2, true, false)
local thing = ray:next()
while thing do -- thing.type, thing.ref
if thing.type == "node" then
local name = minetest.get_node(thing.under).name
if minetest.registered_items[name]
and minetest.registered_items[name].walkable then
return false
end
end
thing = ray:next()
end
return true
end
function mob_class:line_of_sight(pos1, pos2, stepsize)
if minetest.raycast then -- only use if minetest 5.0 is detected
return ray_line_of_sight(self, pos1, pos2)
end
return line_of_sight(self, pos1, pos2, stepsize)
end
-- global function
function mobs:line_of_sight(entity, pos1, pos2, stepsize)
return entity:line_of_sight(pos1, pos2, stepsize)
end
function mob_class:attempt_flight_correction(override)
if self:flight_check() and override ~= true then return true end
-- We are not flying in what we are supposed to.
-- See if we can find intended flight medium and return to it
local pos = self.object:get_pos() ; if not pos then return true end
local searchnodes = self.fly_in
if type(searchnodes) == "string" then
searchnodes = {self.fly_in}
end
local flyable_nodes = minetest.find_nodes_in_area(
{x = pos.x - 1, y = pos.y - 1, z = pos.z - 1},
{x = pos.x + 1, y = pos.y + 0, z = pos.z + 1}, searchnodes)
-- pos.y + 0 hopefully fixes floating swimmers
if #flyable_nodes < 1 then
return false
end
local escape_target = flyable_nodes[random(#flyable_nodes)]
local escape_direction = vdirection(pos, escape_target)
self.object:set_velocity(
vmultiply(escape_direction, 1))
return true
end
-- are we flying in what we are suppose to? (taikedz)
function mob_class:flight_check()
local def = minetest.registered_nodes[self.standing_in]
if not def then return false end
-- are we standing inside what we should be to fly/swim ?
if check_for(self.standing_in, self.fly_in) then
return true
end
-- stops mobs getting stuck inside stairs and plantlike nodes
if def.drawtype ~= "airlike"
and def.drawtype ~= "liquid"
and def.drawtype ~= "flowingliquid" then
return true
end
return false
end
-- turn mob to face position
local yaw_to_pos = function(self, target, rot)
rot = rot or 0
local pos = self.object:get_pos()
local vec = {x = target.x - pos.x, z = target.z - pos.z}
local yaw = (atan(vec.z / vec.x) + rot + pi / 2) - self.rotate
if target.x > pos.x then
yaw = yaw + pi
end
yaw = self:set_yaw(yaw, rot)
return yaw
end
function mobs:yaw_to_pos(self, target, rot)
return yaw_to_pos(self, target, rot)
end
-- if stay near set then periodically check for nodes and turn towards them
function mob_class:do_stay_near()
if not self.stay_near then return false end
local pos = self.object:get_pos()
local searchnodes = self.stay_near[1]
local chance = self.stay_near[2] or 10
if not pos or random(chance) > 1 then
return false
end
if type(searchnodes) == "string" then
searchnodes = {self.stay_near[1]}
end
local r = self.view_range
local nearby_nodes = minetest.find_nodes_in_area(
{x = pos.x - r, y = pos.y - 1, z = pos.z - r},
{x = pos.x + r, y = pos.y + 1, z = pos.z + r}, searchnodes)
if #nearby_nodes < 1 then
return false
end
yaw_to_pos(self, nearby_nodes[random(#nearby_nodes)])
self:set_animation("walk")
self:set_velocity(self.walk_velocity)
return true
end
-- custom particle effects
local effect = function(pos, amount, texture, min_size, max_size,
radius, gravity, glow, fall)
radius = radius or 2
min_size = min_size or 0.5
max_size = max_size or 1
gravity = gravity or -10
glow = glow or 0
if fall == true then
fall = 0
elseif fall == false then
fall = radius
else
fall = -radius
end
minetest.add_particlespawner({
amount = amount,
time = 0.25,
minpos = pos,
maxpos = pos,
minvel = {x = -radius, y = fall, z = -radius},
maxvel = {x = radius, y = radius, z = radius},
minacc = {x = 0, y = gravity, z = 0},
maxacc = {x = 0, y = gravity, z = 0},
minexptime = 0.1,
maxexptime = 1,
minsize = min_size,
maxsize = max_size,
texture = texture,
glow = glow
})
end
function mobs:effect(pos, amount, texture, min_size, max_size,
radius, gravity, glow, fall)
effect(pos, amount, texture, min_size, max_size, radius, gravity, glow, fall)
end
-- update nametag colour
function mob_class:update_tag()
local col = "#00FF00"
local qua = self.hp_max / 4
if self.health <= floor(qua * 3) then
col = "#FFFF00"
end
if self.health <= floor(qua * 2) then
col = "#FF6600"
end
if self.health <= floor(qua) then
col = "#FF0000"
end
-- build infotext
self.infotext = "Health: " .. self.health .. " / " .. self.hp_max
.. "\n" .. "Owner: " .. self.owner
-- set changes
self.object:set_properties({
nametag = self.nametag,
nametag_color = col,
infotext = self.infotext
})
end
-- drop items
function mob_class:item_drop()
-- no drops if disabled by setting or mob is child
if not mobs_drop_items or self.child then return end
local pos = self.object:get_pos()
-- check for drops function
self.drops = type(self.drops) == "function"
and self.drops(pos) or self.drops
-- check for nil or no drops
if not self.drops or #self.drops == 0 then
return
end
-- was mob killed by player?
local death_by_player = self.cause_of_death
and self.cause_of_death.puncher
and self.cause_of_death.puncher:is_player()
local obj, item, num
for n = 1, #self.drops do
if random(self.drops[n].chance) == 1 then
num = random(self.drops[n].min or 0, self.drops[n].max or 1)
item = self.drops[n].name
-- cook items on a hot death
if self.cause_of_death.hot then
local output = minetest.get_craft_result({
method = "cooking", width = 1, items = {item}})
if output and output.item and not output.item:is_empty() then
item = output.item:get_name()
end
end
-- only drop rare items (drops.min = 0) if killed by player
if death_by_player or self.drops[n].min ~= 0 then
obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
end
if obj and obj:get_luaentity() then
obj:set_velocity({
x = random(-10, 10) / 9,
y = 6,
z = random(-10, 10) / 9
})
elseif obj then
obj:remove() -- item does not exist
end
end
end
self.drops = {}
end
-- remove mob and descrease counter
local remove_mob = function(self, decrease)
self.object:remove()
if decrease and active_limit > 0 then
active_mobs = active_mobs - 1
if active_mobs < 0 then
active_mobs = 0
end
end
--print("-- active mobs: " .. active_mobs .. " / " .. active_limit)
end
-- global function for removing mobs
function mobs:remove(self, decrease)
remove_mob(self, decrease)
end
-- check if mob is dead or only hurt
function mob_class:check_for_death(cmi_cause)
-- We dead already
if self.state == "die" then
return true
end
-- has health actually changed?
if self.health == self.old_health and self.health > 0 then
return false
end
local damaged = self.health < self.old_health
self.old_health = self.health
-- still got some health? play hurt sound
if self.health > 0 then
-- only play hurt sound if damaged
if damaged then
self:mob_sound(self.sounds.damage)
end
-- make sure health isn't higher than max
if self.health > self.hp_max then
self.health = self.hp_max
end
-- backup nametag so we can show health stats
-- if not self.nametag2 then
-- self.nametag2 = self.nametag or ""
-- end
-- if show_health
-- and (cmi_cause and cmi_cause.type == "punch") then
-- self.htimer = 2
-- self.nametag = "♥ " .. self.health .. " / " .. self.hp_max
self:update_tag()
-- end
return false
end
self.cause_of_death = cmi_cause
-- drop items
self:item_drop()
self:mob_sound(self.sounds.death)
local pos = self.object:get_pos()
-- execute custom death function
if pos and self.on_die then
self:on_die(pos)
if use_cmi then
cmi.notify_die(self.object, cmi_cause)
end
remove_mob(self, true)
return true
end
-- check for custom death function and die animation
if self.animation
and self.animation.die_start
and self.animation.die_end then
local frames = self.animation.die_end - self.animation.die_start
local speed = self.animation.die_speed or 15
local length = max((frames / speed), 0)
local rot = self.animation.die_rotate and 5
self.attack = nil
self.following = nil
self.v_start = false
self.timer = 0
self.blinktimer = 0
self.passive = true
self.state = "die"
self.object:set_properties({
pointable = false, collide_with_objects = false,
automatic_rotate = rot, static_save = false
})
self:set_velocity(0)
self:set_animation("die")
minetest.after(length, function(self)
if self.object:get_luaentity() then
if use_cmi then
cmi.notify_die(self.object, cmi_cause)
end
remove_mob(self, true)
end
end, self)
return true
elseif pos then -- otherwise remove mod and show particle effect
if use_cmi then
cmi.notify_die(self.object, cmi_cause)
end
remove_mob(self, true)
effect(pos, 20, "tnt_smoke.png")
end
return true
end
-- get node but use fallback for nil or unknown
local node_ok = function(pos, fallback)
fallback = fallback or mobs.fallback_node
local node = minetest.get_node_or_nil(pos)
if node and minetest.registered_nodes[node.name] then
return node
end
return minetest.registered_nodes[fallback]
end
-- Returns true is node can deal damage to self
local is_node_dangerous = function(self, nodename)
if self.water_damage > 0
and minetest.get_item_group(nodename, "water") ~= 0 then
return true
end
if self.lava_damage > 0
and minetest.get_item_group(nodename, "lava") ~= 0 then
return true
end
if self.fire_damage > 0
and minetest.get_item_group(nodename, "fire") ~= 0 then
return true
end
if minetest.registered_nodes[nodename].damage_per_second > 0 then
return true
end