Advertisement
Guest User

Last Stand 1.253 for v0.76

a guest
Mar 31st, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 37.45 KB | None | 0 0
  1. """
  2. LAST STAND SCRIPT V1.253/0.76 BY INFLUX
  3. ONE TEAM IS ASSIGNED TO DEFEND A LOCATION. THE  OTHER TEAM MUST KILL ALL THE DEFENDERS.
  4.  
  5. ONCE THE TIME RUNS OUT, THE FLAG IS CAPTURED OR ALL THE DEFENDERS ARE DEAD,
  6. THE ROLES ARE REVERSED.
  7.  
  8. THE TEAM WHICH KILLS ALL THE DEFENDERS QUICKEST WINS A POINT.
  9.  
  10. To install, save this file (stand.py) to your pyspades/scripts folder, then open your config.txt and change the game_mode to "stand".
  11.  
  12. DO NOT ADD TO THE SCRIPT LIST.
  13.  
  14. For mapping, you must include several things in the 'extensions' dictionary in your map metadata.
  15. These are the following:
  16. 'attspawn': (xyz co-ordinates of the attacker's spawn)
  17. 'defspawn': (xyz co-ordinates of the defender's spawn)
  18. 'north': ([Optional] xyz co-ordinates of the north spawn point)*
  19. 'east': ([Optional] xyz co-ordinates of the east spawn point)*
  20. 'south': ([Optional] xyz co-ordinates of the south spawn point)*
  21. 'west': ([Optional] xyz co-ordinates of the west spawn point)*
  22. 'flag': (xyz co-ordinates of the flag's pole)
  23. 'time_limit': ([Optional] time limit in seconds. Defaults to 300 if this is not included or left blank)
  24.  
  25. * These are optional but there must be at least one directional spawn point otherwise you will get an error.
  26.  
  27.  
  28. THANKS TO:
  29.  
  30. TGM/hompy's Infiltration script (for code used in the intel capture mechanic)
  31. Yourself's Arena script (general tips on how to set about making this)
  32. hompy's Marker script (for code used in marking the flag - I've probably implemented it messily, but it works... mostly)
  33. """
  34.  
  35. import random
  36. from collections import defaultdict
  37. from functools import partial
  38. import markers
  39. from markers import BaseMarker, make_color, parse_string_map, other_markers
  40. from commands import add, get_player
  41. from pyspades.server import create_player, player_left, intel_capture
  42. from pyspades.constants import *
  43. from itertools import chain
  44. from twisted.internet import reactor
  45. from twisted.internet.task import LoopingCall
  46. from twisted.internet.reactor import callLater
  47. from commands import alias, name, add
  48. import math
  49.  
  50.  
  51. FLAG_CAP_ZONE = 15 # Blocks each side of the flag that will count as the cap zone
  52.  
  53.  
  54. class FlagAttBackground(BaseMarker):
  55.     color = make_color(0, 0, 0)
  56.     s = """
  57.    X X . . . . . . .
  58.    X X . . . . . . .
  59.    X X . . . . . . .
  60.    X X . . . . . . .
  61.    X X . . . . . . .
  62.    X X . . . . . . .
  63.    X X . . . . . . .
  64.    X X . . . . . . .
  65.    X X . . . . . . .
  66.    X X . . . . . . .
  67.    X X . . . . . . .
  68.    X X . . . . . . .
  69.    X X . . . . . . .
  70.    """
  71.  
  72. class FlagAtt(BaseMarker):
  73.     background_class = FlagAttBackground
  74.     team_color = True
  75.     s = """
  76.    . . . X X . . . . . . .
  77.    . . . X X X . . X X X .
  78.    . . . X X X X X X X X X
  79.    . . . X X X X X X X X X
  80.    . . . X X X X X X X X X
  81.    . . . X X X X X X X X X
  82.    . . . . . X X X X X X X
  83.    . . . . . . X X . . . X
  84.    . . . . . . . . . . . .
  85.    . . . . . . . . . . . .
  86.    . . . . . . . . . . . .
  87.    . . . . . . . . . . . .
  88.    . . . . . . . . . . . .
  89.    """
  90.    
  91. class FlagDefBackground(BaseMarker):
  92.     color = make_color(190, 190, 190)
  93.     s = """
  94.    X X X X X X X X X
  95.    X X X X X X X X X
  96.    X X X X X X X X X
  97.    X X X X X X X X X
  98.    X X X X X X X X X
  99.    X X X X X X X X X
  100.    X X X X X X X X X
  101.    X X X X X X X X X
  102.    . X X X X X X X .
  103.    . . X X X X X . .
  104.    . . . X X X . . .
  105.    """
  106.  
  107. class FlagDef(BaseMarker):
  108.     background_class = FlagDefBackground
  109.     team_color = True
  110.     s = """
  111.    . . . . . . . . .
  112.    . X X X X X X X .
  113.    . X . . . . . X .
  114.    . X . . . . . X .
  115.    . X . . . . . X .
  116.    . X . . . . . X .
  117.    . X . . . . . X .
  118.    . X . . . . . X .
  119.    . . X . . . X . .
  120.    . . . X X X . . .
  121.    . . . . . . . . .
  122.    """
  123.  
  124.  
  125. flag_markers = [FlagAtt, FlagDef]
  126. background_markers = []
  127.  
  128. for cls in chain(flag_markers, background_markers):
  129.     if cls.background_class:
  130.         background_markers.append(cls.background_class)
  131.     cls.lines, cls.points = parse_string_map(cls.s)
  132.  
  133. class CustomException(Exception):
  134.     def __init__(self, value):
  135.         self.parameter = value
  136.     def __str__(self):
  137.         return repr(self.parameter)
  138.  
  139. class DummyPlayer():
  140.     protocol = None
  141.     team = None
  142.     player_id = None
  143.    
  144.     def __init__(self, protocol, team):
  145.         self.protocol = protocol
  146.         self.team = team
  147.         self.acquire_player_id()
  148.    
  149.     def acquire_player_id(self):
  150.         max_players = min(32, self.protocol.max_players)
  151.         if len(self.protocol.connections) >= max_players:
  152.             try:
  153.                 self.player_id = next(self.team.get_players()).player_id
  154.             except StopIteration:
  155.                 self.player_id = None
  156.             return self.player_id is not None
  157.         self.player_id = self.protocol.player_ids.pop()
  158.         self.protocol.player_ids.put_back(self.player_id)
  159.         create_player.x = 0
  160.         create_player.y = 0
  161.         create_player.z = 63
  162.         create_player.weapon = RIFLE_WEAPON
  163.         create_player.player_id = self.player_id
  164.         create_player.name = self.team.name
  165.         create_player.team = self.team.id
  166.         self.protocol.send_contained(create_player, save = True)
  167.         return True
  168.    
  169.     def score(self):
  170.         if self.protocol.game_mode != CTF_MODE:
  171.             return
  172.         if self.player_id in self.protocol.players:
  173.             self.acquire_player_id()
  174.         if self.player_id is None and not self.acquire_player_id():
  175.             return
  176.         winning = (self.protocol.max_score not in (0, None) and
  177.             self.team.score + 1 >= self.protocol.max_score)
  178.         self.team.score += 1
  179.         intel_capture.player_id = self.player_id
  180.         intel_capture.winning = winning
  181.         self.protocol.send_contained(intel_capture, save = True)
  182.         if winning:
  183.             self.team.initialize()
  184.             self.team.other.initialize()
  185.             for entity in self.protocol.entities:
  186.                 entity.update()
  187.             for player in self.protocol.players.values():
  188.                 player.hp = None
  189.                 if player.team is not None:
  190.                     player.spawn()
  191.             self.protocol.on_game_end()
  192.         else:
  193.             flag = self.team.other.set_flag()
  194.             flag.update()
  195.    
  196.     def __del__(self):
  197.         if self.player_id is None or self.player_id in self.protocol.players:
  198.             return
  199.         player_left.player_id = self.player_id
  200.         self.protocol.send_contained(player_left, save = True)
  201.  
  202. @alias('n')        
  203. def north(connection):
  204.     lsd = connection.protocol.last_stand_dict[connection]
  205.     if 'north' in connection.protocol.spawns.keys():
  206.         spawn = connection.protocol.spawns['north']
  207.     else:
  208.         spawn = None
  209.     if lsd['side'] == 'att':
  210.         if connection.protocol.last_stand_start == False:
  211.             connection.send_chat('The round has not started, you cannot spawn yet.')
  212.         else:
  213.             if spawn != None:
  214.                 if lsd['spawned']:
  215.                     connection.send_chat('You have already spawned and must wait until you respawn to be able to again.')
  216.                 else:
  217.                     connection.set_location_safe(spawn['location'])
  218.                     connection.send_chat('You have spawned NORTH.')
  219.                     lsd['spawned'] = True
  220.             else:
  221.                 connection.send_chat('No north spawn exists. Try another spawn.')
  222.     else:
  223.         connection.send_chat('You are on the defending team and cannot select a spawn.')
  224.        
  225. @alias('e')        
  226. def east(connection):
  227.     lsd = connection.protocol.last_stand_dict[connection]
  228.     if 'east' in connection.protocol.spawns.keys():
  229.         spawn = connection.protocol.spawns['east']
  230.     else:
  231.         spawn = None
  232.     if lsd['side'] == 'att':
  233.         if connection.protocol.last_stand_start == False:
  234.             connection.send_chat('The round has not started, you cannot spawn yet.')
  235.         else:
  236.             if not spawn == None:
  237.                 if lsd['spawned']:
  238.                     connection.send_chat('You have already spawned and must wait until you respawn to be able to again.')
  239.                 else:
  240.                     connection.set_location_safe(spawn['location'])
  241.                     connection.send_chat('You have spawned EAST.')
  242.                     lsd['spawned'] = True
  243.             else:
  244.                 connection.send_chat('No east spawn exists. Try another spawn.')
  245.     else:
  246.         connection.send_chat('You are on the defending team and cannot select a spawn.')
  247.  
  248. @alias('s')        
  249. def south(connection):
  250.     lsd = connection.protocol.last_stand_dict[connection]
  251.     if 'south' in connection.protocol.spawns.keys():
  252.         spawn = connection.protocol.spawns['south']
  253.     else:
  254.         spawn = None
  255.     if lsd['side'] == 'att':
  256.         if connection.protocol.last_stand_start == False:
  257.             connection.send_chat('The round has not started, you cannot spawn yet.')
  258.         else:
  259.             if not spawn == None:
  260.                 if lsd['spawned']:
  261.                     connection.send_chat('You have already spawned and must wait until you respawn to be able to again.')
  262.                 else:
  263.                     connection.set_location_safe(spawn['location'])
  264.                     connection.send_chat('You have spawned SOUTH.')
  265.                     lsd['spawned'] = True
  266.             else:
  267.                 connection.send_chat('No south spawn exists. Try another spawn.')
  268.     else:
  269.         connection.send_chat('You are on the defending team and cannot select a spawn.')
  270.  
  271. @alias('w')        
  272. def west(connection):
  273.     lsd = connection.protocol.last_stand_dict[connection]
  274.     if 'west' in connection.protocol.spawns.keys():
  275.         spawn = connection.protocol.spawns['west']
  276.     else:
  277.         spawn = None
  278.     if lsd['side'] == 'att':
  279.         if connection.protocol.last_stand_start == False:
  280.             connection.send_chat('The round has not started, you cannot spawn yet.')
  281.         else:
  282.             if not spawn == None:
  283.                 if lsd['spawned']:
  284.                     connection.send_chat('You have already spawned and must wait until you respawn to be able to again.')
  285.                 else:
  286.                     connection.set_location_safe(spawn['location'])
  287.                     connection.send_chat('You have spawned WEST.')
  288.                     lsd['spawned'] = True
  289.             else:
  290.                 connection.send_chat('No west spawn exists. Try another spawn.')
  291.     else:
  292.         connection.send_chat('You are on the defending team and cannot select a spawn.')
  293.  
  294. add(north)
  295. add(east)
  296. add(south)
  297. add(west)
  298.  
  299. def apply_script(protocol, connection, config):
  300.  
  301.     class laststandConnection(connection):
  302.  
  303.         def attacker_force_spawn(self):
  304.             if self.name is None or self not in self.protocol.players:
  305.                 return
  306.             lsd = self.protocol.last_stand_dict[self]
  307.             spawn = self.protocol.spawns
  308.             if lsd['side'] == 'att':
  309.                 if not lsd['spawned'] and self.protocol.last_stand_start:
  310.                     spawnlist = self.protocol.spawnlist
  311.                     force_spawn_pos = random.choice(spawnlist)
  312.                     if force_spawn_pos == '/n':
  313.                         self.set_location_safe(self.protocol.spawns['north']['location'])
  314.                         location = 'NORTH'
  315.                     elif force_spawn_pos == '/e':
  316.                         self.set_location_safe(self.protocol.spawns['east']['location'])
  317.                         location = 'EAST'
  318.                     elif force_spawn_pos == '/s':
  319.                         self.set_location_safe(self.protocol.spawns['south']['location'])
  320.                         location = 'SOUTH'
  321.                     elif force_spawn_pos == '/w':
  322.                         self.set_location_safe(self.protocol.spawns['west']['location'])
  323.                         location = 'WEST'
  324.                     self.send_chat("The server has spawned you %s. Try typing '/n', '/e', '/s' or '/w' to spawn yourself next time."% location)
  325.                     lsd['spawned'] = True
  326.  
  327.         def on_spawn(self, pos):
  328.             lsd = self.protocol.last_stand_dict[self]
  329.             if self.team == self.protocol.attackteam:
  330.                 if not lsd['late'] and self != None:
  331.                     if self.protocol.last_stand_start:
  332.                         availspawn = ', '.join(self.protocol.spawnlist)
  333.                         self.send_chat('Type one of the following to spawn: %s' % availspawn)
  334.                     else:
  335.                         self.send_chat('You are on the attacking team')
  336.                 lsd['side'] = 'att'
  337.             else:
  338.                 if not self.protocol.last_stand_start:
  339.                     self.send_chat('You are on the defending team')
  340.                 lsd['side'] = 'def'
  341.             if lsd['side'] == 'def':
  342.                 lsd['spawned'] = True
  343.                 if self.protocol.last_stand_start and self != None:
  344.                     self.kill()
  345.                     self.send_chat('You will respawn when the round is over.')
  346.                     self.send_chat('You can\'t respawn since defenders only have 1 life.')
  347.             elif lsd['side'] == 'att':
  348.                 if lsd['late'] and self.protocol.last_stand_start and self != None:
  349.                     self.kill()
  350.                     self.send_chat('You will be able to join the next round.')
  351.                     self.send_chat('You have been prevented from spawning because the round has started.')
  352.                 else:
  353.                     lsd['spawned'] = False
  354.                     if self.protocol.last_stand_start:
  355.                         lsd['timer'] = callLater(10.0, self.attacker_force_spawn)
  356.             return connection.on_spawn(self, pos)
  357.  
  358.         def on_kill(self, killer, type, grenade):
  359.             lsd = self.protocol.last_stand_dict[self]
  360.             defendalive = 0
  361.             if lsd['side'] == 'def' and not lsd['late']:
  362.                 if self.protocol.last_stand_start:
  363.                     for player in self.protocol.defendteam.get_players():
  364.                         if hasattr(player.world_object, 'dead'):
  365.                             if player.world_object and not player.world_object.dead:
  366.                                 defendalive += 1
  367.                     defendalive -= 1
  368.                     if defendalive > 1:
  369.                         self.protocol.send_chat('%i players remaining on the defending team' % defendalive)
  370.                     elif defendalive == 1:
  371.                         self.protocol.send_chat('1 player remaining on the defending team')
  372.                 else:
  373.                     self.respawn_time = 0.5
  374.             elif lsd['side'] == 'att':
  375.                 if not self.protocol.last_stand_start:
  376.                     self.respawn_time = 0.5
  377.                 else:
  378.                     self.respawn_time = self.protocol.respawn_time
  379.             if lsd['timer'] != None:
  380.                 if lsd['timer'].active():
  381.                     lsd['timer'].cancel()
  382.                 lsd['timer'] = None
  383.             if lsd['capture']:
  384.                 lsd['capture'] = False
  385.             return connection.on_kill(self, killer, type, grenade)
  386.  
  387.         def respawn(self):
  388.             lsd = self.protocol.last_stand_dict[self]
  389.             self.send_markers()
  390.             if lsd['side'] == 'def':
  391.                 if self.protocol.last_stand_start:
  392.                     return False
  393.             elif lsd['side'] == 'att':
  394.                 if self.protocol.last_stand_start and lsd['late']:
  395.                     return False
  396.                 else:
  397.                     lsd['late'] = False
  398.             return connection.respawn(self)
  399.  
  400.         def on_position_update(self):
  401.             playerpos = self.world_object.position
  402.             if self.team == self.protocol.attackteam:
  403.                 if not self.protocol.last_stand_start:
  404.                     if playerpos.x > (self.protocol.attspawn[0] + 10) or playerpos.x < (self.protocol.attspawn[0] - 10) or playerpos.y > (self.protocol.attspawn[1] + 10) or playerpos.y < (self.protocol.attspawn[1] - 10):
  405.                         self.set_location_safe(self.protocol.attspawn)
  406.                 elif self.protocol.last_stand_start:
  407.                     lsd = self.protocol.last_stand_dict[self]
  408.                     if self.team != self.team.spectator:
  409.                         if hasattr(self.world_object, 'dead'):
  410.                             if self.world_object and not self.world_object.dead:
  411.                                 if (self.protocol.flag_pos[0] - FLAG_CAP_ZONE) <= playerpos.x <= (self.protocol.flag_pos[0] + FLAG_CAP_ZONE) and (self.protocol.flag_pos[1] - FLAG_CAP_ZONE) <= playerpos.y <= (self.protocol.flag_pos[1] + FLAG_CAP_ZONE):
  412.                                     lsd['capture'] = True
  413.                                 else:
  414.                                     lsd['capture'] = False
  415.                 else:
  416.                     lsd['capture'] = False
  417.             else:
  418.                 if not self.protocol.last_stand_start:
  419.                     if playerpos.x > (self.protocol.defspawn[0] + 10) or playerpos.x < (self.protocol.defspawn[0] - 10) or playerpos.y > (self.protocol.defspawn[1] + 10) or playerpos.y < (self.protocol.defspawn[1] - 10):
  420.                         self.set_location_safe(self.protocol.defspawn)
  421.             return connection.on_position_update(self)
  422.        
  423.         def on_join(self):
  424.             self.protocol.last_stand_dict[self] = {
  425.             'side': None,
  426.             'spawned': None,
  427.             'late': None,
  428.             'timer': None,
  429.             'capture': None,
  430.             'switch': False,
  431.             'forbid':False
  432.             }
  433.             lsd = self.protocol.last_stand_dict[self]
  434.             if self.protocol.last_stand_start:
  435.                 lsd['late'] = True
  436.             else:
  437.                 lsd['late'] = False
  438.             return connection.on_join(self)
  439.  
  440.         def send_markers(self):
  441.             is_self = lambda player: player is self
  442.             send_me = partial(self.protocol.send_contained, rule = is_self)
  443.             for marker in self.protocol.markers:
  444.                 marker.build(send_me)
  445.                
  446.         def destroy_markers(self):
  447.             is_self = lambda player: player is self
  448.             send_me = partial(self.protocol.send_contained, rule = is_self)
  449.             for marker in self.protocol.markers:
  450.                 marker.destroy(send_me)
  451.        
  452.         def on_login(self, name):
  453.             self.send_markers()
  454.             connection.on_login(self, name)
  455.        
  456.         def on_team_changed(self, old_team):
  457.             if old_team and not old_team.spectator:
  458.                 new_team, self.team = self.team, old_team
  459.                 self.destroy_markers()
  460.                 self.team = new_team
  461.             if self.team and not self.team.spectator:
  462.                 self.send_markers()
  463.             connection.on_team_changed(self, old_team)
  464.        
  465.         def on_team_join(self, team):
  466.             lsd = self.protocol.last_stand_dict[self]
  467.             max_players = min(32, self.protocol.max_players)
  468.             if len(self.protocol.connections) < max_players:
  469.                 if self.protocol.defendteam.count() <= self.protocol.attackteam.count():
  470.                     if team == self.protocol.attackteam:    
  471.                         self.send_chat("Team is locked. Too few people on the defending side.")
  472.                         return self.protocol.defendteam
  473.             if lsd['forbid']:
  474.                 if team == self.protocol.attackteam:
  475.                     self.send_chat('You have been teamswitched to the defending team - you can\'t switch back yet!')
  476.                     return False
  477.             return connection.on_team_join(self, team)
  478.  
  479.         def on_spawn_location(self, pos):
  480.             lsd = self.protocol.last_stand_dict[self]
  481.             if not lsd['late']:
  482.                 if self.team == self.protocol.attackteam:
  483.                     self.send_markers()
  484.                     return self.protocol.attspawn
  485.                 else:
  486.                     self.send_markers()
  487.                     return self.protocol.defspawn
  488.             return connection.on_spawn_location(self, pos)
  489.        
  490.         def on_disconnect(self):
  491.             if self in self.protocol.last_stand_dict:
  492.                 del self.protocol.last_stand_dict[self]
  493.             return connection.on_disconnect(self)
  494.  
  495.     class laststandProtocol(protocol):
  496.         game_mode = CTF_MODE
  497.         force_spawn = None
  498.         attackside = None
  499.         attackteam = None
  500.         oldattackside = None
  501.         defendteam = None
  502.         spawns = {}
  503.         last_stand_dict = {}
  504.         last_stand_start = False
  505.         ls_countdownlist= []
  506.         for ls_countdown in ls_countdownlist:
  507.             ls_countdown = None
  508.         ls_round_timer = None
  509.         ls_round_count = 0
  510.         timemessage = []
  511.         bluedefscore = 0
  512.         greendefscore = 0
  513.         roundtimelimit = None
  514.         timing = None
  515.         timevals = []
  516.         roundgo = None
  517.         spawnlist = []
  518.         firstmap = True
  519.         countdowncount = 0
  520.         newroundwait = None
  521.         defendersalive = None
  522.         flag_pos = None
  523.         capturing = False
  524.         captured = 0
  525.         capturing_tell = True
  526.         uncapturing_tell = False
  527.         markers = None
  528.  
  529.         def on_map_change(self, map):
  530.  
  531.             if self.firstmap == False:
  532.                 if self.last_stand_start:
  533.                     if self.timing.running and not self.timing == None:
  534.                         self.timing.stop()
  535.                 for i in xrange(0, 12, 1):
  536.                     ls_countdown = self.ls_countdownlist[i]
  537.                     if ls_countdown.cancelled == 0 and ls_countdown.called == 0:
  538.                         ls_countdown.cancel()
  539.                         ls_countdown = None
  540.                 if not self.newroundwait == None:
  541.                     if self.newroundwait.active():
  542.                         self.newroundwait.cancel()  
  543.             self.attackside = None    
  544.             self.attackteam = None
  545.             self.oldattackside = None
  546.             self.defendteam = None
  547.             self.last_stand_start = False
  548.             self.ls_round_timer = None
  549.             self.ls_round_count = 0
  550.             self.timemessage = None
  551.             self.timevals = None
  552.             self.bluedefscore = 0
  553.             self.greendefscore = 0
  554.             self.roundtimelimit = None
  555.             self.timing = None
  556.             self.roundgo = None
  557.             self.spawns = {}
  558.             self.spawnlist = []
  559.             self.countdowncount = 0
  560.             self.attackside = random.choice([1, 2])
  561.             self.newroundwait = None
  562.             self.defendersalive = None
  563.             self.flag_pos = None
  564.             self.capturing = False
  565.             self.captured = 0
  566.             self.capturing_tell = True
  567.             self.uncapturing_tell = False
  568.             self.markers = []
  569.             for team in (self.blue_team, self.green_team):
  570.                 team.flag_marker = None
  571.                 team.marker_calls = []
  572.                 team.marker_count = defaultdict(int)
  573.  
  574.             if self.attackside == 1:
  575.                 self.attackteam = self.blue_team
  576.                 self.defendteam = self.green_team
  577.             elif self.attackside == 2:
  578.                 self.attackteam = self.green_team
  579.                 self.defendteam = self.blue_team
  580.  
  581.             extensions = self.map_info.extensions
  582.             if any([attackspawn in extensions for attackspawn in ['Attspawn', 'attspawn']]):
  583.                 self.attspawn = extensions.get(attackspawn)
  584.             else:
  585.                 raise CustomException("Data needed for \'attspawn\' in map extensions'")
  586.  
  587.             if any([defendspawn in extensions for defendspawn in ['Defspawn', 'defspawn']]):
  588.                 self.defspawn = extensions.get(defendspawn)
  589.             else:
  590.                 raise CustomException("Data needed for \'defspawn\' in map extensions'")
  591.  
  592.             if 'time_limit' in extensions:
  593.                 self.roundtimelimit = extensions['time_limit']
  594.             else:
  595.                 self.roundtimelimit = 300
  596.  
  597.             if any([spawnnorth in extensions for spawnnorth in ['North', 'north']]):
  598.                 self.spawns['north'] = {'location': extensions[spawnnorth]}
  599.                 self.spawnlist.append('/n')
  600.             if any([spawneast in extensions for spawneast in ['East', 'east']]):
  601.                 self.spawns['east'] = {'location': extensions[spawneast]}
  602.                 self.spawnlist.append('/e')
  603.             if any([spawnsouth in extensions for spawnsouth in ['South', 'south']]):
  604.                 self.spawns['south'] = {'location': extensions[spawnsouth]}
  605.                 self.spawnlist.append('/s')
  606.             if any([spawnwest in extensions for spawnwest in ['West', 'west']]):
  607.                 self.spawns['west'] = {'location': extensions[spawnwest]}
  608.                 self.spawnlist.append('/w')
  609.             if len(self.spawns)== 0:
  610.                 raise CustomException("At least one north, south, east or west spawn is required")
  611.             if 'flag' in extensions:
  612.                 self.flag_pos = extensions['flag']
  613.                 flagattmark = FlagAtt(self, self.attackteam, self.flag_pos[0], self.flag_pos[1])
  614.                 self.attackteam.flag_marker = flagattmark
  615.                 flagdefmark = FlagDef(self, self.defendteam, self.flag_pos[0], self.flag_pos[1])
  616.                 self.defendteam.flag_marker = flagdefmark
  617.             else:
  618.                 raise CustomException("Map extensions missing data for 'flag'.")
  619.             self.building = False
  620.             self.killing = False
  621.             self.last_stand_new_round()
  622.             return protocol.on_map_change(self, map)
  623.  
  624.         def on_flag_spawn(self, x, y, z, flag, entity_id):
  625.             return (0, 0, 63)
  626.  
  627.         def on_base_spawn(self, x, y, z, base, entity_id):
  628.             return (0, 0, 63)
  629.  
  630.         def last_stand_new_round(self):
  631.             self.firstmap = False
  632.             self.last_stand_start = False
  633.             self.captured = 0
  634.            
  635.             if self.oldattackside == 1:
  636.                  self.attackside = 2
  637.             elif self.oldattackside == 2:
  638.                  self.attackside = 1
  639.             if self.attackside == 1:
  640.                 self.attackteam = self.blue_team
  641.                 self.defendteam = self.green_team
  642.             elif self.attackside == 2:
  643.                 self.attackteam = self.green_team
  644.                 self.defendteam = self.blue_team
  645.             flagattmark = FlagAtt(self, self.attackteam, self.flag_pos[0], self.flag_pos[1])
  646.             self.attackteam.flag_marker = flagattmark
  647.             flagdefmark = FlagDef(self, self.defendteam, self.flag_pos[0], self.flag_pos[1])
  648.             self.defendteam.flag_marker = flagdefmark
  649.             for player in self.players.values():
  650.                 lsd = self.last_stand_dict[player]
  651.                 if lsd['late']:
  652.                     lsd['late'] = False
  653.                 lsd['capture'] = False
  654.                 #if player.team.spectator and not (player.admin or player.user_types.moderator or player.user_types.guard):
  655.                     #if self.defendteam.count() >= self.attackteam.count():
  656.                         #player.set_team(self.attackteam)
  657.                     #else:
  658.                         #player.set_team(self.defendteam)
  659.                     #player.send_chat('You have been forced to join a team by the server')
  660.                 if player.team == self.attackteam:
  661.                     player.spawn(self.attspawn)
  662.                 elif player.team == self.defendteam:
  663.                     player.spawn(self.defspawn)
  664.             self.last_stand_countdown_times()
  665.         def last_stand_countdown_times(self):
  666.             if self.countdowncount == 0:
  667.                 self.timemessage = ['60', '50', '40', '30', '20', '10', '5', '4', '3', '2', '1', 'Go!']
  668.                 self.timevals = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0]
  669.                 for i, time in zip(xrange(0, 12, 1), self.timevals):
  670.                     self.ls_countdownlist.insert(i, reactor.callLater(time, self.last_stand_countdown))
  671.             else:
  672.                 self.timemessage = ['20', '10', '5', '4', '3', '2', '1', 'Go!']
  673.                 self.timevals = [10.0, 20.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0]
  674.                 for i, time in zip(xrange(0, 8, 1), self.timevals):
  675.                     self.ls_countdownlist.insert(i, reactor.callLater(time, self.last_stand_countdown))
  676.             self.countdowncount += 1
  677.  
  678.         def last_stand_countdown(self):
  679.             while self.defendteam.count() < (self.attackteam.count()) and not self.attackteam.count() <= 2:
  680.                 balancelist = []
  681.                 avail = 0
  682.                 for player in self.attackteam.get_players():
  683.                     lsd = self.last_stand_dict[player]
  684.                     if not lsd['switch']:
  685.                         avail += 1
  686.                         playerbalance = balancelist.append(player)
  687.                 if avail == 0:
  688.                     for player in self.attackteam.get_players():
  689.                         lsd = self.last_stand_dict[player]
  690.                         if lsd['switch']:
  691.                             lsd['switch'] = False
  692.                 else:
  693.                     luckywinner = random.choice(balancelist)
  694.                     lsdlw = self.last_stand_dict[luckywinner]
  695.                     luckywinner.respawn_time = 0.5
  696.                     luckywinner.set_team(self.defendteam)
  697.                     luckywinner.send_chat('You have been autobalanced to the defending team')
  698.                     lsdlw['switch'] = True
  699.                     lsdlw['forbid'] = True
  700.             #for player in self.players.values():
  701.                 #if player.team.spectator and not (player.admin or player.user_types.moderator or player.user_types.guard):
  702.                     #if self.defendteam.count() >= self.attackteam.count() + 3:
  703.                         #player.set_team(self.attackteam)
  704.                     #else:
  705.                         #player.set_team(self.defendteam)
  706.                     #player.send_chat('You have been forced to join a team by the server')
  707.             if self.timemessage[0] == 'Go!':
  708.                 for team in (self.green_team, self.blue_team):
  709.                     if team.count() == 0:
  710.                         self.send_chat('Not enough players on the %s team to begin.' % team.name)
  711.                         self.last_stand_start = False
  712.                         self.last_stand_countdown_times()
  713.                         self.roundgo = False
  714.                         return
  715.                     else:
  716.                         self.roundgo = True
  717.                 if self.roundgo:
  718.                         self.send_chat(self.timemessage[0])
  719.                         self.ls_roundstart()
  720.             elif self.timemessage[0] in ['1', '2', '3', '4', '5']:
  721.                 self.send_chat(self.timemessage.pop(0))
  722.             else:
  723.                 self.send_chat('%s seconds before round starts' % self.timemessage.pop(0))
  724.  
  725.         def ls_roundstart(self):
  726.             print 'Last Stand round started'
  727.             self.oldattackside = self.attackside
  728.             self.last_stand_start = True  
  729.             self.killing = True
  730.             for player in self.players.values():
  731.                 player.refill()
  732.             self.timing = LoopingCall(self.defenderscore)
  733.             self.timing.start(1.0)
  734.             availspawn = ', '.join(self.spawnlist)
  735.             for player in self.attackteam.get_players():
  736.                 lsd = self.last_stand_dict[player]
  737.                 lsd['timer'] = callLater(10.0, player.attacker_force_spawn)
  738.                 player.send_chat('Type one of the following to spawn: %s' % availspawn)
  739.  
  740.         def defenderscore(self):
  741.             defendercount = 0
  742.             attackercap = 0
  743.             attackerscore = 0
  744.             if self.last_stand_start:
  745.                 if self.capturing:
  746.                     self.captured += 2.5
  747.                     if self.capturing_tell:
  748.                         self.send_chat('Attackers are capturing the flag!')
  749.                         self.capturing_tell = False
  750.                         self.uncapturing_tell = True
  751.                     for cap_x in [25, 50, 75]:
  752.                         if self.captured == cap_x:
  753.                             self.send_chat('Flag is %i%% captured!' % cap_x)
  754.                 elif not self.capturing:
  755.                     if self.captured > 0:
  756.                         self.captured -= 5
  757.                         if self.uncapturing_tell:
  758.                             self.send_chat('Attackers are no longer capturing the flag.')
  759.                             self.uncapturing_tell = False
  760.                             self.capturing_tell = True
  761.                 for player in self.defendteam.get_players():
  762.                     if player != None:
  763.                         if hasattr(player.world_object, 'dead'):
  764.                             if player.world_object and not player.world_object.dead:
  765.                                 defendercount += 1
  766.                 for attacker in self.attackteam.get_players():
  767.                     lsd = self.last_stand_dict[attacker]
  768.                     if lsd['capture']:
  769.                         self.capturing = True
  770.                         attackercap += 1
  771.                         if self.captured > 0:
  772.                             if self.captured % 10 == 0:
  773.                                 attacker.send_chat('The flag is %i%% captured' % self.captured)
  774.                     elif attackercap == 0:
  775.                         self.capturing = False
  776.                 if self.attackside == 1:
  777.                     self.greendefscore += 1
  778.                     attackerscore = self.greendefscore
  779.                 else:
  780.                     self.bluedefscore += 1
  781.                     attackerscore = self.bluedefscore
  782.                    
  783.                 if attackerscore == self.roundtimelimit:
  784.                     print 'Round ended - Time limit reached'
  785.                     self.send_chat ('Time limit reached!')
  786.                     self.uncapturing_tell = False
  787.                     self.capturing_tell = True
  788.                     for attacker in self.attackteam.get_players():
  789.                         lsd = self.last_stand_dict[attacker]
  790.                         lsd['capture'] = False
  791.                     self.capturing = False
  792.                     self.timing.stop()
  793.                     self.ls_round_end()
  794.                 elif defendercount == 0:
  795.                     print 'Round ended - All defenders dead'
  796.                     self.send_chat('All defenders have been killed!')
  797.                     self.uncapturing_tell = False
  798.                     self.capturing_tell = True
  799.                     for attacker in self.attackteam.get_players():
  800.                         lsd = self.last_stand_dict[attacker]
  801.                         lsd['capture'] = False
  802.                     self.capturing = False
  803.                     self.timing.stop()
  804.                     self.ls_round_end()
  805.                 elif self.captured == 100:
  806.                     print 'Round ended - Flag captured'
  807.                     self.send_chat('Attackers have captured the flag!')
  808.                     for attacker in self.attackteam.get_players():
  809.                         lsd = self.last_stand_dict[attacker]
  810.                         lsd['capture'] = False
  811.                     self.capturing = False
  812.                     self.uncapturing_tell = False
  813.                     self.capturing_tell = True
  814.                     self.timing.stop()
  815.                     self.ls_round_end()
  816.  
  817.         def ls_round_end(self):
  818.             self.roundgo = False
  819.             self.newroundwait = callLater(5, self.last_stand_new_round)
  820.             self.ls_round_count += 1
  821.             for marker in self.markers:
  822.                 marker.expire()
  823.             self.markers = []
  824.             for team in [self.blue_team, self.green_team]:
  825.                 team.flag_marker = None
  826.             for player in self.players.values():
  827.                 player.destroy_markers()
  828.                 lsd = self.last_stand_dict[player]
  829.                 if lsd['forbid']:
  830.                     lsd['forbid'] = False
  831.             if self.ls_round_count % 2 == 1:
  832.                 if self.defendteam == self.green_team:
  833.                     self.send_chat('Green team\'s score for that round: %i' % self.greendefscore)
  834.                 elif self.defendteam == self.blue_team:
  835.                     self.send_chat('Blue team\'s score for that round: %i' % self.bluedefscore)
  836.             elif self.ls_round_count % 2 == 0:
  837.                 if self.greendefscore > self.bluedefscore:
  838.                     self.send_chat('Green team wins the round with a score of %i to Blue\'s score of %i.' % (self.greendefscore, self.bluedefscore))
  839.                     dummy = DummyPlayer(self, self.green_team)
  840.                     dummy.score()
  841.                     self.greendefscore = 0
  842.                     self.bluedefscore = 0
  843.                 elif self.greendefscore < self.bluedefscore:
  844.                     self.send_chat('Blue team wins the round with a score of %i to Green\'s score of %i.' % (self.bluedefscore, self.greendefscore))
  845.                     dummy = DummyPlayer(self, self.blue_team)
  846.                     dummy.score()
  847.                     self.greendefscore = 0
  848.                     self.bluedefscore = 0
  849.                 elif self.greendefscore == self.bluedefscore:
  850.                     self.send_chat('Both teams scored %i and the round is tied!' % self.greendefscore)
  851.                     self.greendefscore = 0
  852.                     self.bluedefscore = 0
  853.  
  854.     return laststandProtocol, laststandConnection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement