| 1 | /*************************************************************************** |
| 2 | board.cpp - description |
| 3 | ------------------- |
| 4 | begin : Thu Aug 17 2000 |
| 5 | copyright : (C) 2000 by Waldemar Baraldi |
| 6 | email : [email protected] |
| 7 | ***************************************************************************/ |
| 8 | |
| 9 | /*************************************************************************** |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | ***************************************************************************/ |
| 17 | |
| 18 | #include "board.h" |
| 19 | #include "player.h" |
| 20 | #include "mosaic.h" |
| 21 | #include "wallpaper.h" |
| 22 | |
| 23 | #define MAX_SPEED 80 |
| 24 | |
| 25 | Board::Board():AnimatedCanvas(){ |
| 26 | int i,j; |
| 27 | |
| 28 | for (i=0;i<BoardRows;i++) |
| 29 | for (j=0;j<BoardColumns;j++){ |
| 30 | board[i][j]=NULL; |
| 31 | changes[i][j]=false; |
| 32 | } |
| 33 | paint_all=true; |
| 34 | lps=new List(); |
| 35 | flow_row=flow_column=-1; |
| 36 | entry=exit=NULL; |
| 37 | actual_output=Void; |
| 38 | actual_input=Void; |
| 39 | state=Playing; |
| 40 | tick_amount=1; |
| 41 | tank=NULL; |
| 42 | background=NULL; |
| 43 | required=0; |
| 44 | still_required=0; |
| 45 | required_changed=false; |
| 46 | entry_row=-1; |
| 47 | entry_column=-1; |
| 48 | } |
| 49 | |
| 50 | Board::~Board(){ |
| 51 | int i,j; |
| 52 | for (i=0;i<BoardRows;i++) |
| 53 | for (j=0;j<BoardColumns;j++) |
| 54 | if (board[i][j]) delete board[i][j]; |
| 55 | delete lps; |
| 56 | if (background) delete background; |
| 57 | if (tank) delete tank; |
| 58 | } |
| 59 | |
| 60 | void Board::setStartDelay(int delay){ |
| 61 | if (tank) delete tank; |
| 62 | tank=new Tank(delay); |
| 63 | tank->setPos(TankX, TankY); |
| 64 | } |
| 65 | |
| 66 | void Board::setEntry(Entry * entry, int row, int column){ |
| 67 | flow_row=row; |
| 68 | flow_column=column; |
| 69 | actual_input=Void; |
| 70 | actual_output=entry->getOutput(Void); |
| 71 | setPipe(entry, row, column); |
| 72 | entry_row=row; |
| 73 | entry_column=column; |
| 74 | entry->setLast(true); |
| 75 | last=entry; |
| 76 | last_row=row; |
| 77 | last_column=column; |
| 78 | } |
| 79 | |
| 80 | void Board::setExit(Exit * exit, int row, int column){ |
| 81 | setPipe(exit, row, column); |
| 82 | } |
| 83 | |
| 84 | bool Board::isRemovable(int row, int column){ |
| 85 | if (board[row][column]) |
| 86 | return board[row][column]->isRemovable(); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | void Board::setPipe(Pipe * pipe, int row, int column){ |
| 91 | required_changed=true; |
| 92 | changes[row][column]=true; |
| 93 | Pipe * aux=board[row][column]; |
| 94 | if (aux==last) { |
| 95 | last=pipe; |
| 96 | pipe->setLast(true); |
| 97 | } |
| 98 | if (aux){ |
| 99 | if (aux->isRemovable()){ |
| 100 | delete aux; |
| 101 | board[row][column]=pipe; |
| 102 | pipe->setPos(column*pipe->width()+x, row*pipe->height()+y); |
| 103 | } |
| 104 | }else{ |
| 105 | board[row][column]=pipe; |
| 106 | pipe->setPos(column*pipe->width()+x, row*pipe->height()+y); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | Pipe * Board::getPipe(int row, int column){ |
| 111 | return board[row][column]; |
| 112 | } |
| 113 | |
| 114 | BoardState Board::getState(){ |
| 115 | return state; |
| 116 | } |
| 117 | |
| 118 | unsigned int Board::getRequired(){ |
| 119 | return required; |
| 120 | } |
| 121 | |
| 122 | unsigned int Board::getStillRequired(){ |
| 123 | if (!required_changed) return still_required; |
| 124 | else return (still_required=countStillRequired()); |
| 125 | } |
| 126 | |
| 127 | unsigned int Board::countStillRequired(){ |
| 128 | int row=entry_row; |
| 129 | int column=entry_column; |
| 130 | bool end=false; |
| 131 | unsigned int used=0; |
| 132 | required_changed=false; |
| 133 | |
| 134 | Pipe * aux_last=board[entry_row][entry_column]; |
| 135 | CardinalPoint con=board[row][column]->getOutput(Void);//Entry output |
| 136 | changes[last_row][last_column]=true; |
| 137 | while (!end){ |
| 138 | used++; |
| 139 | end=true; |
| 140 | switch (con){ |
| 141 | case East:{ |
| 142 | column++; |
| 143 | if (column<BoardColumns) |
| 144 | if (board[row][column] != NULL) |
| 145 | if (board[row][column]->hasConnection(West)) |
| 146 | if (!board[row][column]->isRestrictedAsOutput(West)){ |
| 147 | con=board[row][column]->getOutput(West); |
| 148 | end=false; |
| 149 | } |
| 150 | break; |
| 151 | } |
| 152 | case South:{ |
| 153 | row++; |
| 154 | if (row<BoardRows) |
| 155 | if (board[row][column] != NULL) |
| 156 | if (board[row][column]->hasConnection(North)) |
| 157 | if (!board[row][column]->isRestrictedAsOutput(North)){ |
| 158 | con=board[row][column]->getOutput(North); |
| 159 | end=false; |
| 160 | } |
| 161 | break; |
| 162 | } |
| 163 | case West:{ |
| 164 | column--; |
| 165 | if (column>=0) |
| 166 | if (board[row][column] != NULL) |
| 167 | if (board[row][column]->hasConnection(East)) |
| 168 | if (!board[row][column]->isRestrictedAsOutput(East)){ |
| 169 | con=board[row][column]->getOutput(East); |
| 170 | end=false; |
| 171 | } |
| 172 | break; |
| 173 | } |
| 174 | case North:{ |
| 175 | row--; |
| 176 | if (row>=0) |
| 177 | if (board[row][column] != NULL) |
| 178 | if (board[row][column]->hasConnection(South)) |
| 179 | if (!board[row][column]->isRestrictedAsOutput(South)){ |
| 180 | con=board[row][column]->getOutput(South); |
| 181 | end=false; |
| 182 | } |
| 183 | break; |
| 184 | } |
| 185 | default:{ |
| 186 | used--; //La salida no se cuenta como uno usado |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | if (!end){ |
| 191 | aux_last=board[row][column]; |
| 192 | last_row=row; |
| 193 | last_column=column; |
| 194 | } |
| 195 | }//while |
| 196 | used--; //La entrada no se cuenta como usado |
| 197 | if (last != aux_last){ |
| 198 | last->setLast(false); |
| 199 | last=aux_last; |
| 200 | last->setLast(true); |
| 201 | } |
| 202 | |
| 203 | if (used>required) return 0; |
| 204 | return required-used; |
| 205 | } |
| 206 | |
| 207 | void Board::addPointer(Pointer * p){ |
| 208 | lps->insert(lps->getFirst(), (Pointer *)p); |
| 209 | } |
| 210 | |
| 211 | void Board::removePointer(Pointer * p){ |
| 212 | lps->remove(lps->indexOf((Pointer*)p)); |
| 213 | } |
| 214 | |
| 215 | void Board::movePointer(Pointer *p, int row, int column){ |
| 216 | if (row>=0 && row<BoardRows && column>=0 && column<BoardColumns){ |
| 217 | if (p->getRow()>=0 && p->getRow()<BoardRows && p->getColumn()>=0 && p->getColumn()<BoardColumns) |
| 218 | changes[p->getRow()][p->getColumn()]=true; |
| 219 | p->setRowColumn(row, column); |
| 220 | p->setMoved(true); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void Board::setPos(int x, int y){ |
| 225 | Canvas::setPos(x,y); |
| 226 | int i,j; |
| 227 | for (i=0;i<BoardRows;i++) |
| 228 | for (j=0;j<BoardColumns;j++) |
| 229 | if (board[i][j]) |
| 230 | board[i][j]->setPos(j*board[i][j]->width()+x, i*board[i][j]->height()+y); |
| 231 | paint_all=true; |
| 232 | } |
| 233 | |
| 234 | void Board::setSpeed(int amount){ |
| 235 | tick_amount=amount; |
| 236 | } |
| 237 | |
| 238 | void Board::setMaxSpeed(){ |
| 239 | tick_amount=MAX_SPEED; |
| 240 | } |
| 241 | |
| 242 | void Board::setRequired(unsigned int value){ |
| 243 | required=value; |
| 244 | required_changed=true; |
| 245 | } |
| 246 | |
| 247 | void Board::setBackgroundType(BackgroundType type){ |
| 248 | background_type=type; |
| 249 | } |
| 250 | |
| 251 | void Board::tick(){ |
| 252 | int aux; |
| 253 | |
| 254 | tank->tick(); |
| 255 | last->tick(); |
| 256 | |
| 257 | for (int i=0;i<BoardRows;i++) |
| 258 | for (int j=0;j<BoardColumns;j++) |
| 259 | if (board[i][j]) |
| 260 | if (board[i][j]->getBonus()!=NormalBonus){ |
| 261 | board[i][j]->tick(); |
| 262 | changes[i][j]=true; |
| 263 | } |
| 264 | |
| 265 | changes[last_row][last_column]=true; |
| 266 | if (!(tank->isEmpty())){//Descuento del tanque |
| 267 | tank->decFullLevel(tick_amount); |
| 268 | changes[flow_row][flow_column]=true; |
| 269 | } |
| 270 | else{//Aumento el actual |
| 271 | aux=(getPipe(flow_row, flow_column))->getFullLevel(actual_input); |
| 272 | if (aux<(getPipe(flow_row, flow_column))->full()){ |
| 273 | (getPipe(flow_row, flow_column))->incFullLevel(actual_input, tick_amount); |
| 274 | changes[flow_row][flow_column]=true; |
| 275 | } |
| 276 | else{//Paso al siguiente |
| 277 | Pipe * next=NULL; |
| 278 | Pipe * last=board[flow_row][flow_column]; |
| 279 | actual_input=Void; |
| 280 | switch (actual_output){ |
| 281 | case North:{ |
| 282 | if (flow_row>0){ |
| 283 | next=getPipe(--flow_row, flow_column); |
| 284 | actual_input=South; |
| 285 | } |
| 286 | break; |
| 287 | } |
| 288 | case South:{ |
| 289 | if (flow_row<BoardRows-1){ |
| 290 | next=getPipe(++flow_row, flow_column); |
| 291 | actual_input=North; |
| 292 | } |
| 293 | break; |
| 294 | } |
| 295 | case East:{ |
| 296 | if (flow_column<BoardColumns-1){ |
| 297 | next=getPipe(flow_row, ++flow_column); |
| 298 | actual_input=West; |
| 299 | } |
| 300 | break; |
| 301 | } |
| 302 | case West:{ |
| 303 | if (flow_column>0){ |
| 304 | next=getPipe(flow_row, --flow_column); |
| 305 | actual_input=East; |
| 306 | } |
| 307 | break; |
| 308 | } |
| 309 | default:break; |
| 310 | } |
| 311 | if (actual_output==Void) |
| 312 | if (getStillRequired()==0) |
| 313 | state=OverWon; |
| 314 | else |
| 315 | state=OverLost; |
| 316 | else |
| 317 | if (actual_input==Void){ |
| 318 | state=OverLost; |
| 319 | }else{ |
| 320 | if (next) |
| 321 | if (next->hasConnection(actual_input) && !(next->isRestrictedAsOutput(actual_input))){ |
| 322 | actual_output=next->getOutput(actual_input); |
| 323 | next->incFullLevel(actual_input,tick_amount); |
| 324 | |
| 325 | /** Bonus stuff*/ |
| 326 | if (!next->getOwner() && last->getOwner()) |
| 327 | next->setOwner(last->getOwner()); |
| 328 | |
| 329 | Player * owner; |
| 330 | if ((owner=next->getOwner())) |
| 331 | switch (next->getBonus()){ |
| 332 | case LifeBonus:{ |
| 333 | owner->incLives(); |
| 334 | next->setBonus(NormalBonus); |
| 335 | break; |
| 336 | } |
| 337 | case TimeBonus:{ |
| 338 | //Debera incrementarse el valor del tiempo en board. |
| 339 | break; |
| 340 | } |
| 341 | default:{ |
| 342 | owner->incScore(next->getBonus()); |
| 343 | } |
| 344 | } |
| 345 | /*End of Bonus stuff*/ |
| 346 | |
| 347 | changes[flow_row][flow_column]=true; |
| 348 | }else state=OverLost; |
| 349 | else state=OverLost; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void Board::paint(VideoManager * vm){ |
| 356 | int i,j; |
| 357 | Index * index; |
| 358 | Pointer * aux; |
| 359 | int pc, pr; |
| 360 | |
| 361 | if (!background){ |
| 362 | Image * ima=vm->getImageManager()->getImage(new Str("board_back.jpg")); |
| 363 | switch (background_type){ |
| 364 | case WallpaperBackground:{ |
| 365 | background=new WallPaper(ima); |
| 366 | break; |
| 367 | } |
| 368 | case MosaicBackground:{ |
| 369 | background=new Mosaic(ima); |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | background->setPos(x, y); |
| 374 | } |
| 375 | |
| 376 | /** Pinta todos los pipes.*/ |
| 377 | if (paint_all){ |
| 378 | background->paint(vm); |
| 379 | for (i=0;i<BoardRows;i++) |
| 380 | for (j=0;j<BoardColumns;j++){ |
| 381 | if (board[i][j]) board[i][j]->paint(vm); |
| 382 | changes[i][j]=false; |
| 383 | } |
| 384 | }else{/** Pinta solo los que han cambiado*/ |
| 385 | for (i=0;i<BoardRows;i++) |
| 386 | for (j=0;j<BoardColumns;j++){ |
| 387 | if (changes[i][j]){ |
| 388 | background->repaint(vm, j*PipeWidth, i*PipeHeight, PipeWidth, PipeHeight); |
| 389 | if (board[i][j]) board[i][j]->paint(vm); |
| 390 | changes[i][j]=false; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /** Pinta y marca los punteros para la proxima entrada.*/ |
| 397 | index=lps->getFirst(); |
| 398 | for (i=0;i<lps->nObjects();i++){ |
| 399 | aux=(Pointer*)lps->getObject(index); |
| 400 | pr=aux->getRow(); |
| 401 | pc=aux->getColumn(); |
| 402 | if (aux->moved() |
| 403 | ||(flow_column==pc && flow_row==pr) |
| 404 | ||(last_column==pc && last_row==pr) |
| 405 | ||(board[pr][pc] && board[pr][pc]->getBonus()!=NormalBonus)){ |
| 406 | aux->setPos(pc*aux->width+x, pr*aux->height+y); |
| 407 | aux->paint(vm); |
| 408 | if (aux->moved()) aux->setMoved(false); else aux->setMoved(true); |
| 409 | } |
| 410 | index=lps->getNext(index); |
| 411 | } |
| 412 | paint_all=false; |
| 413 | |
| 414 | /** Pinta la mancha si el juego esta perdido.*/ |
| 415 | if (state==OverLost){ |
| 416 | Image * mancha=vm->getImageManager()->getImage(new Str("splash.png")); |
| 417 | int mx=flow_column*PipeWidth+x; |
| 418 | int my=flow_row*PipeHeight+y; |
| 419 | switch (actual_input){ |
| 420 | case Void:{ //Esta contra un borde |
| 421 | switch (actual_output){ |
| 422 | case North:my-=PipeHeight/2;break; |
| 423 | case South:my+=PipeHeight/2;break; |
| 424 | case West:mx-=PipeWidth/2;break; |
| 425 | case East:mx+=PipeWidth/2;break; |
| 426 | default:break; |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | case North:my-=PipeHeight/2;break; |
| 431 | case South:my+=PipeHeight/2;break; |
| 432 | case West:mx-=PipeWidth/2;break; |
| 433 | case East:mx+=PipeWidth/2;break; |
| 434 | default:break; |
| 435 | } |
| 436 | vm->setClipping(x, y, width(), height()); |
| 437 | vm->enableClipping(true); |
| 438 | vm->blit(mancha, mx, my); |
| 439 | vm->enableClipping(false); |
| 440 | } |
| 441 | if (tank->isChanged()) tank->paint(vm); |
| 442 | } |
| 443 | |
| 444 | int Board::width(){ |
| 445 | return BoardColumns*PipeWidth; |
| 446 | } |
| 447 | |
| 448 | int Board::height(){ |
| 449 | return BoardRows*PipeHeight; |
| 450 | } |
| 451 |