Skip to content

Commit

Permalink
Implement resource locking
Browse files Browse the repository at this point in the history
  • Loading branch information
ogail committed Jan 17, 2014
1 parent 041b867 commit c276a7f
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 31 deletions.
Binary file modified Build/Debug/IStrategizerEx.cb
Binary file not shown.
3 changes: 2 additions & 1 deletion Include/IStrategizer/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace IStrategizer
virtual void InitializePreConditions() = 0;
virtual void OnSucccess(RtsGame& pRtsGame, const WorldClock& p_clock) {};
virtual void OnFailure(RtsGame& pRtsGame, const WorldClock& p_clock) {};
virtual void PreExecution(RtsGame& pRtsGame) {};

public:
int Type() const { return PlanStepEx::_stepTypeId; }
Expand All @@ -35,7 +36,7 @@ namespace IStrategizer
void Copy(IClonable* p_dest);
virtual bool Execute(RtsGame& pRtsGame, const WorldClock& p_clock);
virtual bool AliveConditionsSatisfied(RtsGame& pRtsGame) = 0;
bool PreconditionsSatisfied(RtsGame& pRtsGame) { if (_preCondition == nullptr) InitializeConditions(); return _preCondition->Evaluate(pRtsGame); }
bool PreconditionsSatisfied(RtsGame& pRtsGame);
};
}

Expand Down
1 change: 1 addition & 0 deletions Include/IStrategizer/BuildActionEx.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace IStrategizer
void HandleMessage(RtsGame& pRtsGame, Message* p_msg, bool& p_consumed);
void InitializePostConditions();
void InitializePreConditions();
void PreExecution(RtsGame& pRtsGame);

private:
TID _buildingId;
Expand Down
8 changes: 4 additions & 4 deletions Include/IStrategizer/EngineAssist.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ namespace IStrategizer
CheckReturn int GetPlayerAttribute(IN PlayerType p_playerType, IN PlayerAttribute p_attribute, OUT int& p_value);
CheckReturn int GetTireBaseBuildingId(IN PlayerType p_playerType, IN BaseType p_baseType, OUT EntityClassType& p_entityClassId);
CheckReturn int GetEntities(IN PlayerType p_playerType, IN const vector<EntityClassType>& p_entityTypes, OUT vector<TID>& p_entityObjects);

CheckReturn int ResearchesDone(IN const vector<ResearchType> &p_researchTypes, OUT bool &p_done, IN PlayerType p_playerType = PLAYER_Self);
CheckReturn int PrerequisitesSatisfied(IN int p_entityOrResearchType, OUT bool &p_satisfied, IN PlayerType p_playerType = PLAYER_Self);
CheckReturn bool DoesEntityClassExist(IN pair<EntityClassType, unsigned> p_entityType, IN PlayerType p_playerType = PLAYER_Self);
CheckReturn bool DoesEntityClassExist(IN const std::map<EntityClassType, unsigned> &p_entityTypes, IN PlayerType p_playerType = PLAYER_Self);
CheckReturn bool DoesEntityObjectExist(IN TID p_entityObject, IN PlayerType p_playerType = PLAYER_Self);
CheckReturn bool DoesEntityObjectExist(IN const vector<TID> &p_entityObjects, IN PlayerType p_playerType = PLAYER_Self);
CheckReturn int ResearchesDone(IN const vector<ResearchType> &p_researchTypes, OUT bool &p_done, IN PlayerType p_playerType = PLAYER_Self);
CheckReturn int PrerequisitesSatisfied(IN int p_entityOrResearchType, OUT bool &p_satisfied, IN PlayerType p_playerType = PLAYER_Self);
CheckReturn bool IsEntityCloseToPoint(IN const TID p_entityId, IN const Vector2& p_point, IN const unsigned p_maxDistance);
CheckReturn void GetPrerequisites(int p_entityOrResearchType, PlayerType p_playerType, vector<Expression*>& p_prerequisites);
CheckReturn void GetPrerequisites(IN int p_entityOrResearchType, IN PlayerType p_playerType, OUT vector<Expression*>& p_prerequisites);
CheckReturn void ControlResource(IN int p_entityOrResearchType, IN PlayerType p_playerType, IN bool lock);

#define g_Assist EngineAssist::Instance()
};
Expand Down
11 changes: 11 additions & 0 deletions Include/IStrategizer/PlayerResources.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ namespace IStrategizer
class PlayerResources
{
public:
void Lock(ResourceType p_resourceId, int p_amount);
void Unlock(ResourceType p_resourceId, int p_amount);
int AvailableSupply() { return (Supply() - _lockedSupply); }
int AvailablePrimary() { return (Primary() - _lockedPrimary); }
int AvailableSecondary() { return (Secondary() - _lockedSecondary); }
PlayerResources() : _lockedPrimary(0), _lockedSecondary(0), _lockedSupply(0) { }
virtual ~PlayerResources() {}
virtual bool HasEnough(const WorldResources* p_resources);
virtual int Supply() = 0;
virtual int Secondary() = 0;
virtual int Primary() = 0;

private:
int _lockedSupply;
int _lockedSecondary;
int _lockedPrimary;
};
}

