Skip to content
Snippets Groups Projects
Commit ff440799 authored by David Bergström's avatar David Bergström
Browse files

Remove Building object, use UnitType and CCPosition directly

parent 1abdceb3
No related branches found
No related tags found
No related merge requests found
#include "Building.h"
Building::Building()
: desiredPosition (0,0)
, finalPosition (0,0)
, position (0,0)
, type ()
, buildingUnit ()
, builderUnit ()
, lastOrderFrame (0)
, status (BuildingStatus::Unassigned)
, buildCommandGiven (false)
, underConstruction (false)
{}
// constructor we use most often
Building::Building(UnitType t, CCTilePosition desired)
: desiredPosition (desired)
, finalPosition (0,0)
, position (0,0)
, type (t)
, buildingUnit ()
, builderUnit ()
, lastOrderFrame (0)
, status (BuildingStatus::Unassigned)
, buildCommandGiven (false)
, underConstruction (false)
{}
// equals operator
bool Building::operator == (const Building & b)
{
// buildings are equal if their worker unit and building unit are equal
return (b.buildingUnit == buildingUnit)
&& (b.builderUnit == builderUnit)
&& (b.finalPosition.x == finalPosition.x)
&& (b.finalPosition.y == finalPosition.y);
}
\ No newline at end of file
#pragma once
#include "Common.h"
#include "Unit.h"
#include "UnitType.h"
namespace BuildingStatus
{
enum { Unassigned = 0, Assigned = 1, UnderConstruction = 2, Size = 3 };
}
class Building
{
public:
CCTilePosition desiredPosition;
CCTilePosition finalPosition;
CCTilePosition position;
UnitType type;
Unit buildingUnit;
Unit builderUnit;
size_t status;
int lastOrderFrame;
bool buildCommandGiven;
bool underConstruction;
Building();
// constructor we use most often
Building(UnitType t, CCTilePosition desired);
// equals operator
bool operator == (const Building & b);
};
#include "BuildingData.h"
BuildingData::BuildingData()
{
}
void BuildingData::removeBuilding(const Building & b)
{
const auto & building = std::find(_buildings.begin(), _buildings.end(), b);
if (building != _buildings.end())
{
_buildings.erase(building);
}
}
std::vector<Building> & BuildingData::getBuildings()
{
return _buildings;
}
void BuildingData::addBuilding(const Building & b)
{
_buildings.push_back(b);
}
bool BuildingData::isBeingBuilt(UnitType type)
{
for (auto & b : _buildings)
{
if (b.type == type)
{
return true;
}
}
return false;
}
void BuildingData::removeBuildings(const std::vector<Building> & buildings)
{
for (const auto & b : buildings)
{
removeBuilding(b);
}
}
\ No newline at end of file
#pragma once
#include "Common.h"
#include "Building.h"
class BuildingData
{
std::vector<Building> _buildings;
public:
BuildingData();
std::vector<Building> & getBuildings();
void addBuilding(const Building & b);
void removeBuilding(const Building & b);
void removeBuildings(const std::vector<Building> & buildings);
bool isBeingBuilt(UnitType type);
};
\ No newline at end of file
#include "Common.h" #include "Common.h"
#include "BuildingPlacer.h" #include "BuildingPlacer.h"
#include "IDABot.h" #include "IDABot.h"
#include "Building.h"
#include "Util.h" #include "Util.h"
BuildingPlacer::BuildingPlacer(IDABot & bot) BuildingPlacer::BuildingPlacer(IDABot & bot)
...@@ -21,7 +20,7 @@ bool BuildingPlacer::isInResourceBox(int tileX, int tileY) const ...@@ -21,7 +20,7 @@ bool BuildingPlacer::isInResourceBox(int tileX, int tileY) const
} }
// makes final checks to see if a building can be built at a certain location // makes final checks to see if a building can be built at a certain location
bool BuildingPlacer::canBuildHere(int bx, int by, const Building & b) const bool BuildingPlacer::canBuildHere(int bx, int by, const UnitType & type) const
{ {
if (isInResourceBox(bx, by)) if (isInResourceBox(bx, by))
{ {
...@@ -29,9 +28,9 @@ bool BuildingPlacer::canBuildHere(int bx, int by, const Building & b) const ...@@ -29,9 +28,9 @@ bool BuildingPlacer::canBuildHere(int bx, int by, const Building & b) const
} }
// check the reserve map // check the reserve map
for (int x = bx; x < bx + b.type.tileWidth(); x++) for (int x = bx; x < bx + type.tileWidth(); x++)
{ {
for (int y = by; y < by + b.type.tileHeight(); y++) for (int y = by; y < by + type.tileHeight(); y++)
{ {
if (!m_bot.Map().isValidTile(x, y) || m_reserveMap[x][y]) if (!m_bot.Map().isValidTile(x, y) || m_reserveMap[x][y])
{ {
...@@ -41,7 +40,7 @@ bool BuildingPlacer::canBuildHere(int bx, int by, const Building & b) const ...@@ -41,7 +40,7 @@ bool BuildingPlacer::canBuildHere(int bx, int by, const Building & b) const
} }
// if it overlaps a base location return false // if it overlaps a base location return false
if (tileOverlapsBaseLocation(bx, by, b.type)) if (tileOverlapsBaseLocation(bx, by, type))
{ {
return false; return false;
} }
...@@ -50,19 +49,17 @@ bool BuildingPlacer::canBuildHere(int bx, int by, const Building & b) const ...@@ -50,19 +49,17 @@ bool BuildingPlacer::canBuildHere(int bx, int by, const Building & b) const
} }
//returns true if we can build this type of unit here with the specified amount of space. //returns true if we can build this type of unit here with the specified amount of space.
bool BuildingPlacer::canBuildHereWithSpace(int bx, int by, const Building & b, int buildDist) const bool BuildingPlacer::canBuildHereWithSpace(int bx, int by, const UnitType & type, int buildDist) const
{ {
UnitType type = b.type;
//if we can't build here, we of course can't build here with space //if we can't build here, we of course can't build here with space
if (!canBuildHere(bx, by, b)) if (!canBuildHere(bx, by, type))
{ {
return false; return false;
} }
// height and width of the building // height and width of the building
int width = b.type.tileWidth(); int width = type.tileWidth();
int height = b.type.tileHeight(); int height = type.tileHeight();
// TODO: make sure we leave space for add-ons. These types of units can have addons: // TODO: make sure we leave space for add-ons. These types of units can have addons:
...@@ -85,9 +82,9 @@ bool BuildingPlacer::canBuildHereWithSpace(int bx, int by, const Building & b, i ...@@ -85,9 +82,9 @@ bool BuildingPlacer::canBuildHereWithSpace(int bx, int by, const Building & b, i
{ {
for (int y = starty; y < endy; y++) for (int y = starty; y < endy; y++)
{ {
if (!b.type.isRefinery()) if (!type.isRefinery())
{ {
if (!buildable(b, x, y) || m_reserveMap[x][y]) if (!buildable(type, x, y) || m_reserveMap[x][y])
{ {
return false; return false;
} }
...@@ -98,13 +95,13 @@ bool BuildingPlacer::canBuildHereWithSpace(int bx, int by, const Building & b, i ...@@ -98,13 +95,13 @@ bool BuildingPlacer::canBuildHereWithSpace(int bx, int by, const Building & b, i
return true; return true;
} }
CCTilePosition BuildingPlacer::getBuildLocationNear(const Building & b, int buildDist) const CCTilePosition BuildingPlacer::getBuildLocationNear(const CCTilePosition & p, const UnitType & t, int buildDist) const
{ {
//Timer t; //Timer t;
//t.start(); //t.start();
// get the precomputed vector of tile positions which are sorted closes to this location // get the precomputed vector of tile positions which are sorted closes to this location
auto & closestToBuilding = m_bot.Map().getClosestTilesTo(b.desiredPosition); auto & closestToBuilding = m_bot.Map().getClosestTilesTo(p);
//double ms1 = t.getElapsedTimeInMilliSec(); //double ms1 = t.getElapsedTimeInMilliSec();
...@@ -113,7 +110,7 @@ CCTilePosition BuildingPlacer::getBuildLocationNear(const Building & b, int buil ...@@ -113,7 +110,7 @@ CCTilePosition BuildingPlacer::getBuildLocationNear(const Building & b, int buil
{ {
auto & pos = closestToBuilding[i]; auto & pos = closestToBuilding[i];
if (canBuildHereWithSpace(pos.x, pos.y, b, buildDist)) if (canBuildHereWithSpace(pos.x, pos.y, t, buildDist))
{ {
//double ms = t.getElapsedTimeInMilliSec(); //double ms = t.getElapsedTimeInMilliSec();
//printf("Building Placer Took %d iterations, lasting %lf ms @ %lf iterations/ms, %lf setup ms\n", (int)i, ms, (i / ms), ms1); //printf("Building Placer Took %d iterations, lasting %lf ms @ %lf iterations/ms, %lf setup ms\n", (int)i, ms, (i / ms), ms1);
...@@ -165,10 +162,10 @@ bool BuildingPlacer::tileOverlapsBaseLocation(int x, int y, UnitType type) const ...@@ -165,10 +162,10 @@ bool BuildingPlacer::tileOverlapsBaseLocation(int x, int y, UnitType type) const
return false; return false;
} }
bool BuildingPlacer::buildable(const Building & b, int x, int y) const bool BuildingPlacer::buildable(const UnitType & type, int x, int y) const
{ {
// TODO: does this take units on the map into account? // TODO: does this take units on the map into account?
if (!m_bot.Map().isValidTile(x, y) || !m_bot.Map().canBuildTypeAtPosition(x, y, b.type)) if (!m_bot.Map().isValidTile(x, y) || !m_bot.Map().canBuildTypeAtPosition(x, y, type))
{ {
return false; return false;
} }
...@@ -259,12 +256,7 @@ CCTilePosition BuildingPlacer::getRefineryPosition() ...@@ -259,12 +256,7 @@ CCTilePosition BuildingPlacer::getRefineryPosition()
} }
} }
} }
#ifdef SC2API
return CCTilePosition((int)closestGeyser.x, (int)closestGeyser.y); return CCTilePosition((int)closestGeyser.x, (int)closestGeyser.y);
#else
return CCTilePosition(closestGeyser);
#endif
} }
bool BuildingPlacer::isReserved(int x, int y) const bool BuildingPlacer::isReserved(int x, int y) const
......
#pragma once #pragma once
#include "Common.h" #include "Common.h"
#include "BuildingData.h"
class IDABot; class IDABot;
class BaseLocation; class BaseLocation;
class UnitType;
class BuildingPlacer class BuildingPlacer
{ {
...@@ -13,12 +13,11 @@ class BuildingPlacer ...@@ -13,12 +13,11 @@ class BuildingPlacer
std::vector< std::vector<bool> > m_reserveMap; std::vector< std::vector<bool> > m_reserveMap;
// queries for various BuildingPlacer data // queries for various BuildingPlacer data
bool buildable(const Building & b, int x, int y) const; bool buildable(const UnitType & type, int x, int y) const;
bool isReserved(int x, int y) const; bool isReserved(int x, int y) const;
bool isInResourceBox(int x, int y) const; bool isInResourceBox(int x, int y) const;
bool tileOverlapsBaseLocation(int x, int y, UnitType type) const; bool tileOverlapsBaseLocation(int x, int y, UnitType type) const;
public: public:
BuildingPlacer(IDABot & bot); BuildingPlacer(IDABot & bot);
...@@ -26,11 +25,11 @@ public: ...@@ -26,11 +25,11 @@ public:
void onStart(); void onStart();
// determines whether we can build at a given location // determines whether we can build at a given location
bool canBuildHere(int bx, int by, const Building & b) const; bool canBuildHere(int bx, int by, const UnitType & type) const;
bool canBuildHereWithSpace(int bx, int by, const Building & b, int buildDist) const; bool canBuildHereWithSpace(int bx, int by, const UnitType & type, int buildDist) const;
// returns a build location near a building's desired location // returns a build location near a building's desired location
CCTilePosition getBuildLocationNear(const Building & b, int buildDist) const; CCTilePosition getBuildLocationNear(const CCTilePosition & p, const UnitType & type, int buildDist) const;
void drawReservedTiles(); void drawReservedTiles();
......
...@@ -391,8 +391,7 @@ CCTilePosition IDABot::getBuildPosition(UnitType & building) ...@@ -391,8 +391,7 @@ CCTilePosition IDABot::getBuildPosition(UnitType & building)
else else
{ {
CCPosition position = Bases().getPlayerStartingBaseLocation(Players::Self)->getPosition(); CCPosition position = Bases().getPlayerStartingBaseLocation(Players::Self)->getPosition();
Building b{ building, Util::GetTilePosition(position)}; tile_position = m_buildingPlacer.getBuildLocationNear(Util::GetTilePosition(position), building, 1);
tile_position = m_buildingPlacer.getBuildLocationNear(b, 1);
} }
return tile_position; return tile_position;
} }
...@@ -424,7 +423,8 @@ std::vector<Unit> IDABot::getWorkers() ...@@ -424,7 +423,8 @@ std::vector<Unit> IDABot::getWorkers()
for (auto & unit : GetMyUnits()) for (auto & unit : GetMyUnits())
{ {
if (unit.getType().isWorker()) { if (unit.getType().isWorker())
{
workers.push_back(unit); workers.push_back(unit);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment