-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
5090 lines (3752 loc) · 119 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
local MP = minetest.get_modpath("mobs")
-- Check for translation method
local S
if minetest.get_translator ~= nil then
S = minetest.get_translator("mobs")
else
if minetest.get_modpath("intllib") then
dofile(minetest.get_modpath("intllib").."/init.lua")
if intllib.make_gettext_pair then
S = intllib.make_gettext_pair() -- new gettext method
else
S = intllib.Getter() -- old text file method
end
else -- boilerplate function
S = function(str, ...)
local args = {...}
return str:gsub("@%d+", function(match)
return args[tonumber(match:sub(2))]
end)
end
end
end
local FS = function(...) return minetest.formspec_escape(S(...)) end
-- CMI support check
local use_cmi = minetest.global_exists("cmi")
-- MineClone2 check
local use_mc2 = minetest.get_modpath("mcl_core")
-- Global
mobs = {
mod = "redo",
version = "20231111",
translate = S, intllib = S,
invis = minetest.global_exists("invisibility") and invisibility or {},
node_ice = "default:ice",
node_snow = minetest.registered_aliases["mapgen_snow"]
or (use_mc2 and "mcl_core:snow") or "default:snow",
node_dirt = minetest.registered_aliases["mapgen_dirt"]
or (use_mc2 and "mcl_core:dirt") or "default:dirt"
}
mobs.fallback_node = mobs.node_dirt
-- 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 deg = math.deg
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 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") ~= false
local mob_log_spawn = settings:get_bool("mob_log_spawn") == true
local active_mobs = 0
-- get loop timers for node and main functions
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
-- Use pathfinder mod if available
local pathfinder_enable = settings:get_bool("mob_pathfinder_enable") or true
-- how long before stuck mobs start searching
local pathfinding_stuck_timeout = tonumber(
settings:get("mob_pathfinding_stuck_timeout")) or 3.0
-- how long will mob follow path before giving up
local pathfinding_stuck_path_timeout = tonumber(
settings:get("mob_pathfinding_stuck_path_timeout")) or 5.0
-- which algorithm to use, Dijkstra(default) or A*_noprefetch or A*
-- fix settings not allowing "*"
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
-- max search distance from search positions (default 16)
local pathfinding_searchdistance = tonumber(
settings:get("mob_pathfinding_searchdistance") or 16)
-- max jump height (default 4)
local pathfinding_max_jump = tonumber(settings:get("mob_pathfinding_max_jump") or 4)
-- max drop height (default 6)
local pathfinding_max_drop = tonumber(settings:get("mob_pathfinding_max_drop") or 6)
-- 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
mobs.mob_class = {
state = "stand",
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,
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, -- only used when state = "attack"
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,
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}
-- function mob_sound play sound
function mob_class:mob_sound(sound)
if not sound then return end
-- higher pitch for a child
local pitch = self.child and 1.5 or 1.0
-- a little random pitch to be different
pitch = pitch + random(-10, 10) * 0.005
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
-- function do_class 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 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" then
return true
end
end
-- function collision 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 width = -self.collisionbox[1] + self.collisionbox[4] + 0.5
for _,object in ipairs(minetest.get_objects_inside_radius(pos, width)) do
if is_player(object) then
local pos2 = object:get_pos()
local vec = {x = pos.x - pos2.x, z = pos.z - pos2.z}
local force = (width + 0.5) - vector.distance(
{x = pos.x, y = 0, z = pos.z},
{x = pos2.x, y = 0, z = pos2.z})
x = x + (vec.x * force)
z = z + (vec.z * force)
end
end
return({x, z})
end
-- check if string exists in another string or table
local function check_for(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 not str then goto continue end
if not str or not str.find then print("Every node list (fly_in, follow, specific_attack or runaway_from) should contain strings, but entry '" .. _ .. "' is a " .. type(str) .. ".") end
if not str.find then goto continue end
if str then
if 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
if str == look_for then
return true
end
::continue::
end
end
return false
end
-- function set_velocity 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 {x = 0, y = 0, z = 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 [deprecated]
function mobs:set_velocity(entity, v)
mob_class.set_velocity(entity, v)
end
-- function get_velocity 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
-- function set_yaw 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
-- global function to set mob yaw
function mobs:yaw(entity, yaw, delay)
mob_class.set_yaw(entity, yaw, delay)
end
-- function set_animation 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
-- global function to set mob animation [deprecated
function mobs:set_animation(entity, anim)
entity.set_animation(entity, anim)
end
-- check line of sight (BrunoMine)
local function line_of_sight(self, pos1, pos2, stepsize)
stepsize = stepsize or 1
local s = 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 function new_line_of_sight(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 function ray_line_of_sight(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 line_of_sight autodetectyion with raycasting support if any
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 line of sight [deprecated]
function mobs:line_of_sight(entity, pos1, pos2, stepsize)
return entity:line_of_sight(pos1, pos2, stepsize)
end
-- function attempt_flight_correction internal
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 + 2, z = pos.z + 1}, searchnodes)
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
-- function flight_check - 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
-- function yaw_to_pos 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
yaw = self:set_yaw(yaw, rot)
return yaw
end
-- global [deprecated]
function mobs:yaw_to_pos(self, target, rot)
return self:yaw_to_pos(target, rot)
end
-- function do_stay_near 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
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, 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
-- global function effect
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
-- Thanks Wuzzy for the following editable settings
local HORNY_TIME = 30
local HORNY_AGAIN_TIME = 60 * 5 -- 5 minutes
local CHILD_GROW_TIME = 60 * 20 -- 20 minutes
-- function update_tag update nametag and infotext
function mob_class:update_tag()
local col
local qua = self.hp_max / 6
if self.health <= qua then
col = "#FF0000"
elseif self.health <= (qua * 2) then
col = "#FF7A00"
elseif self.health <= (qua * 3) then
col = "#FFB500"
elseif self.health <= (qua * 4) then
col = "#FFFF00"
elseif self.health <= (qua * 5) then
col = "#B4FF00"
elseif self.health > (qua * 5) then
col = "#00FF00"
end
local text = ""
if self.horny == true then
text = "\nLoving: " .. (self.hornytimer - (HORNY_TIME + HORNY_AGAIN_TIME))
elseif self.child == true 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 = "Health: " .. self.health .. " / " .. self.hp_max
.. (self.owner == "" and "" or "\nOwner: " .. self.owner)
.. text
-- set changes
self.object:set_properties({
nametag = self.nametag, nametag_color = col, infotext = self.infotext})
end
-- function item_drop 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
--print("--- looting level", looting)
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(-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
-- TODO make object class function - remove mob and descrease counter
local function remove_mob(self, decrease)
self.object:remove()
if decrease and active_limit > 1 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
-- function check_for_deathcheck 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
self:update_tag()