/usr/share/games/castle-combat/scripts/buildplayer.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 | import pygame
from pygame.locals import *
from numpy.oldnumeric import *
import common
import random
from copy import copy
from game import game
from field import Field, Grunt
from player import BasePlayer
class BuildPlayer(BasePlayer):
select_pic = common.load_image('select.png', alpha=True)
wait_pic = common.load_image('waitblock.png', alpha=True)
cantbuild_pic = common.load_image('cantbuild.png', alpha=True)
def __init__(self, player):
BasePlayer.__init__(self)
self.player = player
self.wait_for_block = 0
self.block = None
def init(self):
BasePlayer.init(self)
self.pos = self.player.get_center()
self.wait_pic
self.wait_pic = common.colorize(BuildPlayer.wait_pic, self.player.color)
self.select_pic = common.colorize(BuildPlayer.select_pic, self.player.color)
def handle_event(self, event):
self.handle_build_or_place_event(event, (self.put_block, self.rotate_block), self.pos )
def handle_movement(self, passed_milliseconds):
if game.server:
self.wait_for_block -= passed_milliseconds
if self.block == None and self.wait_for_block <= 0:
self.generate_block(random.randint(0, 10), random.randint(0, 3))
def is_allowed(self, pos):
if not 'build_only_near_walls' in game.field.map.game_options:
return True
else:
topleft = common.in_bounds(subtract(pos, 3))
bottomright = common.in_bounds(add(pos, 4))
surroundings = game.field[topleft[0]:bottomright[0], topleft[1]:bottomright[1]]
return sometrue(surroundings == self.player.player_id)
def draw_cursor(self):
if self.block:
for x in range(3):
for y in range(3):
if self.block[x][y]:
xpos = x + self.pos[0] - 1
ypos = y + self.pos[1] - 1
screen_pos = (xpos * common.block_size, ypos * common.block_size)
common.blit(self.select_pic, screen_pos)
if not self.is_allowed((xpos, ypos)):
common.blit(self.cantbuild_pic, screen_pos)
else:
common.blit(self.wait_pic, (self.pos[0] * common.block_size, self.pos[1] * common.block_size) )
def put_block(self):
# Does the player have a block?
if not self.block:
raise common.NoBlockAvailable
field = game.field
# Can the block be places here?
for x in range(3):
for y in range(3):
if self.block[x][y]:
xpos = x + self.pos[0] - 1
ypos = y + self.pos[1] - 1
if 'field_owners' in game.field.map.game_options and field.owner[xpos][ypos] != self.player.player_id:
raise common.ActionNotPossible
if not self.is_allowed((xpos,ypos)):
raise common.ActionNotPossible
if field[xpos][ypos] not in (Field.EMPTY, Field.HOUSE):
raise common.FieldNotEmpty
# Place the block
for x in range(3):
for y in range(3):
if self.block[x][y]:
xpos = x + self.pos[0] - 1
ypos = y + self.pos[1] - 1
if field[xpos][ypos] == Field.HOUSE:
field.blit_background((xpos,ypos))
Grunt((xpos, ypos))
else:
common.backbuffer_blit(self.player.wall_pic, (xpos * common.block_size, ypos * common.block_size) )
field[xpos][ypos] = self.player.player_id
# The player will have to wait a short time until he gets the next block
self.block = None
self.wait_for_block = 500
field.look_for_secured_areas(self.player)
def move(self, vector):
old_pos = self.pos
self.pos = tuple(add(self.pos, vector))
if self.pos != self.bounded_pos():
self.pos = old_pos
raise common.ActionNotPossible
def bounded_pos(self):
# allow moving closer to the edges if outer rows or coloumns are empty
margin = ([1,1], [1,1])
if self.block:
for side in (0,1):
for i in range(3):
if self.block[-side][i]:
margin[0][side] = 0
if self.block[i][-side]:
margin[1][side] = 0
return tuple(common.bound(self.pos[dim], 1-margin[dim][0], common.field_size[dim] - 2 + margin[dim][1]) for dim in (0,1))
def rotate_block(self):
if not self.block:
raise common.ActionNotPossible
self.block = ( (self.block[0][2], self.block[1][2], self.block[2][2]),
(self.block[0][1], self.block[1][1], self.block[2][1]),
(self.block[0][0], self.block[1][0], self.block[2][0]) )
self.pos = self.bounded_pos()
def generate_block(self, piece, turn):
if piece in (0,1):
self.block = ( (0,1,0),
(0,1,0),
(0,1,1), )
elif piece == 2:
self.block = ( (0,1,0),
(0,1,0),
(1,1,0), )
elif piece in (3,4,5):
self.block = ( (0,1,0),
(1,1,0),
(0,0,0), )
elif piece in (6,7):
self.block = ( (0,1,0),
(0,1,0),
(0,1,0), )
elif piece in (8,9):
self.block = ( (1,1,0),
(0,1,0),
(1,1,0), )
elif piece == 10:
self.block = ( (0,0,0),
(0,1,0),
(0,0,0), )
if game.server:
for x in range(turn):
self.rotate_block()
self.pos = self.bounded_pos()
from network import ServerObject, ClientObject, networkify
class BuildPlayerServer(ServerObject, BuildPlayer):
def get_state(self):
return {
'player': self.player,
}
class BuildPlayerClient(ClientObject, BuildPlayer):
def set_state(self, state):
#self.__dict__ = state
BuildPlayer.__init__(self, state['player'])
networkify(
cacheable = BuildPlayerServer,
remote_cache = BuildPlayerClient,
implementation = BuildPlayer,
method_names = ('move', 'put_block', 'rotate_block', 'generate_block')
)
|