~ubuntu-branches/ubuntu/saucy/ricochet/saucy

« back to all changes in this revision

Viewing changes to ricochet_0.2.orig/client-update.5c

  • Committer: Package Import Robot
  • Author(s): Keith Packard
  • Date: 2012-06-11 13:37:57 UTC
  • Revision ID: package-import@ubuntu.com-20120611133757-zn0ukd22vz56ymto
Tags: 0.3
* Improve appearance of board
* Fix user list when removing/adding same user

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
3
 
 *
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.
7
 
 *
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.
12
 
 *
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.
16
 
 */
17
 
 
18
 
autoload Client;
19
 
autoload RR;
20
 
autoload Nichrome::RRboard;
21
 
 
22
 
extend namespace Client {
23
 
        public namespace Update {
24
 
 
25
 
                import Nichrome::RRboard;
26
 
                import RR;
27
 
 
28
 
                public void update(&RR::Board board, string image) {
29
 
 
30
 
                        string[*] lines = String::split(image, "\n");
31
 
 
32
 
                        int     width = (String::length(lines[1]) - 1) / 4;
33
 
                        int     height = (dim(lines) - 2) / 2;
34
 
 
35
 
                        void update_hwall(int x, int y, bool above) {
36
 
                                if (y > 0)
37
 
                                        board[x,y-1].walls.below = above;
38
 
                                if (y < height)
39
 
                                        board[x,y].walls.above = above;
40
 
                        }
41
 
 
42
 
                        void update_vwall(int x, int y, int c) {
43
 
                                bool left;
44
 
 
45
 
                                switch (c) {
46
 
                                case '|':
47
 
                                        left = true;
48
 
                                        break;
49
 
                                case ' ':
50
 
                                        left = false;
51
 
                                        break;
52
 
                                }
53
 
                                if (x > 0)
54
 
                                        board[x-1,y].walls.right = left;
55
 
                                if (x < width)
56
 
                                        board[x,y].walls.left = left;
57
 
                        }
58
 
 
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] == '=');
62
 
                        }
63
 
 
64
 
                        void update_squares(int y, string line) {
65
 
                                int x;
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);
69
 
                                        if (robot == ".")
70
 
                                                board[x,y].robot = RobotOrNone.none;
71
 
                                        else {
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;
76
 
                                        }
77
 
                                        string target = String::substr(line,x*4+2,2);
78
 
                                        if (target[0] == '.') {
79
 
                                                board[x,y].target = TargetOrNone.none;
80
 
                                        } else {
81
 
                                                Color c = color(String::substr(target,0,1));
82
 
                                                Shape s = shape(String::substr(target,1,1));
83
 
                                                Target t = { .color = c,
84
 
                                                             .shape = s,
85
 
                                                             .active = Ctype::isupper(target[0]) };
86
 
                                                board[x,y].target = (TargetOrNone.target) t;
87
 
                                        }
88
 
                                }
89
 
                                update_vwall(x, y, line[x*4]);
90
 
                        }
91
 
 
92
 
                        int x, y;
93
 
                        for (y = 0; y < height; y++) {
94
 
                                update_hwalls(y, lines[y*2 + 1]);
95
 
                                update_squares(y, lines[y*2 + 2]);
96
 
                        }
97
 
                        update_hwalls(y, lines[y*2 + 1]);
98
 
                }
99
 
 
100
 
                public void update_robot(&RR::Board board, Color color, int new_x, int new_y) {
101
 
                        bool active = false;
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) {
105
 
                                        case robot r:
106
 
                                                if (r.color == color) {
107
 
                                                        active = r.active;
108
 
                                                        board[x,y].robot = RobotOrNone.none;
109
 
                                                }
110
 
                                                break;
111
 
                                        default:
112
 
                                                break;
113
 
                                        }
114
 
                                }
115
 
                        }
116
 
                        board[new_x, new_y].robot = (RobotOrNone.robot) (Robot) { .color = color, .active = active };
117
 
                }
118
 
 
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) {
123
 
                                        case target t:
124
 
                                                if (t.color == color && t.shape == shape)
125
 
                                                        board[x,y].target.target.active = true;
126
 
                                                else
127
 
                                                        board[x,y].target.target.active = false;
128
 
                                                break;
129
 
                                        default:
130
 
                                                break;
131
 
                                        }
132
 
                                }
133
 
                        }
134
 
                }
135
 
        }
136
 
}