Differences From Artifact [3baab0ddf11cca7d]:
- File
src/game.d
- 2012-07-14 14:22:32 - part of checkin [a0c3529225] on branch trunk - code cleanup (user: kinaba) [annotate]
- 2012-07-14 16:58:31 - part of checkin [b1ce0206cd] on branch trunk - Revert. (user: kinaba) [annotate]
To Artifact [a105238bfa87f17a]:
- File
src/game.d
- 2012-07-14 17:20:35 - part of checkin [b8acb5f918] on branch trunk - trampoline parsing and rendering. (user: kinaba) [annotate]
88 88
89 89 ////////////////////////////////////////////////////////////////////////////////
90 90
91 91 class Map
92 92 {
93 93 mixin DeriveShow;
94 94
95 - static Map load(string[] raw_data, string[string] params)
95 + static Map load(string[] raw_data, string[string] params, char[char] trampo)
96 96 {
97 97 // TODO: choose optimal representation.
98 - return new Map(raw_data, params);
98 + return new Map(raw_data, params, trampo);
99 99 }
100 100
101 101 char[][] data;
102 102 Pos robot;
103 103 Pos lift;
104 104 int waterproof;
105 + Pos[char] tr_target;
106 + Pos[][char] tr_source;
105 107
106 108 Map clone() const { return new Map(this); }
107 109 this(in Map m) {
108 110 foreach(s; m.data)
109 111 this.data ~= s.dup;
110 112 this.robot = m.robot.clone();
111 113 this.lift = m.lift.clone();
112 114 this.waterproof = m.waterproof;
113 115 }
114 116
115 - this(string[] raw_data, string[string] params)
117 + this(string[] raw_data, string[string] params, char[char] trampo)
116 118 {
117 119 int width = 0;
118 120 foreach(r; raw_data)
119 121 width = max(width, r.length);
120 122 foreach(r; raw_data) {
121 123 this.data ~= r.dup;
122 124 this.data[$-1].length = width;
................................................................................
126 128 for(int y=1; y<=H; ++y)
127 129 for(int x=1; x<=W; ++x) {
128 130 if(this[y,x] == 'R')
129 131 this.robot = new Pos(y,x);
130 132 if(this[y,x] == 'L' || this[y,x] == 'O')
131 133 this.lift = new Pos(y,x);
132 134 }
135 +
136 + Pos[char] tr_pos;
137 + for(int y=1; y<=H; ++y)
138 + for(int x=1; x<=W; ++x) {
139 + char c = this[y,x];
140 + if('1'<=c && c<='9' || 'A'<=c&&c<='I')
141 + tr_pos[c] = new Pos(y,x);
142 + }
133 143
134 144 this.waterproof = params.get("Waterproof", "5").to!int();
145 + foreach(fr,to; trampo) {
146 + tr_target[fr] = tr_pos[to];
147 + if(to !in tr_source) tr_source[to] = [];
148 + tr_source[to] ~= tr_pos[to];
149 + }
135 150 }
136 151
137 152 const @property {
138 153 int H() { return data.length; }
139 154 int W() { return data[0].length; }
140 155 }
141 156
................................................................................
281 296 string[string] params;
282 297
283 298 // Raw map data; read until empty line.
284 299 for(string line; !(line=input.readln().chomp()).empty; )
285 300 raw_data ~= line;
286 301
287 302 // Additional commands; read until EOF.
303 + char[char] trampo;
288 304 for(string line; !(line=input.readln()).empty; ) {
289 305 string[] ss = line.split();
290 306 if( ss.length == 2 )
291 307 params[ss[0]] = ss[1];
308 + if( ss.length == 4 && ss[0]=="Trampoline" && ss[2]=="targets" )
309 + trampo[ss[1][0]] = ss[3][0];
292 310 }
293 311
294 - return load(raw_data, params);
312 + return load(raw_data, params, trampo);
295 313 }
296 314
297 - static Game load(string[] raw_data, string[string] params)
315 + static Game load(string[] raw_data, string[string] params, char[char] trampo = null)
298 316 {
299 - return new Game(raw_data, params);
317 + return new Game(raw_data, params, trampo);
300 318 }
301 319
302 - this(string[] raw_data, string[string] params)
320 + this(string[] raw_data, string[string] params, char[char] trampo)
303 321 {
304 - this.map = Map.load(raw_data, params);
322 + this.map = Map.load(raw_data, params, trampo);
305 323 this.water = Water.load(params);
306 324 }
307 325
308 326 Game clone() const { return new Game(this); }
309 327 this(in Game g) {
310 328 map = g.map.clone();
311 329 water = g.water.clone();