-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbehaviors.lua
More file actions
1296 lines (1166 loc) · 34.8 KB
/
behaviors.lua
File metadata and controls
1296 lines (1166 loc) · 34.8 KB
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 abs = math.abs
local pi = math.pi
local floor = math.floor
local ceil = math.ceil
local sqrt = math.sqrt
local max = math.max
local min = math.min
local pow = math.pow
local sign = math.sign
local time = os.time
local rad = math.rad
local random = water_life.random
local deg=math.deg
local tan = math.tan
local cos = math.cos
local atan=math.atan
-- flying stuff
local function chose_turn(self,a,b)
local remember = mobkit.recall(self,"turn")
if not remember then
if water_life.leftorright() then
remember = "1"
mobkit.remember(self,"time", self.time_total)
mobkit.remember(self,"turn", "1")
else
remember = "0"
mobkit.remember(self,"time", self.time_total)
mobkit.remember(self,"turn", "0")
end
end
if a > b then
mobkit.remember(self,"turn", "1")
mobkit.remember(self,"time", self.time_total)
return false
elseif a < b then
mobkit.remember(self,"turn","0")
mobkit.remember(self,"time", self.time_total)
return true
else
if remember == "0" then
return true
else
return false
end
end
end
local function pitchroll2pitchyaw(aoa,roll)
if roll == 0.0 then
return aoa,0
end
-- assumed vector x=0,y=0,z=1
local p1 = tan(aoa)
local y = cos(roll)*p1
local x = sqrt(p1^2-y^2)
local pitch = atan(y)
local yaw=atan(x)*math.sign(roll)
return pitch,yaw
end
------------------
-- LQ behaviors --
------------------
--[[
params:
lift: [number]
multiplier for lift. faster objects need less, slower need more. typical value: 0.6 for speeds around 4 m/s
pitch: [degrees]
angle between the longitudinal axis and horizontal plane. typical range: <-15.15>
aoa:
[degrees] angle of attack - the angle between the longitudinal axis and velocity vector.
roll: [degrees]
bank angle. positive is right, negative is left, this is how they turn. if set too large they'll loose height rapidly
acc: [number]
propulsion. use with positive pitch to make them fly level or climb, set it to 0 with slight negative pitch to make
them glide. typical value: around 1.0
anim: [string]
animation.
The example uses two simple high level behaviors to keep them between 18 and 24 nodes above ground, seems good already for ambient type flying creatures.
warning: never set_velocity when using these behaviors.
]]
function water_life.lq_fly_aoa(self,lift,aoa,roll,acc,anim)
aoa=rad(aoa)
roll=rad(roll)
local hpitch = 0
local hyaw = 0
local caoa = 0
local laoa = nil
local croll=roll
local lroll = nil
local lastrot = nil
local init = true
local func=function(self)
local rotation=self.object:get_rotation()
local vel = self.object:get_velocity()
local vrot = mobkit.dir_to_rot(vel,lastrot)
lastrot = vrot
if init then
if anim then mobkit.animate(self,anim) end
init = false
end
local accel=self.object:get_acceleration()
-- gradual changes
if abs(roll-rotation.z) > 0.5*self.dtime then
croll = rotation.z+0.5*self.dtime*math.sign(roll-rotation.z)
end
if croll~=lroll then
hpitch,hyaw = pitchroll2pitchyaw(aoa,croll)
lroll = croll
end
local hrot = {x=vrot.x+hpitch,y=vrot.y-hyaw,z=croll}
self.object:set_rotation(hrot)
local hdir = mobkit.rot_to_dir(hrot)
local cross = vector.cross(vel,hdir)
local lift_dir = vector.normalize(vector.cross(cross,hdir))
local daoa = deg(aoa)
-- homegrown formula
local lift_coefficient = 0.24*abs(daoa)*(1/(0.025*daoa+1))^4*math.sign(aoa)
local lift_val = lift*vector.length(vel)^2*lift_coefficient
local lift_acc = vector.multiply(lift_dir,lift_val)
lift_acc=vector.add(vector.multiply(minetest.yaw_to_dir(rotation.y),acc),lift_acc)
self.object:set_acceleration(vector.add(accel,lift_acc))
end
mobkit.queue_low(self,func)
end
function water_life.lq_fly_pitch(self,lift,pitch,roll,acc,anim)
pitch = rad(pitch)
roll=rad(roll)
local cpitch = pitch
local croll = roll
local hpitch = 0
local hyaw = 0
local lpitch = nil
local lroll = nil
local lastrot = nil
local init = true
local func=function(self)
if init then
if anim then mobkit.animate(self,anim) end
init = false
end
local rotation=self.object:get_rotation()
local accel=self.object:get_acceleration()
local vel = self.object:get_velocity()
local speed = vector.length(vel)
local vdir = vector.normalize(vel)
local vrot = mobkit.dir_to_rot(vel,lastrot)
lastrot = vrot
-- gradual changes
if abs(roll-rotation.z) > 0.5*self.dtime then
croll = rotation.z+0.5*self.dtime*math.sign(roll-rotation.z)
end
if abs(pitch-rotation.x) > 0.5*self.dtime then
cpitch = rotation.x+0.5*self.dtime*math.sign(pitch-rotation.x)
end
if cpitch~=lpitch or croll~=lroll then
hpitch,hyaw = pitchroll2pitchyaw(cpitch,croll)
lpitch = cpitch lroll = croll
end
-- angle of attack
local aoa = deg(-vrot.x+cpitch)
-- hull rotation
local hrot = {x=hpitch, y=vrot.y-hyaw, z=croll}
self.object:set_rotation(hrot)
-- hull dir
local hdir = mobkit.rot_to_dir(hrot)
local cross = vector.cross(hdir,vel)
local lift_dir = vector.normalize(vector.cross(hdir,cross))
-- homegrown formula
local lift_coefficient = 0.24*max(aoa,0)*(1/(0.025*max(aoa,0)+1))^4
local lift_val = min(lift*speed^2*lift_coefficient,20)
local lift_acc = vector.multiply(lift_dir,lift_val)
lift_acc=vector.add(vector.multiply(minetest.yaw_to_dir(rotation.y),acc),lift_acc)
accel=vector.add(accel,lift_acc)
-- drag
accel=vector.add(accel,vector.multiply(vdir,-speed*speed*0.02))
-- propeller
accel=vector.add(accel,vector.multiply(hdir,acc))
self.object:set_acceleration(accel)
end
mobkit.queue_low(self,func)
end
function water_life.lq_dumbjump(self,height,anim)
anim = anim or 'stand'
local jump = true
local func=function(self)
local yaw = self.object:get_yaw()
if jump then
mobkit.animate(self,anim)
local dir = minetest.yaw_to_dir(yaw)
dir.y = -mobkit.gravity*sqrt((height+0.35)*2/-mobkit.gravity)
self.object:set_velocity(dir)
jump = false
else -- the eagle has landed
return true
end
end
mobkit.queue_low(self,func)
end
function water_life.lq_dumbwalk(self,dest,speed_factor)
local timer = 3 -- failsafe
speed_factor = speed_factor or 1
local func=function(self)
mobkit.animate(self,'walk')
timer = timer - self.dtime
if timer < 0 then return true end
local pos = mobkit.get_stand_pos(self)
local y = self.object:get_velocity().y
local dir = vector.normalize(vector.direction({x=pos.x,y=0,z=pos.z},
{x=dest.x,y=0,z=dest.z}))
dir = vector.multiply(dir,self.max_speed*speed_factor)
mobkit.turn2yaw(self,minetest.dir_to_yaw(dir))
dir.y = y
self.object:set_velocity(dir)
end
mobkit.queue_low(self,func)
end
function water_life.lq_jumpattack(self,height,target,extra)
local phase=1
local timer=0.5
local tgtbox = water_life.getCollisionBox(target) --target:get_properties().initial_properties.collisionbox
local func=function(self)
local selfname = self.object:get_luaentity().name
if not mobkit.is_alive(target) then return true end
if self.isonground then
if phase==1 then -- collision bug workaround
local vel = self.object:get_velocity()
vel.y = -mobkit.gravity*sqrt(height*2/-mobkit.gravity)
self.object:set_velocity(vel)
mobkit.make_sound(self,'charge')
phase=2
else
mobkit.lq_idle(self,0.3)
return true
end
elseif phase==2 then
local dir = minetest.yaw_to_dir(self.object:get_yaw())
local vy = self.object:get_velocity().y
dir=vector.multiply(dir,6)
dir.y=vy
self.object:set_velocity(dir)
phase=3
elseif phase==3 then -- in air
local tgtpos = target:get_pos()
local pos = self.object:get_pos()
-- calculate attack spot
local yaw = self.object:get_yaw()
local dir = minetest.yaw_to_dir(yaw)
local apos = mobkit.pos_translate2d(pos,yaw,self.attack.range)
if mobkit.is_pos_in_box(apos,tgtpos,tgtbox) then --bite
target:punch(self.object,1,self.attack)
if selfname and target:is_player() then
if selfname == "water_life:snake" then
local meta = target:get_meta()
local name = target:get_player_name()
local join = meta:get_int("jointime")
local immune = meta:get_int("water_life_immune")
local biteCount = meta:get_int("water_life_bitecount")
local snakeCount = meta:get_int("snakecount")
if biteCount < 65535 then
meta:set_int("water_life_bitecount", biteCount + 1)
end
if immune > 0 then
meta:set_int("bitten", 1)
minetest.after(10,function()
meta:set_int("bitten",0)
end,meta)
return true
end
if water_life.checkSnakeImmunity(biteCount, snakeCount) then
meta:set_int("water_life_immune", 1)
minetest.chat_send_player(target:get_player_name(),
minetest.colorize('#fd4000', "CONGRATS!\nAfter "
..(biteCount + 1).." times bitten by a rattlesnake"..
" you are finally IMMUNE to their poison."))
return true
end
if not join or (os.time() - join) >
water_life.newplayerbonus * 86400 then
meta:set_int("snakepoison",1)
water_life.change_hud(target,"poison")
else
local left = water_life.newplayerbonus -
math.floor((os.time() - join) /
86400 * 100) / 100
minetest.chat_send_player(
target:get_player_name(),
minetest.colorize('#fd4000',
">>> A rattlesnake bit you. New player"..
"bonus of "..left.. " days left. Catch "..
"3 snakes to craft antiserum"))
end
meta:set_int("bitten", 1)
minetest.after(10,function()
meta:set_int("bitten",0)
end,meta)
end
end
-- bounce off
local vy = self.object:get_velocity().y
self.object:set_velocity({x=dir.x*-3,y=vy,z=dir.z*-3})
mobkit.make_sound(self,'attack')
phase=4
end
end
end
mobkit.queue_low(self,func)
end
------------------
-- HQ behaviors --
------------------
-- on land only, go to tgt and remove it
function water_life.hq_catch_drop(self,prty,tgt)
local func = function(self)
if self.isinliquid then return true end
if not tgt then return true end
if mobkit.is_queue_empty_low(self) then
local pos = mobkit.get_stand_pos(self)
local tpos = tgt:get_pos()
if pos and tpos then
local dist = vector.distance(pos,tpos)
if dist < 2 then
tgt:remove()
return true
else
if pos.y +0.5 >= tpos.y then
water_life.lq_dumbwalk(self,tpos,0.1)
else
water_life.lq_dumbjump(self,1)
end
end
else
return true
end
end
end
mobkit.queue_high(self,func,prty)
end
function water_life.hq_aquaidle(self,prty,anim)
local init = true
if not anim then anim = 'def' end
local func = function(self)
if init then
mobkit.animate(self,anim)
self.object:set_velocity({x=0,y=0,z=0})
init = false
end
if self.name == "water_life:alligator" then
if random(100) < 5 then
mobkit.animate(self,'roll')
end
end
end
mobkit.queue_high(self,func,prty)
end
-- same as mobkit.hq_aqua_turn but for large mobs
function water_life.big_hq_aqua_turn(self,prty,tyaw,speed)
local func = function(self)
if not speed then speed = 0.4 end
if speed < 0 then speed = speed * -1 end
local finished=mobkit.turn2yaw(self,tyaw,speed)
if finished then return true end
end
mobkit.queue_high(self,func,prty)
end
-- same as mobkit.hq_aqua_roam but for large mobs
function water_life.big_aqua_roam(self,prty,speed,anim)
local tyaw = 0
local init = true
local prvscanpos = {x=0,y=0,z=0}
local center = self.object:get_pos()
if not anim then anim = 'def' end
local func = function(self)
if init then
mobkit.animate(self,anim)
init = false
end
if self.isonground then return true end
local pos = mobkit.get_stand_pos(self)
local yaw = self.object:get_yaw()
local scanpos = mobkit.get_node_pos(mobkit.pos_translate2d(pos,yaw,speed))
if not vector.equals(prvscanpos,scanpos) then
prvscanpos=scanpos
local nyaw,height = water_life.aqua_radar_dumb(pos,yaw,speed,true)
if height and height > pos.y then
local vel = self.object:get_velocity()
vel.y = vel.y + 0.1
self.object:set_velocity(vel)
end
if yaw ~= nyaw then
tyaw=nyaw
mobkit.hq_aqua_turn(self,prty + 1, tyaw,speed)
return
end
end
if mobkit.timer(self,10) then
if vector.distance(pos,center) > water_life.abr*16*0.5 then
tyaw = minetest.dir_to_yaw(vector.direction(pos,
{x=center.x + random() * 10 - 5,y = center.y,
z=center.z + random() * 10 - 5}))
else
if random(10)>=9 then tyaw=tyaw+random()*pi - pi*0.5 end
end
end
if mobkit.timer(self,20) then mobkit.turn2yaw(self,tyaw,-1) end
mobkit.go_forward_horizontal(self,speed)
end
mobkit.queue_high(self,func,prty)
end
-- this is the same as mobkit's, but allows movement in shallow water
function water_life.hq_aqua_roam(self,prty,speed,anim,shallow)
if not anim then anim = "def" end
if shallow == nil then shallow = true end
local tyaw = 0
local init = true
local prvscanpos = {x=0,y=0,z=0}
local center = self.object:get_pos()
local func = function(self)
if init then
mobkit.animate(self,anim)
init = false
end
if mobkit.timer(self,1) and self.isonground then
return true
end
local pos = mobkit.get_stand_pos(self)
local yaw = self.object:get_yaw()
local scanpos = mobkit.get_node_pos(mobkit.pos_translate2d(pos,yaw,speed))
if not vector.equals(prvscanpos,scanpos) then
prvscanpos=scanpos
local nyaw,height = water_life.aqua_radar_dumb(pos,yaw,speed,false,shallow)
if height and height > pos.y then
local vel = self.object:get_velocity()
vel.y = vel.y+1
self.object:set_velocity(vel)
end
if yaw ~= nyaw then
tyaw=nyaw
mobkit.hq_aqua_turn(self,prty + 1,tyaw,speed)
return true
end
end
if mobkit.timer(self,1) then
if vector.distance(pos,center) > water_life.abr*16*0.5 then
tyaw = minetest.dir_to_yaw(vector.direction(
pos,{x=center.x+random()*10-5,y=center.y,
z=center.z+random()*10-5}))
else
if random(10)>=9 then tyaw=tyaw+random()*pi - pi*0.5 end
end
end
mobkit.turn2yaw(self,tyaw,3)
mobkit.go_forward_horizontal(self,speed)
end
mobkit.queue_high(self,func,prty)
end
function water_life.hq_attack(self,prty,tgtobj)
local func = function(self)
if self.isinliquid then return true end
if not mobkit.is_alive(tgtobj) then return true end
if mobkit.is_queue_empty_low(self) then
local meta = nil
local name = self.object:get_luaentity().name
local poison = 0
local noob = 0
local pos = mobkit.get_stand_pos(self)
local tpos = mobkit.get_stand_pos(tgtobj)
local dist = vector.distance(pos,tpos)
if tgtobj:is_player() then
meta = tgtobj:get_meta()
poison = meta:get_int("snakepoison")
noob = meta:get_int("bitten")
end
if ( name == "water_life:snake") and (
poison > 0 or noob > 0) then
return true
else
if dist and dist > 8 then
return true
end
mobkit.lq_turn2pos(self,tpos)
local height = tgtobj:is_player() and 0.35 or
tgtobj:get_luaentity().height*0.3
if tpos.y+height>pos.y then
mobkit.make_sound(self,"attack")
water_life.lq_jumpattack(self,tpos.y+height-pos.y,tgtobj)
else
mobkit.lq_dumbwalk(self,mobkit.pos_shift(tpos,
{x=random()-0.5,z=random()-0.5}))
end
end
end
end
mobkit.queue_high(self,func,prty)
end
function water_life.hq_hunt(self, prty, tgtobj, lost, anim)
if not lost then lost = self.view_range end
if water_life.random(100) < 33 then mobkit.make_sound(self,"attack") end
local func = function(self)
if not mobkit.is_alive(tgtobj) or not tgtobj then return true end
if self.isinliquid then return true end
if mobkit.is_queue_empty_low(self) and self.isonground then
local pos = mobkit.get_stand_pos(self)
local opos = tgtobj:get_pos()
local dist = vector.distance(pos,opos)
local meta = nil
local poison = 0
local noob = 0
if tgtobj:is_player() then
meta = tgtobj:get_meta()
poison = meta:get_int("snakepoison")
noob = meta:get_int("bitten")
end
if poison > 0 or noob > 0 then
return true
end
if mobkit.is_in_deep(tgtobj) then
return true
end
if dist > lost or math.abs(pos.y - opos.y) > 4 then
return true
elseif dist > 3 then
water_life.goto_next_waypoint(self,opos)
else
water_life.hq_attack(self,prty,tgtobj)
return true
end
end
end
mobkit.queue_high(self,func,prty)
end
-- slowly roam on land, breaks are taken with max of 120 seconds
function water_life.hq_slow_roam(self,prty,idle)
if not idle then idle = random(30,120) end
local func=function(self)
if self.isinliquid then return true end
if mobkit.is_queue_empty_low(self) then
local neighbor = random(8)
local height, tpos, liquidflag = mobkit.is_neighbor_node_reachable(
self,neighbor)
if height and tpos then
mobkit.dumbstep(self,height,tpos,0.1)--,idle)
else
return true
end
end
end
mobkit.queue_high(self,func,prty)
end
--find any water nearby and go into it
function water_life.hq_go2water(self,prty,speed)
local pos = mobkit.get_stand_pos(self)
local target = water_life.get_pos_with_depth(pos, self.view_range, 2, 100)
speed = speed or 0.1
local dist = water_life.getCollisionBox(self)[5] --self.initial_properties.collisionbox[5] or 2
dist = dist - water_life.getCollisionBox(self)[2] --(self.initial_properties.collisionbox[2] or 1)
if not target then
return true
end
--water_life.temp_show(target, 5, 15)
local func=function(self)
if self.isinliquid and vector.distance(target, self.object:get_pos()) <= dist then
mobkit.clear_queue_high(self)
mobkit.clear_queue_low(self)
return true
end
if mobkit.is_queue_empty_low(self) then
water_life.dumbstep(self,0,target,speed,0)
end
end
mobkit.queue_high(self,func,prty)
end
-- looks for a landing point on shore under air. tgt is object (optional)
function water_life.hq_go2land(self,prty,tgt)
local init = true
local offset = 1
local target = nil
local start = 1
if tgt then
local ayaw = water_life.get_yaw_to_object(self,tgt)
if ayaw then start = math.deg(ayaw) - 15 end
end
local func = function(self)
local pos = mobkit.get_stand_pos(self)
if init then
target = water_life.getLandPos(self, start)
if not target then
return true
end
init = false
end
local y=self.object:get_velocity().y
local pos2d = {x=pos.x,y=0,z=pos.z}
local dir=vector.normalize(vector.direction(pos2d,target))
local yaw = minetest.dir_to_yaw(dir)
if mobkit.timer(self,1) then
if self.isonground and target and vector.distance(pos,target) < 1 then
mobkit.clear_queue_low(self)
mobkit.clear_queue_high(self)
return true
end
local pos1 = mobkit.pos_shift(mobkit.pos_shift(pos,
{x=-dir.z*offset,z=dir.x*offset}),dir)
local h,l = mobkit.get_terrain_height(pos1)
if h and h > pos.y then
mobkit.lq_freejump(self)
else
local pos2 = mobkit.pos_shift(mobkit.pos_shift(pos,
{x=dir.z*offset,z=-dir.x*offset}),dir)
local h,l = mobkit.get_terrain_height(pos2)
if h and h > pos.y then
mobkit.lq_freejump(self)
end
end
elseif mobkit.turn2yaw(self,yaw) then
dir.y = y
self.object:set_velocity(dir)
end
end
mobkit.queue_high(self,func,prty)
end
function water_life.hq_snail_move(self,prty)
local ground = mobkit.get_stand_pos(self)
local coraltable = minetest.find_nodes_in_area({x=ground.x-3, y=ground.y-1,
z=ground.z-3}, {x=ground.x+3, y=ground.y, z=ground.z+3}, water_life.urchinspawn)
if not coraltable or #coraltable < 1 then return end
local tgpos = coraltable[random(#coraltable)]
local func = function(self)
if not mobkit.is_alive(self) then
return true
end
local pos = mobkit.get_stand_pos(self)
if not tgpos or not pos then
return true
end
local dist = vector.distance(pos,tgpos)
mobkit.drive_to_pos(self,tgpos,0.01,0.1,1.5)
if dist <= 1.8 then return true end
end
mobkit.queue_high(self,func,prty)
end
function water_life.hq_idle(self,prty,duration,anim)
anim = anim or 'stand'
local init = true
local func=function(self)
if init then
mobkit.animate(self,anim)
init=false
end
duration = duration-self.dtime
if duration <= 0 then return true end
end
mobkit.queue_high(self,func,prty)
end
-- swim to the next "node" which is inside viewrange or quit -- node can be string or table of string
-- if tgtpos is given node will be ignored
function water_life.hq_swimto(self,prty,speed,node,tgtpos)
local endpos = tgtpos
local pos = self.object:get_pos()
local r = self.view_range
if not tgtpos then
endpos = minetest.find_node_near(pos, r, node)
end
if not endpos then return true end
local func = function(self)
local yaw = water_life.get_yaw_to_pos(self,endpos)
if not mobkit.is_alive(self) then return true end
local pos = self.object:get_pos()
if mobkit.timer(self,1) then
if vector.distance(pos,endpos) > 1 then
if endpos.y > pos.y then
local vel = self.object:get_velocity()
vel.y = vel.y+0.4
self.object:set_velocity(vel)
end
if endpos.y < pos.y then
local vel = self.object:get_velocity()
vel.y = vel.y-0.1
self.object:set_velocity(vel)
end
mobkit.hq_aqua_turn(self,prty+5,yaw,speed)
pos = self.object:get_pos()
yaw = water_life.get_yaw_to_pos(self,endpos)
else
return true
end
end
end
mobkit.queue_high(self,func,prty)
end
-- turn around 180degrees from tgtob and swim away until out of sight
function water_life.hq_swimfrom(self,prty,tgtobj,speed,outofsight)
local init = true
local func = function(self)
if not outofsight then outofsight = self.view_range * 1.5 end
if not mobkit.is_alive(tgtobj) then return true end
local pos = mobkit.get_stand_pos(self)
local opos = tgtobj:get_pos()
local yaw = water_life.get_yaw_to_object(self,tgtobj) +
math.rad(random(-30,30))+math.rad(180)
local distance = vector.distance(pos,opos)
if self.isonground then return true end
if init then
mobkit.animate(self,"swim")
init=false
end
if distance < outofsight then
local swimto, height = water_life.aqua_radar_dumb(pos,yaw,3)
if height and height > pos.y then
local vel = self.object:get_velocity()
vel.y = vel.y+0.1
self.object:set_velocity(vel)
end
mobkit.hq_aqua_turn(self,51,swimto,speed)
else
return true
end
end
mobkit.queue_high(self,func,prty)
end
function water_life.hq_water_attack(self,tgtobj,prty,speed,shallow)
local pos = self.object:get_pos()
local selfbox = water_life.getCollisionBox(self) --self.object:get_properties().initial_properties.collisionbox
local tgtbox = water_life.getCollisionBox(tgtobj) --tgtobj:get_properties().initial_properties.collisionbox
if not speed then speed = 1 end
local func = function(self)
if not mobkit.is_alive(self) or not mobkit.is_alive(tgtobj) or
tgtobj:get_attach() ~= nil then
return true
end
local pos = self.object:get_pos()
local endpos = tgtobj:get_pos()
if not shallow then
if not mobkit.is_in_deep(tgtobj) and vector.distance (pos,endpos) > 2 then
return true
end
else
if not water_life.isinliquid(tgtobj) and vector.distance (pos,endpos) > 2 then
return true
end
end
local yaw = water_life.get_yaw_to_pos(self,endpos)
local entity = nil
if not tgtobj:is_player() then entity = tgtobj:get_luaentity() end
if vector.distance(pos,endpos) > selfbox[5]+tgtbox[5] then
if endpos.y > pos.y+selfbox[5] then
local vel = vector.add(self.object:get_velocity(),{x=0,y=0.5,z=0})
self.object:set_velocity(vel)
end
if endpos.y < pos.y-selfbox[5] then
local vel = vector.add(self.object:get_velocity(),{x=0,y=-0.5,z=0})
self.object:set_velocity(vel)
end
mobkit.hq_aqua_turn(self,prty+5,yaw,speed)
else
if mobkit.is_alive(tgtobj) then
tgtobj:punch(self.object,1,self.attack)
return true
else
return true
end
end
if entity and string.match(entity.name,"petz") and vector.distance(pos,endpos) < 2 then
if mobkit.is_alive(tgtobj) then
mobkit.hurt(entity,self.attack.damage_groups.fleshy or 4)
else
return true
end
end
end
mobkit.queue_high(self,func,prty)
end
-- hq flying behaviors
function water_life.hq_climb(self,prty,fmin,fmax)
if not max then max = 30 end
if not min then min = 20 end
local func=function(self)
if mobkit.timer(self,1) then
local remember = mobkit.recall(self,"time")
if remember then
if self.time_total - remember > 15 then
mobkit.forget(self,"turn")
mobkit.forget(self,"time")
end
end
self.action = "fly"
local pos = self.object:get_pos()
local yaw = self.object:get_yaw()
local left, right, up, down, under, above = water_life.radar(pos,yaw,32,true)
if (down < 3) and (under >= fmax) then
water_life.hq_glide(self,prty,fmin,fmax)
return true
end
if left > 3 or right > 3 then
local lift = 0.6
local pitch = 8
local roll = 6
local acc = 1.2
roll = (max(left,right)/30 * 7.5)
lift = lift + (down - up) /400
pitch = pitch + (down - up) /30
local turn = chose_turn(self,left,right)
if turn then
mobkit.clear_queue_low(self)
water_life.lq_fly_pitch(self,lift,pitch,roll*-1,acc,'fly')
else
mobkit.clear_queue_low(self)
water_life.lq_fly_pitch(self,lift,pitch,roll,acc,'fly')
end
end
end
if mobkit.timer(self,15)then
mobkit.clear_queue_low(self)
end
if mobkit.is_queue_empty_low(self) then
water_life.lq_fly_pitch(self,0.6,8,(random(2)-1.5)*30,1.2,'fly')
end
end
mobkit.queue_high(self,func,prty)
end
function water_life.hq_glide(self,prty,fmin,fmax)
if not max then fmax = 30 end
if not min then fmin = 20 end
local func = function(self)
if mobkit.timer(self,1) then
self.action = "glide"
local remember = mobkit.recall(self,"time")
if remember then
if self.time_total - remember > 15 then
mobkit.forget(self,"turn")
mobkit.forget(self,"time")
end
end
local pos = self.object:get_pos()
local yaw = self.object:get_yaw()
local left, right, up, down, under, above = water_life.radar(pos,yaw,32,true)
if (down > 10) or (under < fmin) then
water_life.hq_climb(self,prty,fmin,fmax)
return true
end
if left > 3 or right > 3 then
local lift = 0.6
local pitch = 8
local roll = 0
local acc = 1.2
roll = (max(left,right)/30 *7.5)
local turn = chose_turn(self,left,right)
if turn then
mobkit.clear_queue_low(self)
water_life.lq_fly_pitch(self,lift,pitch,roll*-1,acc,'glide')
else
mobkit.clear_queue_low(self)
water_life.lq_fly_pitch(self,lift,pitch,roll,acc,'glide')
end
end
end
if mobkit.timer(self,20) then
mobkit.clear_queue_low(self)
end
if mobkit.is_queue_empty_low(self) then
water_life.lq_fly_pitch(self,0.6,-4,(random(2)-1.5)*30,0,'glide')
end
end
mobkit.queue_high(self,func,prty)
end
function water_life.hq_water_takeoff(self,prty,anim,tyaw)
local init = true
local startup = true
local turned = false
local timer = 0
if not anim then anim = 'def' end
local pos2 = {}
local func=function(self)
local yaw = self.object:get_yaw()
local pos = self.object:get_pos()
if startup then
if tyaw then yaw = tyaw end
for i = 0,330,30 do
pos2 = mobkit.pos_translate2d(pos,yaw+rad(i),self.view_range*2)
if not water_life.find_collision(pos,pos2,false) then
tyaw = yaw + rad(i)
break
end
end
startup = false
end
if not startup then