Smaller results files

This commit is contained in:
Neale Pickett 2010-03-03 12:23:09 -07:00
parent 7b406ce67d
commit 50656d2da3
4 changed files with 136 additions and 106 deletions

View File

@ -1,6 +0,0 @@
VAR = "/opt/ctf/var"
WWW = "/opt/ctf/www"
LIB = "/opt/ctf/lib"
BIN = "/opt/ctf/bin"
SBIN = "/opt/ctf/sbin"
BASE_URL = "/"

View File

@ -30,17 +30,16 @@ class Pflanzarr:
tmpPlayers = os.listdir(self._playerDir) tmpPlayers = os.listdir(self._playerDir)
players = [] players = []
for p in tmpPlayers: AIs = {}
p = unquote(p) for fn in tmpPlayers:
p = unquote(fn)
print (p, fn)
if (not (p.startswith('.') if (not (p.startswith('.')
or p.endswith('#') or p.endswith('#')
or p.endswith('~')) or p.endswith('~'))
and teams.exists(p)): and teams.exists(p)):
players.append(p) players.append(p)
AIs[p] = open(os.path.join(self._playerDir, fn)).read()
AIs = {}
for player in players:
AIs[player] = open(os.path.join(self._playerDir, player)).read()
defaultAIs = self._getDefaultAIs(dir) defaultAIs = self._getDefaultAIs(dir)
if len(players) < 1: if len(players) < 1:
@ -48,16 +47,10 @@ class Pflanzarr:
# The one is added to ensure that there is at least one house # The one is added to ensure that there is at least one house
# bot. # bot.
cols = math.sqrt(len(players) + 1) cols = int(math.ceil(math.sqrt(len(players) + 1)))
if int(cols) != cols:
cols = cols + 1
cols = int(cols)
cols = max(cols, 2) cols = max(cols, 2)
rows = len(players)/cols rows = int(math.ceil(len(players)/float(cols)))
if len(players) % cols != 0:
rows = rows + 1
rows = max(rows, 2) rows = max(rows, 2)
self._board = (cols*self.SPACING, rows*self.SPACING) self._board = (cols*self.SPACING, rows*self.SPACING)
@ -96,8 +89,13 @@ class Pflanzarr:
hdr = StringIO() hdr = StringIO()
hdr.write('<script type="application/javascript" src="../tanks.js"></script>\n' hdr.write('<script type="application/javascript" src="../tanks.js"></script>\n'
'<script type="application/javascript">\n') '<script type="application/javascript">\n')
hdr.write('turns = [\n') hdr.write('turns = [%d, %d,[\n' % self._board)
# Describe tanks
for tank in self._tanks:
tank.describe(hdr)
hdr.write('],\n')
hdr.write('[\n')
turn = 0 turn = 0
lastTurns = 3 lastTurns = 3
while ((maxTurns is None) or turn < maxTurns) and lastTurns > 0: while ((maxTurns is None) or turn < maxTurns) and lastTurns > 0:
@ -105,8 +103,6 @@ class Pflanzarr:
lastTurns = lastTurns - 1 lastTurns = lastTurns - 1
near = self._getNear() near = self._getNear()
deadThisTurn = set()
liveTanks = set(self._tanks).difference(self._deadTanks) liveTanks = set(self._tanks).difference(self._deadTanks)
for tank in liveTanks: for tank in liveTanks:
@ -122,16 +118,8 @@ class Pflanzarr:
self._killTanks(dead, 'Collision') self._killTanks(dead, 'Collision')
hdr.write(' [\n') hdr.write(' [\n')
for tank in self._tanks:
# Draw the explosions
for tank in self._deadTanks:
tank.draw(hdr) tank.draw(hdr)
# Draw the live tanks.
for tank in self._tanksByX:
# Have the tank run its program, move, etc.
tank.draw(hdr)
hdr.write(' ],\n') hdr.write(' ],\n')
# Have the live tanks do their turns # Have the live tanks do their turns
@ -148,7 +136,7 @@ class Pflanzarr:
for tank in self._tanks: for tank in self._tanks:
self._outputErrors(tank) self._outputErrors(tank)
hdr.write('];\n') hdr.write(']];\n')
hdr.write('</script>\n') hdr.write('</script>\n')
# Decide on the winner # Decide on the winner

View File

