[*] Limit the count of AI forces available through Lua functions to 25.

The forces from 25 to 49 are used to hold attacking forces.
This fixes a situation when user couldn't use forces with bigger indexes because they were used to hold attacking force
This commit is contained in:
cybermind 2013-05-12 18:17:20 +06:00
parent 71f78c119e
commit a5d5580115
3 changed files with 18 additions and 17 deletions

View file

@ -448,14 +448,14 @@ void AiForce::ReturnToHome()
AiForceManager::AiForceManager()
{
forces.resize(3);
forces.resize(AI_MAX_FORCES);
memset(script, -1, AI_MAX_FORCES * sizeof(char));
}
unsigned int AiForceManager::FindFreeForce(AiForceRole role)
unsigned int AiForceManager::FindFreeForce(AiForceRole role, int begin)
{
/* find free force */
unsigned int f = 0;
unsigned int f = begin;
while (f < forces.size() && (forces[f].State > AiForceAttackingState_Free)) {
++f;
};
@ -591,7 +591,7 @@ void AiAttackWithForceAt(unsigned int force, int x, int y)
{
const Vec2i pos(x, y);
if (!(force < AI_MAX_FORCES)) {
if (!(force < AI_MAX_FORCE_INTERNAL)) {
DebugPrint("Force out of range: %d" _C_ force);
return ;
}
@ -611,7 +611,7 @@ void AiAttackWithForceAt(unsigned int force, int x, int y)
*/
void AiAttackWithForce(unsigned int force)
{
if (!(force < AI_MAX_FORCES)) {
if (!(force < AI_MAX_FORCE_INTERNAL)) {
DebugPrint("Force out of range: %d" _C_ force);
return ;
}
@ -620,7 +620,7 @@ void AiAttackWithForce(unsigned int force)
// the first force, so we can reuse it
if (!AiPlayer->Force[force].Defending) {
unsigned int top;
unsigned int f = AiPlayer->Force.FindFreeForce();
unsigned int f = AiPlayer->Force.FindFreeForce(AiForceRoleDefault, AI_MAX_FORCE_INTERNAL);
AiPlayer->Force[f].Reset();
AiPlayer->Force[f].Role = AiPlayer->Force[force].Role;
@ -658,7 +658,7 @@ void AiAttackWithForces(int *forces)
const Vec2i invalidPos(-1, -1);
bool found = false;
unsigned int top;
unsigned int f = AiPlayer->Force.FindFreeForce();
unsigned int f = AiPlayer->Force.FindFreeForce(AiForceRoleDefault, AI_MAX_FORCE_INTERNAL);
AiPlayer->Force[f].Reset();

View file

@ -186,7 +186,8 @@ public:
};
// forces
#define AI_MAX_FORCES 50 /// How many forces are supported
#define AI_MAX_FORCES 50 /// How many forces are supported
#define AI_MAX_FORCE_INTERNAL (AI_MAX_FORCES / 2) /// The forces after AI_MAX_FORCE_INTERNAL are for internal use
/**
** AI force manager.
@ -222,7 +223,7 @@ public:
void RemoveDeadUnit();
bool Assign(CUnit &unit);
void Update();
unsigned int FindFreeForce(AiForceRole role = AiForceRoleDefault);
unsigned int FindFreeForce(AiForceRole role = AiForceRoleDefault, int begin = 0);
void CheckUnits(int *counter);
private:
std::vector<AiForce> forces;

View file

@ -692,7 +692,7 @@ static int CclAiForce(lua_State *l)
resetForce = LuaToBoolean(l, 3);
}
int force = LuaToNumber(l, 1);
if (force < 0 || force >= AI_MAX_FORCES) {
if (force < 0 || force >= AI_MAX_FORCE_INTERNAL) {
LuaError(l, "Force out of range: %d" _C_ force);
}
AiForce &aiforce = AiPlayer->Force[AiPlayer->Force.getScriptForce(force)];
@ -762,7 +762,7 @@ static int CclAiForceRole(lua_State *l)
{
LuaCheckArgs(l, 2);
int force = LuaToNumber(l, 1);
if (force < 0 || force >= AI_MAX_FORCES) {
if (force < 0 || force >= AI_MAX_FORCE_INTERNAL) {
LuaError(l, "Force %i out of range" _C_ force);
}
@ -789,7 +789,7 @@ static int CclAiCheckForce(lua_State *l)
{
LuaCheckArgs(l, 1);
int force = LuaToNumber(l, 1);
if (force < 0 || force >= AI_MAX_FORCES) {
if (force < 0 || force >= AI_MAX_FORCE_INTERNAL) {
lua_pushfstring(l, "Force out of range: %d", force);
}
if (AiPlayer->Force[AiPlayer->Force.getScriptForce(force)].Completed) {
@ -809,7 +809,7 @@ static int CclAiWaitForce(lua_State *l)
{
LuaCheckArgs(l, 1);
int force = LuaToNumber(l, 1);
if (force < 0 || force >= AI_MAX_FORCES) {
if (force < 0 || force >= AI_MAX_FORCE_INTERNAL) {
lua_pushfstring(l, "Force out of range: %d", force);
}
if (AiPlayer->Force[AiPlayer->Force.getScriptForce(force)].Completed) {
@ -834,7 +834,7 @@ static int CclAiAttackWithForce(lua_State *l)
{
LuaCheckArgs(l, 1);
int force = LuaToNumber(l, 1);
if (force < 0 || force >= AI_MAX_FORCES) {
if (force < 0 || force >= AI_MAX_FORCE_INTERNAL) {
LuaError(l, "Force out of range: %d" _C_ force);
}
AiAttackWithForce(AiPlayer->Force.getScriptForce(force));
@ -857,7 +857,7 @@ static int CclAiWaitForces(lua_State *l)
for (int i = 0; i < args; ++i) {
const int force = LuaToNumber(l, 1, i + 1);
if (force < 0 || force >= AI_MAX_FORCES) {
if (force < 0 || force >= AI_MAX_FORCE_INTERNAL) {
lua_pushfstring(l, "Force out of range: %d", force);
}
if (!AiPlayer->Force[AiPlayer->Force.getScriptForce(force)].Completed) {
@ -876,7 +876,7 @@ static int CclAiWaitForces(lua_State *l)
*/
static int CclAiAttackWithForces(lua_State *l)
{
int Forces[AI_MAX_FORCES + 1];
int Forces[AI_MAX_FORCE_INTERNAL + 1];
LuaCheckArgs(l, 1);
if (!lua_istable(l, 1)) {
@ -886,7 +886,7 @@ static int CclAiAttackWithForces(lua_State *l)
for (int i = 0; i < args; ++i) {
const int force = LuaToNumber(l, 1, i + 1);
if (force < 0 || force >= AI_MAX_FORCES) {
if (force < 0 || force >= AI_MAX_FORCE_INTERNAL) {
lua_pushfstring(l, "Force out of range: %d", force);
}
Forces[i] = AiPlayer->Force.getScriptForce(force);