2
* Copyright © 2012 Keith Packard <keithp@keithp.com>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 2 of the License.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program; if not, write to the Free Software Foundation, Inc.,
15
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20
autoload Nichrome::RRboard;
22
extend namespace Client {
23
public namespace Update {
25
import Nichrome::RRboard;
28
public void update(&RR::Board board, string image) {
30
string[*] lines = String::split(image, "\n");
32
int width = (String::length(lines[1]) - 1) / 4;
33
int height = (dim(lines) - 2) / 2;
35
void update_hwall(int x, int y, bool above) {
37
board[x,y-1].walls.below = above;
39
board[x,y].walls.above = above;
42
void update_vwall(int x, int y, int c) {
54
board[x-1,y].walls.right = left;
56
board[x,y].walls.left = left;
59
void update_hwalls(int y, string line) {
60
for (int x = 0; x < width; x++)
61
update_hwall(x, y, line[x*4 + 2] == '=');
64
void update_squares(int y, string line) {
66
for (x = 0; x < width; x++) {
67
update_vwall(x, y, line[x*4]);
68
string robot = String::substr(line,x*4+1,1);
70
board[x,y].robot = RobotOrNone.none;
72
Color c = color(robot);
73
Robot r = { .color = color(robot),
74
.active = Ctype::isupper(robot[0]) };
75
board[x,y].robot = (RobotOrNone.robot) r;
77
string target = String::substr(line,x*4+2,2);
78
if (target[0] == '.') {
79
board[x,y].target = TargetOrNone.none;
81
Color c = color(String::substr(target,0,1));
82
Shape s = shape(String::substr(target,1,1));
83
Target t = { .color = c,
85
.active = Ctype::isupper(target[0]) };
86
board[x,y].target = (TargetOrNone.target) t;
89
update_vwall(x, y, line[x*4]);
93
for (y = 0; y < height; y++) {
94
update_hwalls(y, lines[y*2 + 1]);
95
update_squares(y, lines[y*2 + 2]);
97
update_hwalls(y, lines[y*2 + 1]);
100
public void update_robot(&RR::Board board, Color color, int new_x, int new_y) {
102
for (int y = 0; y < RR::Height; y++) {
103
for (int x = 0; x < RR::Width; x++) {
104
union switch (board[x,y].robot) {
106
if (r.color == color) {
108
board[x,y].robot = RobotOrNone.none;
116
board[new_x, new_y].robot = (RobotOrNone.robot) (Robot) { .color = color, .active = active };
119
public void update_target(&RR::Board board, Color color, Shape shape) {
120
for (int y = 0; y < RR::Height; y++) {
121
for (int x = 0; x < RR::Width; x++) {
122
union switch (board[x,y].target) {
124
if (t.color == color && t.shape == shape)
125
board[x,y].target.target.active = true;
127
board[x,y].target.target.active = false;