Changeset 344 for trunk/monsterz/fusion.cpp
 Timestamp:
 Feb 3, 2011, 3:08:58 PM (10 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/monsterz/fusion.cpp
r343 r344 16 16 #include <cmath> 17 17 #include <cstdlib> 18 #include <cstring> 18 19 #include <ctime> 19 20 … … 49 50 int score; 50 51 52 Mash *mashes; 51 53 Emitter *emitter; 52 54 Thumbs *thumbs; … … 92 94 data>current[1].piece>SetPos(int2(4, 7) * 48); 93 95 96 data>mashes = NULL; 97 94 98 data>next[0] = 1 + rand() % data>npieces; 95 99 data>next[1] = 1 + rand() % data>npieces; … … 110 114 111 115 int3 buttons = Input::GetMouseButtons(); 116 117 /* Get rid of finished mashes */ 118 for (Mash **it = &data>mashes; *it; ) 119 { 120 if ((*it)>IsDead()) 121 { 122 Ticker::Unref(*it); 123 *it = (*it)>nextmash; 124 } 125 else 126 it = &(*it)>nextmash; 127 } 112 128 113 129 int column = 1; … … 183 199 data>next[1] = 1 + rand() % data>npieces; 184 200 data>rotation = 0; 201 202 Resolve(); 185 203 } 186 204 … … 200 218 } 201 219 220 void Fusion::Resolve() 221 { 222 int list[MAX_PIECES][MAX_PIECES]; 223 int count[MAX_PIECES * MAX_PIECES]; 224 225 for (int j = 0; j < data>dim.j; j++) 226 for (int i = 0; i < data>dim.i; i++) 227 list[i][j] = 1; 228 memset(count, 0, sizeof(count)); 229 230 int seq = 0, effect = 0; 231 232 printf("\npairs:\n"); 233 for (int j = data>dim.j; j;) 234 { 235 for (int i = 0; i < data>dim.i; i++) 236 printf("% 2i ", data>pairs[i][j].id); 237 printf("\n"); 238 } 239 240 /* Count connected tiles */ 241 for (int j = 0; j < data>dim.j; j++) 242 for (int i = 0; i < data>dim.i; i++) 243 { 244 if (!data>pairs[i][j].id) 245 continue; 246 247 if (list[i][j] != 1) 248 continue; 249 250 list[i][j] = seq; 251 count[seq] = TagNeighbours(list, i, j); 252 if (count[seq] >= 3) 253 effect = 1; 254 seq++; 255 } 256 257 printf("hits:\n"); 258 for (int j = data>dim.j; j;) 259 { 260 for (int i = 0; i < data>dim.i; i++) 261 printf("% 2i ", list[i][j]); 262 printf("\n"); 263 } 264 265 /* Only continue if there is an effect */ 266 if (!effect) 267 return; 268 269 /* Add tiles to a mash; add mash to our list */ 270 Mash *mash = new Mash(data>emitter); 271 Ticker::Ref(mash); 272 273 for (int j = 0; j < data>dim.j; j++) 274 for (int i = 0; i < data>dim.i; i++) 275 { 276 if (list[i][j] == 1) 277 continue; 278 if (count[list[i][j]] < 3) 279 continue; 280 281 mash>AddPiece(data>pairs[i][j].piece); 282 data>pairs[i][j].piece = NULL; 283 } 284 285 mash>nextmash = data>mashes; 286 data>mashes = mash; 287 288 /* Create new pieces where necessary */ 289 for (int j = 0; j < data>dim.j; j++) 290 for (int i = 0; i < data>dim.i; i++) 291 { 292 if (list[i][j] == 1) 293 continue; 294 if (count[list[i][j]] < 3) 295 { 296 if (!data>pairs[i][j].piece) 297 data>pairs[i][j].id = 0; 298 continue; 299 } 300 301 data>pairs[i][j].id++; 302 data>pairs[i][j].piece = new Piece(data>emitter, int2(i, j), 80 + 20 * data>pairs[i][j].id); 303 Ticker::Ref(data>pairs[i][j].piece); 304 data>pairs[i][j].piece>SetPos(int2(i, j) * 48); 305 count[list[i][j]] = 0; 306 list[i][j] = 1; 307 } 308 309 printf("hits 2:\n"); 310 for (int j = data>dim.j; j;) 311 { 312 for (int i = 0; i < data>dim.i; i++) 313 printf("% 2i ", list[i][j]); 314 printf("\n"); 315 } 316 317 /* Move everything down */ 318 for (int j = data>dim.j; j;) for (int i = 0; i < data>dim.i; i++) 319 { 320 if (list[i][j] == 1  data>pairs[i][j].piece) 321 continue; 322 323 for (int j2 = j + 1; j2 < data>dim.j; j2++) 324 { 325 data>pairs[i][j2  1] = data>pairs[i][j2]; 326 if (data>pairs[i][j2  1].id) 327 { 328 data>pairs[i][j2  1].piece>SetCell(int2(i, j2  1)); 329 data>pairs[i][j2  1].piece>Move(int2(i, j2  1) * 48); 330 } 331 list[i][j2  1] = list[i][j2]; 332 } 333 334 data>pairs[i][data>dim.j  1].id = 0; 335 list[i][data>dim.j  1] = 1; 336 } 337 338 /* Start again (FIXME: make this a while() loop) */ 339 Resolve(); 340 } 341 342 int Fusion::TagNeighbours(int list[MAX_PIECES][MAX_PIECES], int i, int j) 343 { 344 int2 const off[] = { int2(1, 0), int2(1, 0), int2(0, 1), int2(0, 1) }; 345 346 int count = 1; 347 348 for (int n = 0; n < 4; n++) 349 { 350 int i2 = i + off[n].i; 351 int j2 = j + off[n].j; 352 353 if (i2 >= 0 && i2 < data>dim.i && j2 >= 0 && j2 < data>dim.j 354 && data>pairs[i2][j2].id == data>pairs[i][j].id 355 && list[i2][j2] == 1) 356 { 357 list[i2][j2] = list[i][j]; 358 count += TagNeighbours(list, i2, j2); 359 } 360 } 361 return count; 362 } 363 202 364 Fusion::~Fusion() 203 365 { … … 205 367 206 368 Ticker::Unref(data>thumbs); 369 while (data>mashes) 370 { 371 Ticker::Unref(data>mashes); 372 data>mashes = data>mashes>nextmash; 373 } 207 374 for (int j = 0; j < data>dim.j; j++) 208 375 for (int i = 0; i < data>dim.i; i++)
Note: See TracChangeset
for help on using the changeset viewer.