-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi.lua
1741 lines (1603 loc) · 48.3 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 (13th August 2015)
mobs_goblins = {}
mobs_goblins.mod = "redo"
-- Initial settings check
local damage_enabled = minetest.settings:get_bool("enable_damage") or true
local peaceful_only = minetest.settings:get_bool("only_peaceful_mobs") or false
local enable_blood = minetest.settings:get_bool("mobs_enable_blood") or true
mobs_goblins.protected = tonumber(minetest.settings:get("mobs_spawn_protected")) or 0
mobs_goblins.remove = minetest.settings:get_bool("remove_far_mobs") or false
function mobs_goblins:register_mob(name, def)
minetest.register_entity(name, {
debugging_goblins = true,
stepheight = def.stepheight or 0.6,
name = name,
description = def.description or name,
fly = def.fly,
fly_in = def.fly_in or "air",
owner = def.owner or "",
order = def.order or "",
on_die = def.on_die,
do_custom = def.do_custom,
jump_height = def.jump_height or 6,
jump_chance = def.jump_chance or 0,
rotate = math.rad(def.rotate or 0), -- 0=front, 90=side, 180=back, 270=side2
lifetimer = def.lifetimer or 180, -- 3 minutes
hp_min = def.hp_min or 5,
hp_max = def.hp_max or 10,
physical = true,
collisionbox = def.collisionbox,
visual = def.visual,
visual_size = def.visual_size or {x = 1, y = 1},
mesh = def.mesh,
makes_footstep_sound = def.makes_footstep_sound or false,
view_range = def.view_range or 5,
walk_velocity = def.walk_velocity or 1,
run_velocity = def.run_velocity or 2,
damage = def.damage,
light_damage = def.light_damage or 0,
water_damage = def.water_damage or 0,
lava_damage = def.lava_damage or 0,
fall_damage = def.fall_damage or 1,
fall_speed = def.fall_speed or -10, -- must be lower than -2 (default: -10)
drops = def.drops or {},
armor = def.armor,
on_rightclick = def.on_rightclick,
type = def.type,
attack_type = def.attack_type,
arrow = def.arrow,
shoot_interval = def.shoot_interval,
sounds = def.sounds or {},
animation = def.animation,
follow = def.follow, -- or "",
jump = def.jump or true,
walk_chance = def.walk_chance or 50,
attacks_monsters = def.attacks_monsters or false,
group_attack = def.group_attack or false,
--fov = def.fov or 120,
passive = def.passive or false,
recovery_time = def.recovery_time or 0.5,
knock_back = def.knock_back or 3,
blood_amount = def.blood_amount or 5,
blood_texture = def.blood_texture or "goblins_blood.png",
shoot_offset = def.shoot_offset or 0,
floats = def.floats or 1, -- floats in water by default
search_rate = def.search_rate, --how often to we search for nodes?
-- how often do we lookfor nodes above or below? (relative to nodes around us, will be same if undefined)
search_rate_above = def.search_rate_above or 1,
search_rate_below = def.search_rate_below or 1,
-- how far away can we look for nodes?
search_offset = def.search_offset or 0,
search_offset_above = def.search_offset_above or 0,
search_offset_below = def.search_offset_below or 0,
replace_rate = def.replace_rate, -- how often do we replace nodes we find?
-- what nodes will be replaced?
replace_what = def.replace_what,
replace_with = def.replace_with,
-- or maybe something different?
--
-- Zero is a BAD default, as random will not take a 1,0 range.
-- The mod fails with an error in the current master.
-- I recommend replacing with an unreasonably large number, or
-- nil (and changing the random call to check for nil).
-- replace_rate_secondary = def.replace_rate_secondary or 0,
replace_rate_secondary = def.replace_rate_secondary or 1000000,
--
replace_with_secondary = def.replace_with_secondary or def.replace_with,
timer = 0,
env_damage_timer = 0, -- only if state = "attack"
attack = {player=nil, dist=nil},
state = "stand",
tamed = false,
pause_timer = 0,
horny = false,
hornytimer = 0,
child = false,
gotten = false,
health = 0,
do_attack = function(self, player, dist)
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 = player
self.attack.dist = dist
end
end,
set_velocity = function(self, v)
v = (v or 0)
if def.drawtype
and def.drawtype == "side" then
self.rotate = math.rad(90)
end
local yaw = self.object:getyaw() + self.rotate
local x = math.sin(yaw) * -v
local z = math.cos(yaw) * v
self.object:setvelocity({x = x, y = self.object:getvelocity().y, z = z})
end,
get_velocity = function(self)
local v = self.object:getvelocity()
return (v.x ^ 2 + v.z ^ 2) ^ (0.5)
end,
--[[
in_fov = function(self,pos)
-- checks if POS is in self's FOV
local yaw = self.object:getyaw() + self.rotate
local vx = math.sin(yaw)
local vz = math.cos(yaw)
local ds = math.sqrt(vx ^ 2 + vz ^ 2)
local ps = math.sqrt(pos.x ^ 2 + pos.z ^ 2)
local d = { x = vx / ds, z = vz / ds }
local p = { x = pos.x / ps, z = pos.z / ps }
local an = ( d.x * p.x ) + ( d.z * p.z )
a = math.deg( math.acos( an ) )
if a > ( self.fov / 2 ) then
return false
else
return true
end
end,
]]
set_animation = function(self, type)
if not self.animation then
return
end
if not self.animation.current then
self.animation.current = ""
end
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, 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_normal, 0)
self.animation.current = "punch"
end
end
end,
on_step = function(self, dtime)
if self.type == "monster"
and peaceful_only then
self.object:remove()
return
end
-- if lifetimer run out and not npc; tamed or attacking then remove mob
if self.type ~= "npc"
and not self.tamed then
self.lifetimer = self.lifetimer - dtime
if self.lifetimer <= 0
and self.state ~= "attack" then
minetest.log("action","lifetimer expired, removed "..self.description)
effect(self.object:getpos(), 15, "tnt_smoke.png")
self.object:remove()
return
end
end
-- check for mob drop/replace (used for chicken egg and sheep eating grass/wheat)
if self.replace_rate
and self.child == false
and math.random(1,self.search_rate) == 1 then
--look for nodes
local pos1 = self.object:getpos()
local pos2 = self.object:getpos()
-- if we are looking, will we look below and by how much?
if math.random(1,self.search_rate_below) == 1 then
pos1.y = pos1.y - self.search_offset_below
end
-- if we are looking, will we look above and by how much?
if math.random(1,self.search_rate_above) == 1 then
pos2.y = pos2.y + self.search_offset_above
end
pos1.x = pos1.x - self.search_offset
pos1.z = pos1.z - self.search_offset
pos2.x = pos2.x + self.search_offset
pos2.z = pos2.z + self.search_offset
if self.debugging_goblins == true then
--print (self.name)
--print ("at " .. self.object:getpos().x, self.object:getpos().y, self.object:getpos().z)
--print ("is searching between" .. pos1.x, pos1.y, pos1.z)
--print ("and " .. pos2.x, pos2.y, pos2.z)
end
local nodelist = minetest.find_nodes_in_area(pos1, pos2, self.replace_what)
if self.replace_what
and #nodelist > 0 then
--print (#nodelist.." nodes found by "..self.description.." !!!")
--for k,v in pairs(nodelist) do print(minetest.get_node(v).name.. " found!!") end
for key,value in pairs(nodelist) do
-- ok we see some nodes around us, are we going to replace them?
if math.random(1,self.replace_rate) == 1 then
if math.random(1,self.replace_rate_secondary) == 1 then
minetest.set_node(value, {name = self.replace_with_secondary})
else
minetest.set_node(value, {name = self.replace_with})
end
if self.debugging_goblins == true then
print(self.replace_with.." placed by " .. self.name .. " at " .. self.object:getpos().x, self.object:getpos().y, self.object:getpos().z)
end
minetest.sound_play(self.sounds.replace, {
object = self.object,
max_hear_distance = self.sounds.distance })
--wait self.replace_delay
end
end
end
end
local yaw = 0
if not self.fly then
-- floating in water (or falling)
local pos = self.object:getpos()
local nod = minetest.get_node_or_nil(pos)
if nod then nod = nod.name else nod = "default:dirt" end
local nodef = minetest.registered_nodes[nod]
local v = self.object:getvelocity()
if v.y > 0.1 then
self.object:setacceleration({
x = 0,
y= self.fall_speed,
z = 0
})
end
if nodef and nodef.groups.water then
if self.floats == 1 then
self.object:setacceleration({
x = 0,
y = -self.fall_speed / (math.max(1, v.y) ^ 2),
z = 0
})
end
else
self.object:setacceleration({
x = 0,
y = self.fall_speed,
z = 0
})
-- fall damage
if self.fall_damage == 1
and self.object:getvelocity().y == 0 then
local d = (self.old_y or 0) - self.object:getpos().y
if d > 5 then
self.object:set_hp(self.object:get_hp() - math.floor(d - 5))
effect(self.object:getpos(), 5, "tnt_smoke.png")
check_for_death(self)
end
self.old_y = self.object:getpos().y
end
end
end
-- knockback timer
if self.pause_timer > 0 then
self.pause_timer = self.pause_timer - dtime
if self.pause_timer < 1 then
self.pause_timer = 0
end
return
end
-- attack timer
self.timer = self.timer + dtime
if self.state ~= "attack" then
if self.timer < 1 then
return
end
self.timer = 0
end
if self.sounds.random
and math.random(1, 100) <= 10 then
minetest.sound_play(self.sounds.random, {
object = self.object,
max_hear_distance = self.sounds.distance
})
end
local do_env_damage = function(self)
local pos = self.object:getpos()
local tod = minetest.get_timeofday()
-- daylight above ground
if self.light_damage ~= 0
and pos.y > 0
and tod > 0.2
and tod < 0.8
and (minetest.get_node_light(pos) or 0) > 12 then
self.object:set_hp(self.object:get_hp() - self.light_damage)
effect(pos, 5, "tnt_smoke.png")
if check_for_death(self) then return end
end
pos.y = pos.y + self.collisionbox[2] -- foot level
local nod = minetest.get_node_or_nil(pos)
if not nod then return end ; -- print ("standing in "..nod.name)
local nodef = minetest.registered_nodes[nod.name]
pos.y = pos.y + 1
-- water
if self.water_damage ~= 0 and nodef ~= nil
and nodef.groups.water then
self.object:set_hp(self.object:get_hp() - self.water_damage)
effect(pos, 5, "bubble.png")
if check_for_death(self) then return end
end
-- lava or fire
if self.lava_damage ~= 0 and nodef ~= nil
and (nodef.groups.lava or nod.name == "fire:basic_flame") then
self.object:set_hp(self.object:get_hp() - self.lava_damage)
effect(pos, 5, "fire_basic_flame.png")
if check_for_death(self) then return end
end
end
local do_jump = function(self)
if self.fly then
return
end
self.jumptimer = (self.jumptimer or 0) + 1
if self.jumptimer < 3 then
local pos = self.object:getpos()
pos.y = (pos.y + self.collisionbox[2]) - 0.2
local nod = minetest.get_node(pos)
--print ("standing on:", nod.name, pos.y)
if not nod
or not minetest.registered_nodes[nod.name]
or minetest.registered_nodes[nod.name].walkable == false then
return
end
if self.direction then
pos.y = pos.y + 0.5
local nod = minetest.get_node_or_nil({
x = pos.x + self.direction.x,
y = pos.y,
z = pos.z + self.direction.z
})
--print ("in front:", nod.name, pos.y)
if nod and nod.name and
(nod.name ~= "air"
or self.walk_chance == 0) then
local def = minetest.registered_items[nod.name]
if (def
and def.walkable
and not nod.name:find("fence"))
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,
max_hear_distance = self.sounds.distance
})
end
end
end
end
else
self.jumptimer = 0
end
end
-- environmental damage timer
self.env_damage_timer = self.env_damage_timer + dtime
if self.state == "attack"
and self.env_damage_timer > 1 then
self.env_damage_timer = 0
do_env_damage(self)
elseif self.state ~= "attack" then
do_env_damage(self)
end
-- find someone to attack
if self.type == "monster"
and damage_enabled
and self.state ~= "attack" then
local s = self.object:getpos()
local p, sp, dist
local player = nil
local type = nil
local obj = nil
local min_dist = self.view_range + 1
local min_player = nil
for _,oir in ipairs(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
p.y = p.y + 1
sp.y = sp.y + 1 -- aim higher to make looking up hills more realistic
dist = ((p.x - s.x) ^ 2 + (p.y - s.y) ^ 2 + (p.z - s.z) ^ 2) ^ 0.5
if dist < self.view_range then
-- and self.in_fov(self,p) then
-- choose closest player to attack
if minetest.line_of_sight(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
self.do_attack(self, min_player, min_dist)
end
end
-- npc, find closest monster to attack
local min_dist = self.view_range + 1
local min_player = nil
if self.type == "npc"
and self.attacks_monsters
and self.state ~= "attack" then
local s = self.object:getpos()
local obj = 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
-- attack monster
p = obj.object:getpos()
dist = ((p.x - s.x) ^ 2 + (p.y - s.y) ^ 2 + (p.z - s.z) ^ 2) ^ 0.5
if dist < min_dist then
min_dist = dist
min_player = obj.object
end
end
end
if min_player then
self.do_attack(self, min_player, min_dist)
end
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
and self.child == false then
self.hornytimer = self.hornytimer + 1
if self.hornytimer >= 240 then
self.hornytimer = 0
self.horny = false
end
end
-- if animal is 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,
})
end
end
-- if animal is horny, find another same animal who is horny and mate
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, self.view_range)
local num = 0
local ent = nil
for i,obj in ipairs(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
if num > 1 then
self.hornytimer = 41
ent.hornytimer = 41
minetest.after(7, function(dtime)
local mob = minetest.add_entity(pos, self.name)
local ent2 = mob:get_luaentity()
local textures = self.base_texture
if def.child_texture then
textures = def.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.following = ent -- follow mother
end)
num = 0
break
end
end
end
-- find player to follow
if (self.follow ~= ""
or self.order == "follow")
and not self.following
and self.state ~= "attack" then
local s, p, dist
for _,player in pairs(minetest.get_connected_players()) do
s = self.object:getpos()
p = player:getpos()
dist = ((p.x - s.x) ^ 2 + (p.y - s.y) ^ 2 + (p.z - s.z) ^ 2) ^ 0.5
if dist < self.view_range then
self.following = player
break
end
end
end
-- custom function (defined in mob lua file)
if self.do_custom then
self.do_custom(self)
end
if self.type == "npc"
and self.order == "follow"
and self.state ~= "attack" then
-- npc stop following player if not owner
if self.following
and self.type == "npc"
and self.owner
and self.owner ~= self.following:get_player_name() then
self.following = nil
end
else
-- stop following player if not holding specific item
if self.following
and self.following.is_player
--and self.following:get_wielded_item():get_name() ~= self.follow then
and follow_holding(self, self.following) == false then
self.following = nil
end
end
-- follow player or mob
if self.following then
local s = self.object:getpos()
local p
if self.following.is_player
and self.following:is_player() then
p = self.following:getpos()
elseif self.following.object then
p = self.following.object:getpos()
end
if p then
local dist = ((p.x - s.x) ^ 2 + (p.y - s.y) ^ 2 + (p.z - s.z) ^ 2) ^ 0.5
if dist > self.view_range then
self.following = nil
else
local vec = {x = p.x - s.x, y = p.y - s.y, z = p.z - s.z}
yaw = (math.atan(vec.z / vec.x) + math.pi / 2) - self.rotate
if p.x > s.x then
yaw = yaw + math.pi
end
self.object:setyaw(yaw)
-- anyone but standing npc's can move along
if dist > 2
and self.order ~= "stand" then
if (self.jump
and self.get_velocity(self) <= 0.5
and self.object:getvelocity().y == 0)
or (self.object:getvelocity().y == 0
and self.jump_chance > 0) then
self.direction = {
x = math.sin(yaw) * -1,
y = -20,
z = math.cos(yaw)
}
do_jump(self)
end
self.set_velocity(self, self.walk_velocity)
if self.walk_chance ~= 0 then
self:set_animation("walk")
end
else
self.set_velocity(self, 0)
self:set_animation("stand")
end
return
end
end
end
if self.state == "stand" then
-- randomly turn
if math.random(1, 4) == 1 then
-- if there is a player nearby look at them
local lp = nil
local s = self.object:getpos()
if self.type == "npc" then
local o = minetest.get_objects_inside_radius(self.object:getpos(), 3)
local yaw = 0
for _,o in ipairs(o) do
if o:is_player() then
lp = o:getpos()
break
end
end
end
if lp ~= nil then
local vec = {x = lp.x - s.x, y = lp.y - s.y, z = lp.z - s.z}
yaw = (math.atan(vec.z / vec.x) + math.pi / 2) - self.rotate
if lp.x > s.x then
yaw = yaw + math.pi
end
else
yaw = self.object:getyaw() + ((math.random(0, 360) - 180) / 180 * math.pi)
end
self.object:setyaw(yaw)
end
self.set_velocity(self, 0)
self.set_animation(self, "stand")
-- npc's ordered to stand stay standing
if self.type == "npc"
and self.order == "stand" then
self.set_velocity(self, 0)
self.state = "stand"
self:set_animation("stand")
else
if self.walk_chance ~= 0
and math.random(1, 100) <= self.walk_chance then
self.set_velocity(self, self.walk_velocity)
self.state = "walk"
self.set_animation(self, "walk")
end
-- jumping mobs only
-- if self.jump and math.random(1, 100) <= self.jump_chance then
-- self.direction = {x = 0, y = 0, z = 0}
-- do_jump(self)
-- self.set_velocity(self, self.walk_velocity)
-- end
end
elseif self.state == "walk" then
local s = self.object:getpos()
local lp = minetest.find_node_near(s, 1, {"group:water"})
-- water swimmers cannot move out of water
if self.fly
and self.fly_in == "default:water_source"
and not lp then
print ("out of water")
self.set_velocity(self, 0)
self.state = "flop" -- change to undefined state so nothing more happens
self:set_animation("stand")
return
end
-- if water nearby then turn away
if lp then
local vec = {x = lp.x - s.x, y = lp.y - s.y, z = lp.z - s.z}
yaw = math.atan(vec.z / vec.x) + 3 * math.pi / 2 - self.rotate
if lp.x > s.x then
yaw = yaw + math.pi
end
self.object:setyaw(yaw)
-- otherwise randomly turn
elseif math.random(1, 100) <= 30 then
self.object:setyaw(self.object:getyaw() + ((math.random(0, 360) - 180) / 180 * math.pi))
end
if self.jump and self.get_velocity(self) <= 0.5
and self.object:getvelocity().y == 0 then
self.direction = {
x = math.sin(yaw) * -1,
y = -20,
z = math.cos(yaw)
}
do_jump(self)
end
self:set_animation("walk")
self.set_velocity(self, self.walk_velocity)
if math.random(1, 100) <= 30 then
self.set_velocity(self, 0)
self.state = "stand"
self:set_animation("stand")
end
-- exploding mobs
elseif self.state == "attack" and self.attack_type == "explode" then
if not self.attack.player
or not self.attack.player:is_player() then
self.state = "stand"
self:set_animation("stand")
self.timer = 0
self.blinktimer = 0
return
end
local s = self.object:getpos()
local p = self.attack.player:getpos()
local dist = ((p.x - s.x) ^ 2 + (p.y - s.y) ^ 2 + (p.z - s.z) ^ 2) ^ 0.5
if dist > self.view_range or self.attack.player:get_hp() <= 0 then
self.state = "stand"
self.v_start = false
self.set_velocity(self, 0)
self.timer = 0
self.blinktimer = 0
self.attack = {player = nil, dist = nil}
self:set_animation("stand")
return
else
self:set_animation("walk")
self.attack.dist = dist
end
local vec = {x = p.x - s.x, y = p.y - s.y, z = p.z - s.z}
yaw = math.atan(vec.z / vec.x) + math.pi / 2 - self.rotate
if p.x > s.x then
yaw = yaw + math.pi
end
self.object:setyaw(yaw)
if self.attack.dist > 3 then
if not self.v_start then
self.v_start = true
self.set_velocity(self, self.run_velocity)
self.timer = 0
self.blinktimer = 0
else
self.timer = 0
self.blinktimer = 0
if self.get_velocity(self) <= 0.5
and self.object:getvelocity().y == 0 then
local v = self.object:getvelocity()
v.y = 5
self.object:setvelocity(v)
end
self.set_velocity(self, self.run_velocity)
end
self:set_animation("run")
else
self.set_velocity(self, 0)
self.timer = self.timer + dtime
self.blinktimer = (self.blinktimer or 0) + dtime
if self.blinktimer > 0.2 then
self.blinktimer = 0
if self.blinkstatus then
self.object:settexturemod("")
else
self.object:settexturemod("^[brighten")
end
self.blinkstatus = not self.blinkstatus
end
if self.timer > 3 then
local pos = vector.round(self.object:getpos())
entity_physics(pos, 3) -- hurt player/mobs caught in blast area
if minetest.find_node_near(pos, 1, {"group:water"})
or minetest.is_protected(pos, "") then
self.object:remove()
if self.sounds.explode ~= "" then
minetest.sound_play(self.sounds.explode, {
pos = pos,
gain = 1.0,
max_hear_distance = 16
})
end
effect(pos, 15, "tnt_smoke.png", 5)
return
end
self.object:remove()
pos.y = pos.y - 1
mobs_goblins:explosion(pos, 2, 0, 1, self.sounds.explode)
end
end
-- end of exploding mobs
elseif self.state == "attack"
and self.attack_type == "dogfight" then
if not self.attack.player
or not self.attack.player:getpos() then
print("stop attacking")
self.state = "stand"
self:set_animation("stand")
return
end
local s = self.object:getpos()
local p = self.attack.player:getpos()
local dist = ((p.x - s.x) ^ 2 + (p.y - s.y) ^ 2 + (p.z - s.z) ^ 2) ^ 0.5
-- fly bit modified from BlockMens creatures mod
if self.fly
and dist > 2 then
local nod = minetest.get_node_or_nil(s)
local p1 = s
local me_y = math.floor(p1.y)
local p2 = p
local p_y = math.floor(p2.y + 1)
local v = self.object:getvelocity()
if nod
and nod.name == self.fly_in then
if me_y < p_y then
self.object:setvelocity({
x = v.x,
y = 1 * self.walk_velocity,
z = v.z
})
elseif me_y > p_y then
self.object:setvelocity({
x = v.x,
y = -1 * self.walk_velocity,
z = v.z
})
end
else
if me_y < p_y then
self.object:setvelocity({
x = v.x,
y = 0.01,
z = v.z
})
elseif me_y > p_y then
self.object:setvelocity({
x = v.x,
y = -0.01,
z = v.z
})
end
end
end
-- end fly bit
if dist > self.view_range
or self.attack.player:get_hp() <= 0 then
self.state = "stand"
self.set_velocity(self, 0)
self.attack = {player = nil, dist = nil}
self:set_animation("stand")
return
else
self.attack.dist = dist
end
local vec = {x = p.x - s.x, y = p.y - s.y, z = p.z - s.z}
yaw = (math.atan(vec.z / vec.x) + math.pi / 2) - self.rotate
if p.x > s.x then
yaw = yaw + math.pi
end
self.object:setyaw(yaw)
-- attack distance is 2 + half of mob width so the bigger mobs can attack (like slimes)
if self.attack.dist > ((-self.collisionbox[1] + self.collisionbox[4]) / 2) + 2 then
-- jump attack
if (self.jump
and self.get_velocity(self) <= 0.5
and self.object:getvelocity().y == 0)
or (self.object:getvelocity().y == 0
and self.jump_chance > 0) then
self.direction = {
x = math.sin(yaw) * -1,
y = -20,
z = math.cos(yaw)
}
do_jump(self)
end