void Cell::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::RightButton){ emit deleteShip(this->data(DATA_KEY).toInt()); } else if (event->button() == Qt::LeftButton){ first = event->scenePos(); emit setArrows(this->data(DATA_KEY).toInt()); } }
bool Board::attack(const Bomb &b) { bool hit = true; Position<unsigned int> coordenates; int partNumber; coordenates.lin = (int)(b.getTargetPosition().lin - 'A'); coordenates.col = (int)(b.getTargetPosition().col - 'a'); clrscr(); gotoxy(0, 0); if (coordenates.lin < 0 || coordenates.col < 0 || coordenates.lin > (unsigned int) numLines - 1 || coordenates.col > (unsigned int) numColumns - 1) hit = false; else if (board[coordenates.lin][coordenates.col] == -1 ) hit = false; if (hit) { if (ships[board[coordenates.lin][coordenates.col]].getOrientation() == 'H') partNumber = coordenates.col - ships[board[coordenates.lin][coordenates.col]].getPosition().col; else if (ships[board[coordenates.lin][coordenates.col]].getOrientation() == 'V') partNumber = coordenates.lin - ships[board[coordenates.lin][coordenates.col]].getPosition().lin; char h = ships[board[coordenates.lin][coordenates.col]].getStatus()[partNumber]; if (islower(h)) { cout << "Position has already been hit! \n"; } ships[board[coordenates.lin][coordenates.col]].attack(partNumber); setcolor(10, 0); cout << "\nHit! \n"; if (ships[board[coordenates.lin][coordenates.col]].isDestroyed()) { setcolor(7, 0); cout << "Ship "; setcolor(ships[board[coordenates.lin][coordenates.col]].getColor(), 0); cout << ships[board[coordenates.lin][coordenates.col]].getSymbol(); setcolor(7, 0); cout << " has been destroyed!"; deleteShip(ships[board[coordenates.lin][coordenates.col]]); } setcolor(7, 0); } else { setcolor(12, 0); cout << "\nMiss! \n"; setcolor(7, 0); } return hit; }
void Game::moveEnemies() { int i; i = 0; while (i < _enemyId) { deleteShip(_enemy[i]); _enemy[i].goLeft(); if (_enemy[i].getPosX() < 0) _win.renderLose(); if (_enemy[i].getPosY() > _win.getH() || _enemy[i].getPosY() < 0) deleteEntity(_enemy[i]); if (!checkIfCollision(_enemy[i])) drawShip(_enemy[i]); i++; } }
void Game::deleteEntity(AGameEntity & entity) { int index; int i; int j; Missile fake_miss; Enemy fake_enemy; if (entity.getType() == MISSILE) { _board.setMatriceValueAt(entity.getPosX(), entity.getPosY(), 0); index = static_cast<Missile &>(entity).getId(); i = index; j = index + 1; while (j <= _missileId) { _missile[i] = _missile[j]; _missile[i].setId(i); i++; j++; } _missile[i] = fake_miss; _missileId--; } else { deleteShip(static_cast<Enemy &>(entity)); index = static_cast<Enemy &>(entity).getId(); i = index; j = index + 1; while (j <= _enemyId) { _enemy[i] = _enemy[j]; _enemy[i].setId(i); i++; j++; } _enemy[i] = fake_enemy; _enemyId--; if (_enemyId == 0) _win.renderWin(); } }
void Universe::deleteShipOnIsle(const uint inShipId) { uint isleId = 0; for(Ship *s : m_ships) { ShipInfo sInfo = s->info(); if(sInfo.id == inShipId) { isleId = sInfo.isleId; // just to get sure if((sInfo.posType != ShipPositionEnum::SP_OCEAN) and isleId > 0) { deleteShip(inShipId); IsleInfo iInfo; isleForId(isleId, iInfo); showHumanIsle(iInfo); } break; } } }
void Board::moveShips() // tries to randmonly move all the ships of the fleet { int count = 0; for (unsigned int i = 0; i < ships.size(); i++) { if (!ships[i].isDestroyed()) { Ship temp = ships[i]; bool move = ships[i].moveRand(0, 0, numLines - 1, numColumns - 1); if (move || ships[i].getOrientation() != temp.getOrientation()) { deleteShip(temp); while (1) { if (putShip(ships[i], i)) break; else if (count >= 20) { ships[i] = temp; putShip(temp, i); } else { ships[i] = temp; ships[i].moveRand(0, 0, numLines - 1, numColumns - 1); } count++; } } else { ships[i] = temp; } } } }
void Universe::nextRound(UniverseScene *& inOutUniverseScene) { qInfo() << "BEGIN NEXTROUND =================="; prepareStrategies(); for(ComputerPlayer *player : m_computerPlayers) { if(player->isDead()) continue; QList<ComputerMove> computerMoves; player->nextRound(computerMoves); processStrategyCommands(player->owner(), computerMoves); } for(Isle *isle : m_isles) { if(isle->nextRound()) { createShipOnIsle(inOutUniverseScene, isle->info()); } } for(Ship *ship : m_ships) { ShipInfo shipInfo = ship->info(); if(ship->isDead()) continue; if( ship->nextRound() ) // ship->nextRound() returns true on arrive { // arrived // as ships which arrived in heaven get cought above, the ships here MUST // have a target, so it is save to call: Target target = ship->currentTarget(); if(target.tType == Target::TargetEnum::T_ISLE) { // land or fight IsleInfo isleInfo; isleForId(target.id, isleInfo); if(isleInfo.owner == Player::PLAYER_UNSETTLED) { // isle has no inhabitants shipFightIslePatol(ship, target.id); if(ship->isDead()) continue; // ship is destroyed if(shipInfo.shipType == ShipTypeEnum::ST_COLONY) { setIsleOwnerById(isleInfo.id, shipInfo.owner, shipInfo.color); shipLandOnIsle(ship, isleInfo.id); // colony ships get destroyed as they land, because // the ship's material is urgently needed for housing and // such things ship->setDead(); } else if(shipInfo.shipType == ShipTypeEnum::ST_FLEET) { // does it contain a colony? if(ship->fleetContainsShipType(ShipTypeEnum::ST_COLONY)) { setIsleOwnerById(isleInfo.id, shipInfo.owner, shipInfo.color); shipLandOnIsle(ship, isleInfo.id); // delete the first colony ship in the fleet ship->fleetRemoveFirstColonyShip(); } else { ship->landOnIsle(target.id, target.pos); ship->setPositionType(ShipPositionEnum::SP_PATROL); } } else { // send to orbit ship->landOnIsle(target.id, target.pos); ship->setPositionType(ShipPositionEnum::SP_PATROL); } } else if(isleInfo.owner == shipInfo.owner) { // own isle // courier takes tech first ship->setCarryTechnology(isleInfo.technology); shipLandOnIsle(ship, isleInfo.id); } else { // enemy isle -> fight qInfo() << "ship fights... id= " << ship->id(); // 1. fight isles patrol shipFightIslePatol(ship, target.id); // 2. fight isle if( shipFightIsle(ship, target.id) ) { // ship has won, isle is now owned by ship's owner shipLandOnIsle(ship, target.id); // every other enemy ship on this isle is now owned by the winner float local_tech_max = 0.1f; for(Ship *isleShip : m_ships) { ShipInfo isleShipInfo = isleShip->info(); if(isleShipInfo.posType == ShipPositionEnum::SP_ONISLE and isleShipInfo.isleId == target.id) { // set new owner isleShip->setOwner(shipInfo.owner, shipInfo.color); // find the maximum technology for pirated ships local_tech_max = isleShipInfo.technology > local_tech_max ? isleShipInfo.technology : local_tech_max; } if(local_tech_max > shipInfo.technology) { // maybe, one of the pirated ships has higher tech than the ship which landed int isleIndex = isleIndexForId(target.id); if(isleIndex >= 0) m_isles[isleIndex]->setMaxTechnology(local_tech_max); } } } else { qInfo() << "ship lost id = " << ship->id(); ShipInfo info = ship->info(); qInfo() << "ship lost id = " << ship->id() << " damage: " << info.damage << " delete: " << (info.posType == ShipPositionEnum::SP_TRASH); } } } else if(target.tType == Target::TargetEnum::T_SHIP) { // fight or rendez vous // find the other ship Ship *otherShip; int shipIndex = shipIndexForId(target.id); if(shipIndex >= 0) otherShip = m_ships[shipIndex]; ShipInfo otherShipInfo = otherShip->info(); if(shipInfo.owner == otherShipInfo.owner) { // same owner: just rendez vous // @fixme: maybe add to fleet? ship->setTargetFinished(); } else { // different owner -> fight shipFightShip(ship, otherShip); shipInfo = ship->info(); // update info if(shipInfo.posType != ShipPositionEnum::SP_TRASH) ship->setTargetFinished(); } } else // Target::TargetEnum::T_WATER { // nothing to do here ship->setTargetFinished(); } } else // ship's nextround() returned false { // repair ships on isle if(shipInfo.posType == ShipPositionEnum::SP_ONISLE and (!ship->isDead())) { ship->repair(); } } } // empty trash for(Ship *deleteThatShip : m_ships) { ShipInfo dmgShipInfo = deleteThatShip->info(); if(dmgShipInfo.shipType == ShipTypeEnum::ST_FLEET) deleteThatShip->updateFleet(); bool shouldDelete = false; bool doDelete = false; if(deleteThatShip->isDead()) { qDebug() << " -- should delete: " << dmgShipInfo.id; shouldDelete = true; } if(deleteThatShip->positionType() == ShipPositionEnum::SP_TRASH) { qDebug() << " -- delete " << dmgShipInfo.id; deleteShip(deleteThatShip->id()); doDelete = true; } Q_ASSERT(shouldDelete == doDelete); } // update the target position for ships with tType T_SHIP, because they could have moved // this is very straight forward (for every ship check target ships and update pos or delete target entry) // but it is worse in the meaning of complexity O(n^4) :-( for( Ship *updateShip : m_ships ) { QVector<Target> targets = updateShip->targets(); for(Target t : targets) { if(t.tType == Target::T_SHIP) { bool targetIsAlive = false; int shipIndex = shipIndexForId(t.id); if(shipIndex >= 0) { targetIsAlive = true; QPointF newPos = m_ships.at(shipIndex)->info().pos; updateShip->updateTargetPos(t.id, newPos); } if(! targetIsAlive) { // the target ship was deleted, so delete the target too updateShip->removeTargetShip(t.id); } } } } qInfo() << "END NEXTROUND =================="; }