@ -433,6 +433,16 @@ class Tank(object):
while self._angle > math.pi * 2: while self._angle > math.pi * 2:
self._angle = self._angle - math.pi * 2 self._angle = self._angle - math.pi * 2
def describe(self, f):
"""Output a description of this tank"""
f.write(' ["%s",[' % self.color)
for i in range(len(self._sensors)):
dist, sensorAngle, width, tSens = self._sensors[i]
f.write('[%d,%.2f,%.2f,%d],' % (dist, sensorAngle, width, tSens))
f.write(']],\n')
def draw(self, f): def draw(self, f):
"""Output this tank's state as JSON. """Output this tank's state as JSON.
@ -440,40 +450,16 @@ class Tank(object):
""" """
f.write(' [') if self.isDead:
f.write(str(int(self.isDead))); f.write(' 0,\n')
f.write(',') else:
f.write(repr(self.color)) flags = (self._fired << 0) | (self.led << 1)
f.write(',') sensors = 0
f.write('%d' % self.pos[0]) for i in range(len(self._sensorState)):
f.write(',') sensors |= self._sensorState[i] << i
f.write('%d' % self.pos[1]) f.write(' [%d,%d,%.2f,%.2f,%d,%d],\n' % (self.pos[0],
f.write(',') self.pos[1],
f.write('%.2f' % self._angle) self._angle,
f.write(',') self._tAngle,
f.write('%.2f' % self._tAngle) flags,
f.write(',') sensors))
f.write(str(int(self.led)))
f.write(',')
f.write('%d' % (self._fired and self.FIRE_RANGE) or 0)
if not self.isDead:
f.write(',[')
for i in range(len(self._sensors)):
dist, sensorAngle, width, tSens = self._sensors[i]
# If the angle is tied to the turret, add that.
if tSens:
sensorAngle = sensorAngle + self._tAngle
f.write('[')
f.write(str(int(dist)))
f.write(',')
f.write('%.2f' % (sensorAngle - width/2));
f.write(',')
f.write('%.2f' % (sensorAngle + width/2));
f.write(',')
f.write(str(int(self._sensorState[i])))
f.write('],')
f.write(']')
f.write('],\n')

View File

