forked from djoslin0/sm64ex-coop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfootball.lua
1327 lines (1123 loc) · 40.1 KB
/
football.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
-- name: Football (soccer)
-- description: Play football in castle grounds.
-- incompatible: gamemode
---------------
-- constants --
---------------
ballGravity = 1.5
ballRadius = 50
ballFriction = 0.988
ballRestitution = 0.65
ballWaterDrag = 0.9
ballParallelInertia = 0.3
ballPerpendicularInertia = 1
ballActionValues = {
[ACT_WATER_PUNCH] = { xz = 20, y = 40, directionless = false },
[ACT_MOVE_PUNCHING] = { xz = 35, y = 0, directionless = false },
[ACT_PUNCHING] = { xz = 35, y = 0, directionless = false },
[ACT_GROUND_POUND] = { xz = 40, y = 40, directionless = true },
[ACT_GROUND_POUND_LAND] = { xz = 40, y = 40, directionless = true },
[ACT_JUMP_KICK] = { xz = 10, y = 32, directionless = false },
[ACT_SLIDE_KICK_SLIDE] = { xz = -7, y = 25, directionless = false },
[ACT_SLIDE_KICK] = { xz = -7, y = 25, directionless = false },
[ACT_LONG_JUMP] = { xz = 0, y = 0, directionless = false },
[ACT_CROUCH_SLIDE] = { xz = 0, y = 0, directionless = false },
}
---------------
-- globals --
---------------
gBallTouchedLocal = false
gCachedBalls = {}
-----------
-- utils --
-----------
function clamp(x, a, b)
if x < a then return a end
if x > b then return b end
return x
end
function vec3f_degrees_between(a, b)
local ansAgain = math.acos(vec3f_dot(a, b) / (vec3f_length(a) * vec3f_length(b)))
return math.deg(ansAgain)
end
function my_global_index()
return gNetworkPlayers[gMarioStates[0].playerIndex].globalIndex
end
function my_location()
local np = gNetworkPlayers[gMarioStates[0].playerIndex]
return tostring(np.currCourseNum) .. '-' .. tostring(np.currLevelNum) .. '-' .. tostring(np.currAreaIndex) .. '-' .. tostring(np.currActNum)
end
function active_player(m)
local np = gNetworkPlayers[m.playerIndex]
if m.playerIndex == 0 then
return true
end
if not np.connected then
return false
end
return is_player_active(m)
end
function get_cached_ball(obj)
local key = my_location() .. '-' .. tostring(obj.oSyncID)
if gCachedBalls[key] == nil then
local cb = {}
cb.oGlobalOwner = obj.oGlobalOwner
cb.oHitTime = obj.oHitTime
cb.oNetworkTime = obj.oNetworkTime
cb.oPosX = obj.oPosX
cb.oPosY = obj.oPosY
cb.oPosZ = obj.oPosZ
cb.oVelX = obj.oVelX
cb.oVelY = obj.oVelY
cb.oVelZ = obj.oVelZ
gCachedBalls[key] = cb
end
return gCachedBalls[key]
end
function should_reject_packet(obj)
local cb = get_cached_ball(obj)
if obj.oHitTime < cb.oHitTime then
return true
end
if obj.oHitTime == cb.oHitTime and obj.oGlobalOwner > cb.oGlobalOwner then
return true
end
if obj.oHitTime == cb.oHitTime and obj.oGlobalOwner == cb.oGlobalOwner and obj.oNetworkTime < cb.oNetworkTime then
return true
end
return false
end
function find_ball()
local obj = obj_get_first(OBJ_LIST_DEFAULT)
while obj ~= nil do
if get_id_from_behavior(obj.behavior) == id_bhvBall then
return obj
end
obj = obj_get_next(obj)
end
return nil
end
function spawn_or_move_ball(x, y, z)
-- search for ball
local obj = find_ball()
if obj ~= nil then
-- move ball
obj.oPosX = x
obj.oPosY = y
obj.oPosZ = z
obj.oVelX = 0
obj.oVelY = 0
obj.oVelZ = 0
obj.oGlobalOwner = my_global_index()
obj.oHitTime = get_network_area_timer()
obj.oNetworkTime = get_network_area_timer()
network_send_object(obj, false)
return obj
end
-- don't spawn unless server
if my_global_index() ~= 0 then
return nil
end
-- spawn ball
return spawn_sync_object(
id_bhvBall,
E_MODEL_SPINY_BALL,
x, y, z,
function(obj)
obj.oGlobalOwner = my_global_index()
end)
end
--------------
-- behavior --
--------------
-- define ball's custom fields
define_custom_obj_fields({
oNetworkTime = 'u32',
oHitTime = 'u32',
oGlobalOwner = 'u32',
oFrozen = 'u32',
})
--- @param obj Object
function bhv_ball_particle_trail(obj)
local spi = obj_get_temp_spawn_particles_info(E_MODEL_SPARKLES)
if spi == nil then
return nil
end
spi.behParam = 2
spi.count = 1
spi.offsetY = -1 * ballRadius
spi.forwardVelBase = 8
spi.forwardVelRange = 0
spi.velYBase = 6
spi.velYRange = 0
spi.gravity = 0
spi.dragStrength = 5
spi.sizeBase = 10
spi.sizeRange = 30
cur_obj_spawn_particles(spi)
end
--- @param obj Object
function bhv_ball_particle_bounce(obj)
local spi = obj_get_temp_spawn_particles_info(E_MODEL_MIST)
if spi == nil then
return nil
end
spi.behParam = 3
spi.count = 5
spi.offsetY = -1 * ballRadius
spi.forwardVelBase = 6
spi.forwardVelRange = -6
spi.velYBase = 6
spi.velYRange = -6
spi.gravity = 0
spi.dragStrength = 5
spi.sizeBase = 8
spi.sizeRange = 13
cur_obj_spawn_particles(spi)
end
--- @param obj Object
function bhv_ball_init(obj)
-- flags and such
obj.oFlags = OBJ_FLAG_UPDATE_GFX_POS_AND_ANGLE
obj.oGraphYOffset = 0
cur_obj_scale(1.4)
-- physics
obj.oWallHitboxRadius = 40.00
obj.oGravity = 2.50
obj.oBounciness = -0.75
obj.oDragStrength = 0.00
obj.oFriction = 0.99
obj.oBuoyancy = -2.00
-- hitbox
obj.hitboxRadius = 100
obj.hitboxHeight = 100
-- custom values
obj.oNetworkTime = 0
obj.oHitTime = 0
obj.oFrozen = 0
-- cache
local cb = get_cached_ball(obj)
network_init_object(obj, false, {
'oPosX',
'oPosY',
'oPosZ',
'oVelX',
'oVelY',
'oVelZ',
'oGlobalOwner',
'oNetworkTime',
'oHitTime',
})
end
--- @param obj Object
function bhv_ball_player_collision(obj)
local alterPos = { x = 0, y = 0, z = 0}
local m = nearest_mario_state_to_object(obj)
if m == nil then return alterPos end
local player = m.marioObj
if player == nil then return alterPos end
local playerRadius = 37
local playerHeight = 160
local objPoint = { x = obj.oPosX, y = obj.oPosY, z = obj.oPosZ }
local v = { x = obj.oVelX, y = obj.oVelY, z = obj.oVelZ }
-- figure out player-to-ball radius
local alterBallFlags = (ACT_FLAG_ATTACKING | ACT_FLAG_BUTT_OR_STOMACH_SLIDE | ACT_FLAG_DIVING)
local playerBallRadius = ballRadius
if ballActionValues[m.action] ~= nil or (m.action & alterBallFlags) ~= 0 then
playerBallRadius = playerBallRadius + 50
end
------------------------------------------------
-- calculate position and determine collision --
------------------------------------------------
-- calculate cylinder values
local cylY1 = player.oPosY + playerRadius
local cylY2 = player.oPosY + playerHeight - playerRadius
local cylPoint = { x = player.oPosX, y = clamp(obj.oPosY, cylY1, cylY2), z = player.oPosZ }
local cylDist = vec3f_dist(cylPoint, objPoint)
-- check for collision
if cylDist > (playerBallRadius + playerRadius) then
return alterPos
end
gBallTouchedLocal = (m.playerIndex == 0)
local vDifference = { x = objPoint.x - cylPoint.x, y = objPoint.y - cylPoint.y, z = objPoint.z - cylPoint.z }
local differenceDir = { x = vDifference.x, y = vDifference.y, z = vDifference.z }
if vec3f_length(differenceDir) ~= 0 then
vec3f_normalize(differenceDir)
end
alterPos.x = (cylPoint.x + differenceDir.x * (playerBallRadius + playerRadius + 1)) - objPoint.x
alterPos.y = (cylPoint.y + differenceDir.y * (playerBallRadius + playerRadius + 1)) - objPoint.y
alterPos.z = (cylPoint.z + differenceDir.z * (playerBallRadius + playerRadius + 1)) - objPoint.z
-----------------------------------------
-- figure out player's attack velocity --
-----------------------------------------
local vPlayer = { x = player.oVelX, y = player.oVelY, z = player.oVelZ }
local playerTheta = (m.faceAngle.y / 0x8000) * math.pi
-- have attacks alter velocity further
local alterXz = 0
local alterY = 0
local alterDirectionless = false
if ballActionValues[m.action] ~= nil then
alterXz = ballActionValues[m.action].xz
alterY = ballActionValues[m.action].y
alterDirectionless = ballActionValues[m.action].directionless
elseif ((m.action & (ACT_FLAG_BUTT_OR_STOMACH_SLIDE | ACT_FLAG_DIVING)) ~= 0) or (m.action == ACT_SLIDE_KICK_SLIDE) or (m.action == ACT_SLIDE_KICK) then
-- dive or slide sends it upward, and slows xz
alterXz = -7
alterY = 25
elseif (m.action & ACT_FLAG_ATTACKING) ~= 0 then
-- other attacks should just do something reasonable
alterXz = 10
alterY = 10
end
-- adjust angle
local theta = playerTheta
if alterDirectionless and differenceDir.z ~= 0 then
theta = math.atan2(differenceDir.x, differenceDir.z)
end
vPlayer.x = vPlayer.x + math.sin(theta) * alterXz
vPlayer.z = vPlayer.z + math.cos(theta) * alterXz
if vPlayer.y < alterY then vPlayer.y = vPlayer.y + alterY end
local vPlayerMag = vec3f_length(vPlayer)
-------------------------------------------------
-- figure out which velocity interaction to do --
-------------------------------------------------
local v = { x = obj.oVelX, y = obj.oVelY, z = obj.oVelZ }
local doReflection = (vPlayerMag == 0)
-- make sure ball is offset in the vPlayer direction
if not doReflection then
local objCylDir = { x = cylPoint.x - objPoint.x, y = cylPoint.y - objPoint.y, z = cylPoint.z - objPoint.z }
local objCylDirXZ = { x = objCylDir.x, y = 0, z = objCylDir.z }
local objCylDirMag = vec3f_length(objCylDir)
local vPlayerXZ = { x = vPlayer.x, y = 0, z = vPlayer.z }
local vPlayerXZMag = vec3f_length(vPlayerXZ)
if objCylDirMag > 0 and vPlayerXZMag > 0 then
doReflection = (vec3f_degrees_between(vPlayer, objCylDir)) <= 120
and (vec3f_degrees_between(vPlayerXZ, objCylDirXZ)) <= 120
end
end
-- make sure player has a velocity
if not doReflection and vPlayerMag == 0 then
doReflection = true
end
--------------------------------------
-- calculate velocity (interaction) --
--------------------------------------
if not doReflection then
local vPlayerDir = { x = vPlayer.x, y = vPlayer.y, z = vPlayer.z }
vec3f_normalize(vPlayerDir)
-- split velocity into parallel/perpendicular to normal
local perpendicular = vec3f_project(v, vPlayerDir)
local parallel = { x = v.x - perpendicular.x, y = v.y - perpendicular.y, z = v.z - perpendicular.z }
-- apply friction
vec3f_mul(parallel, 0.5)
local parallelMag = vec3f_length(parallel)
local perpendicularMag = vec3f_length(perpendicular)
if perpendicularMag == 0 or perpendicularMag < vPlayerMag then
vec3f_copy(perpendicular, vPlayer)
end
-- reflect velocity along normal
local reflect = {
x = parallel.x + perpendicular.x,
y = parallel.y + perpendicular.y,
z = parallel.z + perpendicular.z
}
-- set new velocity
obj.oVelX = reflect.x
obj.oVelY = reflect.y
obj.oVelZ = reflect.z
end
-------------------------------------
-- calculate velocity (reflection) --
-------------------------------------
if doReflection then
-- split velocity into parallel/perpendicular to normal
local perpendicular = vec3f_project(v, differenceDir)
local parallel = { x = v.x - perpendicular.x, y = v.y - perpendicular.y, z = v.z - perpendicular.z }
-- apply friction and restitution
vec3f_mul(parallel, ballFriction)
vec3f_mul(perpendicular, ballRestitution)
-- play sounds
local parallelLength = vec3f_length(parallel)
local perpendicularLength = vec3f_length(perpendicular)
if perpendicularLength > 5 then
cur_obj_play_sound_2(SOUND_GENERAL_BOX_LANDING_2)
elseif parallelLength > 3 then
cur_obj_play_sound_2(SOUND_ENV_SLIDING)
end
local pushOutMag = 10
-- reflect velocity along normal
local reflect = {
x = parallel.x - perpendicular.x,
y = parallel.y - perpendicular.y,
z = parallel.z - perpendicular.z
}
-- set new velocity
obj.oVelX = reflect.x + differenceDir.x * pushOutMag
obj.oVelY = reflect.y + differenceDir.y * pushOutMag
obj.oVelZ = reflect.z + differenceDir.z * pushOutMag
end
return alterPos
end
--- @param obj Object
--- @param offset Vec3f
function bhv_ball_resolve(obj, offset)
local a = { x = obj.oPosX + 0, y = obj.oPosY + 0, z = obj.oPosZ + 0 }
local dir = { x = offset.x * -1.0, y = offset.y * -1.0, z = offset.z * -1.0}
info = collision_find_surface_on_ray(
a.x, a.y, a.z,
dir.x, dir.y, dir.z)
obj.oPosX = info.hitPos.x + offset.x
obj.oPosY = info.hitPos.y + offset.y
obj.oPosZ = info.hitPos.z + offset.z
if info.surface == nil then return nil end
return { x = info.surface.normal.x, y = info.surface.normal.y, z = info.surface.normal.z }
end
--- @param obj Object
function bhv_ball_loop(obj)
if obj.oFrozen ~= 0 then
return
end
gBallTouchedLocal = false
local cb = get_cached_ball(obj)
-- detect when a packet was received
if obj.oNetworkTime ~= cb.oNetworkTime then
if should_reject_packet(obj) then
-- reject packet
obj.oGlobalOwner = cb.oGlobalOwner
obj.oHitTime = cb.oHitTime
obj.oNetworkTime = cb.oNetworkTime
obj.oPosX = cb.oPosX
obj.oPosY = cb.oPosY
obj.oPosZ = cb.oPosZ
obj.oVelX = cb.oVelX
obj.oVelY = cb.oVelY
obj.oVelZ = cb.oVelZ
end
end
local orig = { x = obj.oPosX, y = obj.oPosY, z = obj.oPosZ }
obj.oVelY = obj.oVelY - ballGravity
if obj.oVelX == 0 and obj.oVelY == 0 and obj.oVelZ == 0 then
obj.oVelY = obj.oVelY + 0.01
end
-- detect player collisions
local alterPos = bhv_ball_player_collision(obj)
-- alter end-point based on player collisions
local a = { x = obj.oPosX, y = obj.oPosY, z = obj.oPosZ }
local v = { x = obj.oVelX, y = obj.oVelY, z = obj.oVelZ }
local b = { x = v.x, y = v.y, z = v.z }
vec3f_sum(b, b, alterPos)
-- regular movement
local info = collision_find_surface_on_ray(
a.x, a.y, a.z,
b.x, b.y, b.z)
obj.oPosX = info.hitPos.x
obj.oPosY = info.hitPos.y
obj.oPosZ = info.hitPos.z
-- detect normal along movement vector
local vMag = vec3f_length(v)
if vMag > 0 then
local vNorm = { x = v.x / vMag, y = v.y / vMag, z = v.z / vMag }
b = { x = v.x + vNorm.x * (vMag + ballRadius), y = v.y + vNorm.y * (vMag + ballRadius), z = v.z + vNorm.z * (vMag + ballRadius) }
end
info = collision_find_surface_on_ray(
a.x, a.y, a.z,
b.x, b.y, b.z)
-- figure out the standard normal
local colNormals = {}
if info.surface ~= nil then
table.insert(colNormals, { x = info.surface.normal.x, y = info.surface.normal.y, z = info.surface.normal.z })
if vMag > 5 then
bhv_ball_particle_bounce(obj)
end
else
table.insert(colNormals, nil)
end
-- resolve collisions around ball
table.insert(colNormals, bhv_ball_resolve(obj, { x = ballRadius, y = 0, z = 0 }))
table.insert(colNormals, bhv_ball_resolve(obj, { x = -ballRadius, y = 0, z = 0 }))
table.insert(colNormals, bhv_ball_resolve(obj, { x = 0, y = 0, z = ballRadius }))
table.insert(colNormals, bhv_ball_resolve(obj, { x = 0, y = 0, z = -ballRadius }))
table.insert(colNormals, bhv_ball_resolve(obj, { x = 0, y = ballRadius, z = 0 }))
table.insert(colNormals, bhv_ball_resolve(obj, { x = 0, y = -ballRadius, z = 0 }))
-- figure out collision normal
local collisionN = { x = 0, y = 0, z = 0 }
local collisionCount = 0
for _, colN in ipairs(colNormals) do
if colN ~= nil then
vec3f_sum(collisionN, collisionN, colN)
collisionCount = collisionCount + 1
end
end
-- reflect collisions
if collisionCount > 0 then
-- calculate total normal
vec3f_mul(collisionN, 1.0 / collisionCount)
vec3f_normalize(collisionN)
-- split velocity into parallel/perpendicular to normal
local perpendicular = vec3f_project(v, collisionN)
local parallel = { x = v.x - perpendicular.x, y = v.y - perpendicular.y, z = v.z - perpendicular.z }
-- apply friction and restitution
vec3f_mul(parallel, ballFriction)
vec3f_mul(perpendicular, ballRestitution)
-- stop ball in parallel axis
local parallelLength = vec3f_length(parallel)
if parallelLength < ballParallelInertia then
vec3f_mul(parallel, 0)
end
-- stop ball in perpendicular axis
local perpendicularLength = vec3f_length(perpendicular)
if perpendicularLength < ballPerpendicularInertia then
vec3f_mul(perpendicular, 0)
end
-- play sounds
if perpendicularLength > 5 then
cur_obj_play_sound_2(SOUND_GENERAL_BOX_LANDING_2)
elseif parallelLength > 3 then
cur_obj_play_sound_2(SOUND_ENV_SLIDING)
end
-- reflect velocity along normal
local reflect = {
x = parallel.x - perpendicular.x,
y = parallel.y - perpendicular.y,
z = parallel.z - perpendicular.z
}
-- set new velocity
obj.oVelX = reflect.x
obj.oVelY = reflect.y
obj.oVelZ = reflect.z
end
-- float in water
local waterLevel = find_water_level(obj.oPosX, obj.oPosZ)
if obj.oPosY < waterLevel then
obj.oVelX = obj.oVelX * ballWaterDrag
obj.oVelY = obj.oVelY * ballWaterDrag + 2
obj.oVelZ = obj.oVelZ * ballWaterDrag
end
-- sanity check floor
local floor = find_floor_height(obj.oPosX, obj.oPosY, obj.oPosZ)
if obj.oPosY <= floor or floor <= -10000 then
obj.oPosX = orig.x
obj.oPosY = orig.y
obj.oPosZ = orig.z
obj.oVelX = -obj.oVelX
obj.oVelY = -obj.oVelY
obj.oVelZ = -obj.oVelZ
end
-- update rotation
if obj.oVelX ~= 0 or obj.oVelZ ~= 0 then
local moveAngle = atan2s(obj.oVelZ * 100, obj.oVelX * 100)
local xzMag = math.sqrt(obj.oVelX * obj.oVelX + obj.oVelZ * obj.oVelZ)
obj.oFaceAngleYaw = moveAngle
obj.oFaceAnglePitch = obj.oFaceAnglePitch + xzMag * 100
end
-- send out object if we touched it
local updateRateSend = (obj.oGlobalOwner == my_global_index() and (get_network_area_timer() - obj.oNetworkTime) > 5)
if gBallTouchedLocal or updateRateSend then
if gBallTouchedLocal then
obj.oGlobalOwner = my_global_index()
obj.oHitTime = get_network_area_timer()
end
obj.oNetworkTime = get_network_area_timer()
network_send_object(obj, false)
end
-- spawn a particle trail
if vMag > 50 then
bhv_ball_particle_trail(obj)
end
-- hack: make sure we never set velocity to nan
if obj.oVelX ~= obj.oVelX then obj.oVelX = 0 end
if obj.oVelY ~= obj.oVelY then obj.oVelY = 0 end
if obj.oVelZ ~= obj.oVelZ then obj.oVelZ = 0 end
-- hack: save pos/vel to detect packets
cb.oGlobalOwner = obj.oGlobalOwner
cb.oHitTime = obj.oHitTime
cb.oNetworkTime = obj.oNetworkTime
cb.oPosX = obj.oPosX
cb.oPosY = obj.oPosY
cb.oPosZ = obj.oPosZ
cb.oVelX = obj.oVelX
cb.oVelY = obj.oVelY
cb.oVelZ = obj.oVelZ
end
id_bhvBall = hook_behavior(nil, OBJ_LIST_DEFAULT, true, bhv_ball_init, bhv_ball_loop)
----------------------------------------------------------------------------------------------------------------
--------------
-- gamemode --
--------------
GAME_STATE_WAIT = 0
GAME_STATE_ACTIVE = 1
GAME_STATE_SCORE = 2
GAME_STATE_OOB = 3
GAME_STATE_OVER = 4
sSoccerBall = nil
sBallSpawnPos = { x = (4573 + -4634) / 2, y = -50, z = (-2077 + -212) / 2 }
sBallHidePos = { x = sBallSpawnPos.x, y = -5000, z = sBallSpawnPos.z }
sGameModeInitialized = false
sWaitTimeout = 30 * 5
sOobTimeout = 30 * 5
sScoreTimeout = 30 * 5
sOverTimeout = 30 * 15
sStateTimer = sWaitTimeout
sMaxScore = 5
gGlobalSyncTable.gameState = GAME_STATE_WAIT
gGlobalSyncTable.displayText = ' '
gGlobalSyncTable.displayFont = FONT_HUD
gGlobalSyncTable.displayColor = 0xFFFFFF
gGlobalSyncTable.scoreRed = 0
gGlobalSyncTable.scoreBlue = 0
function gamemode_initialize()
-- hide the SM64 HUD
hud_hide()
-- prevent warp doors from working
local wasRefreshed = false
local obj = obj_get_first(OBJ_LIST_SURFACE)
while obj ~= nil do
local behaviorId = get_id_from_behavior(obj.behavior)
if behaviorId == id_bhvDoorWarp then
obj.oInteractType = 0
end
-- hide exclamation box
if behaviorId == id_bhvExclamationBox then
obj.oPosX = sBallHidePos.x
obj.oPosY = sBallHidePos.y
obj.oPosZ = sBallHidePos.z
end
if behaviorId == id_bhvLllHexagonalMesh then
wasRefreshed = true
end
obj = obj_get_next(obj)
end
-- server spawns objects
if my_global_index() == 0 and not wasRefreshed then
-- block vanish cap
spawn_sync_object(
id_bhvLllHexagonalMesh,
E_MODEL_TRAMPOLINE,
-3390, -512, -2023,
function (obj)
obj.oOpacity = 255
obj.header.gfx.scale.x = 3
obj.header.gfx.scale.z = 3;
end)
-- block basement door
spawn_sync_object(
id_bhvLllHexagonalMesh,
E_MODEL_TRAMPOLINE,
3279 + 45, -511, -2937 + 45,
function (obj)
obj.oOpacity = 255
obj.header.gfx.scale.x = 2
obj.header.gfx.scale.z = 2
obj.oFaceAngleYaw = 0x6000
obj.oFaceAngleRoll = 0x4000
end)
-- block area near ramp
spawn_sync_object(
id_bhvStaticCheckeredPlatform,
E_MODEL_CHECKERBOARD_PLATFORM,
4430 - 65, 545 + 250 - 5, -6000 - 65,
function (obj)
obj.oOpacity = 255
obj.oFaceAngleYaw = 0x2000
obj.oFaceAngleRoll = 0x4000
end)
-- block area near ramp
for i=0,6 do
spawn_sync_object(
id_bhvStaticCheckeredPlatform,
E_MODEL_CHECKERBOARD_PLATFORM,
4460, 545 + 250, -6000 + i * 305,
function (obj)
obj.oOpacity = 255
obj.oFaceAngleYaw = 0
obj.oFaceAngleRoll = 0x4000
end)
end
-- block cannon
spawn_sync_object(
id_bhvLllHexagonalMesh,
E_MODEL_TRAMPOLINE,
2385, 88, 1956,
function (obj)
obj.oOpacity = 255
obj.header.gfx.scale.x = 0.9
obj.header.gfx.scale.z = 0.9
end)
end
sGameModeInitialized = true
end
function gamemode_shuffle()
local t = {}
local count = 0
-- create table of players
for i = 0, (MAX_PLAYERS-1) do
local m = gMarioStates[i]
local s = gPlayerSyncTable[i]
if active_player(m) then
table.insert(t, s)
count = count + 1
end
end
-- shuffle
for i = #t, 2, -1 do
local j = math.random(i)
t[i], t[j] = t[j], t[i]
end
-- assign teams
local team1Count = 0
local team2Count = 0
local oddS = nil
for i, s in ipairs(t) do
if (i - 1) < count / 2 then
s.team = 1
team1Count = team1Count + 1
oddS = s
else
s.team = 2
team2Count = team2Count + 1
end
end
-- shuffle odd player
if team1Count > team2Count then
oddS.team = math.random(1, 2)
end
end
function gamemode_wait()
sSoccerBall = spawn_or_move_ball(sBallSpawnPos.x, sBallSpawnPos.y, sBallSpawnPos.z)
-- server only
if my_global_index() == 0 then
-- claim the ball
if sSoccerBall.oGlobalOwner ~= my_global_index() then
sSoccerBall.oGlobalOwner = my_global_index()
sSoccerBall.oHitTime = get_network_area_timer()
sSoccerBall.oNetworkTime = get_network_area_timer()
network_send_object(sSoccerBall, false)
end
-- clear sparkles
for i=0,(MAX_PLAYERS-1) do
local sm = gPlayerSyncTable[i]
sm.sparkle = false
end
-- decrement timer
sStateTimer = sStateTimer - 1
-- update the visible timer
if math.floor(sStateTimer / 30) ~= math.floor((sStateTimer + 1) / 30) then
gGlobalSyncTable.displayFont = FONT_HUD
gGlobalSyncTable.displayText = tostring(1 + math.floor(sStateTimer / 30))
gGlobalSyncTable.displayColor = 0xFFFFFF
end
-- start the round
if sStateTimer <= 0 then
gGlobalSyncTable.gameState = GAME_STATE_ACTIVE
gGlobalSyncTable.displayFont = FONT_HUD
gGlobalSyncTable.displayText = ' '
gGlobalSyncTable.displayColor = 0xFFFFFF
end
end
end
function gamemode_active()
-- server only
if my_global_index() == 0 then
local validGoalY = ((sSoccerBall.oPosY + ballRadius) < -550)
local validGoalZ = (sSoccerBall.oPosX < sBallSpawnPos.x and sSoccerBall.oPosZ < -2846) or (sSoccerBall.oPosX > sBallSpawnPos.x and sSoccerBall.oPosZ > -1205)
if validGoalY and validGoalZ then
if sSoccerBall.oPosX < sBallSpawnPos.x and sSoccerBall.oPosZ < -2846 then
end
spawn_sync_object(id_bhvExplosion, E_MODEL_EXPLOSION, sSoccerBall.oPosX, sSoccerBall.oPosY, sSoccerBall.oPosZ, nil)
local scoringTeam = 0
if sSoccerBall.oPosX < sBallSpawnPos.x then
scoringTeam = 1
else
scoringTeam = 2
end
local gameOver = false
local displayName = ''
local scorerNp = network_player_from_global_index(sSoccerBall.oGlobalOwner)
if scorerNp ~= nil then
local scorerS = gPlayerSyncTable[scorerNp.localIndex]
if scorerS.team == scoringTeam then
displayName = ' (' .. scorerNp.name .. ')'
end
end
if scoringTeam == 1 then
gGlobalSyncTable.scoreRed = gGlobalSyncTable.scoreRed + 1
gGlobalSyncTable.displayFont = FONT_NORMAL
gGlobalSyncTable.displayColor = 0xFF9999
if gGlobalSyncTable.scoreRed >= sMaxScore then
gGlobalSyncTable.displayText = 'red team wins!'
gameOver = true
else
gGlobalSyncTable.displayText = 'red team scored' .. displayName
end
else
gGlobalSyncTable.scoreBlue = gGlobalSyncTable.scoreBlue + 1
gGlobalSyncTable.displayFont = FONT_NORMAL
gGlobalSyncTable.displayColor = 0x9999FF
if gGlobalSyncTable.scoreBlue >= sMaxScore then
gGlobalSyncTable.displayText = 'blue team wins!'
gameOver = true
else
gGlobalSyncTable.displayText = 'blue team scored' .. displayName
end
end
if gameOver then
-- set sparkle
for i=0,(MAX_PLAYERS-1) do
local im = gMarioStates[i]
local sm = gPlayerSyncTable[i]
if active_player(im) then
if sm.team == scoringTeam then
sm.sparkle = true
end
end
end
gGlobalSyncTable.gameState = GAME_STATE_OVER
sStateTimer = sOverTimeout
else
-- set sparkle
local scorerS = gPlayerSyncTable[scorerNp.localIndex]
if scorerNp ~= nil and scorerS.team == scoringTeam then
scorerS.sparkle = true
end
gGlobalSyncTable.gameState = GAME_STATE_SCORE
sStateTimer = sScoreTimeout
end
end
-- check for oob
local ignoreOob = (sSoccerBall.oPosX >= 3230 and sSoccerBall.oPosX <= 4460 and sSoccerBall.oPosZ >= -6230 and sSoccerBall.oPosZ <= -2725) -- ramp near bridge
if not ignoreOob and sSoccerBall.oPosY > 0 then
local floorHeight = find_floor_height(sSoccerBall.oPosX, sSoccerBall.oPosY, sSoccerBall.oPosZ)
if sSoccerBall.oPosY - ballRadius - 10 < floorHeight then
gGlobalSyncTable.gameState = GAME_STATE_OOB
sStateTimer = sOobTimeout
end
end
-- check for other OOB
if validGoalY and not validGoalZ then
gGlobalSyncTable.gameState = GAME_STATE_OOB
sStateTimer = sOobTimeout
end
end
end
function gamemode_score()
sSoccerBall = spawn_or_move_ball(sBallHidePos.x, sBallHidePos.y, sBallHidePos.z)
-- server only
if my_global_index() == 0 then
-- decrement timer
sStateTimer = sStateTimer - 1
-- start the round
if sStateTimer <= 0 then
gGlobalSyncTable.gameState = GAME_STATE_WAIT
sStateTimer = sWaitTimeout
end
end
end
function gamemode_oob()
sSoccerBall = spawn_or_move_ball(sBallHidePos.x, sBallHidePos.y, sBallHidePos.z)
-- server only
if my_global_index() == 0 then
-- decrement timer
sStateTimer = sStateTimer - 1
gGlobalSyncTable.displayFont = FONT_NORMAL
gGlobalSyncTable.displayText = 'out of bounds'
gGlobalSyncTable.displayColor = 0xFFFFFF
-- start the round
if sStateTimer <= 0 then
gGlobalSyncTable.gameState = GAME_STATE_WAIT
sStateTimer = sWaitTimeout
end
end
end
function gamemode_over()
sSoccerBall = spawn_or_move_ball(sBallHidePos.x, sBallHidePos.y, sBallHidePos.z)
-- server only
if my_global_index() == 0 then
-- decrement timer
sStateTimer = sStateTimer - 1
-- start the round
if sStateTimer <= 0 then
-- shuffle teams
gamemode_shuffle()
gGlobalSyncTable.scoreRed = 0