Expand Down
1 change: 1 addition & 0 deletions Include/IStrategizer/ResearchAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace IStrategizer

protected:
bool ExecuteAux(RtsGame& pRtsGame, const WorldClock& p_clock);
void PreExecution(RtsGame& pRtsGame);
void InitializePostConditions();
void InitializePreConditions();

Expand Down
1 change: 1 addition & 0 deletions Include/IStrategizer/TrainAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace IStrategizer
void HandleMessage(RtsGame& pRtsGame, Message* p_msg, bool& p_consumed);
void InitializePostConditions();
void InitializePreConditions();
void PreExecution(RtsGame& pRtsGame);

private:
TID _trainerId;
Expand Down
9 changes: 9 additions & 0 deletions Source/IStrategizer/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ void Action::State(ExecutionStateType p_state, RtsGame& pRtsGame, const WorldClo
break;
}
}
bool Action::PreconditionsSatisfied(RtsGame& pRtsGame)
{
if (_preCondition == nullptr) { InitializeConditions(); }
bool satisfied = _preCondition->Evaluate(pRtsGame);

if (satisfied) { PreExecution(pRtsGame); }

This comment has been minimized.

Copy link
@MHesham

MHesham Jan 18, 2014

I expected from the name PreExecution that this method is executed by the base before ExecuteAux, now it is more confusing to me.
Can you explain the design more?


return satisfied;
}
//////////////////////////////////////////////////////////////////////////
void Action::InitializeConditions()
{
Expand Down
44 changes: 24 additions & 20 deletions Source/IStrategizer/BuildActionEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "GameEntity.h"
#include "AdapterEx.h"
#include "EntityClassExist.h"
#include "PlayerResources.h"

using namespace IStrategizer;

Expand Down Expand Up @@ -66,10 +67,10 @@ void BuildActionEx::HandleMessage(RtsGame& pRtsGame, Message* p_msg, bool& p_con
{
if(PlanStepEx::State() == ESTATE_Executing && p_msg->MessageTypeID() == MSG_EntityCreate)
{
EntityCreateMessage* pMsg = static_cast<EntityCreateMessage*>(p_msg);
TID buildingId;
GameEntity *pGameBuilding;
Vector2 msgBuildPosition;
EntityCreateMessage* pMsg = static_cast<EntityCreateMessage*>(p_msg);
TID buildingId;
GameEntity *pGameBuilding;
Vector2 msgBuildPosition;

if (pMsg->Data()->OwnerId != PLAYER_Self)
return;
Expand All @@ -89,24 +90,26 @@ void BuildActionEx::HandleMessage(RtsGame& pRtsGame, Message* p_msg, bool& p_con
{
_buildingId = pGameBuilding->Id();
_buildStarted = true;
g_Assist.ControlResource(_params[PARAM_EntityClassId], PLAYER_Self, false);
}
}
}
//////////////////////////////////////////////////////////////////////////
bool BuildActionEx::AliveConditionsSatisfied(RtsGame& pRtsGame)
{
bool builderExist = false;
bool buildingExist = false;
bool isBuilderConstructing = false;
bool success = false;
GameEntity *pEntity = nullptr;
bool builderExist = false;
bool buildingExist = false;
bool isBuilderConstructing = false;
bool success = false;
GameEntity *pEntity = nullptr;

assert(PlanStepEx::State() == ESTATE_Executing);

builderExist = g_Assist.DoesEntityObjectExist(_builderId);

if (builderExist)
{
success = true;
pEntity = pRtsGame.Self()->GetEntity(_builderId);

assert(pEntity);
Expand All @@ -122,15 +125,11 @@ bool BuildActionEx::AliveConditionsSatisfied(RtsGame& pRtsGame)
{
buildingExist = g_Assist.DoesEntityObjectExist(_buildingId);

if (buildingExist)
if (!buildingExist)
{
success = true;
success = false;
}
}
else
{
success = true;
}
}
}
else
Expand Down Expand Up @@ -161,13 +160,13 @@ bool BuildActionEx::SuccessConditionsSatisfied(RtsGame& pRtsGame)
//////////////////////////////////////////////////////////////////////////
bool BuildActionEx::ExecuteAux(RtsGame& pRtsGame, const WorldClock& p_clock)
{
EntityClassType buildingType;
GameEntity *pGameBuilder;
AbstractAdapter *pAdapter = g_OnlineCaseBasedPlanner->Reasoner()->Adapter();
bool bOk = false;
EntityClassType buildingType;
GameEntity *pGameBuilder;
AbstractAdapter *pAdapter = g_OnlineCaseBasedPlanner->Reasoner()->Adapter();
bool bOk = false;

// Adapt builder
_builderId = pAdapter->GetEntityObjectId(g_Game->Self()->GetWorkerType(),AdapterEx::WorkerStatesRankVector);
_builderId = pAdapter->GetEntityObjectId(pRtsGame.Self()->GetWorkerType(),AdapterEx::WorkerStatesRankVector);

if (_builderId != INVALID_TID)
{
Expand Down Expand Up @@ -214,4 +213,9 @@ void BuildActionEx::InitializePreConditions()
m_terms.push_back(new EntityClassExist(PLAYER_Self, builderType, 1, true));
g_Assist.GetPrerequisites(buildingType, PLAYER_Self, m_terms);
_preCondition = new And(m_terms);
}
//----------------------------------------------------------------------------------------------
void BuildActionEx::PreExecution(RtsGame& pRtsGame)
{
g_Assist.ControlResource(_params[PARAM_EntityClassId], PLAYER_Self, true);
}
48 changes: 45 additions & 3 deletions Source/IStrategizer/EngineAssist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ int EngineAssist::GetResourceAmount(PlayerType p_playerIndex, ResourceType p_res
switch(p_resourceId)
{
case RESOURCE_Supply:
p_availableAmount = m_resources->Supply();
p_availableAmount = m_resources->AvailableSupply();
break;
case RESOURCE_Primary:
p_availableAmount = m_resources->Primary();
p_availableAmount = m_resources->AvailablePrimary();
break;
case RESOURCE_Secondary:
p_availableAmount = m_resources->Secondary();
p_availableAmount = m_resources->AvailableSecondary();
break;
default:
return ERR_InvalidParameterValue;
Expand Down Expand Up @@ -622,4 +622,46 @@ void EngineAssist::GetPrerequisites(int p_entityOrResearchType, PlayerType p_pla
p_prerequisites.push_back(new ResourceExist(p_playerType, RESOURCE_Primary, pReqResources->Primary()));
p_prerequisites.push_back(new ResourceExist(p_playerType, RESOURCE_Secondary, pReqResources->Secondary()));
p_prerequisites.push_back(new ResourceExist(p_playerType, RESOURCE_Supply, pReqResources->Supply()));
}
//------------------------------------------------------------------------------------------------------------------------------------------------
void EngineAssist::ControlResource(int p_entityOrResearchType, PlayerType p_playerType, bool lock)

This comment has been minimized.

Copy link
@MHesham

MHesham Jan 18, 2014

Lets write more comments and document our code, we are 5 working on the same source code now.
What on earth is this method doing? It seems to doing a good magic

{
GamePlayer *pPlayer = nullptr;
GameType *pEntityType = nullptr;
GameResearch *pResearchType = nullptr;
WorldResources *pReqResources = nullptr;

pPlayer = g_Game->GetPlayer(p_playerType);
assert(pPlayer);

if (BELONG(ResearchType, p_entityOrResearchType))
{
pResearchType = g_Game->GetResearch((ResearchType)p_entityOrResearchType);
assert(pResearchType);

pReqResources = pResearchType->RequiredResources();
assert(pReqResources);
}
else if (BELONG(EntityClassType, p_entityOrResearchType))
{
pEntityType = g_Game->GetEntityType((EntityClassType)p_entityOrResearchType);
assert(pEntityType);

pReqResources = pEntityType->RequiredResources();
assert(pReqResources);
}
else assert(0);

This comment has been minimized.

Copy link
@MHesham

MHesham Mar 4, 2014

_ASSERTE(!"Not supported type");

use _ASSERTE instead of assert in the engine
dont assert on 0 ever, it is a good practice to use a string expression like the one above to give more useful information to developer about the problem

This comment has been minimized.

Copy link
@ogail

ogail Mar 4, 2014

Author Owner

The assert(0) pattern is repeated in the code in many places I was just following the convention. I've filed RtsAiResearch#107 to track this

This comment has been minimized.

Copy link
@MHesham

MHesham Mar 4, 2014

assert(0) causes problems in debugging, stay away from it. Make sure to replace it in this CR. And whoever touch a file that contains assert replace it with _ASSERT


if (lock)
{
pPlayer->Resources()->Lock(RESOURCE_Primary, pReqResources->Primary());
pPlayer->Resources()->Lock(RESOURCE_Secondary, pReqResources->Secondary());
pPlayer->Resources()->Lock(RESOURCE_Supply, pReqResources->Supply());
}
else
{
pPlayer->Resources()->Unlock(RESOURCE_Primary, pReqResources->Primary());
pPlayer->Resources()->Unlock(RESOURCE_Secondary, pReqResources->Secondary());
pPlayer->Resources()->Unlock(RESOURCE_Supply, pReqResources->Supply());
}
}
32 changes: 32 additions & 0 deletions Source/IStrategizer/PlayerResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,35 @@ bool PlayerResources::HasEnough(const WorldResources* p_resources)
this->Secondary() >= p_resources->Secondary() &&
this->Supply() >= p_resources->Supply();
}
//////////////////////////////////////////////////////////////////////////
void PlayerResources::Lock(ResourceType p_resourceId, int p_amount)
{
switch(p_resourceId)
{
case RESOURCE_Supply:
_lockedSupply += p_amount;
break;
case RESOURCE_Primary:
_lockedPrimary += p_amount;
break;
case RESOURCE_Secondary:
_lockedSecondary += p_amount;
break;
}
}
//////////////////////////////////////////////////////////////////////////
void PlayerResources::Unlock(ResourceType p_resourceId, int p_amount)
{
switch(p_resourceId)
{
case RESOURCE_Supply:
_lockedSupply -= p_amount;
break;
case RESOURCE_Primary:
_lockedPrimary -= p_amount;
break;
case RESOURCE_Secondary:
_lockedSecondary -= p_amount;
break;
}
}
12 changes: 9 additions & 3 deletions Source/IStrategizer/ResearchAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ bool ResearchAction::SuccessConditionsSatisfied(RtsGame& pRtsGame)
//----------------------------------------------------------------------------------------------
bool ResearchAction::ExecuteAux(RtsGame& pRtsGame, const WorldClock& p_clock)
{
ResearchType researchType = (ResearchType)_params[PARAM_ResearchId];
GameEntity *pGameResearcher;
AbstractAdapter *pAdapter = g_OnlineCaseBasedPlanner->Reasoner()->Adapter();
ResearchType researchType = (ResearchType)_params[PARAM_ResearchId];
GameEntity *pGameResearcher;
AbstractAdapter *pAdapter = g_OnlineCaseBasedPlanner->Reasoner()->Adapter();

// Adapt researcher
_researcherId = pAdapter->AdaptBuildingForResearch(researchType);
Expand All @@ -57,6 +57,7 @@ bool ResearchAction::ExecuteAux(RtsGame& pRtsGame, const WorldClock& p_clock)
pGameResearcher = pRtsGame.Self()->GetEntity(_researcherId);
assert(pGameResearcher);

g_Assist.ControlResource(researchType, PLAYER_Self, false);

This comment has been minimized.

Copy link
@MHesham

MHesham Feb 9, 2014

class GameResources : public SharedResource

PreConditions Satisfied:
GameResources entityRequirements =
GameResources::FromEntityRequiredResources(_params[PARAM_ResearchId])
.....
Execute:
entityRequirements.Lock(this);
pGrameResearcher-Research(researchType);

Fail/Success
entityRequirements.Unlock(this);

return pGameResearcher->Research(researchType);
}
//----------------------------------------------------------------------------------------------
Expand All @@ -77,3 +78,8 @@ void ResearchAction::InitializePreConditions()
g_Assist.GetPrerequisites(researchType, PLAYER_Self, m_terms);
_preCondition = new And(m_terms);
}
//----------------------------------------------------------------------------------------------
void ResearchAction::PreExecution(RtsGame& pRtsGame)
{
g_Assist.ControlResource(_params[PARAM_ResearchId], PLAYER_Self, true);
}
6 changes: 6 additions & 0 deletions Source/IStrategizer/TrainAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void TrainAction::HandleMessage(RtsGame& pRtsGame, Message* p_msg, bool& p_consu
{
_trainStarted = true;
_traineeId = entityId;
g_Assist.ControlResource(_params[PARAM_EntityClassId], PLAYER_Self, false);
}
}
}
Expand Down Expand Up @@ -179,4 +180,9 @@ void TrainAction::InitializePreConditions()
m_terms.push_back(new EntityClassExist(PLAYER_Self, trainerType, 1, true));
g_Assist.GetPrerequisites(traineeType, PLAYER_Self, m_terms);
_preCondition = new And(m_terms);
}
//----------------------------------------------------------------------------------------------
void TrainAction::PreExecution(RtsGame& pRtsGame)
{
g_Assist.ControlResource(_params[PARAM_EntityClassId], PLAYER_Self, true);
}

0 comments on commit c276a7f

Please sign in to comment.