Skip to content

Commit

Permalink
Merge pull request #543 from Wargus/clean_up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 authored Oct 17, 2023
2 parents c23bc26 + 0fac336 commit 4ee1b1d
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 143 deletions.
32 changes: 16 additions & 16 deletions src/ai/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ void SaveAi(CFile &file)
*/
void AiInit(CPlayer &player)
{
PlayerAi *pai = new PlayerAi;
auto pai = std::make_unique<PlayerAi>();

pai->Player = &player;

Expand Down Expand Up @@ -468,7 +468,7 @@ void AiInit(CPlayer &player)
pai->Collect[WoodCost] = 50;
pai->Collect[OilCost] = 0;

player.Ai = pai;
player.Ai = std::move(pai);
}

/**
Expand All @@ -486,7 +486,6 @@ void InitAiModule()
void CleanAi()
{
for (int p = 0; p < PlayerMax; ++p) {
delete Players[p].Ai;
Players[p].Ai = nullptr;
}
}
Expand Down Expand Up @@ -527,15 +526,15 @@ void FreeAi()
** @param type Unit-type which is now available.
** @return True, if unit-type was found in list.
*/
static bool AiRemoveFromBuilt2(PlayerAi *pai, const CUnitType &type)
static bool AiRemoveFromBuilt2(PlayerAi &pai, const CUnitType &type)
{
auto it = ranges::find_if(pai->UnitTypeBuilt,
auto it = ranges::find_if(pai.UnitTypeBuilt,
[&](const AiBuildQueue &q) { return q.Made && q.Type == &type; });
if (it != pai->UnitTypeBuilt.end())
if (it != pai.UnitTypeBuilt.end())
{
--(*it).Made;
if (!--(*it).Want) {
pai->UnitTypeBuilt.erase(it);
pai.UnitTypeBuilt.erase(it);
}
return true;
}
Expand All @@ -548,7 +547,7 @@ static bool AiRemoveFromBuilt2(PlayerAi *pai, const CUnitType &type)
** @param pai Computer AI player.
** @param type Unit-type which is now available.
*/
static void AiRemoveFromBuilt(PlayerAi *pai, const CUnitType &type)
static void AiRemoveFromBuilt(PlayerAi &pai, const CUnitType &type)
{
if (AiRemoveFromBuilt2(pai, type)) {
return;
Expand All @@ -562,7 +561,7 @@ static void AiRemoveFromBuilt(PlayerAi *pai, const CUnitType &type)
return;
}
}
if (pai->Player == ThisPlayer) {
if (pai.Player == ThisPlayer) {
DebugPrint("My guess is that you built something under ai me. naughty boy!\n");
return;
}
Expand Down Expand Up @@ -776,7 +775,8 @@ void AiWorkComplete(CUnit *unit, CUnit &what)
}

Assert(what.Player->Type != PlayerTypes::PlayerPerson);
AiRemoveFromBuilt(what.Player->Ai, *what.Type);
Assert(what.Player->Ai);
AiRemoveFromBuilt(*what.Player->Ai, *what.Type);
}

/**
Expand Down Expand Up @@ -821,7 +821,7 @@ static void AiMoveUnitInTheWay(CUnit &unit)
Vec2i movablepos[16];
int movablenb;

AiPlayer = unit.Player->Ai;
AiPlayer = unit.Player->Ai.get();

// No more than 1 move per 10 cycle ( avoid stressing the pathfinder )
if (GameCycle <= AiPlayer->LastCanNotMoveGameCycle + 10) {
Expand Down Expand Up @@ -924,7 +924,7 @@ void AiCanNotMove(CUnit &unit)
const int gw = unit.pathFinderData->input.GetGoalSize().x;
const int gh = unit.pathFinderData->input.GetGoalSize().y;

AiPlayer = unit.Player->Ai;
AiPlayer = unit.Player->Ai.get();
if (PlaceReachable(unit, goalPos, gw, gh, 0, 255, false)) {
// Path probably closed by unit here
AiMoveUnitInTheWay(unit);
Expand Down Expand Up @@ -959,8 +959,8 @@ void AiTrainingComplete(CUnit &unit, CUnit &what)
unit.tilePos.y);

Assert(unit.Player->Type != PlayerTypes::PlayerPerson);

AiRemoveFromBuilt(unit.Player->Ai, *what.Type);
Assert(unit.Player->Ai);
AiRemoveFromBuilt(*unit.Player->Ai, *what.Type);

unit.Player->Ai->Force.RemoveDeadUnit();
unit.Player->Ai->Force.Assign(what);
Expand Down Expand Up @@ -1013,7 +1013,7 @@ void AiResearchComplete(CUnit &unit, const CUpgrade *what)
*/
void AiEachCycle(CPlayer &player)
{
AiPlayer = player.Ai;
AiPlayer = player.Ai.get();
}

/**
Expand All @@ -1023,7 +1023,7 @@ void AiEachCycle(CPlayer &player)
*/
void AiEachSecond(CPlayer &player)
{
AiPlayer = player.Ai;
AiPlayer = player.Ai.get();
#ifdef DEBUG
if (!AiPlayer) {
return;
Expand Down
75 changes: 38 additions & 37 deletions src/ai/script_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ static int CclAiDump(lua_State *l)
** @param l Lua state.
** @param ai PlayerAi pointer which should be filled with the data.
*/
static void CclParseBuildQueue(lua_State *l, PlayerAi *ai, int offset)
static void CclParseBuildQueue(lua_State *l, PlayerAi &ai, int offset)
{
if (!lua_istable(l, offset)) {
LuaError(l, "incorrect argument");
Expand Down Expand Up @@ -1602,7 +1602,7 @@ static void CclParseBuildQueue(lua_State *l, PlayerAi *ai, int offset)
queue.Made = made;
queue.Pos = pos;

ai->UnitTypeBuilt.push_back(queue);
ai.UnitTypeBuilt.push_back(queue);
pos.x = -1;
pos.y = -1;
}
Expand All @@ -1619,12 +1619,13 @@ static int CclDefineAiPlayer(lua_State *l)
const unsigned int playerIdx = LuaToNumber(l, 0 + 1);

Assert(playerIdx <= PlayerMax);
DebugPrint("%p %d\n", (void *)Players[playerIdx].Ai, Players[playerIdx].AiEnabled);
DebugPrint("%p %d\n", (void *)Players[playerIdx].Ai.get(), Players[playerIdx].AiEnabled);
// FIXME: lose this:
// Assert(!Players[playerIdx].Ai && Players[playerIdx].AiEnabled);

PlayerAi *ai = Players[playerIdx].Ai = new PlayerAi;
ai->Player = &Players[playerIdx];
Players[playerIdx].Ai = std::unique_ptr<PlayerAi>();
PlayerAi &ai = *Players[playerIdx].Ai;
ai.Player = &Players[playerIdx];

// Parse the list: (still everything could be changed!)
const int args = lua_gettop(l);
Expand All @@ -1638,43 +1639,43 @@ static int CclDefineAiPlayer(lua_State *l)
if (ait == nullptr) {
LuaError(l, "ai-type not found: %s", aiName.data());
}
ai->AiType = ait;
ai->Script = ait->Script;
ai.AiType = ait;
ai.Script = ait->Script;
} else if (value == "script") {
ai->Script = LuaToString(l, j + 1);
ai.Script = LuaToString(l, j + 1);
} else if (value == "script-debug") {
ai->ScriptDebug = LuaToBoolean(l, j + 1);
ai.ScriptDebug = LuaToBoolean(l, j + 1);
} else if (value == "sleep-cycles") {
ai->SleepCycles = LuaToNumber(l, j + 1);
ai.SleepCycles = LuaToNumber(l, j + 1);
} else if (value == "force") {
if (!lua_istable(l, j + 1)) {
LuaError(l, "incorrect argument");
}
const int subargs = lua_rawlen(l, j + 1);
[[maybe_unused]]const int cclforceIdx = LuaToNumber(l, j + 1, 1);
const int forceIdx = ai->Force.FindFreeForce(AiForceRole::Default);
const int forceIdx = ai.Force.FindFreeForce(AiForceRole::Default);

for (int k = 1; k < subargs; ++k) {
std::string_view value = LuaToString(l, j + 1, k + 1);
++k;
if (value == "complete") {
ai->Force[forceIdx].Completed = true;
ai.Force[forceIdx].Completed = true;
--k;
} else if (value == "recruit") {
ai->Force[forceIdx].Completed = false;
ai.Force[forceIdx].Completed = false;
--k;
} else if (value == "attack") {
ai->Force[forceIdx].Attacking = true;
ai.Force[forceIdx].Attacking = true;
--k;
} else if (value == "defend") {
ai->Force[forceIdx].Defending = true;
ai.Force[forceIdx].Defending = true;
--k;
} else if (value == "role") {
value = LuaToString(l, j + 1, k + 1);
if (value == "attack") {
ai->Force[forceIdx].Role = AiForceRole::Attack;
ai.Force[forceIdx].Role = AiForceRole::Attack;
} else if (value == "defend") {
ai->Force[forceIdx].Role = AiForceRole::Defend;
ai.Force[forceIdx].Role = AiForceRole::Defend;
} else {
LuaError(l, "Unsupported force tag: %s", value.data());
}
Expand All @@ -1692,7 +1693,7 @@ static int CclDefineAiPlayer(lua_State *l)

queue.Want = num;
queue.Type = &UnitTypeByIdent(ident);
ai->Force[forceIdx].UnitTypes.push_back(queue);
ai.Force[forceIdx].UnitTypes.push_back(queue);
}
lua_pop(l, 1);
} else if (value == "units") {
Expand All @@ -1707,15 +1708,15 @@ static int CclDefineAiPlayer(lua_State *l)
#if 0
[[maybe_unused]]const std::string_view ident = LuaToString(l, -1, subk + 1);
#endif
ai->Force[forceIdx].Units.push_back(&UnitManager->GetSlotUnit(num));
ai.Force[forceIdx].Units.push_back(&UnitManager->GetSlotUnit(num));
}
lua_pop(l, 1);
} else if (value == "state") {
ai->Force[forceIdx].State = AiForceAttackingState(LuaToNumber(l, j + 1, k + 1));
ai.Force[forceIdx].State = AiForceAttackingState(LuaToNumber(l, j + 1, k + 1));
} else if (value == "goalx") {
ai->Force[forceIdx].GoalPos.x = LuaToNumber(l, j + 1, k + 1);
ai.Force[forceIdx].GoalPos.x = LuaToNumber(l, j + 1, k + 1);
} else if (value == "goaly") {
ai->Force[forceIdx].GoalPos.y = LuaToNumber(l, j + 1, k + 1);
ai.Force[forceIdx].GoalPos.y = LuaToNumber(l, j + 1, k + 1);
} else if (value == "must-transport") {
// Keep for backward compatibility
} else {
Expand All @@ -1732,7 +1733,7 @@ static int CclDefineAiPlayer(lua_State *l)
++k;
int num = LuaToNumber(l, j + 1, k + 1);
const int resId = GetResourceIdByName(l, type);
ai->Reserve[resId] = num;
ai.Reserve[resId] = num;
}
} else if (value == "used") {
if (!lua_istable(l, j + 1)) {
Expand All @@ -1744,7 +1745,7 @@ static int CclDefineAiPlayer(lua_State *l)
++k;
const int num = LuaToNumber(l, j + 1, k + 1);
const int resId = GetResourceIdByName(l, type);
ai->Used[resId] = num;
ai.Used[resId] = num;
}
} else if (value == "needed") {
if (!lua_istable(l, j + 1)) {
Expand All @@ -1756,7 +1757,7 @@ static int CclDefineAiPlayer(lua_State *l)
++k;
const int num = LuaToNumber(l, j + 1, k + 1);
const int resId = GetResourceIdByName(l, type);
ai->Needed[resId] = num;
ai.Needed[resId] = num;
}
} else if (value == "collect") {
if (!lua_istable(l, j + 1)) {
Expand All @@ -1768,7 +1769,7 @@ static int CclDefineAiPlayer(lua_State *l)
++k;
const int num = LuaToNumber(l, j + 1, k + 1);
const int resId = GetResourceIdByName(l, type);
ai->Collect[resId] = num;
ai.Collect[resId] = num;
}
} else if (value == "need-mask") {
if (!lua_istable(l, j + 1)) {
Expand All @@ -1778,10 +1779,10 @@ static int CclDefineAiPlayer(lua_State *l)
for (int k = 0; k < subargs; ++k) {
const std::string_view type = LuaToString(l, j + 1, k + 1);
const int resId = GetResourceIdByName(l, type);
ai->NeededMask |= (1 << resId);
ai.NeededMask |= (1 << resId);
}
} else if (value == "need-supply") {
ai->NeedSupply = true;
ai.NeedSupply = true;
--j;
} else if (value == "exploration") {
if (!lua_istable(l, j + 1)) {
Expand All @@ -1800,27 +1801,27 @@ static int CclDefineAiPlayer(lua_State *l)
const int mask = LuaToNumber(l, -1, 3);
lua_pop(l, 1);
AiExplorationRequest queue(pos, mask);
ai->FirstExplorationRequest.push_back(queue);
ai.FirstExplorationRequest.push_back(queue);
}
} else if (value == "last-exploration-cycle") {
ai->LastExplorationGameCycle = LuaToNumber(l, j + 1);
ai.LastExplorationGameCycle = LuaToNumber(l, j + 1);
} else if (value == "last-can-not-move-cycle") {
ai->LastCanNotMoveGameCycle = LuaToNumber(l, j + 1);
ai.LastCanNotMoveGameCycle = LuaToNumber(l, j + 1);
} else if (value == "unit-type") {
if (!lua_istable(l, j + 1)) {
LuaError(l, "incorrect argument");
}
const int subargs = lua_rawlen(l, j + 1);
int i = 0;
if (subargs) {
ai->UnitTypeRequests.resize(subargs / 2);
ai.UnitTypeRequests.resize(subargs / 2);
}
for (int k = 0; k < subargs; ++k) {
const std::string_view ident = LuaToString(l, j + 1, k + 1);
++k;
const int count = LuaToNumber(l, j + 1, k + 1);
ai->UnitTypeRequests[i].Type = &UnitTypeByIdent(ident);
ai->UnitTypeRequests[i].Count = count;
ai.UnitTypeRequests[i].Type = &UnitTypeByIdent(ident);
ai.UnitTypeRequests[i].Count = count;
++i;
}
} else if (value == "upgrade") {
Expand All @@ -1830,7 +1831,7 @@ static int CclDefineAiPlayer(lua_State *l)
const int subargs = lua_rawlen(l, j + 1);
for (int k = 0; k < subargs; ++k) {
const std::string_view ident = LuaToString(l, j + 1, k + 1);
ai->UpgradeToRequests.push_back(&UnitTypeByIdent(ident));
ai.UpgradeToRequests.push_back(&UnitTypeByIdent(ident));
}
} else if (value == "research") {
if (!lua_istable(l, j + 1)) {
Expand All @@ -1839,12 +1840,12 @@ static int CclDefineAiPlayer(lua_State *l)
const int subargs = lua_rawlen(l, j + 1);
for (int k = 0; k < subargs; ++k) {
const std::string_view ident = LuaToString(l, j + 1, k + 1);
ai->ResearchRequests.push_back(CUpgrade::Get(ident));
ai.ResearchRequests.push_back(CUpgrade::Get(ident));
}
} else if (value == "building") {
CclParseBuildQueue(l, ai, j + 1);
} else if (value == "repair-building") {
ai->LastRepairBuilding = LuaToNumber(l, j + 1);
ai.LastRepairBuilding = LuaToNumber(l, j + 1);
} else {
LuaError(l, "Unsupported tag: %s", value.data());
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/editloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static void EditorUndoAction();
static void EditorRedoAction();
static void EditorAddUndoAction(EditorAction action);

extern gcn::Gui *Gui;
extern std::unique_ptr<gcn::Gui> Gui;
static std::unique_ptr<gcn::Container> editorContainer;
static std::unique_ptr<gcn::Slider> editorSlider;
static std::unique_ptr<gcn::DropDown> toolDropdown;
Expand Down
2 changes: 1 addition & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ bool UseHPForXp = false; /// true if gain XP by dealing damage, fal
-- Functions
----------------------------------------------------------------------------*/

extern gcn::Gui *Gui;
extern std::unique_ptr<gcn::Gui> Gui;

/**
** Save game settings.
Expand Down
6 changes: 3 additions & 3 deletions src/include/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
#include <string>
#include <vector>

#include "../ai/ai_local.h"
#include "color.h"
#include "settings.h"
#include "upgrade_structs.h"

#include "vec2i.h"

#include "settings.h"

class CGraphic;

Expand Down Expand Up @@ -249,7 +249,7 @@ class CPlayer
int UnitTypesAiActiveCount [UnitTypeMax]{}; /// total units of unit-type that have their AI set to active

bool AiEnabled; /// handle AI on local computer
PlayerAi *Ai; /// Ai structure pointer
std::unique_ptr<PlayerAi> Ai; /// Ai structure pointer

int NumBuildings; /// # buildings
int Supply; /// supply available/produced
Expand Down
Loading

0 comments on commit 4ee1b1d

Please sign in to comment.