diff --git a/unit.cpp b/unit.cpp index 5abc271..a8d00f2 100755 --- a/unit.cpp +++ b/unit.cpp @@ -302,9 +302,26 @@ namespace battleship{ } void Unit::eject(Order order){ + Map *map = Map::getSingleton(); + int currCellId = map->getCellId(pos); + vector &cells = map->getCells(); + int adjWaterCellId = -1, adjLandCellId = -1; + + for(Map::Edge edge : cells[currCellId].edges){ + if(adjLandCellId == -1 && cells[edge.destCellId].type == Map::Cell::LAND) + adjLandCellId = edge.destCellId; + if(adjWaterCellId == -1 && cells[edge.destCellId].type == Map::Cell::WATER) + adjWaterCellId = edge.destCellId; + } + if(garrisonSlots.size() > 0){ - for(Order::Target targ : order.targets) - ((Vehicle*)targ.unit)->exitGarrisonable(); + for(Order::Target targ : order.targets){ + bool exitToLandCell = (targ.unit->getType() == UnitType::LAND && adjLandCellId != -1); + bool exitToWaterCell = ((targ.unit->getType() == UnitType::SEA_LEVEL || targ.unit->getType() == UnitType::UNDERWATER) && adjWaterCellId != -1); + + if(exitToLandCell || exitToWaterCell) + ((Vehicle*)targ.unit)->exitGarrisonable(cells[exitToLandCell ? adjLandCellId : adjWaterCellId].pos); + } removeOrder(0); } diff --git a/vehicle.cpp b/vehicle.cpp index 77837e5..a44f2ed 100644 --- a/vehicle.cpp +++ b/vehicle.cpp @@ -165,8 +165,8 @@ namespace battleship{ removeOrder(0); } - void Vehicle::exitGarrisonable(){ - placeAt(garrisonable->getPos()); + void Vehicle::exitGarrisonable(Vector3 exitPos){ + placeAt(exitPos); garrisonable->updateGarrison(this, false); garrisonable = nullptr; } @@ -196,7 +196,7 @@ namespace battleship{ void Vehicle::garrison(Order order){ Unit *targUnit = order.targets[0].unit; - float distToGarrisonable = pos.getDistanceFrom(targUnit->getPos()), garrisonDist = .5 * Map::getSingleton()->getCellSize().x; + float distToGarrisonable = pos.getDistanceFrom(targUnit->getPos()), garrisonDist = Map::getSingleton()->getCellSize().x; if(distToGarrisonable > garrisonDist) navigateToTarget(garrisonDist); @@ -244,13 +244,16 @@ namespace battleship{ int dest = map->getCellId(destPos); vector path = pathfinder->findPath(cells, source, dest, (int)type); - for(int i = 0; i < path.size(); i++) - if((ship && cells[path[i]].type != Map::Cell::WATER) || (type == UnitType::LAND && cells[path[i]].type != Map::Cell::LAND)){ + for(int i = 0; i < path.size(); i++){ + if((ship && cells[path[i]].type != Map::Cell::WATER) || (order.type != Order::TYPE::GARRISON && type == UnitType::LAND && cells[path[i]].type != Map::Cell::LAND)){ path = vector(path.begin(), path.begin() + i); order.targets[0].unit = nullptr; order.targets[0].pos = cells[path[i - 1]].pos; break; } + else if(order.type == Order::TYPE::GARRISON && type == UnitType::LAND && cells[path[i]].type != Map::Cell::LAND && path.size() - 1 != i) + return; + } for(int p : path) addPathpoint(cells[p].pos); diff --git a/vehicle.h b/vehicle.h index 9c4c0af..6313d8d 100644 --- a/vehicle.h +++ b/vehicle.h @@ -14,7 +14,7 @@ namespace battleship{ ~Vehicle(); virtual void update(); void move(Order); - void exitGarrisonable(); + void exitGarrisonable(vb01::Vector3); inline Unit* getGarrisonable(){return garrisonable;} inline int getGarrisonCategory(){return garrisonCategory;} private: