forked from Night-Stalkers/lm-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kraken_old.py
1635 lines (1476 loc) · 61.4 KB
/
kraken_old.py
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
"""
Kraken
Author: hompy
Editors: thepolm3, lecom, JohnRambozo, Monstarules
Documenter: Monstarules
Last modified: 14th of April, 2020
Last modified by: Monstarules
"""
# Imports from other libraries
from gc import get_referrers
from collections import deque
from math import *
from random import randrange, uniform, choice
from operator import itemgetter, attrgetter
from itertools import product
from scheduler import Scheduler
# Imports from Twisted libraries
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
# Imports from the Pyspades libraries
from pyspades.server import *
from pyspades.world import Grenade
from pyspades.common import Vertex3, Quaternion, make_color, coordinates, get_color
from pyspades.collision import vector_collision, distance_3d_vector
from pyspades.constants import *
from commands import name, add, get_player, admin
from pyspades.weapon import Rifle,SMG,Shotgun
# Boolean constants for features
NEW_WEAPONS = True
ENABLE_REGEN = True
FAST_RUN = True
ALLOW_KRAKEN_COMMAND = True
USE_DAYCYCLE = False
KRAKEN_ADD_SCORE = True
# Modifies the weapons for this gamemode.
if NEW_WEAPONS:
Shotgun.ammo = 1 # Shotgun maximum magazine size: 1
Shotgun.stock = 15 # Shotgun ammo: 1 in the inizial magazine + 15 reserved
Shotgun.reload_time = 7 # Shotgun reload time: 7 seconds
Rifle.stock = 30 # Rifle ammo: 10 in the initial magazine + 30 reserved
SMG.stock = 90 # SMG ammo: 30 in initial magazine + 90 reserved
# Removes water damage after spawning, for however many seconds you set it to
NO_WATER_DAMAGE_TIME = 10
# Kracken specific constants
BLOCK_REMOVE_TENTACLE_HIT = 1
BLOCK_REMOVE_TENTACLE_KILL = 2
BLOCK_REMOVE_KRAKEN_KILL = 4
KRAKEN_PLAYER_ID = 31
TRAPPED_FREE_TIME = 40 # How many seconds the player is trapped
GRAB_DAMAGE = 40 # How much damage a player takes when grabbed
EYE_PAIN_TIME = 8.0 if not FAST_RUN else 1.0
KRAKEN_BLACK = make_color(20,20,35)
KRAKEN_BLOOD = make_color(120, 255, 120)
KRAKEN_EYE_SMALL = [
( 0, 0, -1, 0xC00000),
( 0, 0, -2, 0x400000),
( 0, 0, -3, 0xC00000),
(-1, 0, -1, 0xFF0000),
(-1, 0, -2, 0xC00000),
(-1, 0, -3, 0x800000),
( 1, 0, -1, 0xFF0000),
( 1, 0, -2, 0xC00000),
( 1, 0, -3, 0xFFFFFF)]
KRAKEN_EYE_SMALL_CLOSED = [
(-1, 0, -1, KRAKEN_BLACK),
(-1, 0, -2, KRAKEN_BLACK),
(-1, 0, -3, KRAKEN_BLACK),
( 1, 0, -1, KRAKEN_BLACK),
( 1, 0, -2, KRAKEN_BLACK),
( 1, 0, -3, KRAKEN_BLACK),
( 0, 0, -1, KRAKEN_BLACK),
( 0, 0, -2, KRAKEN_BLACK),
( 0, 0, -3, KRAKEN_BLACK)]
# We WONNED! (apparently)
KRAKEN_WIN_ANNOUNCEMENT = 'Arrrrrrrrrrr! You won and killed all krakens!'
# Some weapon related constants.
SHOT_DELAY = 0.017
CANNON_SHOT_SPEED = 7
# Regeneration/restock constants
REGEN_PLAYER_RATIO = 7 # The length between 2 regen periods is the player count divided by this
RESTOCK_PLAYER_RATIO = 12 # Ditto
REGEN_ONSET = 2.0
REGEN_FREQUENCY = 0.15
REGEN_AMOUNT = 3
RESTOCK_TIMER_SPEED = 0.08
REGEN_TIMER_SPEED = 0.25
RESTOCK_COUNT = (
5, # Primary weapon
20, # Blocks
2 # Grenades
)
# Map constants
WATER_DAMAGE = 25
MAP_CHANGE_DELAY = 5
RESPAWN_TIME = 15
# Falling block constants
FALLING_BLOCK_DAMAGE = 100
FALLING_BLOCK_Z = 0
FALLING_BLOCK_COUNT = 15
FALLING_BLOCK_START_DELAY = 2
FALLING_BLOCK_COLOR = make_color (40, 45, 53)
# Some base method declarations/definitions
# Starting with lovely shapes!
def cube (s):
s0, s1 = -s / 2 + 1, s / 2 + 1
return product (xrange (s0, s1), repeat = 3)
def prism (x, y, z, w, d, h):
return product (xrange (x, x + w), xrange (y, y + d), xrange(z, z + h))
def plane (r):
r0, r1 = -r / 2 + 1, r / 2 + 1
return product (xrange (r0, r1), repeat = 2)
def disc (rr, x = 0, y = 0, min_rr = None):
for u, v in plane (rr):
d = u * u + v * v
if d <= rr or not (min_rr and d < min_rr):
yield x + u, y + v
def sphere(r, x = 0, y = 0, z = 0, min_r = None):
rr = r * r
min_rr = min_r and min_r * min_r
for w, v, u in cube (r):
d = u * u + v * v + w * w
if d <= rr or not (min_r and d < min_rr):
yield x + u, y + v, z + w
# Block location
def block_location (x, y, z, i, j, k, w, d, h):
return not (x < i or x > i + w or y < j or y > j + d or z < k or z > k + h)
def block_location_centered (x, y, z, i, j, k, s):
return not (x < i - s or x > i + s or y < j - s or y > j + s or
z < k - s or z > k + s)
# Determines when exactly a player is gracefully shattering his knees
def fall_eta (height):
return 2.0 * (height / 64.0) ** 0.75
# "Hey man, does that look like an enemy to you?"
def is_valid_enemy (player):
return (player.world_object and not (player.world_object.dead or player.grabbed_by or player.trapped or player.regenerating or player.god))
# Animation class, for animation of the kraken
class Animated:
blocks_per_cycle = 3
build_interval = 0.01
build_queue = None
build_loop = None
blocks = None
# Begin kraken construction
def __init__ (self, protocol):
self.protocol = protocol
self.build_queue = deque ()
self.build_loop = LoopingCall (self.build_cycle)
self.build_loop.start (self.build_interval)
self.blocks = set ()
# Kraken build cycle
def build_cycle (self):
if not self.build_queue:
return
blocks_left = self.blocks_per_cycle
last_color = None
while self.build_queue and blocks_left > 0: # Keep alive when the kraken is still building
x, y, z, color = self.build_queue.popleft ()
if color != last_color:
self.protocol.set_block_color (color)
last_color = color
if self.protocol.build_block (x, y, z, color):
blocks_left -= 1
# Tentacle class, implements the Animated class
class Tentacle (Animated):
# Initial booleans
dead = False
dying = False
growing = True
withdraw = False
# Other variables
on_death = None
on_removed = None
parent = None
protocol = None
origin = None
up = None
orientation = None
start_orientation = None
target_orientation = None
lerp_t = None
facing = None
sections = None
radius = 2
spread = radius / 2.0
follow = None
follow_interval = 1.3
follow_timer = follow_interval
initial_growth_interval = 0.2
growth_interval = initial_growth_interval
growth_timer = growth_interval
blocks_destroyed = None
last_block_destroyed = None
grabbed_player = None
max_hp = None
# Begin tentacle construction
def __init__ (self, protocol, parent, (x, y, z)):
Animated.__init__ (self, protocol)
self.parent = parent
self.origin = Vertex3 (x, y, z)
self.up = Vertex3 (0.0, 0.0, -1.0)
self.orientation = Quaternion ()
self.facing = self.orientation.transform_vector (self.up)
self.sections = []
self.blocks_destroyed = []
self.parent.tentacles.append (self)
self.find_target ()
# Target a player
def find_target (self):
best = None
best_dist = None
best_followed = None
for player in self.protocol.players.values():
if is_valid_enemy (player): # Is it a target? If it is:
dist = distance_3d_vector (player.world_object.position, self.origin) # Grab the distance
followed = self.parent.is_enemy_targeted (player) # That's our target
if not best or dist < best_dist or best_followed and not followed: # If we have nobody, or find someone better, we set our new target
best, best_dist, best_followed = player, dist, followed
self.follow = best # And we follow the best
# Kraken tentacle AI
def think (self, dt):
tip = self.sections and self.sections[-1][0] or self.origin
self.follow_timer -= dt
if self.follow and not is_valid_enemy (self.follow): # Locates a target if we don't have one
self.find_target ()
if not self.follow: # In case we die or need to switch it up
self.growth_timer = 0.66
self.growing = False
self.withdraw = True
elif self.follow and self.follow_timer <= 0.0: # Functions as an updater to ensure we stay on our last known target
self.follow_timer = self.follow_interval
follow_pos = self.follow.world_object.position
direction = follow_pos - tip
q = self.facing.get_rotation_to (direction)
self.start_orientation = Quaternion (*self.orientation.get ())
self.target_orientation = q * self.orientation
self.lerp_t = 0.0
if self.target_orientation and self.lerp_t <= 1.0: # Reorientates us in case we need to ready a change in direction
self.orientation = self.start_orientation.slerp (self.target_orientation, self.lerp_t)
self.lerp_t += 0.02
self.facing = self.orientation.transform_vector (self.up)
self.facing.normalize ()
self.growth_timer -= dt
if self.growth_timer <= 0.0: # Ensures we are still moving by resetting the timer
self.growth_timer = self.growth_interval
if self.growing and self.follow:
tip = self.grow (tip.copy ())
elif self.withdraw: # Are we dying or withdrawing?
if self.sections:
pos, blocks = self.sections.pop ()
tip = pos
for uvw in blocks: # Declares which blocks to destroy
if not self.parent.is_location_inside (uvw, skip = self):
self.protocol.remove_block (*uvw)
self.blocks.discard (uvw) # Destroys blocks
else: # Declares the tentacle dead
for uvw in self.blocks:
if not self.parent.is_location_inside (uvw, skip = self):
self.protocol.remove_block (*uvw)
self.dead = True
if self.on_removed:
self.on_removed(self)
self.clear_mem () # Space saver (TM)
return
player = self.grabbed_player # Has/had it grabbed someone?
if player: # If so...
if self.dead or not player.world_object: # Releases if dead or if no longer a valid object
player.grabbed_by = None
self.grabbed_player = None
else: # However, if it has snatched this said unfortunate soul...
player.set_location((tip.x, tip.y, tip.z - 1.0))
if tip.z >= 63: # We are hitting the water (it doesn't always mean the kraken has killed them)
player.got_water_damage = True
player.kraken_kill () # If it has killed them, the tentacle gets credit
player.got_water_damage = False
# Prevents players from digging out of the kraken (I think)
def on_block_destroy (self, x, y, z, mode):
if mode == SPADE_DESTROY and (x, y, z) in self.blocks:
return False
# Damage to the tentacles
def on_block_removed (self, x, y, z):
xyz = (x, y, z)
if xyz not in self.blocks:
return 0
self.blocks.discard (xyz)
total_damage = 0.0
for u, v, w in self.blocks_destroyed:
xu, yv, zw = x - u, y - v, z - w
d = sqrt (xu ** 2 + yv ** 2 + zw ** 2)
total_damage += d >= 1.0 and 1.0 / d or 1.0
if total_damage > self.max_hp: # If the tentacle should die...
self.fracture (x, y, z) # Immediately break the tentacle
self.last_block_destroyed = None
self.die () # Dead tentacle
if self.on_death:
self.on_death (self)
return BLOCK_REMOVE_TENTACLE_KILL
if self.last_block_destroyed: #When a block is destroyed, this code is used
self.protocol.set_block_color (KRAKEN_BLOOD)
u, v, w = self.last_block_destroyed
self.protocol.build_block (u, v, w, KRAKEN_BLOOD, force = True)
self.blocks.add (self.last_block_destroyed)
self.last_block_destroyed = xyz
self.blocks_destroyed.append (xyz) # Destroys the block
return BLOCK_REMOVE_TENTACLE_HIT # Returns which blocks are destroyed
# Death of the tentacle
def die (self):
self.follow = None
self.target_orientation = None
if self.grabbed_player: # Drop the player if tentacle is dead
self.grabbed_player.grabbed_by = None
self.grabbed_player = None
self.growth_timer = 0.66
speedup = 2.0 + max (len (self.sections) / 140.0, 1.0)
self.growth_interval = self.initial_growth_interval / speedup # Tentacle destruction enabled at said rate
self.growing = False
self.withdraw = True
self.dying = True
return
# Fracturing of tentacle
def fracture (self, x, y, z):
protocol = self.protocol
radius = self.radius
for uvw in sphere (int (radius * 1.5), x, y, z): # Determines where we are breaking
if not self.parent.is_location_inside (uvw, skip = self):
protocol.remove_block (*uvw)
self.blocks.discard (uvw)
to_remove = []
breakpoint = False # Breaking point
while self.sections:
pos, blocks = self.sections.pop ()
for uvw in blocks:
if not self.parent.is_location_inside (uvw, skip = self):
if breakpoint:
protocol.remove_block (*uvw)
else:
to_remove.append (uvw)
self.blocks.discard (uvw)
if breakpoint:
break
i, j, k = pos.get ()
breakpoint = block_location_centered (x, y, z, i, j, k, radius) # Let's break it, right here.
if self.sections:
self.sections.pop()
for u, v, w in to_remove:
protocol.remove_block(u, v, w) # Actually deletes the blocks
# Tentacle growing
def grow (self, tip):
if self.sections:
tip += self.facing * self.spread
map = self.protocol.map
radius = self.radius
# This incredibly long if statement sets tip.z to 60 if the conditions are met.
if tip.z > 60 and len (self.blocks) < 20 and self.follow and self.follow.world_object and self.follow.world_object.position.z >= 62:
tip.z = 60
ix, iy, iz = int (tip.x), int (tip.y), int (tip.z)
blocks = []
destroyed = 0
for x, y, z in sphere (radius, ix, iy, iz):
if not (x < 0 or x >= 512 or y < 0 or y >= 512 or z < 0 or z >= 63):
xyz = (x, y, z)
if xyz not in self.blocks: # Builds up the beast by queueing up some blocks
if not map.get_solid (x, y, z):
blocks.append (xyz)
self.blocks.add (xyz)
self.build_queue.append (xyz + (KRAKEN_BLACK,))
elif not self.parent.is_location_inside (xyz, skip = self):
destroyed += 1 # Lets the server know to destroy blocks
if destroyed >= radius: # If we're destroying blocks...
for x, y, z in sphere (radius + 2, ix, iy, iz, min_r = radius):
if not self.parent.is_location_inside((x, y, z)): # Don't remove our OWN blocks.
self.protocol.remove_block (x, y, z)
self.protocol.create_explosion_effect (tip) # Go ahead and blow it up for that Michael Bay TM effect
for player in self.protocol.players.values():
if is_valid_enemy (player): # Do we have a target?
pos = player.world_object.position # Update position
if vector_collision(pos, tip, radius * 0.75): # Have we grabbed them?
self.follow = None
self.target_orientation = None
self.growth_timer = 0.4
self.growing = False
self.withdraw = True # Retreat back into Davy Jones' Locker
self.grabbed_player = player # Yoink!
player.grabbed_by = self
player.set_location (tip.x, tip.y, tip.z - 1.0)
player.kraken_hit (GRAB_DAMAGE) # Applies the damage of being defiled by a tentacle
break
self.sections.append (tip, blocks) # Final update of the tip
return tip
# Garbage collector to delete excess tentacles
def clear_mem(self):
self.build_loop.stop ()
self.build_loop=None
self.build_queue=None
self.parent.tentacles.pop (self.parent.tentacles.index(self))
get_referrers (self)
del self
self = None
# The eye of the storm... er... kraken
class Eye():
# Initial booleans
dead = False
closed=False
# Other variables
parent = None
protocol = None
blocks = None
origin_x = None
pos = None
base = None
hits = None
look_interval_min = 0.8
look_interval_max = 2.5
look_timer = look_interval_max
on_hit = None
create_call = None
# Creates the eye of the kraken
def __init__ (self, parent, base, ox, oy, oz, hits = 3):
self.parent = parent
self.protocol = parent.protocol
self.blocks = set ()
# Rotates the pupil
if parent.yrotation:
oy *= -1
if parent.size < 8:
1
else:
oy+=1
if parent.xrotation:
ox, oy = (oy, ox)
oy -= 2
self.pos = parent.origin.copy ().translate (ox, oy, oz)
if parent.xrotation:
self.origin_x = self.pos.y
else:
self.origin_x = self.pos.x
self.base = base[:]
self.hits = hits
parent.eyes.append(self)
# Eye AI
def think (self, dt):
if not self.blocks: # Don't bother running if it's not there currently
return
self.look_timer -= dt
if self.look_timer <= 0.0: # Resets time if it's less than 0
self.look_timer = uniform (self.look_interval_min, self.look_interval_max)
offset = choice ([1, -1])
if not self.parent.xrotation:
if abs (self.pos.x + offset-self.origin_x) < 2:
self.protocol.set_block_color(KRAKEN_BLACK)
for x, y, z in self.blocks:
self.protocol.build_block(x, y, z, KRAKEN_BLACK,
force = True)
self.blocks = set()
self.pos.x+=offset
self.create_instant()
else:
if abs(self.pos.y+offset-self.origin_x)<2:
self.protocol.set_block_color(KRAKEN_BLACK)
for x, y, z in self.blocks:
self.protocol.build_block(x, y, z, KRAKEN_BLACK,
force = True)
self.blocks = set()
self.pos.y+=offset
self.create_instant()
def create(self, block_queue = None, close = False):
if block_queue is None:
block_queue = deque(self.base)
last_color = None
x, y, z = self.pos.get()
x_d = None
while block_queue:
u, v, w, color = block_queue[0]
if self.parent.xrotation:
u,v=(v,u)
if x_d is None:
x_d = abs(u)
elif abs(u) != x_d:
break
if color != last_color:
self.protocol.set_block_color(color)
last_color = color
u, v, w = x + u, y + v, z + w
uvw = (u, v, w)
self.protocol.build_block(u, v, w, color, force = True)
if not close:
self.parent.head.discard(uvw)
self.blocks.add(uvw)
block_queue.popleft()
if block_queue:
self.create_call = reactor.callLater(0.25, self.create, block_queue, close)
self.closed=close
def create_instant(self, block_list = None):
if block_list is None:
block_list = self.base
last_color = None
x, y, z = self.pos.get()
block_list = sorted(block_list, key = itemgetter(3))
for u, v, w, color in block_list:
if self.parent.xrotation:
u,v = (v,u)
if color != last_color:
self.protocol.set_block_color(color)
last_color = color
u, v, w = x + u, y + v, z + w
uvw = (u, v, w)
self.protocol.build_block(u, v, w, color, force = True)
self.parent.head.discard(uvw)
self.blocks.add(uvw)
def on_block_destroy(self,x,y,z,mode):
xyz=(x,y,z)
if xyz not in self.blocks:
return True
return not self.closed
def on_block_removed(self, x, y, z):
xyz = (x, y, z)
if self.dead or (xyz not in self.blocks) or self.closed:
return
protocol = self.protocol
protocol.create_explosion_effect(Vertex3(x, y, z))
self.parent.build_queue.append((x, y, z, KRAKEN_BLOOD))
self.hits -= 1
if self.hits > 0:
self.pain()
if self.parent.xrotation:
uvw = (y - self.pos.y, x - self.pos.x, z - self.pos.z)
else:
uvw = (x - self.pos.x, y - self.pos.y, z - self.pos.z)
i = [uvwc[:-1] for uvwc in self.base].index(uvw)
self.base[i] = uvw + (KRAKEN_BLOOD,)
if self.on_hit:
self.on_hit (self)
else:
self.close()
self.dead = True
return
def close(self):
self.parent.head.update(self.blocks)
self.blocks.clear()
if self.create_call and self.create_call.active():
self.create_call.cancel()
reactor.callLater(0.5, self.create, deque(KRAKEN_EYE_SMALL_CLOSED),
close = True)
def pain(self):
self.close()
reactor.callLater(EYE_PAIN_TIME, self.create)
self.look_timer = EYE_PAIN_TIME + self.look_interval_min
class Kraken(Animated):
dead = False
origin = None
tentacles = None
head = None
eyes = None
max_hp = 10.0
hp = max_hp
size = 7
on_last_tentacle_death = None
on_death = None
on_removed = None
finally_call = None
phase = 0
yrotation=False
xrotation=False
def __init__(self, protocol, (x, y, z)):
Animated.__init__(self, protocol)
self.origin = Vertex3(x, y, z)
self.head = set()
self.eyes = []
self.tentacles = []
self.yrotation=self.protocol.kraken_flip
self.xrotation=self.protocol.kraken_rotate_x
self.protocol.kraken_flip=False
self.protocol.kraken_rotate_x=False
def is_location_inside(self, location, skip = None):
if location in self.head:
return True
for eye in self.eyes:
if location in eye.blocks:
return True
for t in self.tentacles:
if t is not skip and location in t.blocks:
return True
return False
def is_enemy_targeted(self, player):
for t in self.tentacles:
if t.follow is player:
return True
return False
def on_block_destroy(self, x, y, z, mode):
for t in self.tentacles:
if t.on_block_destroy(x, y, z, mode) == False:
return False
for eye in self.eyes:
if eye.on_block_destroy(x,y,z,mode) == False:
return False
def on_block_removed(self, x, y, z):
ReturnValue = 0
eye_died = False
for eye in self.eyes:
eye.on_block_removed(x, y, z)
eye_died = eye_died or eye.dead
if eye_died:
self.eyes = [eye for eye in self.eyes if not eye.dead]
if not self.eyes:
self.die()
ReturnValue=BLOCK_REMOVE_KRAKEN_KILL
for t in self.tentacles:
ReturnValue|=t.on_block_removed(x, y, z)
return ReturnValue
def die(self):
protocol = self.protocol
def remove(this, remover, blocks):
if blocks:
remover(*blocks.pop())
reactor.callLater(0.01, this, this, remover, blocks)
elif self.on_removed:
self.on_removed(self)
def explode(this, effect, blocks, left):
x = self.origin.x + uniform(-5.0, 5.0)
y = self.origin.y + self.size + 1.0
z = self.origin.z + uniform(-15.0, 0.0)
effect(Vertex3(x, y, z))
if not blocks or left <= 0:
return
delay = uniform(0.3, 0.8)
left -= 1
reactor.callLater(delay, this, this, effect, blocks, left)
remove(remove, protocol.remove_block, self.head)
explode(explode, protocol.create_explosion_effect, self.head, 10)
self.dead = True
for t in self.tentacles:
t.die()
if self.on_death:
self.on_death(self)
clear_mem(self.protocol,self)
def think(self, dt):
for eye in self.eyes:
eye.think(dt)
rebuild_list = False
for t in self.tentacles:
if t:
t.think(dt)
rebuild_list = rebuild_list or t.dead
if rebuild_list:
_tentacles=[]
for t in self.tentacles:
if t or not t.dead:
_tentacles.append(t)
self.tentacles = _tentacles
if not self.tentacles and self.on_last_tentacle_death:
self.on_last_tentacle_death(self)
def hit(self, value, rate):
hp_bar = self.protocol.hp_bar
if not hp_bar.shown:
hp_bar.progress = 1.0 - self.hp / self.max_hp
hp_bar.show()
self.hp = max(self.hp - value, 0)
previous_rate = hp_bar.rate
hp_bar.get_progress(True)
hp_bar.rate = rate
hp_bar.update_rate()
hp_bar.send_progress()
target_progress = 1.0 - self.hp / self.max_hp
delay = (target_progress - hp_bar.progress) / hp_bar.rate_value
hp_call = hp_bar.hp_call
if hp_call and hp_call.active():
if previous_rate == 0:
hp_call.cancel()
else:
hp_call.reset(delay)
return
if delay<0:
delay=0
hp_bar.hp_call = reactor.callLater(delay, hp_bar.stop)
def create_head(self, head_list, height = None):
height = height or len(head_list)
x, y, z = self.origin.get()
for d in head_list[-height:]:
for u, v in d:
if self.yrotation:
v=-v
if self.xrotation:
u,v=(-v,u)
xyzc = (x + u, y + v, z, KRAKEN_BLACK)
self.build_queue.append(xyzc)
self.head.add(xyzc[:-1])
z -= 1
if height < len(head_list):
delay = 0.6
reactor.callLater(delay, self.create_head, head_list, height + 6)
def clear_kraken_mem(protocol,kraken):
kraken.build_loop.stop()
kraken.build_loop=None
kraken.build_queue=None
del kraken
return
@admin
def kraken(connection, value = None):
protocol = connection.protocol
if protocol.game_mode != TC_MODE:
return 'Unfortunately, the game mode is required to be TC. Change it then restart'
if protocol.boss:
return "There is already a kraken! Why can't I hold all these krakens?"
try:
x, y = coordinates(value)
except (ValueError):
return 'Need coordinates where to spawn the kraken, e.g /kraken E3'
start_kraken(protocol, max(x, 64), max(y, 64))
if ALLOW_KRAKEN_COMMAND:
add(kraken)
@admin
def kraken_const(self, constname, value=None, type=None):
try:
constval=globals()[constname]
except KeyError:
return 'No such variable'
if not value or not type:
return '%s = %s' % (constname, constval)
if 's' in type:
pass
elif 'd' in type:
value=int(value)
elif 'f' in type:
value=float(value)
elif 'b' in type:
value=True if value=='True' else False
else:
return 'Unknown type'
globals()[constname]=value
return 'Set %s(%s) to %s' % (constname, constval, value)
add(kraken_const)
def get_kraken_ratio(protocol):
if not protocol.kraken_kills:
return 1.0
return float(protocol.kraken_deaths)/float(protocol.kraken_kills)
def kraken_ratio(self):
return ('Kraken kill/death ratio:%s, kills:%s, deaths:%s' % (get_kraken_ratio(self.protocol),
self.protocol.kraken_kills, self.protocol.kraken_deaths))
add(kraken_ratio)
def kraken_player_ratio(self):
kraken_ratio=get_kraken_ratio(self.protocol)
player_count=len(self.protocol.players)
return ('Kraken ratio/player ratio:%s, player count:%s' % (kraken_ratio/max(1,player_count), player_count))
add(kraken_player_ratio)
def start_kraken(protocol, x, y, hardcore = False, finally_call = None):
y += 32
boss = Kraken(protocol, (x, y - 12, 63))
protocol.boss = boss
protocol.map_change_on_kraken_win=hardcore
if USE_DAYCYCLE and protocol.daycycle_loop.running:
protocol.daycycle_loop.stop()
arena = getattr(protocol.map_info.info, 'arena', None)
if arena:
arena_center = (int((arena[2] - arena[0]) / 2.0 + arena[0]),
int((arena[3] - arena[1]) / 2.0 + arena[1]))
arena_radius = min(arena[2] - arena[0], arena[3] - arena[1]) / 2.0
def randring():
min_r, max_r = 12.0, 32.0
r = uniform(min_r, max_r)
a = uniform(0.0, pi)
return x + cos(a) * r, y + sin(a) * r, 63
def randring_arena():
if not arena:
return randring()
r = uniform(arena_radius, arena_radius * 1.2)
a = uniform(0.0, 2*pi)
x, y = arena_center
return x + cos(a) * r, y + sin(a) * r, 63
def minor_hit(caller = None):
boss.hit(1.0, 1)
caller.on_removed = None
def major_hit(caller = None):
boss.hit(3.0, 1)
def major_hit_and_progress(caller = None):
caller.on_hit = major_hit
major_hit()
progress()
def major_hit_and_pain(caller = None):
major_hit()
boss_alive = False
for eye in caller.parent.eyes:
if eye is not caller and not eye.dead:
eye.pain()
boss_alive = True
if boss_alive and caller.dead:
falling_blocks_start()
def respawn_tentacle(caller = None):
if boss and not boss.dead:
reactor.callLater(5.0-float(FAST_RUN)*4.0, spawn_tentacles, 1, True)
def spawn_tentacles(amount, respawn = False, fast = False, arena = False,
no_hit = False):
if not hardcore:
toughness = max(3.0, min(10.0, len(protocol.players) * 0.5))
else:
toughness = max(5.0, min(13.0, len(protocol.players) * 0.85))
if boss and not boss.dead:
for i in xrange(amount):
origin = randring_arena() if arena else randring()
t = Tentacle(protocol, boss, origin)
t.max_hp = toughness
t.growth_timer = uniform(i * 1.0, i * 1.2)
if hardcore:
t.initial_growth_interval *= 0.8
if fast:
t.initial_growth_interval *= 0.5
else:
t.follow_timer = 2.0
t.growth_interval = t.initial_growth_interval
if respawn:
t.on_removed = respawn_tentacle
elif not no_hit:
t.on_death = minor_hit
t.on_removed = minor_hit
def falling_blocks_cycle():
alive_players = filter (is_valid_enemy, protocol.players.values ())
if not alive_players:
return
player = choice(alive_players)
x, y, z = player.world_object.position.get()
protocol.create_falling_block(int(x), int(y), randrange(2, 4), 2)
def falling_blocks_start():
protocol.send_chat('LOOK UP!', global_message = True)
for i in range(FALLING_BLOCK_COUNT):
reactor.callLater(i * 0.4+FALLING_BLOCK_START_DELAY, falling_blocks_cycle)
def squid_head():
h = []
for i in xrange(37, 5, -2):
h.append(list(disc(i, min_rr = i - 15)))
return h
def squid_head_large():
h = []
for i in xrange(42, 3, -2):
ii = int(i ** 1.3)
h.append(list(disc(ii, y = int(sqrt(i)), min_rr = i + 10)))
return h
def regenerate_players():
for player in protocol.players.values():
if player.world_object:
player.free_from_kraken()
player.last_hit = reactor.seconds()
player.regenerating = True
if not player.world_object.dead:
player.regen_loop.start(REGEN_FREQUENCY)
else:
player.spawn(player.world_object.position.get())
def round_end(caller = None):
regenerate_players()
reactor.callLater(8.0-float(FAST_RUN)*7.0, progress)
def round_end_delay(caller = None):
reactor.callLater(10.0-float(FAST_RUN)*9.0, round_end)
def round_start(caller = None):
for player in protocol.players.values():
player.regenerating = False
def progress_delay(caller = None):
reactor.callLater(6.0-float(FAST_RUN)*5.0, progress)
def victory(caller = None):
regenerate_players()
if USE_DAYCYCLE:
protocol.current_time = 23.30
protocol.update_day_color()
def cleanup(caller = None):
round_start()
clear_mem(protocol,protocol.boss)
protocol.boss = None
if USE_DAYCYCLE and protocol.daycycle_loop.running:
protocol.daycycle_loop.stop()
if caller.finally_call:
caller.finally_call(caller)
if protocol.map_change_on_kraken_win:
protocol.send_chat(KRAKEN_WIN_ANNOUNCEMENT,True)
reactor.callLater(MAP_CHANGE_DELAY,protocol.advance_rotation,'The round was won.')
def red_sky():
if USE_DAYCYCLE:
protocol.day_colors = [
( 0.00, (0.5527, 0.24, 0.94), False),
( 0.10, (0.0, 0.05, 0.05), True),
( 0.20, (0.0, 1.00, 0.34), False),
(23.30, (0.0, 1.00, 0.34), False),
(23.50, (0.5527, 0.24, 0.94), False)]
protocol.current_time = 0.00
protocol.target_color_index = 0
protocol.update_day_color()
if not protocol.daycycle_loop.running:
protocol.daycycle_loop.start(protocol.day_update_frequency)
progress = None
def progress_normal(caller = None):
boss.phase += 1
round_start()
if boss.phase == 1:
boss.on_last_tentacle_death = progress_delay
spawn_tentacles(2)
elif boss.phase == 2:
boss.on_last_tentacle_death = round_end
spawn_tentacles(4)
elif boss.phase == 3:
boss.on_last_tentacle_death = round_end
spawn_tentacles(3, fast = True)
elif boss.phase == 4:
boss.on_last_tentacle_death = None
boss.on_death = round_end_delay
boss.size = 7