Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #671

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/ai/ai_force.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,22 @@
/*----------------------------------------------------------------------------
-- Types
----------------------------------------------------------------------------*/
#define AIATTACK_RANGE 0
#define AIATTACK_ALLMAP 1
#define AIATTACK_BUILDING 2
#define AIATTACK_AGRESSIVE 3

enum class EAttackFindType
{
Range,
AllMap,
Building,
Aggressive
};

class EnemyUnitFinder
{
public:
friend TerrainTraversal;

static CUnit *find(const CUnit& unit, int find_type) {
static CUnit *find(const CUnit &unit, EAttackFindType find_type)
{
// Terrain traversal by Andrettin
TerrainTraversal terrainTraversal;
terrainTraversal.SetSize(Map.Info.MapWidth, Map.Info.MapHeight);
Expand All @@ -74,7 +79,7 @@ class EnemyUnitFinder
}

private:
EnemyUnitFinder(const CUnit &unit, int find_type) :
EnemyUnitFinder(const CUnit &unit, EAttackFindType find_type) :
unit(unit),
movemask(unit.Type->MovementMask & ~(MapFieldLandUnit | MapFieldAirUnit | MapFieldSeaUnit)),
attackrange(unit.Stats->Variables[ATTACKRANGE_INDEX].Max),
Expand All @@ -86,7 +91,7 @@ class EnemyUnitFinder
const CUnit &unit;
unsigned int movemask;
const int attackrange;
const int find_type;
const EAttackFindType find_type;
CUnit *result_unit = nullptr;
};

Expand All @@ -112,7 +117,8 @@ VisitResult EnemyUnitFinder::Visit(TerrainTraversal &terrainTraversal, const Vec
continue;
}

if ((find_type != AIATTACK_BUILDING || dtype.BoolFlag[BUILDING_INDEX].value) && (find_type != AIATTACK_AGRESSIVE || dest->IsAgressive())) {
if ((find_type != EAttackFindType::Building || dtype.BoolFlag[BUILDING_INDEX].value)
&& (find_type != EAttackFindType::Aggressive || dest->IsAgressive())) {
result_unit = dest;
return VisitResult::Finished;
} else if (result_unit == nullptr) { // if trying to search for buildings or aggressive units specifically, still put the first found unit (even if it doesn't fit those parameters) as the result unit, so that it can be returned if no unit with the specified parameters is found
Expand All @@ -122,7 +128,7 @@ VisitResult EnemyUnitFinder::Visit(TerrainTraversal &terrainTraversal, const Vec
return VisitResult::Ok;
}

template <int FIND_TYPE>
template <EAttackFindType FIND_TYPE>
class AiForceEnemyFinder
{
public:
Expand All @@ -148,7 +154,7 @@ class AiForceEnemyFinder
if (unit->Type->CanAttack == false) {
return enemy == nullptr;
}
if constexpr (FIND_TYPE == AIATTACK_RANGE) {
if constexpr (FIND_TYPE == EAttackFindType::Range) {
enemy = AttackUnitsInReactRange(*unit);
} else {
enemy = EnemyUnitFinder::find(*unit, FIND_TYPE);
Expand Down Expand Up @@ -417,9 +423,9 @@ void AiForce::Attack(const Vec2i &pos)
bool isDefenceForce = false;
if (Map.Info.IsPointOnMap(goalPos) == false) {
/* Search in entire map */
const CUnit *enemy = isTransporter ? AiForceEnemyFinder<AIATTACK_AGRESSIVE>::find(*this)
: isNaval ? AiForceEnemyFinder<AIATTACK_ALLMAP>::find(*this)
: AiForceEnemyFinder<AIATTACK_BUILDING>::find(*this);
const CUnit *enemy = isTransporter ? AiForceEnemyFinder<EAttackFindType::Aggressive>::find(*this)
: isNaval ? AiForceEnemyFinder<EAttackFindType::AllMap>::find(*this)
: AiForceEnemyFinder<EAttackFindType::Building>::find(*this);
if (enemy) {
goalPos = enemy->tilePos;
}
Expand Down Expand Up @@ -921,9 +927,9 @@ void AiForce::Update()
--WaitOnRallyPoint;
}
if (maxDist <= thresholdDist || !WaitOnRallyPoint) {
const CUnit *unit = AiForceEnemyFinder<AIATTACK_BUILDING>::find(*this);
const CUnit *unit = AiForceEnemyFinder<EAttackFindType::Building>::find(*this);
if (unit == nullptr) {
unit = AiForceEnemyFinder<AIATTACK_ALLMAP>::find(*this);
unit = AiForceEnemyFinder<EAttackFindType::AllMap>::find(*this);
if (unit == nullptr) {
// No enemy found, give up
// FIXME: should the force go home or keep trying to attack?
Expand Down Expand Up @@ -966,8 +972,8 @@ void AiForce::Update()
const bool isNaval = ranges::any_of(this->Units, [](const CUnit *unit) {
return unit->Type->MoveType == EMovement::Naval && unit->Type->CanAttack;
});
const CUnit *unit = isNaval ? AiForceEnemyFinder<AIATTACK_ALLMAP>::find(*this)
: AiForceEnemyFinder<AIATTACK_BUILDING>::find(*this);
const CUnit *unit = isNaval ? AiForceEnemyFinder<EAttackFindType::AllMap>::find(*this)
: AiForceEnemyFinder<EAttackFindType::Building>::find(*this);
if (!unit) {
// No enemy found, give up
// FIXME: should the force go home or keep trying to attack?
Expand Down Expand Up @@ -1042,7 +1048,7 @@ void AiForceManager::Update()
if (aiunit->MapDistanceTo(force.GoalPos) <= nearDist) {
// Look if still enemies in attack range.
maxPathing--;
if (AiForceEnemyFinder<AIATTACK_RANGE>::find(force) == nullptr) {
if (AiForceEnemyFinder<EAttackFindType::Range>::find(force) == nullptr) {
force.ReturnToHome();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/editor/edmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ static void EditorRandomizeTile(int tile, int count, int max_size)
** @param count the number of times to add the unit
** @param value resources to be stored in that unit
*/
static void EditorRandomizeUnit(const char *unit_type, int count, int value, int tileIndexUnderUnit)
static void EditorRandomizeUnit(const std::string_view unit_type, int count, int value, int tileIndexUnderUnit)
{
const Vec2i mpos(Map.Info.MapWidth, Map.Info.MapHeight);
CUnitType &type = UnitTypeByIdent(unit_type);
Expand Down Expand Up @@ -468,7 +468,7 @@ void CEditor::CreateRandomMap(bool shuffleTranslitions) const
TileToolRandom = oldRandom;

for (std::tuple<std::string, int, int, int> t : RandomUnits) {
EditorRandomizeUnit(std::get<0>(t).c_str(), mz / 64 * std::get<1>(t), std::get<2>(t), std::get<3>(t));
EditorRandomizeUnit(std::get<0>(t), mz / 64 * std::get<1>(t), std::get<2>(t), std::get<3>(t));
UI.Minimap.Update();
EditorUpdateDisplay();
}
Expand Down
3 changes: 2 additions & 1 deletion src/include/net_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//@{

#include <cstdint>
#include <string_view>
#include <vector>

#include "settings.h"
Expand Down Expand Up @@ -316,7 +317,7 @@ class CInitMessage_MapFileFragment
{
public:
CInitMessage_MapFileFragment() = default;
CInitMessage_MapFileFragment(const char *path, const std::vector<char> &data, uint32_t Fragment);
CInitMessage_MapFileFragment(const std::string_view path, const std::vector<char> &data, uint32_t Fragment);
explicit CInitMessage_MapFileFragment(uint32_t Fragment);
const CInitMessage_Header &GetHeader() const { return header; }
std::vector<unsigned char> Serialize() const;
Expand Down
6 changes: 3 additions & 3 deletions src/network/net_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,15 @@ CInitMessage_MapFileFragment::CInitMessage_MapFileFragment(uint32_t fragment) :
this->FragmentIndex = fragment;
}

CInitMessage_MapFileFragment::CInitMessage_MapFileFragment(const char *path, const std::vector<char> &data, uint32_t fragment) :
CInitMessage_MapFileFragment::CInitMessage_MapFileFragment(const std::string_view path, const std::vector<char> &data, uint32_t fragment) :
header(MessageInit_FromServer, ICMMapNeeded)
{
int pathSize = strlen(path);
const auto pathSize = path.size();
Assert(pathSize <= 256);
Assert(sizeof(this->Data) >= pathSize + data.size());
this->PathSize = pathSize;
this->DataSize = data.size();
memcpy(this->Data, path, pathSize);
memcpy(this->Data, path.data(), pathSize);
memcpy(this->Data + pathSize, data.data(), data.size());
this->FragmentIndex = fragment;
}
Expand Down
2 changes: 1 addition & 1 deletion src/network/netconnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ void CServer::Send_MapFragment(const CNetworkHost &host, uint32_t fragmentIdx)
networkName.c_str(),
data.size(),
offset);
const CInitMessage_MapFileFragment message(networkName.c_str(), data, fragmentIdx);
const CInitMessage_MapFileFragment message(networkName, data, fragmentIdx);
NetworkSendICMessage_Log(*socket, CHost(host.Host, host.Port), message);
return;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/sound/music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

/// flag is set when a MusicFinishedCallback was enqueued in the event loop and should be handled, and unset when the handler has run.
static std::atomic_flag MusicFinishedEventQueued = ATOMIC_FLAG_INIT;
static volatile bool IsCallbackEnabled = false;
static std::atomic<bool> IsCallbackEnabled = false;

/*----------------------------------------------------------------------------
-- Functions
Expand Down
4 changes: 2 additions & 2 deletions src/stratagus/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,8 @@ int CPlayer::CheckCosts(const int (&costs)[MaxCosts], bool notify) const
continue;
}
if (notify) {
const char *name = DefaultResourceNames[i].c_str();
const char *actionName = DefaultActions[i].c_str();
const auto &name = DefaultResourceNames[i];
const auto &actionName = DefaultActions[i];

Notify(_("Not enough %s...%s more %s."), _(name), _(actionName), _(name));

Expand Down
2 changes: 1 addition & 1 deletion src/ui/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2260,7 +2260,7 @@ void DrawPieMenu()
text = "ESC";
} else {
buf[0] = toupper(buttons[i].Key);
text = (const char *)buf;
text = buf;
}
label.DrawClip(x + 4, y + 4, text);
}
Expand Down
9 changes: 2 additions & 7 deletions src/ui/script_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,16 +1025,11 @@ static int CclDefineButton(lua_State *l)
if (lua_isfunction(l, -1)) {
ba.Payload = new LuaCallback<void(int, int)>(l, -1);
} else {
char buf[64];
const char *s2;

if (lua_isnumber(l, -1)) {
snprintf(buf, sizeof(buf), "%ld", (long int)lua_tonumber(l, -1));
s2 = buf;
ba.ValueStr = std::to_string((long int) lua_tonumber(l, -1));
} else {
s2 = lua_tostring(l, -1);
ba.ValueStr = lua_tostring(l, -1);
}
ba.ValueStr = s2;
}
} else if (value == "Allowed") {
value = LuaToString(l, -1);
Expand Down
Loading