-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
4647 lines (3313 loc) · 118 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
-- translation and mod checks
local S = minetest.get_translator("mobs")
local FS = function(...) return minetest.formspec_escape(S(...)) end
local use_cmi = minetest.global_exists("cmi")
local use_mc2 = minetest.get_modpath("mcl_core") -- MineClonia support
local use_vh1 = minetest.get_modpath("visual_harm_1ndicators")
local use_tr = minetest.get_modpath("toolranks")
local use_invisibility = minetest.get_modpath("invisibility")
-- Node check helper
local function has(nodename)
return minetest.registered_nodes[nodename] and nodename
end
-- Global table
mobs = {
mod = "redo", version = "20241228",
spawning_mobs = {}, translate = S,
node_snow = has(minetest.registered_aliases["mapgen_snow"])
or has("mcl_core:snow") or has("default:snow") or "air",
node_dirt = has(minetest.registered_aliases["mapgen_dirt"])
or has("mcl_core:dirt") or has("default:dirt") or "mobs:fallback_node"
}
mobs.fallback_node = mobs.node_dirt
-- localize common functions
local pi, abs, min, max = math.pi, math.abs, math.min, math.max
local square, random = math.sqrt, math.random
local sin, cos, rad, deg = math.sin, math.cos, math.rad, math.deg
local floor, ceil, vdirection = math.floor, math.ceil, vector.direction
local vmultiply, vsubtract = vector.multiply, vector.subtract
local settings, atann = minetest.settings, math.atan
local function atan(x)
if not x or x ~= x then return 0 else return atann(x) end
end
local table_copy, table_remove = table.copy, table.remove
-- 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 mob_hit_effect = settings:get_bool("mob_hit_effect")
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 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 mob_height_fix = settings:get_bool("mob_height_fix")
local mob_log_spawn = settings:get_bool("mob_log_spawn") == true
local active_mobs = 0
-- loop interval for node and main functions timers
local node_timer_interval = tonumber(settings:get("mob_node_timer_interval") or 0.25)
local main_timer_interval = tonumber(settings:get("mob_main_timer_interval") or 1.0)
-- pathfinding settings
local pathfinding_enable = settings:get_bool("mob_pathfinding_enable") or true
local pathfinding_stuck_timeout = tonumber(
settings:get("mob_pathfinding_stuck_timeout")) or 3.0
local pathfinding_stuck_path_timeout = tonumber(
settings:get("mob_pathfinding_stuck_path_timeout")) or 5.0
local pathfinding_algorithm = settings:get("mob_pathfinding_algorithm") or "Dijkstra"
if pathfinding_algorithm == "AStar_noprefetch" then
pathfinding_algorithm = "A*_noprefetch"
elseif pathfinding_algorithm == "AStar" then
pathfinding_algorithm = "A*"
end
local pathfinding_max_jump = tonumber(settings:get("mob_pathfinding_max_jump") or 4)
local pathfinding_max_drop = tonumber(settings:get("mob_pathfinding_max_drop") or 6)
local pathfinding_searchdistance = tonumber(
settings:get("mob_pathfinding_searchdistance") or 16)
-- 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
-- can we attack Creatura mobs ?
local creatura = minetest.get_modpath("creatura") and
settings:get_bool("mobs_attack_creatura") == true
-- default mob settings
mobs.mob_class = {
state = "stand",
fly_in = "air",
owner = "",
order = "",
jump = true, jump_height = 4,
lifetimer = 180, -- 3 minutes
texture_mods = "",
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,
node_damage = true,
suffocation = 2,
fall_damage = true,
fall_speed = -10, -- must be lower than -2 (default: -10)
drops = {},
armor = 100,
sounds = {},
knock_back = true,
walk_chance = 50,
stand_chance = 30,
attack_chance = 5,
attack_patience = 11,
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,
tamed = false,
pause_timer = 0,
horny = false,
hornytimer = 0,
child = false,
gotten = false,
health = 0,
reach = 3,
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,
attack_ignore = nil,
friendly_fire = true,
facing_fence = false,
_breed_countdown = nil,
_tame_countdown = nil,
_cmi_is_mob = true
}
local mob_class = mobs.mob_class -- Compatibility
local mob_class_meta = {__index = mob_class}
-- return True if number of mobs is at limit
local function at_limit()
if active_limit and active_limit > 0
and active_mobs and active_mobs >= active_limit then
return true
end
end
-- play sound
function mob_class:mob_sound(sound)
if not sound then return end
local pitch = self.child and 1.5 or 1.0 -- higher pitch for a child
pitch = pitch + random(-10, 10) * 0.005 -- little random pitch difference
minetest.sound_play(sound, {
object = self.object,
gain = 1.0,
max_hear_distance = (self.sounds and self.sounds.distance) or 10,
pitch = pitch
}, true)
end
-- attack player/mob
function mob_class:do_attack(player, force)
if self.state == "attack" and not force then return end
self.attack = player ; self.state = "attack"
if random(100) < 90 then self:mob_sound(self.sounds.war_cry) end
end
-- calculate distance
local function get_distance(a, b)
if not a or not b then return 50 end -- nil check and default distance
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
-- are we a real player ?
local function is_player(player)
if player and type(player) == "userdata" and minetest.is_player(player) then
return true
end
end
-- collision function based on jordan4ibanez' open_ai mod
function mob_class:collision()
local pos = self.object:get_pos() ; if not pos then return 0, 0 end
local x, z = 0, 0
local prop = self.object:get_properties()
local width = -prop.collisionbox[1] + prop.collisionbox[4] + 0.5
for _,object in pairs(minetest.get_objects_inside_radius(pos, width)) do
if object:is_player() then
local pos2 = object:get_pos()
local vx, vz = pos.x - pos2.x, pos.z - pos2.z
local force = width - (vx * vx + vz * vz) ^ 0.5
if force > 0 then
force = force * 2 ; x = x + vx * force ; z = z + vz * force
end
end
end
return x, z
end
-- check if string exists in another string or table
local function check_for(look_for, look_inside)
if 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 and str:find("group:") then
local group = str:split(":")[2] or ""
if minetest.get_item_group(look_for, group) ~= 0 then return true end
end
end
end
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 = 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 {y = 0}
self.object:set_velocity({
x = (sin(yaw) * -v) + c_x, y = vel.y, z = (cos(yaw) * v) + c_y})
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
-- simplified yaw clamp
if yaw > 6.283185 then yaw = yaw - 6.283185
elseif yaw < 0 then yaw = 6.283185 + yaw
end
if delay == 0 then self.object:set_yaw(yaw) ; return yaw ; end
self.target_yaw = yaw
self.delay = delay
return self.target_yaw
end
function mobs:yaw(entity, yaw, delay) -- [deprecated]
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 and force ~= true)
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) -- [deprecated]
entity.set_animation(entity, anim)
end
-- check line of sight using raycasting (thanks Astrobe)
function mob_class:line_of_sight(pos1, pos2)
local ray = minetest.raycast(pos1, pos2, true, false) -- ignore entities
local thing = ray:next()
local name, nodedef
while thing do
if thing.type == "node" then
name = minetest.get_node(thing.under).name
nodedef = minetest.registered_items[name]
if nodedef and nodedef.walkable then return false end
end
thing = ray:next()
end
return true
end
function mobs:line_of_sight(entity, pos1, pos2) -- [deprecated]
return entity:line_of_sight(pos1, pos2)
end
-- if mob not flying in medium it's suppose to, try and find some to return to
function mob_class:attempt_flight_correction(override)
if self:flight_check() and override ~= true then return true end
local pos = self.object:get_pos() ; if not pos then return true 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 + 1, z = pos.z + 1}, self.fly_in)
if #flyable_nodes == 0 then return false end
local escape_target = flyable_nodes[random(#flyable_nodes)]
-- stop swimming mobs moving above water surface
if escape_target.y > pos.y and #minetest.find_nodes_in_area(
{x = escape_target.x, y = escape_target.y + 1, z = escape_target.z},
{x = escape_target.x, y = escape_target.y + 1, z = escape_target.z},
self.fly_in) == 0 then
escape_target.y = pos.y
end
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
end
-- turn mob to face position
function mob_class:yaw_to_pos(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
return self:set_yaw(yaw, rot)
end
function mobs:yaw_to_pos(self, target, rot) -- [deprecated]
return self:yaw_to_pos(target, rot)
end
-- if stay near set then periodically check for nodes and move towards them
function mob_class:do_stay_near()
if not self.stay_near then return false end
local pos = self.object:get_pos()
local chance = self.stay_near[2] or 10
if not pos or random(chance) > 1 then return false 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}, self.stay_near[1])
if #nearby_nodes == 0 then return false end
self:yaw_to_pos(nearby_nodes[random(#nearby_nodes)])
self:set_animation("walk")
self:set_velocity(self.walk_velocity)
return true
end
-- custom particle effects
local function effect(pos, amount, texture, min_size, max_size, radius, grav, glow, fall)
radius = radius or 2
grav = grav or -10
fall = fall == true and 0 or fall == false and radius or -radius
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 = grav, z = 0},
maxacc = {x = 0, y = grav, z = 0},
minexptime = 0.1, maxexptime = 1,
minsize = min_size or 0.5, maxsize = max_size or 1,
texture = texture,
glow = glow or 0
})
end
function mobs:effect(pos, amount, texture, min_size, max_size, radius, grav, glow, fall)
effect(pos, amount, texture, min_size, max_size, radius, grav, glow, fall)
end
-- Thanks Wuzzy for the editable settings
local HORNY_TIME = 30
local HORNY_AGAIN_TIME = 60 * 5 -- 5 minutes
local CHILD_GROW_TIME = 60 * 20 -- 20 minutes
-- update nametag and infotext
function mob_class:update_tag(newname)
local col
local prop = self.object:get_properties()
local qua = prop.hp_max / 6
local old_nametag = prop.nametag
local old_nametag_color = self.nametag_col
-- backwards compatibility
if self.nametag and self.nametag ~= "" then
newname = self.nametag ; self.nametag = nil
end
if newname or (self._nametag and self._nametag ~= "") then
self._nametag = newname or self._nametag -- adopt new name if one found
-- choose tag colour depending on mob health
if self.health <= qua then self.nametag_col = "#FF0000"
elseif self.health <= (qua * 2) then self.nametag_col = "#FF7A00"
elseif self.health <= (qua * 3) then self.nametag_col = "#FFB500"
elseif self.health <= (qua * 4) then self.nametag_col = "#FFFF00"
elseif self.health <= (qua * 5) then self.nametag_col = "#B4FF00"
elseif self.health > (qua * 5) then self.nametag_col = "#00FF00"
end
if self._nametag ~= old_nametag or self.nametag_col ~= old_nametag_color then
self.object:set_properties({
nametag = self._nametag, nametag_color = self.nametag_col})
end
end
local text = ""
if self.horny then
text = "\nLoving: " .. (self.hornytimer - (HORNY_TIME + HORNY_AGAIN_TIME))
elseif self.child then
text = "\nGrowing: " .. (self.hornytimer - CHILD_GROW_TIME)
elseif self._tame_countdown then
text = "\nTaming: " .. self._tame_countdown
elseif self._breed_countdown then
text = "\nBreeding: " .. self._breed_countdown
end
if self.protected then
if self.protected == 2 then
text = text .. "\nProtection: Level 2"
else
text = text .. "\nProtection: Level 1"
end
end
self.infotext = "Entity: " .. self.name .. " | Type: " .. self.type
.. ("\nHealth: " .. self.health .. " / " .. prop.hp_max)
.. (self.owner == "" and "" or "\nOwner: " .. self.owner) .. text
-- set infotext changes
if self.infotext ~= prop.infotext then
self.object:set_properties({infotext = self.infotext})
end
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 is_player(self.cause_of_death.puncher)
-- check for tool 'looting_level' under tool_capabilities as default, or use
-- meta string 'looting_level' if found (max looting level is 3).
local looting = 0
if death_by_player then
local wield_stack = self.cause_of_death.puncher:get_wielded_item()
local wield_name = wield_stack:get_name()
local wield_stack_meta = wield_stack:get_meta()
local item_def = minetest.registered_items[wield_name]
local item_looting = item_def and item_def.tool_capabilities and
item_def.tool_capabilities.looting_level or 0
looting = tonumber(wield_stack_meta:get_string("looting_level")) or item_looting
looting = min(looting, 3)
end
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 + looting)))
end
if obj and obj:get_luaentity() then
obj:set_velocity({x = random() - 0.5, y = 4, z = random() - 0.5})
elseif obj then
obj:remove() -- item does not exist
end
end
end
self.drops = {}
end
-- remove mob and descrease counter
local function remove_mob(self, decrease)
self.object:remove()
if decrease and active_limit and active_limit > 1 then
active_mobs = active_mobs - 1
--print("-- active mobs: " .. active_mobs .. " / " .. active_limit)
end
end
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
local prop = self.object:get_properties()
self.old_health = self.health
if use_vh1 then VH1.update_bar(self.object, self.health) end
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 > prop.hp_max then self.health = prop.hp_max end
self:update_tag() ; return false
end
self.cause_of_death = cmi_cause
-- drop items and play death sound
self:item_drop()
self:mob_sound(self.sounds.death)
local pos = self.object:get_pos()
-- execute official engine on_death function if found
if self.on_death then
-- only return killer if punched by player
if cmi_cause.type == "punch" and is_player(cmi_cause.puncher) then
cmi_cause = cmi_cause.puncher
else
cmi_cause = nil
end
self:on_death(cmi_cause)
remove_mob(self, true) ; return true
end
-- 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
-- reset vars and set state
self.attack = nil
self.following = nil
self.v_start = false ; self.timer = 0 ; self.blinktimer = 0
self.passive = true
self.state = "die"
self.fly = false
-- 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.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 mob 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 function node_ok(pos, fallback)
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 or mobs.fallback_node)]
end
function mobs:node_ok(pos, fallback)
return node_ok(pos, fallback)
end
-- Returns true if node can deal damage to self
local function is_node_dangerous(self, nodename)
local def = minetest.registered_nodes[nodename]
if (self.water_damage and def.groups.water)
or (self.lava_damage and def.groups.lava)
or (self.fire_damage and def.groups.fire) then return true end
if self.node_damage and def.damage_per_second > 0 then
-- check for node immunity or special damage
local damage = def.damage_per_second
for n = 1, #self.immune_to do
if self.immune_to[n][1] == nodename then
damage = self.immune_to[n][2] or 0 ; break
end
end
if damage > 0 then return true end
end
end
function mobs:is_node_dangerous(mob_object, nodename)
return is_node_dangerous(mob_object, nodename)
end
-- is mob facing a cliff
function mob_class:is_at_cliff()
if self.driver or self.fear_height == 0 then -- 0 for no falling protection!
return false
end
-- get yaw but if nil returned object no longer exists
local yaw = self.object:get_yaw() ; if not yaw then return false end
local prop = self.object:get_properties()
local dir_x = -sin(yaw) * (prop.collisionbox[4] + 0.5)
local dir_z = cos(yaw) * (prop.collisionbox[4] + 0.5)
local pos = self.object:get_pos()
local ypos = pos.y + prop.collisionbox[2] -- just above floor
local free_fall, blocker = minetest.line_of_sight(
{x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
{x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z})
-- check for straight drop
if free_fall then return true end
local bnode = node_ok(blocker, "air")
-- will we drop onto dangerous node?
if is_node_dangerous(self, bnode.name) then return true end
local def = minetest.registered_nodes[bnode.name]
return (not def and def.walkable)
end
-- check for nodes or groups inside mob collision area
function mob_class:is_inside(itemtable)
local cb = self.object:get_properties().collisionbox
local pos = self.object:get_pos()
local nn = minetest.find_nodes_in_area(
vector.offset(pos, cb[1], cb[2], cb[3]),
vector.offset(pos, cb[4], cb[5], cb[6]), itemtable)
if nn and #nn > 0 then return true end
end
-- environmental damage (water, lava, fire, light etc.)
function mob_class:do_env_damage()
local pos = self.object:get_pos() ; if not pos then return end
self:update_tag()
self.time_of_day = minetest.get_timeofday()
-- halt mob if standing inside ignore node
if self.standing_in == "ignore" then
self.object:set_velocity({x = 0, y = 0, z = 0}) ; return true
end
local prop = self.object:get_properties()
local py = {x = pos.x, y = pos.y + prop.collisionbox[5], z = pos.z}
local nodef = minetest.registered_nodes[self.standing_in]
-- water damage
if self.water_damage ~= 0 and nodef.groups.water then
self.health = self.health - self.water_damage
effect(py, 5, "bubble.png", nil, nil, 1, nil)
if self:check_for_death({type = "environment",
pos = pos, node = self.standing_in}) then return true end
end
-- lava damage
if self.lava_damage ~= 0 and nodef.groups.lava then
self.health = self.health - self.lava_damage
effect(py, 15, "fire_basic_flame.png", 1, 5, 1, 0.2, 15, true)
if self:check_for_death({type = "environment", pos = pos,
node = self.standing_in, hot = true}) then return true end
end
-- fire damage
if self.fire_damage ~= 0 and nodef.groups.fire then
self.health = self.health - self.fire_damage
effect(py, 15, "fire_basic_flame.png", 1, 5, 1, 0.2, 15, true)
if self:check_for_death({type = "environment", pos = pos,
node = self.standing_in, hot = true}) then return true end
end
-- damage_per_second node check (not fire and lava)
if self.node_damage and nodef.damage_per_second and nodef.damage_per_second ~= 0
and nodef.groups.lava == nil and nodef.groups.fire == nil then
local damage = nodef.damage_per_second
-- check for node immunity or special damage
for n = 1, #self.immune_to do
if self.immune_to[n][1] == self.standing_in then
damage = self.immune_to[n][2] or 0 ; break
end
end
self.health = self.health - damage
if damage > 0 then effect(py, 5, "tnt_smoke.png") end
if self:check_for_death({type = "environment",
pos = pos, node = self.standing_in}) then return true end
end
-- air damage
if self.air_damage ~= 0 and self.standing_in == "air" then
self.health = self.health - self.air_damage
effect(py, 3, "bubble.png", 1, 1, 1, 0.2)
if self:check_for_death({type = "environment",
pos = pos, node = self.standing_in}) then return true end