Skip to content

Commit

Permalink
Merge pull request #529 from Wargus/clean_up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 authored Oct 7, 2023
2 parents e247176 + 16894ce commit 0855820
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 271 deletions.
40 changes: 15 additions & 25 deletions src/game/trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
#include "unit_find.h"
#include "unittype.h"

#include <vector>

/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/

CTimer GameTimer; /// The game timer
static int Trigger;
static bool *ActiveTriggers;
static std::vector<bool> ActiveTriggers;

/// Some data accessible for script during the game.
TriggerDataType TriggerData;
Expand Down Expand Up @@ -300,22 +302,15 @@ static int CclIfRescuedNearUnit(lua_State *l)
*/
int GetNumOpponents(int player)
{
int n = 0;

// Check the player opponents
for (int i = 0; i < PlayerMax; ++i) {
return ranges::count_if(Players, [&](const CPlayer &p) {
// Check the player opponents
// This player is our enemy and has units left.
if ((Players[player].IsEnemy(Players[i])) || (Players[i].IsEnemy(Players[player]))) {
// Don't count walls
for (CUnit *unit : Players[i].GetUnits()) {
if (unit->Type->BoolFlag[WALL_INDEX].value == false) {
++n;
break;
}
}
}
}
return n;
// Don't count walls
return (p.IsEnemy(Players[player]) || Players[player].IsEnemy(p))
&& ranges::any_of(p.GetUnits(), [](const CUnit *unit) {
return unit->Type->BoolFlag[WALL_INDEX].value == false;
});
});
}