@ -1,3 +1,8 @@
function dbg(o) {
e = document.getElementById("debug");
e.innerHTML = o;
}
function torgba(color, alpha) { function torgba(color, alpha) {
var r = parseInt(color.substring(1,3), 16); var r = parseInt(color.substring(1,3), 16);
var g = parseInt(color.substring(3,5), 16); var g = parseInt(color.substring(3,5), 16);
@ -6,22 +11,50 @@ function torgba(color, alpha) {
return "rgba(" + r + "," + g + "," + b + "," + alpha + ")"; return "rgba(" + r + "," + g + "," + b + "," + alpha + ")";
} }
function start(turns) { function Tank(ctx, color, sensors) {
var canvas = document.getElementById('battlefield'); var craterStroke = torgba(color, 0.5);
var ctx = canvas.getContext('2d'); var craterFill = torgba(color, 0.2);
var loop_id; var sensorStroke = torgba(color, 0.4);
this.x = 0;
this.y = 0;
this.rotation = 0;
this.turret = 0;
function crater(color, x, y, rotation) { // Do all the yucky math up front
this.sensors = new Array();
for (i in sensors) {
s = sensors[i];
// r, angle, width, turret
this.sensors[i] = new Array();
this.sensors[i][0] = s[0];
this.sensors[i][1] = s[1] - (s[2]/2);
this.sensors[i][2] = s[1] + (s[2]/2);
this.sensors[i][3] = s[3]?1:0;
}
// Set up our state, for later interleaved draw requests
this.set_state = function(x, y, rotation, turret, flags, sensor_state) {
this.x = x;
this.y = y;
this.rotation = rotation;
this.turret = turret;
this.fire = flags & 1;
this.led = flags & 2;
this.sensor_state = sensor_state;
}
this.draw_crater = function() {
var points = 7; var points = 7;
var angle = Math.PI / points; var angle = Math.PI / points;
ctx.save(); ctx.save();
ctx.translate(x, y); ctx.translate(this.x, this.y);
ctx.rotate(rotation); ctx.rotate(this.rotation);
ctx.lineWidth = 2; ctx.lineWidth = 2;
ctx.strokeStyle = torgba(color, 0.5); ctx.strokeStyle = craterStroke;
ctx.fillStyle = torgba(color, 0.2); ctx.fillStyle = craterFill;
ctx.beginPath(); ctx.beginPath();
ctx.moveTo(12, 0); ctx.moveTo(12, 0);
for (i = 0; i < points; i += 1) { for (i = 0; i < points; i += 1) {
@ -33,64 +66,92 @@ function start(turns) {
ctx.closePath() ctx.closePath()
ctx.stroke(); ctx.stroke();
ctx.fill(); ctx.fill();
ctx.restore(); ctx.restore();
} }
function sensors(color, x, y, rotation, sensors) { this.draw_sensors = function() {
var sensor_color = torgba(color, 0.4);
ctx.save(); ctx.save();
ctx.translate(x, y); ctx.translate(this.x, this.y);
ctx.rotate(rotation); ctx.rotate(this.rotation);
ctx.lineWidth = 1; ctx.lineWidth = 1;
for (i in sensors) { for (i in this.sensors) {
s = sensors[i]; var s = this.sensors[i];
if (s[3]) { var adj = this.turret * s[3];
if (self.sensor_state & (1 << i)) {
// Sensor is triggered
ctx.strokeStyle = "#000"; ctx.strokeStyle = "#000";
} else { } else {
ctx.strokeStyle = sensor_color; ctx.strokeStyle = sensorStroke;
} }
ctx.beginPath(); ctx.beginPath();
ctx.moveTo(0, 0); ctx.moveTo(0, 0);
ctx.arc(0, 0, s[0], s[1], s[2], false); ctx.arc(0, 0, s[0], s[1] + adj, s[2] + adj, false);
ctx.closePath(); ctx.closePath();
ctx.stroke(); ctx.stroke();
} }
ctx.restore(); ctx.restore();
} }
function tank(color, x, y, rotation, turret, led, fire) { this.draw_tank = function() {
ctx.save(); ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation);
ctx.fillStyle = color; ctx.fillStyle = color;
ctx.translate(x, y);
ctx.rotate(rotation);
ctx.fillRect(-5, -4, 10, 8); ctx.fillRect(-5, -4, 10, 8);
ctx.fillStyle = "#777777"; ctx.fillStyle = "#777";
ctx.fillRect(-7, -9, 15, 5); ctx.fillRect(-7, -9, 15, 5);
ctx.fillRect(-7, 4, 15, 5); ctx.fillRect(-7, 4, 15, 5);
ctx.rotate(turret); ctx.rotate(this.turret);
if (fire) { if (this.fire) {
ctx.fillStyle = color; ctx.fillStyle = color;
ctx.fillRect(0, -1, fire, 2); ctx.fillRect(0, -1, 45, 2);
} else { } else {
if (led) { if (this.led) {
ctx.fillStyle = "#ff0000"; ctx.fillStyle = "#f00";
} else { } else {
ctx.fillStyle = "#000000"; ctx.fillStyle = "#000";
} }
ctx.fillRect(0, -1, 10, 2); ctx.fillRect(0, -1, 10, 2);
} }
ctx.restore(); ctx.restore();
} }
}
function start(game) {
var canvas = document.getElementById('battlefield');
var ctx = canvas.getContext('2d');
var loop_id;
canvas.width = game[0];
canvas.height = game[1];
// game[2] is tank descriptions
var turns = game[3];
// Set up tanks
var tanks = new Array();
for (i in game[2]) {
var desc = game[2][i];
tanks[i] = new Tank(ctx, desc[0], desc[1]);
}
var frame = 0; var frame = 0;
var lastframe = 0; var lastframe = 0;
var fps = document.getElementById('fps'); var fps = document.getElementById('fps');
function update_fps() { function update_fps() {
fps.innerHTML = (frame - lastframe); fps.innerHTML = (frame - lastframe);
lastframe = frame; lastframe = frame;
} }
function update() { function update() {
var idx = frame % (turns.length + 20); var idx = frame % (turns.length + 0);
var turn;
frame += 1; frame += 1;
if (idx >= turns.length) { if (idx >= turns.length) {
@ -103,23 +164,24 @@ function start(turns) {
// Draw craters first // Draw craters first
for (i in turn) { for (i in turn) {
t = turn[i]; t = turn[i];
if (t[0]) { if (! t) {
crater(t[1], t[2], t[3], t[4]); tanks[i].draw_crater();
} }
} }
// Then sensors // Then sensors
for (i in turn) { for (i in turn) {
t = turn[i]; t = turn[i];
if (! t[0]) { if (t) {
sensors(t[1], t[2], t[3], t[4], t[8]); // Surely there's a better way to do this.
tanks[i].set_state(t[0], t[1], t[2], t[3], t[4], t[5]);
tanks[i].draw_sensors();
} }
} }
// Then tanks // Then tanks
for (i in turn) { for (i in turn) {
t = turn[i]; t = turn[i];
if (! t[0]) { if (t) {
// Surely there's a better way. CBA right now. tanks[i].draw_tank()
tank(t[1], t[2], t[3], t[4], t[5], t[6], t[7]);
} }
} }
} }