/usr/share/games/castle-combat/scripts/gamephases.py is in castle-combat 0.8.1.dfsg.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import pygame
from pygame.locals import *
from numpy.oldnumeric import *
import common
from game import game
from state import State, IgnoreEvent
class GameState(State):
def handle_keypress(self, event):
if common.debug and event.type == KEYDOWN:
if event.key == K_BACKSPACE:
game.server_call('next_phase')
raise IgnoreEvent
if event.key == K_EQUALS:
try:
import cannon
reload(cannon)
print "reload successful"
except:
print "WARNING: Module reload failed!"
import traceback
traceback.print_exc()
def handle(self):
game.phase = game.phase.next_phase()
def quit(self):
# allow GameState to quit event if it's not the topmost state
while State.stack[-1] != self:
State.stack[-1].quit()
State.quit(self)
if game.server:
game.server.quit()
persistent_events = {
KEYDOWN: handle_keypress,
}
class Phase(State):
def __init__(self):
State.__init__(self)
for player in game.players:
player.init_phase(self.phase_name)
self.time_left = self.duration
self.clock = pygame.time.Clock()
self.clock.tick()
def handle(self):
if self.phase_end():
game.server_call('next_phase')
else:
passed_milliseconds = min(self.clock.tick(), 500)
# handle redraws and other actions that occur each frame
self.actions(passed_milliseconds)
for player in game.players:
player.handle_movement(passed_milliseconds)
player.draw_cursor()
self.time_left -= passed_milliseconds * 0.001
game.print_time()
common.update()
def handle_keypress(self, event):
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
if game.server:
game.server.call('pause')
else:
game.server_game.callRemote('pause')
raise IgnoreEvent
for player in game.players:
if player.local:
player.handle_event(event)
events = {
KEYDOWN: handle_keypress,
KEYUP: handle_keypress,
}
class AnnouncePhase(State):
"""Shows the 'Phase X begins' messages"""
box_pic = common.load_image('box.png', alpha=True)
phase_name = 'announce'
def __init__(self, next_phase):
State.__init__(self)
announce_text = getattr(game.field.map, next_phase.phase_name + "_phase_text")
self.make_announce_surface(*announce_text)
common.blit(
self.announce_surface,
divide(
subtract(common.screen.get_size(), self.announce_surface.get_size()),
2
)
)
common.update()
self.start_time = pygame.time.get_ticks()
self.next_phase = next_phase
def proceed_if_waited_long_enough(self, event):
if pygame.time.get_ticks() > self.start_time + 500:
game.server_call('next_phase')
raise IgnoreEvent
events = {
KEYDOWN: proceed_if_waited_long_enough,
}
def cleanup(self):
pass
def make_announce_surface(self, title, instructions, buttons):
self.announce_surface = self.box_pic.convert_alpha()
title_pic = common.font.render(title, True, (0,0,0))
instructions_pic = common.small_font.render(instructions, True, (0,0,0))
buttons_pic = common.small_font.render("Buttons: " + buttons, True, (0,0,0))
if game.server:
continue_string = "< Press any button to continue >"
else:
continue_string = "< Waiting for Server to continue >"
continue_pic = common.small_font.render(continue_string, True, (0,0,0))
self.announce_surface.blit(
title_pic,
((self.announce_surface.get_width() - title_pic.get_width()) / 2, 15)
)
self.announce_surface.blit(instructions_pic, (30, 65))
self.announce_surface.blit(buttons_pic, (30, 90))
self.announce_surface.blit(
continue_pic,
((self.announce_surface.get_width() - continue_pic.get_width()) / 2, 160)
)
class BuildPhase(Phase):
phase_name = "build"
duration = 18
def __init__(self):
def build_actions(passed_milliseconds):
pygame.time.delay(1)
pygame.time.set_timer(USEREVENT, 500)
self.actions = build_actions
self.next_phase = lambda: AnnouncePhase(PlacePhase)
Phase.__init__(self)
def phase_end(self):
return self.time_left <= 0
def cleanup(self):
def clean_walls():
from field import Field
for player in game.players:
player_id = player.player_id
field = game.field.array
# count how many walls are adjecent to each wall
adj_walls = zeros(common.field_size)
adj_walls[1:, :] += where(field[:-1 ,:] == player_id, 1, 0)
adj_walls[:-1, :] += where(field[1: ,:] == player_id, 1, 0)
adj_walls[:, 1:] += where(field[:, :-1] == player_id, 1, 0)
adj_walls[:, :-1] += where(field[:, 1:] == player_id, 1, 0)
# delete all lonely walls
mask = logical_and(where(adj_walls == 0, 1, 0), where(field == player_id, 1, 0))
putmask(field, mask, (Field.EMPTY, ))
# delete every fourth wall with only one adjacent wall
mask = logical_and(where(adj_walls == 1, 1, 0), where(field == player_id, 1, 0))
putmask(field, mask, (Field.EMPTY, ) + (player_id, )*3 )
game.field.look_for_secured_areas(player)
game.field.draw_backbuffer()
clean_walls()
game.field.add_houses(5)
pygame.time.set_timer(USEREVENT, 0)
# eleminate players
from field import Castle
for player in game.players:
if not [castle for castle in game.field.map.castles if player.secured[castle.pos]]:
player.die()
class PlacePhase(Phase):
phase_name = "place"
duration = 15
def __init__(self):
def place_actions(passed_milliseconds):
pygame.time.delay(1)
pygame.time.set_timer(USEREVENT, 500)
self.actions = place_actions
self.next_phase = lambda: AnnouncePhase(BattlePhase)
Phase.__init__(self)
def phase_end(self):
return self.time_left <= 0 or not filter(lambda player: player.place_player.new_cannons > 0, game.players)
def cleanup(self):
pygame.time.set_timer(USEREVENT, 0)
class BattlePhase(Phase):
phase_name = "battle"
duration = 10
def __init__(self):
def battle_actions(passed_milliseconds):
from cannon import Shot
for can in game.cannons:
can.blit()
for shot in game.shots:
shot.handle(passed_milliseconds)
shot.draw()
from field import Grunt
for grunt in Grunt.instances:
grunt.handle(passed_milliseconds)
def remove_garbage():
from field import Field
game.field.array += logical_and (
where(game.field.array >= Field.GARBAGE_NEW, 1, 0),
where(game.field.array <= Field.GARBAGE_OLD, 1, 0)
)
self.actions = battle_actions
self.next_phase = lambda: AnnouncePhase(BuildPhase)
remove_garbage()
self.announce_surface = common.font.render("Shoot at your enemy's walls!", True, (0,0,0))
Phase.__init__(self)
game.field.draw_backbuffer(draw_cannons=False)
def phase_end(self):
from cannon import Shot
return self.time_left <= 0 and not game.shots
def cleanup(self):
game.field.draw_backbuffer()
class SelectPhase(Phase):
phase_name = "select"
duration = 20
def __init__(self):
def select_actions(passed_milliseconds):
pygame.time.delay(1)
self.actions = select_actions
self.next_phase = lambda: AnnouncePhase(PlacePhase)
Phase.__init__(self)
def phase_end(self):
return self.time_left <= 0 or not filter(lambda player: not player.select_player.finished, game.players)
def cleanup(self):
for player in game.players:
if not player.select_player.finished:
player.select_player.remote_confirm_select()
|