/
server.py
1756 lines (1590 loc) · 64.2 KB
/
server.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
# Copyright (c) Mathias Kaerlev 2011-2012.
# This file is part of pyspades.
# pyspades is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# pyspades is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with pyspades. If not, see <http://www.gnu.org/licenses/>.
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
from pyspades.protocol import BaseConnection, BaseProtocol
from pyspades.bytes import ByteReader, ByteWriter
from pyspades.packet import load_client_packet
from pyspades.common import *
from pyspades.constants import *
from pyspades import contained as loaders
from pyspades.types import MultikeyDict, IDPool
from pyspades.master import get_master_connection
from pyspades.collision import vector_collision, collision_3d
from pyspades import world
from pyspades.debug import *
from pyspades.weapon import WEAPONS
import enet
import random
import math
import shlex
import textwrap
import collections
import zlib
COMPRESSION_LEVEL = 9
create_player = loaders.CreatePlayer()
position_data = loaders.PositionData()
orientation_data = loaders.OrientationData()
input_data = loaders.InputData()
grenade_packet = loaders.GrenadePacket()
set_tool = loaders.SetTool()
set_color = loaders.SetColor()
fog_color = loaders.FogColor()
existing_player = loaders.ExistingPlayer()
player_left = loaders.PlayerLeft()
block_action = loaders.BlockAction()
kill_action = loaders.KillAction()
chat_message = loaders.ChatMessage()
map_data = loaders.MapChunk()
map_start = loaders.MapStart()
state_data = loaders.StateData()
ctf_data = loaders.CTFState()
tc_data = loaders.TCState()
intel_drop = loaders.IntelDrop()
intel_pickup = loaders.IntelPickup()
intel_capture = loaders.IntelCapture()
restock = loaders.Restock()
move_object = loaders.MoveObject()
set_hp = loaders.SetHP()
change_weapon = loaders.ChangeWeapon()
change_team = loaders.ChangeTeam()
weapon_reload = loaders.WeaponReload()
territory_capture = loaders.TerritoryCapture()
progress_bar = loaders.ProgressBar()
world_update = loaders.WorldUpdate()
block_line = loaders.BlockLine()
weapon_input = loaders.WeaponInput()
def check_nan(*values):
for value in values:
if math.isnan(value):
return True
return False
def parse_command(input):
value = encode(input)
try:
splitted = shlex.split(value)
except ValueError:
# shlex failed. let's just split per space
splitted = value.split(' ')
if splitted:
command = splitted.pop(0)
else:
command = ''
splitted = [decode(value) for value in splitted]
return decode(command), splitted
class SlidingWindow(object):
def __init__(self, entries):
self.entries = entries
self.window = collections.deque()
def add(self, value):
self.window.append(value)
if len(self.window) <= self.entries:
return
self.window.popleft()
def check(self):
return len(self.window) == self.entries
def get(self):
return self.window[0], self.window[-1]
class MapGeneratorChild(object):
pos = 0
def __init__(self, generator):
self.parent = generator
def get_size(self):
return self.parent.get_size()
def read(self, size):
pos = self.pos
if pos + size > self.parent.pos:
self.parent.read(size)
data = self.parent.all_data[pos:pos+size]
self.pos += len(data)
return data
def data_left(self):
return self.parent.data_left() or self.pos < self.parent.pos
class ProgressiveMapGenerator(object):
data = ''
done = False
# parent attributes
all_data = ''
pos = 0
def __init__(self, map, parent = False):
self.parent = parent
self.generator = map.get_generator()
self.compressor = zlib.compressobj(COMPRESSION_LEVEL)
def get_size(self):
return 1.5 * 1024 * 1024 # 2 mb
def read(self, size):
data = self.data
generator = self.generator
if len(data) < size and generator is not None:
while 1:
map_data = generator.get_data(1024)
if generator.done:
self.generator = None
data += self.compressor.flush()
break
data += self.compressor.compress(map_data)
if len(data) >= size:
break
if self.parent:
# save the data in case we are a parent
self.all_data += data
self.pos += len(data)
else:
self.data = data[size:]
return data[:size]
def get_child(self):
return MapGeneratorChild(self)
def data_left(self):
return bool(self.data) or self.generator is not None
class ServerConnection(BaseConnection):
address = None
player_id = None
map_packets_sent = 0
team = None
weapon = None
weapon_object = None
name = None
kills = 0
hp = None
tool = None
color = (0x70, 0x70, 0x70)
grenades = None
blocks = None
spawn_call = None
respawn_time = None
saved_loaders = None
last_refill = None
last_block_destroy = None
filter_visibility_data = False
filter_animation_data = False
freeze_animation = False
filter_weapon_input = False
speedhack_detect = False
rapid_hack_detect = False
timers = None
world_object = None
last_block = None
map_data = None
last_position_update = None
def __init__(self, *arg, **kw):
BaseConnection.__init__(self, *arg, **kw)
protocol = self.protocol
address = self.peer.address
self.address = (address.host, address.port)
self.respawn_time = protocol.respawn_time
self.rapids = SlidingWindow(RAPID_WINDOW_ENTRIES)
def on_connect(self):
if self.peer.eventData != self.protocol.version:
self.disconnect(ERROR_WRONG_VERSION)
return
max_players = min(32, self.protocol.max_players)
if len(self.protocol.connections) > max_players:
self.disconnect(ERROR_FULL)
return
if self.protocol.max_connections_per_ip:
shared = [conn for conn in
self.protocol.connections.values()
if conn.address[0] == self.address[0]]
if len(shared) > self.protocol.max_connections_per_ip:
self.disconnect(ERROR_KICKED)
return
if not self.disconnected:
self._connection_ack()
def loader_received(self, loader):
if self.player_id is not None:
contained = load_client_packet(ByteReader(loader.data))
if contained.id in (loaders.ExistingPlayer.id,
loaders.ShortPlayerData.id):
old_team = self.team
team = self.protocol.teams[contained.team]
if self.on_team_join(team) == False:
if not team.spectator:
team = team.other
self.team = team
if self.name is None:
name = contained.name
# vanilla AoS behaviour
if name == 'Deuce':
name = name + str(self.player_id)
self.name = self.protocol.get_name(name)
self.protocol.players[self.name, self.player_id] = self
self.on_login(self.name)
else:
self.on_team_changed(old_team)
self.set_weapon(contained.weapon, True)
if self.protocol.speedhack_detect:
self.speedhack_detect = True
self.rapid_hack_detect = True
if team.spectator:
if self.world_object is not None:
self.world_object.delete()
self.world_object = None
self.spawn()
return
if self.hp:
world_object = self.world_object
if contained.id == loaders.OrientationData.id:
x, y, z = contained.x, contained.y, contained.z
if check_nan(x, y, z):
self.on_hack_attempt(
'Invalid orientation data received')
return
returned = self.on_orientation_update(x, y, z)
if returned == False:
return
if returned is not None:
x, y, z = returned
world_object.set_orientation(x, y, z)
elif contained.id == loaders.PositionData.id:
current_time = reactor.seconds()
last_update = self.last_position_update
self.last_position_update = current_time
if last_update is not None:
dt = current_time - last_update
if dt < MAX_POSITION_RATE:
self.set_location()
return
x, y, z = contained.x, contained.y, contained.z
if check_nan(x, y, z):
self.on_hack_attempt(
'Invalid position data received')
return
position = world_object.position
if not self.is_valid_position(x, y, z):
# vanilla behaviour
self.set_location()
return
if not self.freeze_animation:
world_object.set_position(x, y, z)
self.on_position_update()
if self.filter_visibility_data:
return
game_mode = self.protocol.game_mode
if game_mode == CTF_MODE:
other_flag = self.team.other.flag
if vector_collision(world_object.position,
self.team.base):
if other_flag.player is self:
self.capture_flag()
self.check_refill()
if other_flag.player is None and vector_collision(
world_object.position, other_flag):
self.take_flag()
elif game_mode == TC_MODE:
for entity in self.protocol.entities:
collides = vector_collision(entity,
world_object.position, TC_CAPTURE_DISTANCE)
if self in entity.players:
if not collides:
entity.remove_player(self)
else:
if collides:
entity.add_player(self)
if collides and vector_collision(entity,
world_object.position):
self.check_refill()
elif contained.id == loaders.WeaponInput.id:
primary = contained.primary
secondary = contained.secondary
if world_object.primary_fire != primary:
if self.tool == WEAPON_TOOL:
self.weapon_object.set_shoot(primary)
if self.tool == WEAPON_TOOL or self.tool == SPADE_TOOL:
self.on_shoot_set(primary)
if world_object.secondary_fire != secondary:
self.on_secondary_fire_set(secondary)
world_object.primary_fire = primary
world_object.secondary_fire = secondary
if self.filter_weapon_input:
return
contained.player_id = self.player_id
self.protocol.send_contained(contained, sender = self)
elif contained.id == loaders.InputData.id:
returned = self.on_walk_update(contained.up, contained.down,
contained.left, contained.right)
if returned is not None:
up, down, left, right = returned
if (up != contained.up or down != contained.down or
left != contained.left or right != contained.right):
(contained.up, contained.down, contained.left,
contained.right) = returned
## XXX unsupported
#~ self.send_contained(contained)
if not self.freeze_animation:
world_object.set_walk(contained.up, contained.down,
contained.left, contained.right)
contained.player_id = self.player_id
z_vel = world_object.velocity.z
if contained.jump and not (z_vel >= 0 and z_vel < 0.017):
contained.jump = False
## XXX unsupported for now
# returned = self.on_animation_update(contained.primary_fire,
# contained.secondary_fire, contained.jump,
# contained.crouch)
# if returned is not None:
# fire1, fire2, jump, crouch = returned
# if (fire1 != contained.primary_fire or
# fire2 != contained.secondary_fire or
# jump != contained.jump or
# crouch != contained.crouch):
# (contained.primary_fire, contained.secondary_fire,
# contained.jump, contained.crouch) = returned
# self.send_contained(contained)
returned = self.on_animation_update(contained.jump,
contained.crouch, contained.sneak, contained.sprint)
if returned is not None:
jump, crouch, sneak, sprint = returned
if (jump != contained.jump or crouch != contained.crouch or
sneak != contained.sneak or sprint != contained.sprint):
(contained.jump, contained.crouch, contained.sneak,
contained.sprint) = returned
self.send_contained(contained)
if not self.freeze_animation:
world_object.set_animation(contained.jump,
contained.crouch, contained.sneak, contained.sprint)
if self.filter_visibility_data or self.filter_animation_data:
return
self.protocol.send_contained(contained, sender = self)
elif contained.id == loaders.WeaponReload.id:
self.weapon_object.reload()
if self.filter_animation_data:
return
contained.player_id = self.player_id
self.protocol.send_contained(contained, sender = self)
elif contained.id == loaders.HitPacket.id:
value = contained.value
is_melee = value == MELEE
if not is_melee and self.weapon_object.is_empty():
return
try:
player = self.protocol.players[contained.player_id]
except KeyError:
return
valid_hit = world_object.validate_hit(player.world_object,
value, HIT_TOLERANCE)
if not valid_hit:
return
position1 = world_object.position
position2 = player.world_object.position
if is_melee:
if not vector_collision(position1, position2,
MELEE_DISTANCE):
return
hit_amount = self.protocol.melee_damage
else:
hit_amount = self.weapon_object.get_damage(
value, position1, position2)
if is_melee:
type = MELEE_KILL
elif contained.value == HEAD:
type = HEADSHOT_KILL
else:
type = WEAPON_KILL
returned = self.on_hit(hit_amount, player, type, None)
if returned == False:
return
elif returned is not None:
hit_amount = returned
player.hit(hit_amount, self, type)
elif contained.id == loaders.GrenadePacket.id:
if check_nan(contained.value) or check_nan(*contained.position) or check_nan(*contained.velocity):
self.on_hack_attempt("Invalid grenade data")
return
if not self.grenades:
return
self.grenades -= 1
if not self.is_valid_position(*contained.position):
contained.position = self.world_object.position.get()
if self.on_grenade(contained.value) == False:
return
grenade = self.protocol.world.create_object(
world.Grenade, contained.value,
Vertex3(*contained.position), None,
Vertex3(*contained.velocity), self.grenade_exploded)
grenade.team = self.team
self.on_grenade_thrown(grenade)
if self.filter_visibility_data:
return
contained.player_id = self.player_id
self.protocol.send_contained(contained,
sender = self)
elif contained.id == loaders.SetTool.id:
if self.on_tool_set_attempt(contained.value) == False:
return
old_tool = self.tool
self.tool = contained.value
if old_tool == WEAPON_TOOL:
self.weapon_object.set_shoot(False)
if self.tool == WEAPON_TOOL:
self.on_shoot_set(self.world_object.primary_fire)
self.weapon_object.set_shoot(
self.world_object.primary_fire)
self.world_object.set_weapon(self.tool == WEAPON_TOOL)
self.on_tool_changed(self.tool)
if self.filter_visibility_data or self.filter_animation_data:
return
set_tool.player_id = self.player_id
set_tool.value = contained.value
self.protocol.send_contained(set_tool, sender = self)
elif contained.id == loaders.SetColor.id:
color = get_color(contained.value)
if self.on_color_set_attempt(color) == False:
return
self.color = color
self.on_color_set(color)
if self.filter_animation_data:
return
contained.player_id = self.player_id
self.protocol.send_contained(contained, sender = self,
save = True)
elif contained.id == loaders.BlockAction.id:
value = contained.value
if value == BUILD_BLOCK:
interval = TOOL_INTERVAL[BLOCK_TOOL]
elif self.tool == WEAPON_TOOL:
if self.weapon_object.is_empty():
return
interval = WEAPON_INTERVAL[self.weapon]
else:
interval = TOOL_INTERVAL[self.tool]
current_time = reactor.seconds()
last_time = self.last_block
self.last_block = current_time
if (self.rapid_hack_detect and last_time is not None and
current_time - last_time < interval):
self.rapids.add(current_time)
if self.rapids.check():
start, end = self.rapids.get()
if end - start < MAX_RAPID_SPEED:
print 'RAPID HACK:', self.rapids.window
self.on_hack_attempt('Rapid hack detected')
return
map = self.protocol.map
x = contained.x
y = contained.y
z = contained.z
if z >= 62:
return
if value == BUILD_BLOCK:
self.blocks -= 1
pos = world_object.position
if self.blocks < -BUILD_TOLERANCE:
return
elif not collision_3d(pos.x, pos.y, pos.z, x, y, z,
MAX_BLOCK_DISTANCE):
return
elif self.on_block_build_attempt(x, y, z) == False:
return
elif not map.build_point(x, y, z, self.color):
return
self.on_block_build(x, y, z)
else:
if not map.get_solid(x, y, z):
return
pos = world_object.position
if self.tool == SPADE_TOOL and not collision_3d(
pos.x, pos.y, pos.z, x, y, z, MAX_DIG_DISTANCE):
return
if self.on_block_destroy(x, y, z, value) == False:
return
elif value == DESTROY_BLOCK:
if map.destroy_point(x, y, z):
self.blocks = min(50, self.blocks + 1)
self.on_block_removed(x, y, z)
elif value == SPADE_DESTROY:
if map.destroy_point(x, y, z):
self.on_block_removed(x, y, z)
if map.destroy_point(x, y, z + 1):
self.on_block_removed(x, y, z + 1)
if map.destroy_point(x, y, z - 1):
self.on_block_removed(x, y, z - 1)
self.last_block_destroy = reactor.seconds()
block_action.x = x
block_action.y = y
block_action.z = z
block_action.value = contained.value
block_action.player_id = self.player_id
self.protocol.send_contained(block_action, save = True)
self.protocol.update_entities()
elif contained.id == loaders.BlockLine.id:
x1, y1, z1 = (contained.x1, contained.y1, contained.z1)
x2, y2, z2 = (contained.x2, contained.y2, contained.z2)
pos = world_object.position
if not collision_3d(pos.x, pos.y, pos.z, x2, y2, z2,
MAX_BLOCK_DISTANCE):
return
points = world.cube_line(x1, y1, z1, x2, y2, z2)
if not points:
return
if len(points) > (self.blocks + BUILD_TOLERANCE):
return
map = self.protocol.map
if self.on_line_build_attempt(points) == False:
return
for point in points:
x, y, z = point
if not map.build_point(x, y, z, self.color):
break
self.blocks -= len(points)
self.on_line_build(points)
contained.player_id = self.player_id
self.protocol.send_contained(contained, save = True)
self.protocol.update_entities()
if self.name:
if contained.id == loaders.ChatMessage.id:
if not self.name:
return
value = contained.value
if value.startswith('/'):
self.on_command(*parse_command(value[1:]))
else:
global_message = contained.chat_type == CHAT_ALL
result = self.on_chat(value, global_message)
if result == False:
return
elif result is not None:
value = result
contained.chat_type = [CHAT_TEAM, CHAT_ALL][
int(global_message)]
contained.value = value
contained.player_id = self.player_id
if global_message:
team = None
else:
team = self.team
for player in self.protocol.players.values():
if not player.deaf:
if team is None or team is player.team:
player.send_contained(contained)
self.on_chat_sent(value, global_message)
elif contained.id == loaders.FogColor.id:
color = get_color(contained.color)
self.on_command('fog', [str(item) for item in color])
elif contained.id == loaders.ChangeWeapon.id:
if self.on_weapon_set(contained.weapon) == False:
return
self.weapon = contained.weapon
self.set_weapon(self.weapon)
elif contained.id == loaders.ChangeTeam.id:
team = self.protocol.teams[contained.team]
if self.on_team_join(team) == False:
return
self.set_team(team)
def is_valid_position(self, x, y, z, distance = None):
if not self.speedhack_detect:
return True
if distance is None:
distance = RUBBERBAND_DISTANCE
position = self.world_object.position
return (math.fabs(x - position.x) < distance and
math.fabs(y - position.y) < distance and
math.fabs(z - position.z) < distance)
def check_refill(self):
last_refill = self.last_refill
if (last_refill is None or
reactor.seconds() - last_refill > self.protocol.refill_interval):
self.last_refill = reactor.seconds()
if self.on_refill() != False:
self.refill()
def get_location(self):
position = self.world_object.position
return position.x, position.y, position.z
def is_location_free(self, x, y, z):
return (self.protocol.map.get_solid(x, y, z) == 0 and
self.protocol.map.get_solid(x, y, z + 1) == 0 and
self.protocol.map.get_solid(x, y, z + 2) == 0 and
self.protocol.map.get_solid(x, y, z + 3) == 1)
def set_location_safe(self, location, center = True):
x, y, z = location
if center:
x -= 0.5
y -= 0.5
z += 0.5
x = int(x)
y = int(y)
z = int(z)
# search for valid locations near the specified point
modpos = 0
pos_table = self.protocol.pos_table
while (modpos<len(pos_table) and not
self.is_location_free(x + pos_table[modpos][0],
y + pos_table[modpos][1],
z + pos_table[modpos][2])):
modpos+=1
if modpos == len(pos_table): # nothing nearby
return
x = x + pos_table[modpos][0]
y = y + pos_table[modpos][1]
z = z + pos_table[modpos][2]
self.set_location((x, y, z))
def set_location(self, location = None):
if location is None:
# used for rubberbanding
position = self.world_object.position
x, y, z = position.x, position.y, position.z
else:
x, y, z = location
if self.world_object is not None:
self.world_object.set_position(x, y, z)
x += 0.5
y += 0.5
z -= 0.5
position_data.x = x
position_data.y = y
position_data.z = z
self.send_contained(position_data)
def refill(self, local = False):
self.hp = 100
self.grenades = 3
self.blocks = 50
self.weapon_object.restock()
if not local:
self.send_contained(restock)
def respawn(self):
if self.spawn_call is None:
self.spawn_call = reactor.callLater(
self.get_respawn_time(), self.spawn)
def get_spawn_location(self):
game_mode = self.protocol.game_mode
if game_mode == TC_MODE:
try:
base = random.choice(list(self.team.get_entities()))
return base.get_spawn_location()
except IndexError:
pass
return self.team.get_random_location(True)
def get_respawn_time(self):
if not self.respawn_time:
return 0
if self.protocol.respawn_waves:
offset = reactor.seconds() % self.respawn_time
else:
offset = 0
return self.respawn_time - offset
def spawn(self, pos = None):
self.spawn_call = None
if self.team is None:
return
spectator = self.team.spectator
if not spectator:
if pos is None:
x, y, z = self.get_spawn_location()
x += 0.5
y += 0.5
z -= 2.4
else:
x, y, z = pos
returned = self.on_spawn_location((x, y, z))
if returned is not None:
x, y, z = returned
if self.world_object is not None:
self.world_object.set_position(x, y, z, True)
else:
position = Vertex3(x, y, z)
self.world_object = self.protocol.world.create_object(
world.Character, position, None, self._on_fall)
self.world_object.dead = False
self.tool = WEAPON_TOOL
self.refill(True)
create_player.x = x
create_player.y = y
create_player.z = z
create_player.weapon = self.weapon
create_player.player_id = self.player_id
create_player.name = self.name
create_player.team = self.team.id
if self.filter_visibility_data and not spectator:
self.send_contained(create_player)
else:
self.protocol.send_contained(create_player, save = True)
if not spectator:
self.on_spawn((x, y, z))
def take_flag(self):
if not self.hp:
return
flag = self.team.other.flag
if flag.player is not None:
return
if self.on_flag_take() == False:
return
flag.player = self
intel_pickup.player_id = self.player_id
self.protocol.send_contained(intel_pickup, save = True)
def capture_flag(self):
other_team = self.team.other
flag = other_team.flag
player = flag.player
if player is not self:
return
self.add_score(10) # 10 points for intel
if (self.protocol.max_score not in (0, None) and
self.team.score + 1 >= self.protocol.max_score):
self.on_flag_capture()
self.protocol.reset_game(self)
self.protocol.on_game_end()
else:
intel_capture.player_id = self.player_id
intel_capture.winning = False
self.protocol.send_contained(intel_capture, save = True)
self.team.score += 1
flag = other_team.set_flag()
flag.update()
self.on_flag_capture()
def drop_flag(self):
protocol = self.protocol
game_mode = protocol.game_mode
if game_mode == CTF_MODE:
for flag in (protocol.blue_team.flag, protocol.green_team.flag):
player = flag.player
if player is not self:
continue
position = self.world_object.position
x = int(position.x)
y = int(position.y)
z = max(0, int(position.z))
z = self.protocol.map.get_z(x, y, z)
flag.set(x, y, z)
flag.player = None
intel_drop.player_id = self.player_id
intel_drop.x = flag.x
intel_drop.y = flag.y
intel_drop.z = flag.z
self.protocol.send_contained(intel_drop, save = True)
self.on_flag_drop()
break
elif game_mode == TC_MODE:
for entity in protocol.entities:
if self in entity.players:
entity.remove_player(self)
def on_disconnect(self):
if self.name is not None:
self.drop_flag()
player_left.player_id = self.player_id
self.protocol.send_contained(player_left, sender = self,
save = True)
del self.protocol.players[self]
if self.player_id is not None:
self.protocol.player_ids.put_back(self.player_id)
self.protocol.update_master()
self.reset()
def reset(self):
if self.spawn_call is not None:
self.spawn_call.cancel()
self.spawn_call = None
if self.world_object is not None:
self.world_object.delete()
self.world_object = None
if self.team is not None:
old_team = self.team
self.team = None
self.on_team_changed(old_team)
self.on_reset()
self.name = self.hp = self.world_object = None
def hit(self, value, by = None, type = WEAPON_KILL):
if self.hp is None:
return
if by is not None and self.team is by.team:
friendly_fire = self.protocol.friendly_fire
if friendly_fire == 'on_grief':
if (type == MELEE_KILL and
not self.protocol.spade_teamkills_on_grief):
return
hit_time = self.protocol.friendly_fire_time
if (self.last_block_destroy is None
or reactor.seconds() - self.last_block_destroy >= hit_time):
return
elif not friendly_fire:
return
self.set_hp(self.hp - value, by, type = type)
def set_hp(self, value, hit_by = None, type = WEAPON_KILL,
hit_indicator = None, grenade = None):
value = int(value)
self.hp = max(0, min(100, value))
if self.hp <= 0:
self.kill(hit_by, type, grenade)
return
set_hp.hp = self.hp
set_hp.not_fall = int(type != FALL_KILL)
if hit_indicator is None:
if hit_by is not None and hit_by is not self:
hit_indicator = hit_by.world_object.position.get()
else:
hit_indicator = (0, 0, 0)
x, y, z = hit_indicator
set_hp.source_x = x
set_hp.source_y = y
set_hp.source_z = z
self.send_contained(set_hp)
def set_weapon(self, weapon, local = False, no_kill = False):
self.weapon = weapon
if self.weapon_object is not None:
self.weapon_object.reset()
self.weapon_object = WEAPONS[weapon](self._on_reload)
if not local:
self.protocol.send_contained(change_weapon, save = True)
if not no_kill:
self.kill(type = CLASS_CHANGE_KILL)
def set_team(self, team):
if team is self.team:
return
self.drop_flag()
old_team = self.team
self.team = team
self.on_team_changed(old_team)
if old_team.spectator:
self.respawn()
else:
self.kill(type = TEAM_CHANGE_KILL)
def kill(self, by = None, type = WEAPON_KILL, grenade = None):
if self.hp is None:
return
if self.on_kill(by, type, grenade) is False:
return
self.drop_flag()
self.hp = None
self.weapon_object.reset()
kill_action.kill_type = type
if by is None:
kill_action.killer_id = kill_action.player_id = self.player_id
else:
kill_action.killer_id = by.player_id
kill_action.player_id = self.player_id
if by is not None and by is not self:
by.add_score(1)
kill_action.respawn_time = self.get_respawn_time() + 1
self.protocol.send_contained(kill_action, save = True)
self.world_object.dead = True
self.respawn()
def add_score(self, score):
self.kills += score
def _connection_ack(self):
self._send_connection_data()
self.send_map(ProgressiveMapGenerator(self.protocol.map))
def _send_connection_data(self):
saved_loaders = self.saved_loaders = []
if self.player_id is None:
for player in self.protocol.players.values():
if player.name is None:
continue
existing_player.name = player.name
existing_player.player_id = player.player_id
existing_player.tool = player.tool or 0
existing_player.weapon = player.weapon
existing_player.kills = player.kills
existing_player.team = player.team.id
existing_player.color = make_color(*player.color)
saved_loaders.append(existing_player.generate())
self.player_id = self.protocol.player_ids.pop()
self.protocol.update_master()
# send initial data
blue = self.protocol.blue_team
green = self.protocol.green_team
state_data.player_id = self.player_id
state_data.fog_color = self.protocol.fog_color
state_data.team1_color = blue.color
state_data.team1_name = blue.name
state_data.team2_color = green.color
state_data.team2_name = green.name
game_mode = self.protocol.game_mode
if game_mode == CTF_MODE:
blue_base = blue.base
blue_flag = blue.flag
green_base = green.base
green_flag = green.flag
ctf_data.cap_limit = self.protocol.max_score
ctf_data.team1_score = blue.score
ctf_data.team2_score = green.score
ctf_data.team1_base_x = blue_base.x
ctf_data.team1_base_y = blue_base.y
ctf_data.team1_base_z = blue_base.z
ctf_data.team2_base_x = green_base.x
ctf_data.team2_base_y = green_base.y
ctf_data.team2_base_z = green_base.z
if green_flag.player is None:
ctf_data.team1_has_intel = 0
ctf_data.team2_flag_x = green_flag.x
ctf_data.team2_flag_y = green_flag.y
ctf_data.team2_flag_z = green_flag.z
else:
ctf_data.team1_has_intel = 1
ctf_data.team2_carrier = green_flag.player.player_id
if blue_flag.player is None:
ctf_data.team2_has_intel = 0
ctf_data.team1_flag_x = blue_flag.x
ctf_data.team1_flag_y = blue_flag.y
ctf_data.team1_flag_z = blue_flag.z
else:
ctf_data.team2_has_intel = 1
ctf_data.team1_carrier = blue_flag.player.player_id
state_data.state = ctf_data
elif game_mode == TC_MODE:
state_data.state = tc_data
generated_data = state_data.generate()
saved_loaders.append(generated_data)