/**
Expand Down Expand Up @@ -432,7 +427,7 @@ static int CclAddTrigger(lua_State *l)
}

const int i = lua_rawlen(l, -1);
if (ActiveTriggers && !ActiveTriggers[i / 2]) {
if (!ActiveTriggers.empty() && !ActiveTriggers[i / 2]) {
lua_pushnil(l);
lua_rawseti(l, -2, i + 1);
lua_pushnil(l);
Expand Down Expand Up @@ -465,7 +460,7 @@ static int CclSetActiveTriggers(lua_State *l)
{
const int args = lua_gettop(l);

ActiveTriggers = new bool[args];
ActiveTriggers.resize(args);
for (int j = 0; j < args; ++j) {
ActiveTriggers[j] = LuaToBoolean(l, j + 1);
}
Expand All @@ -489,11 +484,7 @@ static bool TriggerExecuteAction(int script)
for (int j = 0; j < args; ++j) {
lua_rawgeti(Lua, -1, j + 1);
LuaCall(0, 0);
if (lua_gettop(Lua) > base + 1 && lua_toboolean(Lua, -1)) {
ret = true;
} else {
ret = false;
}
ret = lua_gettop(Lua) > base + 1 && lua_toboolean(Lua, -1);
lua_settop(Lua, base + 1);
}
lua_pop(Lua, 1);
Expand Down Expand Up @@ -646,8 +637,7 @@ void CleanTriggers()

Trigger = 0;

delete[] ActiveTriggers;
ActiveTriggers = nullptr;
ActiveTriggers.clear();

GameTimer.Reset();
}
Expand Down
37 changes: 15 additions & 22 deletions src/include/title.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@

#include "filesystem.h"

#include <memory>
#include <string>

#include <vector>

class CFont;

Expand All @@ -45,28 +46,20 @@ enum {
class TitleScreenLabel
{
public:
TitleScreenLabel() : Font(0), Xofs(0), Yofs(0), Flags(0) {}
TitleScreenLabel() = default;

std::string Text;
CFont *Font;
int Xofs;
int Yofs;
int Flags;
CFont *Font = nullptr;
int Xofs = 0;
int Yofs = 0;
int Flags = 0;
};

class TitleScreen
{
public:
TitleScreen() : StretchImage(true), Timeout(0), Iterations(0), Editor(0), Labels(nullptr) {}
~TitleScreen()
{
if (this->Labels) {
for (int i = 0; this->Labels[i]; ++i) {
delete this->Labels[i];
}
delete[] this->Labels;
}
}
TitleScreen() = default;
~TitleScreen() = default;

void ShowTitleImage();

Expand All @@ -75,14 +68,14 @@ class TitleScreen
public:
fs::path File;
std::string Music;
bool StretchImage;
int Timeout;
int Iterations;
int Editor;
TitleScreenLabel **Labels;
bool StretchImage = true;
int Timeout = 0;
int Iterations = 0;
int Editor = 0;
std::vector<std::unique_ptr<TitleScreenLabel>> Labels;
};

extern TitleScreen **TitleScreens; /// File for title screen
extern std::vector<std::unique_ptr<TitleScreen>> TitleScreens; /// File for title screen

extern void ShowTitleScreens();

Expand Down
102 changes: 48 additions & 54 deletions src/include/ui/popup.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "color.h"
#include "script.h"
#include "vec2i.h"
#include "video.h"

#include <optional>
#include <string>
Expand All @@ -55,31 +56,24 @@ enum class ButtonCmd;
class PopupConditionPanel
{
public:
PopupConditionPanel() : HasHint(false), HasDescription(false), HasDependencies(false),
ButtonAction(std::nullopt), BoolFlags(nullptr), Variables(nullptr) {}
~PopupConditionPanel()
{
delete[] BoolFlags;
delete[] Variables;
}

bool HasHint; /// check if button has hint.
bool HasDescription; /// check if button has description.
bool HasDependencies; /// check if button has dependencies or restrictions.
std::optional<ButtonCmd> ButtonAction; /// action type of button
PopupConditionPanel() = default;
~PopupConditionPanel() = default;

bool HasHint = false; /// check if button has hint.
bool HasDescription = false; /// check if button has description.
bool HasDependencies = false; /// check if button has dependencies or restrictions.
std::optional<ButtonCmd> ButtonAction; /// action type of button
std::string ButtonValue; /// value used in ValueStr field of button

char *BoolFlags; /// array of condition about user flags.
char *Variables; /// array of variable to verify (enable and max > 0)
std::vector<char> BoolFlags; /// array of condition about user flags.
std::vector<char> Variables; /// array of variable to verify (enable and max > 0)
};

class CPopupContentType
{
public:
CPopupContentType() : pos(0, 0),
MarginX(MARGIN_X), MarginY(MARGIN_Y), minSize(0, 0),
Wrap(true), Condition(nullptr) {}
virtual ~CPopupContentType() { delete Condition; }
CPopupContentType() = default;
virtual ~CPopupContentType() = default;

/// Tell how show the variable Index.
virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const = 0;
Expand All @@ -90,20 +84,20 @@ class CPopupContentType

virtual void Parse(lua_State *l) = 0;

static CPopupContentType *ParsePopupContent(lua_State *l);
static std::unique_ptr<CPopupContentType> ParsePopupContent(lua_State *l);

public:
PixelPos pos; /// position to draw.
PixelPos pos{0, 0}; /// position to draw.

int MarginX; /// Left and right margin width.
int MarginY; /// Upper and lower margin height.
PixelSize minSize; /// Minimal size covered by content type.
bool Wrap; /// If true, the next content will be placed on the next "line".
int MarginX = MARGIN_X; /// Left and right margin width.
int MarginY = MARGIN_Y; /// Upper and lower margin height.
PixelSize minSize{0, 0}; /// Minimal size covered by content type.
bool Wrap = true; /// If true, the next content will be placed on the next "line".
protected:
std::string TextColor; /// Color used for plain text in content.
std::string HighlightColor; /// Color used for highlighted letters.
public:
PopupConditionPanel *Condition; /// Condition to show the content; if nullptr, no condition.
std::unique_ptr<PopupConditionPanel> Condition; /// Condition to show the content; if nullptr, no condition.
};

enum PopupButtonInfo_Types {
Expand All @@ -115,8 +109,8 @@ enum PopupButtonInfo_Types {
class CPopupContentTypeButtonInfo : public CPopupContentType
{
public:
CPopupContentTypeButtonInfo() : InfoType(0), MaxWidth(0), Font(nullptr) {}
virtual ~CPopupContentTypeButtonInfo() {}
CPopupContentTypeButtonInfo() = default;
virtual ~CPopupContentTypeButtonInfo() = default;

virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const;

Expand All @@ -126,16 +120,16 @@ class CPopupContentTypeButtonInfo : public CPopupContentType
virtual void Parse(lua_State *l);

private:
int InfoType; /// Type of information to show.
unsigned int MaxWidth; /// Maximum width of multilined information.
CFont *Font; /// Font to use.
int InfoType = 0; /// Type of information to show.
unsigned int MaxWidth = 0; /// Maximum width of multilined information.
CFont *Font = nullptr; /// Font to use.
};

class CPopupContentTypeText : public CPopupContentType
{
public:
CPopupContentTypeText() : MaxWidth(0), Font(nullptr) {}
virtual ~CPopupContentTypeText() {}
CPopupContentTypeText() = default;
virtual ~CPopupContentTypeText() = default;

virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const;

Expand All @@ -145,16 +139,16 @@ class CPopupContentTypeText : public CPopupContentType
virtual void Parse(lua_State *l);

private:
std::string Text; /// Text to display
unsigned int MaxWidth; /// Maximum width of multilined text.
CFont *Font; /// Font to use.
std::string Text; /// Text to display
unsigned int MaxWidth = 0; /// Maximum width of multilined text.
CFont *Font = nullptr; /// Font to use.
};

class CPopupContentTypeCosts : public CPopupContentType
{
public:
CPopupContentTypeCosts() : Font(nullptr), Centered(0) {}
virtual ~CPopupContentTypeCosts() {}
CPopupContentTypeCosts() = default;
virtual ~CPopupContentTypeCosts() = default;

virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const;

Expand All @@ -164,15 +158,15 @@ class CPopupContentTypeCosts : public CPopupContentType
virtual void Parse(lua_State *l);

private:
CFont *Font; /// Font to use.
char Centered; /// if true, center the display.
CFont *Font = nullptr; /// Font to use.
bool Centered = false; /// if true, center the display.
};

class CPopupContentTypeLine : public CPopupContentType
{
public:
CPopupContentTypeLine();
virtual ~CPopupContentTypeLine() {}
CPopupContentTypeLine() = default;
virtual ~CPopupContentTypeLine() = default;

virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const;

Expand All @@ -182,9 +176,9 @@ class CPopupContentTypeLine : public CPopupContentType
virtual void Parse(lua_State *l);

private:
IntColor Color; /// Color used for line.
unsigned int Width; /// line height
unsigned int Height; /// line height
IntColor Color = ColorWhite; /// Color used for line.
unsigned int Width = 0; /// line height
unsigned int Height = 1; /// line height
};

class CPopupContentTypeVariable : public CPopupContentType
Expand All @@ -209,18 +203,18 @@ class CPopupContentTypeVariable : public CPopupContentType
class CPopup
{
public:
CPopup();
~CPopup();
CPopup() = default;
~CPopup() = default;

std::vector<CPopupContentType *> Contents; /// Array of contents to display.
std::vector<std::unique_ptr<CPopupContentType>> Contents; /// Array of contents to display.
std::string Ident; /// Ident of the popup.
int MarginX; /// Left and right margin width.
int MarginY; /// Upper and lower margin height.
int MinWidth; /// Minimal width covered by popup.
int MinHeight; /// Minimal height covered by popup.
CFont *DefaultFont; /// Default font for content.
IntColor BackgroundColor; /// Color used for popup's background.
IntColor BorderColor; /// Color used for popup's borders.
int MarginX = MARGIN_X; /// Left and right margin width.
int MarginY = MARGIN_Y; /// Upper and lower margin height.
int MinWidth = 0; /// Minimal width covered by popup.
int MinHeight = 0; /// Minimal height covered by popup.
CFont *DefaultFont = nullptr; /// Default font for content.
IntColor BackgroundColor = ColorBlue; /// Color used for popup's background.
IntColor BorderColor = ColorWhite; /// Color used for popup's borders.
};


Expand Down
2 changes: 1 addition & 1 deletion src/include/unittype.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ class CUnitType
Vec2i GetHalfTileSize() const { return Vec2i(TileWidth / 2, TileHeight / 2); }
PixelSize GetPixelSize() const;

bool CheckUserBoolFlags(const char *BoolFlags) const;
bool CheckUserBoolFlags(const std::vector<char> &BoolFlags) const;
bool CanTransport() const { return MaxOnBoard > 0 && !GivesResource; }
bool CanMove() const;

Expand Down
Loading

0 comments on commit 0855820

Please sign in to comment.