-
Notifications
You must be signed in to change notification settings - Fork 27
/
api.lua
2874 lines (2115 loc) · 58.6 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
-- Mobs Api (16th April 2016)
mobs = {}
mobs.mod = "redo"
-- Load settings
local damage_enabled = minetest.setting_getbool("enable_damage")
local peaceful_only = minetest.setting_getbool("only_peaceful_mobs")
local disable_blood = minetest.setting_getbool("mobs_disable_blood")
local creative = minetest.setting_getbool("creative_mode")
local spawn_protected = tonumber(minetest.setting_get("mobs_spawn_protected")) or 1
local remove_far = minetest.setting_getbool("remove_far_mobs")
-- pathfinding settings
local enable_pathfinding = true
local enable_pathfind_digging = false
local stuck_timeout = 3 -- how long before mob gets stuck in place and starts searching
local stuck_path_timeout = 10 -- how long will mob follow path before giving up
-- internal functions
local pi = math.pi
local square = math.sqrt
local atan = function(x)
if x ~= x then
--error("atan bassed NaN")
print ("atan based NaN")
return 0
else
return math.atan(x)
end
end
do_attack = function(self, player)
if self.state ~= "attack" then
if math.random(0,100) < 90
and self.sounds.war_cry then
minetest.sound_play(self.sounds.war_cry,{
object = self.object,
max_hear_distance = self.sounds.distance
})
end
self.state = "attack"
self.attack = player
end
end
set_velocity = function(self, v)
v = v or 0
local yaw = (self.object:getyaw() + self.rotate) or 0
self.object:setvelocity({
x = math.sin(yaw) * -v,
y = self.object:getvelocity().y,
z = math.cos(yaw) * v
})
end
get_velocity = function(self)
local v = self.object:getvelocity()
return (v.x * v.x + v.z * v.z) ^ 0.5
end
set_animation = function(self, type)
if not self.animation then
return
end
self.animation.current = self.animation.current or ""
self.animation.speed_normal = self.animation.speed_normal or 15
if type == "stand"
and self.animation.current ~= "stand" then
if self.animation.stand_start
and self.animation.stand_end
and self.animation.speed_normal then
self.object:set_animation({
x = self.animation.stand_start,
y = self.animation.stand_end},
self.animation.speed_normal, 0)
self.animation.current = "stand"
end
elseif type == "walk"
and self.animation.current ~= "walk" then
if self.animation.walk_start
and self.animation.walk_end
and self.animation.speed_normal then
self.object:set_animation({
x = self.animation.walk_start,
y = self.animation.walk_end},
self.animation.speed_normal, 0)
self.animation.current = "walk"
end
elseif type == "run"
and self.animation.current ~= "run" then
if self.animation.run_start
and self.animation.run_end
and self.animation.speed_run then
self.object:set_animation({
x = self.animation.run_start,
y = self.animation.run_end},
(self.animation.speed_run or self.animation.speed_normal), 0)
self.animation.current = "run"
end
elseif type == "punch"
and self.animation.current ~= "punch" then
if self.animation.punch_start
and self.animation.punch_end
and self.animation.speed_normal then
self.object:set_animation({
x = self.animation.punch_start,
y = self.animation.punch_end},
(self.animation.speed_punch or self.animation.speed_normal), 0)
self.animation.current = "punch"
end
end
end
-- check line of sight for walkers and swimmers alike
function line_of_sight_water(self, pos1, pos2, stepsize)
local s, pos_w = minetest.line_of_sight(pos1, pos2, stepsize)
-- normal walking and flying mobs can see you through air
if s == true then
return true
end
-- swimming mobs can see you through water
if s == false
and self.fly
and self.fly_in == "default:water_source" then
local nod = minetest.get_node(pos_w).name
if nod == "default:water_source"
or nod == "default:water_flowing" then
return true
end
end
return false
end
-- particle effects
function effect(pos, amount, texture, max_size, radius)
radius = radius or 2
minetest.add_particlespawner({
amount = amount,
time = 0.25,
minpos = pos,
maxpos = pos,
minvel = {x = -radius, y = -radius, z = -radius},
maxvel = {x = radius, y = radius, z = radius},
minacc = {x = -radius, y = -radius, z = -radius},
maxacc = {x = radius, y = radius, z = radius},
minexptime = 0.1,
maxexptime = 1,
minsize = 0.5,
maxsize = (max_size or 1),
texture = texture,
})
end
-- update nametag colour
function update_tag(self)
local col = "#00FF00"
local qua = self.hp_max / 4
if self.health <= math.floor(qua * 3) then
col = "#FFFF00"
end
if self.health <= math.floor(qua * 2) then
col = "#FF6600"
end
if self.health <= math.floor(qua) then
col = "#FF0000"
end
self.object:set_properties({
nametag = self.nametag,
nametag_color = col
})
end
-- check if mob is dead or only hurt
function check_for_death(self)
-- has health actually changed?
if self.health == self.old_health then
return
end
self.old_health = self.health
-- still got some health? play hurt sound
if self.health > 0 then
if self.sounds.damage then
minetest.sound_play(self.sounds.damage,{
object = self.object,
gain = 1.0,
max_hear_distance = self.sounds.distance
})
end
-- make sure health isn't higher than max
if self.health > self.hp_max then
self.health = self.hp_max
end
update_tag(self)
return false
end
-- drop items when dead
local obj
local pos = self.object:getpos()
for _,drop in pairs(self.drops) do
if math.random(1, drop.chance) == 1 then
obj = minetest.add_item(pos,
ItemStack(drop.name .. " "
.. math.random(drop.min, drop.max)))
if obj then
obj:setvelocity({
x = math.random(-1, 1),
y = 6,
z = math.random(-1, 1)
})
end
end
end
-- play death sound
if self.sounds.death then
minetest.sound_play(self.sounds.death,{
object = self.object,
gain = 1.0,
max_hear_distance = self.sounds.distance
})
end
-- execute custom death function
if self.on_die then
self.on_die(self, pos)
end
self.object:remove()
return true
end
-- check if within map limits (-30911 to 30927)
function within_limits(pos, radius)
if (pos.x - radius) > -30913
and (pos.x + radius) < 30928
and (pos.y - radius) > -30913
and (pos.y + radius) < 30928
and (pos.z - radius) > -30913
and (pos.z + radius) < 30928 then
return true -- within limits
end
return false -- beyond limits
end
-- is mob facing a cliff
local function is_at_cliff(self)
if self.fear_height == 0 then -- if 0, no falling protection!
return false
end
local yaw = self.object:getyaw()
local dir_x = -math.sin(yaw) * (self.collisionbox[4] + 0.5)
local dir_z = math.cos(yaw) * (self.collisionbox[4] + 0.5)
local pos = self.object:getpos()
local ypos = pos.y + self.collisionbox[2] -- just above floor
if 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}
, 1) then
return true
end
return false
end
-- get node but use fallback for nil or unknown
local function node_ok(pos, fallback)
fallback = fallback or "default:dirt"
local node = minetest.get_node_or_nil(pos)
if not node then
return minetest.registered_nodes[fallback]
end
if minetest.registered_nodes[node.name] then
return node
end
return minetest.registered_nodes[fallback]
end
-- environmental damage (water, lava, fire, light)
do_env_damage = function(self)
-- feed/tame text timer (so mob 'full' messages dont spam chat)
if self.htimer > 0 then
self.htimer = self.htimer - 1
end
local pos = self.object:getpos()
self.time_of_day = minetest.get_timeofday()
-- remove mob if beyond map limits
if not within_limits(pos, 0) then
self.object:remove()
return
end
-- daylight above ground
if self.light_damage ~= 0
and pos.y > 0
and self.time_of_day > 0.2
and self.time_of_day < 0.8
and (minetest.get_node_light(pos) or 0) > 12 then
self.health = self.health - self.light_damage
effect(pos, 5, "tnt_smoke.png")
end
-- what is mob standing in?
pos.y = pos.y + self.collisionbox[2] + 0.1 -- foot level
self.standing_in = node_ok(pos, "air").name
--print ("standing in " .. self.standing_in)
if self.water_damage ~= 0
or self.lava_damage ~= 0 then
local nodef = minetest.registered_nodes[self.standing_in]
pos.y = pos.y + 1
-- water
if self.water_damage ~= 0
and nodef.groups.water then
self.health = self.health - self.water_damage
effect(pos, 5, "bubble.png")
end
-- lava or fire
if self.lava_damage ~= 0
and (nodef.groups.lava
or self.standing_in == "fire:basic_flame"
or self.standing_in == "fire:permanent_flame") then
self.health = self.health - self.lava_damage
effect(pos, 5, "fire_basic_flame.png")
end
end
check_for_death(self)
end
-- jump if facing a solid node (not fences or gates)
do_jump = function(self)
if self.fly
or self.child then
return
end
local pos = self.object:getpos()
-- what is mob standing on?
pos.y = pos.y + self.collisionbox[2] - 0.2
local nod = node_ok(pos)
--print ("standing on:", nod.name, pos.y)
if minetest.registered_nodes[nod.name].walkable == false then
return
end
-- where is front
local yaw = self.object:getyaw()
local dir_x = -math.sin(yaw) * (self.collisionbox[4] + 0.5)
local dir_z = math.cos(yaw) * (self.collisionbox[4] + 0.5)
-- what is in front of mob?
local nod = node_ok({
x = pos.x + dir_x,
y = pos.y + 0.5,
z = pos.z + dir_z
})
-- thin blocks that do not need to be jumped
if nod.name == "default:snow" then
return
end
--print ("in front:", nod.name, pos.y + 0.5)
if (minetest.registered_items[nod.name].walkable
and not nod.name:find("fence")
and not nod.name:find("gate"))
or self.walk_chance == 0 then
local v = self.object:getvelocity()
v.y = self.jump_height + 1
v.x = v.x * 2.2
v.z = v.z * 2.2
self.object:setvelocity(v)
if self.sounds.jump then
minetest.sound_play(self.sounds.jump, {
object = self.object,
gain = 1.0,
max_hear_distance = self.sounds.distance
})
end
else
if self.state ~= "attack" then
self.state = "stand"
set_animation(self, "stand")
end
end
end
-- this is a faster way to calculate distance
local get_distance = function(a, b)
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
-- blast damage to entities nearby (modified from TNT mod)
function entity_physics(pos, radius)
radius = radius * 2
local objs = minetest.get_objects_inside_radius(pos, radius)
local obj_pos, dist
for _, obj in pairs(objs) do
obj_pos = obj:getpos()
dist = math.max(1, get_distance(pos, obj_pos))
local damage = math.floor((4 / dist) * radius)
local ent = obj:get_luaentity()
if obj:is_player() then
obj:set_hp(obj:get_hp() - damage)
else --if ent.health then
obj:punch(obj, 1.0, {
full_punch_interval = 1.0,
damage_groups = {fleshy = damage},
}, nil)
end
end
end
-- should mob follow what I'm holding ?
function follow_holding(self, clicker)
local item = clicker:get_wielded_item()
local t = type(self.follow)
-- single item
if t == "string"
and item:get_name() == self.follow then
return true
-- multiple items
elseif t == "table" then
for no = 1, #self.follow do
if self.follow[no] == item:get_name() then
return true
end
end
end
return false
end
-- find two animals of same type and breed if nearby and horny
local function breed(self)
-- child take 240 seconds before growing into adult
if self.child == true then
self.hornytimer = self.hornytimer + 1
if self.hornytimer > 240 then
self.child = false
self.hornytimer = 0
self.object:set_properties({
textures = self.base_texture,
mesh = self.base_mesh,
visual_size = self.base_size,
collisionbox = self.base_colbox,
})
-- jump when fully grown so not to fall into ground
self.object:setvelocity({
x = 0,
y = self.jump_height,
z = 0
})
end
return
end
-- horny animal can mate for 40 seconds,
-- afterwards horny animal cannot mate again for 200 seconds
if self.horny == true
and self.hornytimer < 240 then
self.hornytimer = self.hornytimer + 1
if self.hornytimer >= 240 then
self.hornytimer = 0
self.horny = false
end
end
-- find another same animal who is also horny and mate if close enough
if self.horny == true
and self.hornytimer <= 40 then
local pos = self.object:getpos()
effect({x = pos.x, y = pos.y + 1, z = pos.z}, 4, "heart.png")
local ents = minetest.get_objects_inside_radius(pos, 3)
local num = 0
local ent = nil
for i, obj in pairs(ents) do
ent = obj:get_luaentity()
-- check for same animal with different colour
local canmate = false
if ent then
if ent.name == self.name then
canmate = true
else
local entname = string.split(ent.name,":")
local selfname = string.split(self.name,":")
if entname[1] == selfname[1] then
entname = string.split(entname[2],"_")
selfname = string.split(selfname[2],"_")
if entname[1] == selfname[1] then
canmate = true
end
end
end
end
if ent
and canmate == true
and ent.horny == true
and ent.hornytimer <= 40 then
num = num + 1
end
-- found your mate? then have a baby
if num > 1 then
self.hornytimer = 41
ent.hornytimer = 41
-- spawn baby
minetest.after(5, function(dtime)
local mob = minetest.add_entity(pos, self.name)
local ent2 = mob:get_luaentity()
local textures = self.base_texture
if self.child_texture then
textures = self.child_texture[1]
end
mob:set_properties({
textures = textures,
visual_size = {
x = self.base_size.x / 2,
y = self.base_size.y / 2
},
collisionbox = {
self.base_colbox[1] / 2,
self.base_colbox[2] / 2,
self.base_colbox[3] / 2,
self.base_colbox[4] / 2,
self.base_colbox[5] / 2,
self.base_colbox[6] / 2
},
})
ent2.child = true
ent2.tamed = true
ent2.owner = self.owner
end)
num = 0
break
end
end
end
end
-- find and replace what mob is looking for (grass, wheat etc.)
function replace(self, pos)
if self.replace_rate
and self.child == false
and math.random(1, self.replace_rate) == 1 then
local pos = self.object:getpos()
pos.y = pos.y + self.replace_offset
-- print ("replace node = ".. minetest.get_node(pos).name, pos.y)
if self.replace_what
and self.replace_with
and self.object:getvelocity().y == 0
and #minetest.find_nodes_in_area(pos, pos, self.replace_what) > 0 then
minetest.set_node(pos, {name = self.replace_with})
-- when cow/sheep eats grass, replace wool and milk
if self.gotten == true then
self.gotten = false
self.object:set_properties(self)
end
end
end
end
-- check if daytime and also if mob is docile during daylight hours
function day_docile(self)
if self.docile_by_day == false then
return false
elseif self.docile_by_day == true
and self.time_of_day > 0.2
and self.time_of_day < 0.8 then
return true
end
end
-- path finding and smart mob routine by rnd
function smart_mobs(self, s, p, dist, dtime)
local s1 = self.path.lastpos
-- is it becoming stuck?
if math.abs(s1.x - s.x) + math.abs(s1.z - s.z) < 1.5 then
self.path.stuck_timer = self.path.stuck_timer + dtime
else
self.path.stuck_timer = 0
end
self.path.lastpos = {x = s.x, y = s.y, z = s.z}
-- im stuck, search for path
if (self.path.stuck_timer > stuck_timeout and not self.path.following)
or (self.path.stuck_timer > stuck_path_timeout
and self.path.following) then
self.path.stuck_timer = 0
-- lets try find a path, first take care of positions
-- since pathfinder is very sensitive
local sheight = self.collisionbox[5] - self.collisionbox[2]
-- round position to center of node to avoid stuck in walls
-- also adjust height for player models!
s.x = math.floor(s.x + 0.5)
s.y = math.floor(s.y + 0.5) - sheight
s.z = math.floor(s.z + 0.5)
local ssight, sground
ssight, sground = minetest.line_of_sight(s, {
x = s.x, y = s.y - 4, z = s.z}, 1)
-- determine node above ground
if not ssight then
s.y = sground.y + 1
end
local p1 = self.attack:getpos()
p1.x = math.floor(p1.x + 0.5)
p1.y = math.floor(p1.y + 0.5)
p1.z = math.floor(p1.z + 0.5)
self.path.way = minetest.find_path(s, p1, 16, 2, 6, "Dijkstra") --"A*_noprefetch")
-- attempt to unstick mob that is "daydreaming"
self.object:setpos({
x = s.x + 0.1 * (math.random() * 2 - 1),
y = s.y + 1,
z = s.z + 0.1 * (math.random() * 2 - 1)
})
self.state = ""
do_attack(self, self.attack)
-- no path found, try something else
if not self.path.way then
self.path.following = false
-- self.path.stuck = true
-- lets make way by digging/building if not accessible
if enable_pathfind_digging then
-- add block and remove one block above so
-- there is room to jump if needed
if s.y < p1.y then
if not minetest.is_protected(s, "") then
minetest.set_node(s, {name = "default:dirt"})
end
local sheight = math.ceil(self.collisionbox[5]) + 1
-- assume mob is 2 blocks high so it digs above its head
s.y = s.y + sheight
if not minetest.is_protected(s, "") then
local node1 = minetest.get_node(s).name
if node1 ~= "air"
and node1 ~= "ignore" then
minetest.set_node(s, {name = "air"})
minetest.add_item(s, ItemStack(node1))
end
end
s.y = s.y - sheight
self.object:setpos({x = s.x, y = s.y + 2, z = s.z})
else -- dig 2 blocks to make door toward player direction
local yaw1 = self.object:getyaw() + pi / 2
local p1 = {
x = s.x + math.cos(yaw1),
y = s.y,
z = s.z + math.sin(yaw1)
}
if not minetest.is_protected(p1, "") then
local node1 = minetest.get_node(p1).name
if node1 ~= "air"
and node1 ~= "ignore" then
minetest.add_item(p1, ItemStack(node1))
minetest.set_node(p1, {name = "air"})
end
p1.y = p1.y + 1
node1 = minetest.get_node(p1).name
if node1 ~= "air"
and node1 ~= "ignore" then
minetest.add_item(p1, ItemStack(node1))
minetest.set_node(p1, {name = "air"})
end
end
end
end
-- will try again in 2 second
self.path.stuck_timer = stuck_timeout - 2
-- frustration! cant find the damn path :(
if self.sounds.random then
minetest.sound_play(self.sounds.random, {
object = self.object,
max_hear_distance = self.sounds.distance
})
end
else
-- yay i found path
if self.sounds.attack then
set_velocity(self, self.walk_velocity)
minetest.sound_play(self.sounds.attack, {
object = self.object,
max_hear_distance = self.sounds.distance
})
end
-- follow path now that it has it
self.path.following = true
end
end
end
-- monster find someone to attack
local monster_attack = function(self)
if self.type ~= "monster"
or not damage_enabled
or self.state == "attack"
or day_docile(self) then
return
end
local s = self.object:getpos()
local p, sp, dist
local player, type, obj, min_player = nil, nil, nil, nil
local min_dist = self.view_range + 1
for _,oir in pairs(minetest.get_objects_inside_radius(s, self.view_range)) do
if oir:is_player() then
player = oir
type = "player"
else
obj = oir:get_luaentity()
if obj then
player = obj.object
type = obj.type
end
end
if type == "player"
or type == "npc" then
s = self.object:getpos()
p = player:getpos()
sp = s
-- aim higher to make looking up hills more realistic
p.y = p.y + 1
sp.y = sp.y + 1
dist = get_distance(p, s)
if dist < self.view_range then
-- field of view check goes here
-- choose closest player to attack
--if minetest.line_of_sight(sp, p, 2) == true
if line_of_sight_water(self, sp, p, 2) == true
and dist < min_dist then
min_dist = dist
min_player = player
end
end
end
end
-- attack player
if min_player then
do_attack(self, min_player)
end
end
-- npc, find closest monster to attack
local npc_attack = function(self)
if self.type ~= "npc"
or not self.attacks_monsters
or self.state == "attack" then
return
end
local s = self.object:getpos()
local min_dist = self.view_range + 1
local obj, min_player = nil, nil
for _, oir in pairs(minetest.get_objects_inside_radius(s, self.view_range)) do
obj = oir:get_luaentity()
if obj
and obj.type == "monster" then
p = obj.object:getpos()
dist = get_distance(p, s)
if dist < min_dist then
min_dist = dist
min_player = obj.object
end
end
end
if min_player then
do_attack(self, min_player)
end
end
-- follow player if owner or holding item, if fish outta water then flop
local follow_flop = function(self)
-- find player to follow
if (self.follow ~= ""
or self.order == "follow")
and not self.following
and self.state ~= "attack"
and self.state ~= "runaway" then
local s, p, dist
for _,player in pairs(minetest.get_connected_players()) do
s = self.object:getpos()