2014-12-16 13:12:50 +00:00
|
|
|
#include "game.h"
|
2014-12-16 13:12:59 +00:00
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
#include <algorithm>
|
2014-12-16 13:13:02 +00:00
|
|
|
#include <string.h>
|
2014-12-16 13:12:59 +00:00
|
|
|
|
|
|
|
// Interpret a string of orders
|
|
|
|
int CTTRTSGame::IssueOrders( player_id_t player, const std::string& _orders )
|
|
|
|
{
|
|
|
|
COrderVector orderVector;
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Copy the const orders into a buffer we can edit
|
|
|
|
std::string orders = _orders;
|
|
|
|
|
|
|
|
// Find a line end
|
2014-12-16 13:12:59 +00:00
|
|
|
size_t pos;
|
2014-12-16 13:13:02 +00:00
|
|
|
while ( (pos = orders.find('\n')) != std::string::npos )
|
2014-12-16 13:12:59 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// Grab the string up to the line end
|
2014-12-16 13:12:59 +00:00
|
|
|
const std::string sorder = orders.substr(0, pos);
|
2014-12-16 13:13:02 +00:00
|
|
|
|
|
|
|
// Erase all of string up to and including the line end
|
2014-12-16 13:12:59 +00:00
|
|
|
orders.erase(0,pos+1);
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Create an order from the string and push it back
|
2014-12-16 13:12:59 +00:00
|
|
|
COrder order = GetOrderFromString( sorder );
|
|
|
|
orderVector.push_back(order);
|
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Call our add order by vector method
|
2014-12-16 13:12:59 +00:00
|
|
|
return IssueOrders(player,orderVector);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue orders by vector to the game
|
|
|
|
int CTTRTSGame::IssueOrders( player_id_t player, const COrderVector& orders )
|
|
|
|
{
|
|
|
|
// verify all the orders
|
2014-12-16 13:13:01 +00:00
|
|
|
for ( auto order : orders )
|
2014-12-16 13:12:59 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// If any order returns non-zero, back out
|
2014-12-16 13:13:01 +00:00
|
|
|
if ( IssueOrder(player,order) )
|
2014-12-16 13:12:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue a single order
|
|
|
|
int CTTRTSGame::IssueOrder( player_id_t player, const COrder& order )
|
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// Verify the order
|
2014-12-16 13:12:59 +00:00
|
|
|
if ( VerifyOrder(player,order) )
|
|
|
|
return 1;
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Get the right unit for the order
|
2014-12-16 13:13:01 +00:00
|
|
|
for ( OrderUnitPair& pair : m_OrderUnitPairs )
|
|
|
|
{
|
|
|
|
if ( pair.unit.getID() == order.unit )
|
|
|
|
{
|
|
|
|
pair.order = order;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 13:12:59 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Unit was not found, return 2
|
|
|
|
return 2;
|
2014-12-16 13:12:59 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
// Verify a position
|
|
|
|
int CTTRTSGame::VerifyPos(uvector2 vec) const
|
2014-12-16 13:12:59 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// Simply check if within the bounds of our dimensions for now
|
2014-12-16 13:13:02 +00:00
|
|
|
if ( ( vec.x >= dimensions.x )
|
|
|
|
|| ( vec.y >= dimensions.y ) )
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-16 13:12:59 +00:00
|
|
|
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
// Get a units new position
|
|
|
|
uvector2 CTTRTSGame::GetNewPosition( const OrderUnitPair& pair ) const
|
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
|
|
|
|
// Grab the order
|
2014-12-16 13:13:01 +00:00
|
|
|
switch ( pair.order.order )
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// For forward orders, grab in front
|
2014-12-16 13:13:01 +00:00
|
|
|
case order_c::F:
|
|
|
|
return pair.unit.getInFront();
|
2014-12-16 13:13:02 +00:00
|
|
|
break;
|
2014-12-16 13:13:02 +00:00
|
|
|
// For all other orders, just grab the old position
|
2014-12-16 13:13:02 +00:00
|
|
|
default:
|
2014-12-16 13:13:02 +00:00
|
|
|
return pair.unit.getPos();
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate and progress to the next turn
|
|
|
|
// Returns non-zero if simulation failed
|
|
|
|
int CTTRTSGame::SimulateToNextTurn()
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
// Attempt all movement orders
|
2014-12-16 13:13:01 +00:00
|
|
|
for ( OrderUnitPair& pair : m_OrderUnitPairs )
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
|
|
|
switch ( pair.order.order )
|
|
|
|
{
|
|
|
|
case order_c::F:
|
|
|
|
{
|
|
|
|
// Verify new unit position will be on the board
|
2014-12-16 13:13:01 +00:00
|
|
|
uvector2 newpos = GetNewPosition(pair);
|
|
|
|
|
|
|
|
// Verify the position is even available
|
|
|
|
bool possible = ( VerifyPos(newpos) == 0 );
|
|
|
|
|
|
|
|
if ( possible )
|
|
|
|
{
|
|
|
|
// If any unit is in this spot, or moving unit moving to said spot, reject this
|
|
|
|
for ( const OrderUnitPair& pair2 : m_OrderUnitPairs )
|
|
|
|
{
|
|
|
|
if( GetNewPosition(pair2) != newpos )
|
|
|
|
{
|
|
|
|
possible = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the movement is still possible
|
|
|
|
if ( possible )
|
|
|
|
{
|
|
|
|
pair.unit.setPos(newpos);
|
|
|
|
pair.order = COrder();
|
|
|
|
}
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-12-16 13:13:02 +00:00
|
|
|
default:
|
|
|
|
break;
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vector of units to kill
|
|
|
|
std::vector< unit_id_t > toKill;
|
|
|
|
|
|
|
|
// Attempt all actions
|
2014-12-16 13:13:01 +00:00
|
|
|
for ( OrderUnitPair& pair : m_OrderUnitPairs )
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
|
|
|
switch ( pair.order.order )
|
|
|
|
{
|
|
|
|
case order_c::A:
|
|
|
|
{
|
|
|
|
// Verify that there's a unit in front to attack
|
2014-12-16 13:13:01 +00:00
|
|
|
uvector2 infront = pair.unit.getInFront();
|
|
|
|
|
|
|
|
// Check if there's any unit in front
|
|
|
|
// FRIENDLY FIRE IS ENABLED
|
|
|
|
for ( const OrderUnitPair& pair2 : m_OrderUnitPairs )
|
|
|
|
{
|
|
|
|
// if the unit is infront of our unit, then add it to the kill list
|
|
|
|
if( pair2.unit.getPos() == infront )
|
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
toKill.push_back(pair2.unit.getID());
|
2014-12-16 13:13:01 +00:00
|
|
|
pair.order = COrder();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case order_c::L:
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// Simply turn left
|
2014-12-16 13:13:01 +00:00
|
|
|
pair.unit.turnLeft();
|
|
|
|
pair.order = COrder();
|
|
|
|
}
|
|
|
|
break;
|
2014-12-16 13:13:01 +00:00
|
|
|
case order_c::R:
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// Simply turn right
|
2014-12-16 13:13:01 +00:00
|
|
|
pair.unit.turnRight();
|
|
|
|
pair.order = COrder();
|
|
|
|
}
|
2014-12-16 13:13:01 +00:00
|
|
|
break;
|
2014-12-16 13:13:02 +00:00
|
|
|
default:
|
|
|
|
break;
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
// Sort and erase all duplicates
|
|
|
|
std::sort( toKill.begin(), toKill.end() );
|
|
|
|
toKill.erase( std::unique( toKill.begin(), toKill.end() ), toKill.end() );
|
|
|
|
|
|
|
|
// Iterate through all kill orders
|
2014-12-16 13:13:01 +00:00
|
|
|
for ( auto id : toKill )
|
|
|
|
{
|
2014-12-16 13:13:01 +00:00
|
|
|
// Kill the units
|
|
|
|
for ( OrderUnitPairVector::iterator it = m_OrderUnitPairs.begin();
|
|
|
|
it != m_OrderUnitPairs.end();
|
|
|
|
it++ )
|
|
|
|
{
|
|
|
|
if( (*it).unit.getID() == id )
|
|
|
|
{
|
|
|
|
// Add the dead unit to our dead unit list
|
|
|
|
m_deadUnits.push_back(std::move((*it).unit));
|
|
|
|
|
|
|
|
// Remove the unit from our alive unit pairs
|
|
|
|
m_OrderUnitPairs.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 13:13:01 +00:00
|
|
|
|
|
|
|
}
|
2014-12-16 13:12:59 +00:00
|
|
|
|
|
|
|
// Clear all orders
|
2014-12-16 13:13:01 +00:00
|
|
|
for ( OrderUnitPair& pair : m_OrderUnitPairs )
|
|
|
|
{
|
|
|
|
pair.order = COrder();
|
|
|
|
}
|
2014-12-16 13:12:59 +00:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a unit, nonzero return value indicates error
|
2014-12-16 13:13:01 +00:00
|
|
|
int CTTRTSGame::AddUnit( CUnit&& unit )
|
2014-12-16 13:13:00 +00:00
|
|
|
{
|
|
|
|
// Verify the unit
|
2014-12-16 13:13:02 +00:00
|
|
|
if( !unit.valid() )
|
|
|
|
return 1;
|
2014-12-16 13:13:00 +00:00
|
|
|
|
|
|
|
// Verify if the unit can be placed on the current board
|
2014-12-16 13:13:01 +00:00
|
|
|
const uvector2 pos = unit.getPos();
|
2014-12-16 13:13:02 +00:00
|
|
|
if( (pos.x >= dimensions.x) || (pos.y >= dimensions.y) )
|
|
|
|
return 2;
|
2014-12-16 13:13:00 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// If any unit's position matches, reject this
|
2014-12-16 13:13:01 +00:00
|
|
|
for ( const OrderUnitPair& pair: m_OrderUnitPairs )
|
|
|
|
{
|
|
|
|
if( pair.unit.getPos() == unit.getPos() )
|
2014-12-16 13:13:02 +00:00
|
|
|
return 3;
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
2014-12-16 13:13:02 +00:00
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
// Add the unit with a blank order
|
|
|
|
m_OrderUnitPairs.push_back( OrderUnitPair(std::move(unit), COrder()) );
|
2014-12-16 13:13:00 +00:00
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-16 13:12:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a units, nonzero return value indicates error
|
2014-12-16 13:13:01 +00:00
|
|
|
int CTTRTSGame::AddUnits( CUnitVector&& units )
|
2014-12-16 13:13:00 +00:00
|
|
|
{
|
2014-12-16 13:13:01 +00:00
|
|
|
CUnitVector::iterator it;
|
2014-12-16 13:12:59 +00:00
|
|
|
|
2014-12-16 13:13:00 +00:00
|
|
|
for ( it = units.begin(); it != units.end(); it++ )
|
|
|
|
{
|
|
|
|
// Attempt the unit add
|
2014-12-16 13:13:01 +00:00
|
|
|
if ( AddUnit( std::move(*it) ) )
|
2014-12-16 13:13:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2014-12-16 13:12:59 +00:00
|
|
|
|
2014-12-16 13:13:00 +00:00
|
|
|
// All units added successfully
|
2014-12-16 13:12:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify any order
|
2014-12-16 13:13:01 +00:00
|
|
|
int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order ) const
|
2014-12-16 13:12:59 +00:00
|
|
|
{
|
2014-12-16 13:13:01 +00:00
|
|
|
int ret = 1;
|
|
|
|
|
2014-12-16 13:12:59 +00:00
|
|
|
// Grab the unit ID
|
|
|
|
const unit_id_t unitID = order.unit;
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
// Attempt to find the unit
|
|
|
|
for ( const OrderUnitPair& pair : m_OrderUnitPairs )
|
2014-12-16 13:12:59 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// Accept if we have the unit
|
2014-12-16 13:13:01 +00:00
|
|
|
if ( pair.unit.getID() == unitID )
|
2014-12-16 13:12:59 +00:00
|
|
|
{
|
2014-12-16 13:13:01 +00:00
|
|
|
ret = 0;
|
2014-12-16 13:12:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for now, as long as the unit exists we can attempt the order
|
2014-12-16 13:13:01 +00:00
|
|
|
return ret;
|
2014-12-16 13:13:00 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
// Get unit by unit ID
|
|
|
|
const CUnit& CTTRTSGame::GetUnitByIDConst( unit_id_t id ) const
|
|
|
|
{
|
|
|
|
for ( const OrderUnitPair& pair : m_OrderUnitPairs )
|
|
|
|
{
|
|
|
|
// Attempt the unit add
|
|
|
|
if ( pair.unit.getID() )
|
|
|
|
return pair.unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an invalid unit
|
|
|
|
static CUnit invalid_unit;
|
|
|
|
return invalid_unit;
|
|
|
|
}
|
2014-12-16 13:13:01 +00:00
|
|
|
|
|
|
|
// Get unit by unit ID
|
2014-12-16 13:13:01 +00:00
|
|
|
CUnit& CTTRTSGame::GetUnitByID( unit_id_t id )
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
2014-12-16 13:13:01 +00:00
|
|
|
for ( OrderUnitPair& pair : m_OrderUnitPairs )
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
|
|
|
// Attempt the unit add
|
2014-12-16 13:13:01 +00:00
|
|
|
if ( pair.unit.getID() )
|
|
|
|
return pair.unit;
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an invalid unit
|
|
|
|
static CUnit invalid_unit;
|
|
|
|
return invalid_unit;
|
|
|
|
}
|
2014-12-16 13:13:02 +00:00
|
|
|
|
|
|
|
// Check if we have a win state
|
|
|
|
Team CTTRTSGame::CheckForWin() const
|
|
|
|
{
|
|
|
|
// Array of units for each Team
|
|
|
|
unsigned int units[(int) Team::NUM_INVALID];
|
|
|
|
memset(units,0,sizeof(units));
|
|
|
|
|
|
|
|
// Count up all the units for each Team
|
|
|
|
for ( const OrderUnitPair& pair : m_OrderUnitPairs )
|
|
|
|
{
|
|
|
|
const int team = (int)pair.unit.getTeam();
|
|
|
|
units[team] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default winning Team to invalid (no win)
|
|
|
|
Team winningTeam = Team::NUM_INVALID;
|
|
|
|
|
|
|
|
// For each of the teams
|
|
|
|
for ( unsigned int i = 0; i < _countof(units); i++ )
|
|
|
|
{
|
|
|
|
// if there are still units in this Team, and the winning Team hasn't been set
|
|
|
|
if( units[i] > 0 && winningTeam == Team::NUM_INVALID )
|
|
|
|
{
|
|
|
|
winningTeam = (Team)i;
|
|
|
|
}
|
|
|
|
// Otherwise, if there are units in this Team and the winning Team HAS been set
|
|
|
|
else if ( units[i] > 0 )
|
|
|
|
{
|
|
|
|
// Set back to invalid and break out of the loop
|
|
|
|
winningTeam = Team::NUM_INVALID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return winningTeam;
|
|
|
|
}
|