Advertisement
Guest User

Untitled

a guest
Apr 12th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. Tug of War game mode, where you must progressively capture the enemy CPs in a
  3. straight line to win.
  4.  
  5. Maintainer: mat^2
  6. """
  7.  
  8. from pyspades.constants import *
  9. from pyspades.server import Territory
  10. import random
  11. import math
  12. from math import pi
  13.  
  14. HELP = [
  15.     "In Tug of War, you capture your opponents' front CP to advance."
  16. ]
  17.  
  18. class TugTerritory(Territory):
  19.     disabled = True
  20.    
  21.     def add_player(self, player):
  22.         if self.disabled:
  23.             return
  24.         Territory.add_player(self, player)
  25.    
  26.     def enable(self):
  27.         self.disabled = False
  28.    
  29.     def disable(self):
  30.         for player in self.players.copy():
  31.             self.remove_player(player)
  32.         self.disabled = True
  33.         self.progress = float(self.team.id)
  34.  
  35. def get_index(value):
  36.     if value < 0:
  37.         raise IndexError()
  38.     return value
  39.  
  40. def apply_script(protocol, connection, config):
  41.     class TugConnection(connection):
  42.         def get_spawn_location(self):
  43.             if self.team.spawn_cp is None:
  44.                 base = self.team.last_spawn
  45.             else:
  46.                 base = self.team.spawn_cp
  47.             return base.get_spawn_location()
  48.            
  49.         def on_spawn(self, pos):
  50.             for line in HELP:
  51.                 self.send_chat(line)
  52.             return connection.on_spawn(self, pos)
  53.            
  54.     class TugProtocol(protocol):
  55.         game_mode = TC_MODE
  56.        
  57.         def get_cp_entities(self):
  58.             # generate positions
  59.            
  60.             map = self.map
  61.             blue_cp = []
  62.             green_cp = []
  63.  
  64.             points = self.map_info.extensions
  65.            
  66.             for i, point in enumerate(points['tow_locations']):
  67.                 if i < len(points['tow_locations']) / 2:
  68.                     blue_cp.append(point)
  69.                 else:
  70.                     green_cp.append(point)
  71.            
  72.             # make entities
  73.            
  74.             index = 0
  75.             entities = []
  76.            
  77.             for i, (x, y) in enumerate(blue_cp):
  78.                 entity = TugTerritory(index, self, *(x, y, map.get_z(x, y)))
  79.                 entity.team = self.blue_team
  80.                 if i == 0:
  81.                     self.blue_team.last_spawn = entity
  82.                     entity.id = -1
  83.                 else:
  84.                     entities.append(entity)
  85.                     index += 1
  86.            
  87.             self.blue_team.cp = entities[-1]
  88.             self.blue_team.cp.disabled = False
  89.             self.blue_team.spawn_cp = entities[-2]
  90.                
  91.             for i, (x, y) in enumerate(green_cp):
  92.                 entity = TugTerritory(index, self, *(x, y, map.get_z(x, y)))
  93.                 entity.team = self.green_team
  94.                 if i == len(green_cp) - 1:
  95.                     self.green_team.last_spawn = entity
  96.                     entity.id = index
  97.                 else:
  98.                     entities.append(entity)
  99.                     index += 1
  100.  
  101.             self.green_team.cp = entities[-(len(points)-2)/2]
  102.             self.green_team.cp.disabled = False
  103.             self.green_team.spawn_cp = entities[-(len(points)-2)/2 + 1]
  104.            
  105.             return entities
  106.    
  107.         def on_cp_capture(self, territory):
  108.             team = territory.team
  109.             if team.id:
  110.                 move = -1
  111.             else:
  112.                 move = 1
  113.             for team in [self.blue_team, self.green_team]:
  114.                 try:
  115.                     team.cp = self.entities[get_index(team.cp.id + move)]
  116.                     team.cp.enable()
  117.                 except IndexError:
  118.                     pass
  119.                 try:
  120.                     team.spawn_cp = self.entities[get_index(
  121.                         team.spawn_cp.id + move)]
  122.                 except IndexError:
  123.                     team.spawn_cp = team.last_spawn
  124.             cp = (self.blue_team.cp, self.green_team.cp)
  125.             for entity in self.entities:
  126.                 if not entity.disabled and entity not in cp:
  127.                     entity.disable()
  128.  
  129.     return TugProtocol, TugConnection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement