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

Ipochto/highground fixes #581

Merged
merged 30 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bcc96b0
forced reduction of the tile index when an index beyond the slot size…
ipochto Mar 31, 2023
4892681
Replace lua check args macros with functions
ipochto Nov 12, 2023
fcf60e1
Lua_register'ed get method for Map.Info.Highgrouns. Also moved a coup…
ipochto Nov 12, 2023
92c6d8c
Register enable highground func, some fixes
ipochto Nov 13, 2023
25ef33a
Fix typo
ipochto Nov 14, 2023
b60d1d2
Added information about enabled highgrounds into the map presentation…
ipochto Nov 14, 2023
27c3f66
"Layers" -> "Overlays"
ipochto Nov 14, 2023
1161285
PresentMap args changed to some more human readable format.
ipochto Nov 14, 2023
425fa26
Added drop down list's methods to set/get selected Items instead of i…
ipochto Nov 22, 2023
1cd638b
Fix accidentally damaged character
ipochto Nov 23, 2023
e4c0afb
Removed few debug lines of code
ipochto Nov 23, 2023
889aa3c
check for call without arguments
ipochto Nov 24, 2023
b830561
Update src/include/map.h
ipochto Nov 25, 2023
f6231bf
removed unnecessary const, optimized variable name
ipochto Nov 25, 2023
5baf643
change from static to inline for functions in a header file
ipochto Nov 25, 2023
aaf7b0b
Highgrounds->HighgroundsEnabled
ipochto Nov 25, 2023
473fae6
Update src/map/script_map.cpp
ipochto Nov 25, 2023
63dae4e
Update src/map/mapfield.cpp
ipochto Nov 25, 2023
5601b3f
typo
ipochto Nov 25, 2023
166fefd
Update src/map/script_map.cpp
ipochto Nov 25, 2023
2d5ff4e
Update src/map/script_map.cpp
ipochto Nov 25, 2023
5e49b8d
Update src/map/script_map.cpp
ipochto Nov 25, 2023
d2d6b13
Update src/include/script.h
ipochto Nov 25, 2023
5ab2804
change return to 0, because t Nothing is returned in Lua
ipochto Nov 25, 2023
979a786
Merge branch 'ipochto/highground-fixes' of github.com:Wargus/stratagu…
ipochto Nov 25, 2023
b9f41e6
Revert changes of guichan.
ipochto Nov 25, 2023
3e08359
Revert accidental changes in editloop.cpp
ipochto Nov 27, 2023
c936051
Revert accidental changes in the lismodel.h
ipochto Nov 27, 2023
7aa38e1
fixed variable name in case we will add more optional args
ipochto Nov 28, 2023
dc9cc18
Remove 'FIXME' comment
ipochto Nov 28, 2023
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
2 changes: 1 addition & 1 deletion src/editor/editloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,7 @@ void EditorMainLoop()
editorContainer->add(toolDropdown.get(), 0, 0);
}

std::vector<std::string> overlaysListStrings = { "Layers: None", "Unpassable", "No building allowed", "Elevation", "Opaque" };
std::vector<std::string> overlaysListStrings = { "Overlays: None", "Unpassable", "No building allowed", "Elevation", "Opaque" };
auto overlaysList = std::make_unique<StringListModel>(overlaysListStrings);
overlaysDropdown = std::make_unique<gcn::DropDown>(overlaysList.get());
auto overlaysDropdownListener = std::make_unique<LambdaActionListener>([&overlaysListStrings](const std::string&) {
Expand Down
5 changes: 3 additions & 2 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ static bool WriteMapPresentation(const fs::path &mapname, CMap &map, Vec2i newSi
newSize.y = map.Info.MapHeight;
}

f->printf("PresentMap(\"%s\", %d, %d, %d, %d)\n",
f->printf("PresentMap(\"%s\", %d, %d, %d, %d%s)\n",
map.Info.Description.c_str(), numplayers, newSize.x, newSize.y,
map.Info.MapUID + 1);
map.Info.MapUID + 1,
Map.Info.IsHighgroundsEnabled()? ", \"highgrounds-enabled\"" : "");

if (map.Info.Filename.find(".sms") == std::string::npos && !map.Info.Filename.empty()) {
f->printf("DefineMapSetup(\"%s\")\n", map.Info.Filename.c_str());
Expand Down
6 changes: 6 additions & 0 deletions src/include/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class CMapInfo

bool IsPointOnMap(const Vec2i &pos) const { return IsPointOnMap(pos.x, pos.y); }

bool IsHighgroundsEnabled() const { return HighgroundsEnabled; }
void EnableHighgrounds(bool enable = true) { HighgroundsEnabled = enable; }

void Clear();

public:
Expand All @@ -141,6 +144,9 @@ class CMapInfo
PlayerTypes PlayerType[PlayerMax]; /// Same player->Type
int PlayerSide[PlayerMax]; /// Same player->Side
unsigned int MapUID; /// Unique Map ID (hash)

private:
bool HighgroundsEnabled = false; /// Map has highgrounds
};

/*----------------------------------------------------------------------------
Expand Down
24 changes: 18 additions & 6 deletions src/include/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,24 @@ extern int LuaCall(lua_State *L, int narg, int nresults, int base, bool exitOnEr
lua_error(l); \
} while (0)

#define LuaCheckArgs(l, args) \
do { \
if (lua_gettop(l) != args) { \
LuaError(l, "incorrect argument"); \
} \
} while (0)
inline void LuaCheckArgs(lua_State *l, int args)
{
if (lua_gettop(l) != args) {
LuaError(l, "incorrect argument");
}
}

inline void LuaCheckArgs_min(lua_State *l, int args)
{
if (lua_gettop(l) < args) {
LuaError(l, "incorrect argument");
}
}

inline int LuaGetArgsNum(lua_State *l)
{
return lua_gettop(l);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For next times, don't hesitate to split your PR.
That change might be a dedicated PR, is unrelated to HighgroundsEnabled feature.


#if LUA_VERSION_NUM <= 501

Expand Down
1 change: 1 addition & 0 deletions src/include/tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ constexpr uint8_t MapFieldSubtilesMax {16};
constexpr uint8_t MapFieldSubtilesUnpassableShift {48};
constexpr tile_flags MapFieldSubtilesUnpassableMask {tile_flags(0xFFFF) << MapFieldSubtilesUnpassableShift}; /// Up to 16 unpassable subtiles, never used in MapField, only in CTile

constexpr tile_index ExtendedTilesetBeginIdx {0x1010}; /// the extended tiles indexes start form here

/**
** These are used for lookup tiles types
Expand Down
4 changes: 4 additions & 0 deletions src/include/widgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ class StringListModel : public gcn::ListModel

int getNumberOfElements() override { return list.size(); }
std::string getElementAt(int i) override { return list[i]; }
int getIdxOfElement(std::string_view element);
};

class LuaListModel : public gcn::ListModel
Expand All @@ -330,6 +331,7 @@ class LuaListModel : public gcn::ListModel
void setList(lua_State *lua, lua_Object *lo);
int getNumberOfElements() override { return list.size(); }
std::string getElementAt(int i) override { return list[i]; }
int getIdxOfElement(std::string_view element);
};

class ImageListBox : public gcn::ListBox
Expand Down Expand Up @@ -471,7 +473,9 @@ class ImageDropDownWidget : public DropDownWidget
void setSize(int width, int height) override;
void setListModel(LuaListModel *listModel);
int getSelected();
std::string getSelectedItem();
void setSelected(int selected);
int setSelectedItem(lua_State *lua, lua_Object *lo);
void adjustHeight();
void setListBox(ImageListBox *listBox);
void setFont(gcn::Font *font);
Expand Down
2 changes: 1 addition & 1 deletion src/map/fov.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void CFieldOfView::Refresh(const CPlayer &player, const CUnit &unit, const Vec2i
return;
}
if (GameSettings.FoV == FieldOfViewTypes::cShadowCasting && !unit.Type->AirUnit) {
/// FIXME: add high-/lowground

OpaqueFields = unit.Type->BoolFlag[ELEVATED_INDEX].value ? 0 : this->Settings.OpaqueFields;
if (GameSettings.Inside) {
OpaqueFields &= ~(MapFieldRocks); /// because of rocks-flag is used as an obstacle for ranged attackers
Expand Down
2 changes: 2 additions & 0 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ void CMapInfo::Clear()
memset(this->PlayerSide, 0, sizeof(this->PlayerSide));
memset(this->PlayerType, 0, sizeof(this->PlayerType));
this->MapUID = 0;

this->HighgroundsEnabled = false;
}

CMap::~CMap()
Expand Down
11 changes: 9 additions & 2 deletions src/map/mapfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ bool CMapField::IsTerrainResourceOnMap() const

void CMapField::setTileIndex(const CTileset &tileset, const tile_index tileIndex, const int value, const uint8_t elevation, const int subtile /* = -1 */)
{
const CTile &tile = tileset.tiles[tileIndex];
uint8_t compShift = 0; // [0..F] in case that current tileset slot length is shorter than map's original

if (tileIndex >= ExtendedTilesetBeginIdx) { // tile from extended tileset
while(tileset.tiles[tileIndex - compShift].tile == 0 && ((tileIndex & 0xF) - compShift) > 0) {
compShift++;
}
}
const CTile &tile = tileset.tiles[tileIndex - compShift];
this->tile = tile.tile;
this->Value = value;
this->ElevationLevel = elevation;
Expand Down Expand Up @@ -116,7 +123,7 @@ void CMapField::setTileIndex(const CTileset &tileset, const tile_index tileIndex
#endif
this->cost = 1 << (tile.flag & MapFieldSpeedMask);
#ifdef DEBUG
this->tilesetTile = tileIndex;
this->tilesetTile = tileIndex - compShift;
#endif
}

Expand Down
70 changes: 70 additions & 0 deletions src/map/script_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,71 @@ static int CclGetIsGameHoster(lua_State *l)
return 1;
}

/**
** <b>Description</b>
**
** Set basic map caracteristics.
**
** @param l Lua state.
**
** Example:
**
** <div class="example"><code><strong>PresentMap</strong>("Map description", PlayerCount, Width, Height, uid_number [, "highgrounds-enabled"])</code></div>
*/
static int CclPresentMap(lua_State *l)
{
LuaCheckArgs_min(l, 5);

Map.Info.Description = LuaToString(l, 1);
// Number of players in LuaToNumber(l, 2); // Not used yet.
Map.Info.MapWidth = LuaToNumber(l, 3);
Map.Info.MapHeight = LuaToNumber(l, 4);
Map.Info.MapUID = LuaToNumber(l, 5);

if(LuaGetArgsNum(l) >= 6) {
const std::string_view value = LuaToString(l, 6);
if (value == "highgrounds-enabled") {
Map.Info.EnableHighgrounds();
} else {
LuaError(l, "Unknown value %s\n", value.data());
}
ipochto marked this conversation as resolved.
Show resolved Hide resolved
}

return 0;
}

static int CclMapEnableHighgrounds(lua_State *l)
{
Map.Info.EnableHighgrounds(LuaGetArgsNum(l) >= 1 ? LuaToBoolean(l, 1) : true);

return 0;
}

static int CclIsHighgroundsEnabled(lua_State *l)
{
lua_pushboolean(l, Map.Info.IsHighgroundsEnabled());
return 1;
}
Comment on lines +1092 to +1103
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might probably go in map.pkg (tolua++) instead.

Copy link
Member Author

@ipochto ipochto Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference in principle?

I just did it by analogy, depending on how existing functions with similar functionality were translated into lua. If, for example, what concerns gui was translated through 'tolua++', then almost everything I've encountered concerning engine settings or parameters of the current game was done through lua_register() .

To change the method is not a problem. I just want to understand for the future on what principle to choose the translation method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure neither ;-)

to_lua has been added after.

BTW, the flag seems unused in the Engine :-/ ...
Is there incoming PRs next?
How does it work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used here: Wargus/wargus#446

Copy link
Member Author

@ipochto ipochto Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this flag we choose to load base tileset or run extended tileset generator to make tiles for highgrounds/ramps/cliffs etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently

      Map.Info.Postamble    = ""
      MapEnableHighgrounds(highgroundsCheckBox:isMarked())

With Tolua, it would be more consistent:

      Map.Info.Postamble    = ""
      Map.Info.HighgroundsEnabled = highgroundsCheckBox:isMarked()

but it seems that Highgrounds is more related to tileset than map.
wonder if it is then the right flag/place.
Shouldn't we choose between summer/summer_highgrounds/swamp/swamp_highgrounds...?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map.Info.HighgroundsEnabled = highgroundsCheckBox:isMarked()

This is a private member and I don't want to open it in case if we will need to do something else with enabling it in the future. At least the network synchronization may be required.

I could use Tolua to open MapEnableHighgrounds()/IsHighgroundsEnabled(), but definitely don't want to make Map.Info.HighgroundsEnabled variable as public.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we choose between summer/summer_highgrounds/swamp/swamp_highgrounds...?

Don't breed entities. )

The player simply selects the basic tileset as before, and wargus chooses whether to load the extended version or not. Moreover, if wargus "sees" that the map contains highgrounds, it will simply hide the possibility to select (in dropDownList) the base tileset, for which there is no possibility to generate the extended version (I am talking about swamp specifically - there are certain difficulties with generation).

For the editor, there is a checkbox to enable highgrounds or not.

So the Map.Info.HighgroundsEnabled is map's property, on the basis of which the choice of which tileset to load is made

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the map presentation is loaded before tileset. Tileset loads with game start, but map presentation is uploaded to the stage of choosing the game parameters by the player (in the create a new game menu).


/**
** <b>Description</b>
**
** Define the lua file that will build the map
**
** @param l Lua state.
**
** Example:
**
** <div class="example"><code>-- Load map setup from file
** <strong>DefineMapSetup</strong>("Setup.sms")</code></div>
*/
static int CclDefineMapSetup(lua_State *l)
{
LuaCheckArgs(l, 1);
Map.Info.Filename = LuaToString(l, 1);

return 0;
}
/**
** Register CCL features for map.
*/
Expand Down Expand Up @@ -1114,6 +1179,11 @@ void MapCclRegister()

lua_register(Lua, "GetIsGameHoster", CclGetIsGameHoster);

lua_register(Lua, "PresentMap", CclPresentMap);
lua_register(Lua, "MapEnableHighgrounds", CclMapEnableHighgrounds);
lua_register(Lua, "IsHighgroundsEnabled", CclIsHighgroundsEnabled);

lua_register(Lua, "DefineMapSetup", CclDefineMapSetup);
}

//@}
2 changes: 2 additions & 0 deletions src/tolua/ui.pkg
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ class DropDownWidget : public DropDown
class ImageDropDownWidget : public DropDown
{
ImageDropDownWidget();
std::string getSelectedItem();
int setSelectedItem(lua_State *lua, lua_Object *lo);
void setList(lua_State *lua, lua_Object *lo);
virtual ListBox *getListBox();
virtual void setSize(int width, int height);
Expand Down
46 changes: 0 additions & 46 deletions src/ui/script_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1245,49 +1245,6 @@ static int CclSetGroupKeys(lua_State *l)
return 0;
}

/**
** <b>Description</b>
**
** Set basic map caracteristics.
**
** @param l Lua state.
**
** Example:
**
** <div class="example"><code><strong>PresentMap</strong>("Map description", 1, 128, 128, 17)</code></div>
*/
static int CclPresentMap(lua_State *l)
{
LuaCheckArgs(l, 5);

Map.Info.Description = LuaToString(l, 1);
// Number of players in LuaToNumber(l, 3); // Not used yet.
Map.Info.MapWidth = LuaToNumber(l, 3);
Map.Info.MapHeight = LuaToNumber(l, 4);
Map.Info.MapUID = LuaToNumber(l, 5);

return 0;
}

/**
** <b>Description</b>
**
** Define the lua file that will build the map
**
** @param l Lua state.
**
** Example:
**
** <div class="example"><code>-- Load map setup from file
** <strong>DefineMapSetup</strong>("Setup.sms")</code></div>
*/
static int CclDefineMapSetup(lua_State *l)
{
LuaCheckArgs(l, 1);
Map.Info.Filename = LuaToString(l, 1);

return 0;
}
/**
** <b>Description</b>
**
Expand Down Expand Up @@ -1357,9 +1314,6 @@ void UserInterfaceCclRegister()

lua_register(Lua, "DefineButtonStyle", CclDefineButtonStyle);

lua_register(Lua, "PresentMap", CclPresentMap);
lua_register(Lua, "DefineMapSetup", CclDefineMapSetup);

//
// Look and feel of units
//
Expand Down
47 changes: 46 additions & 1 deletion src/ui/widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "netconnect.h"
#include "editor.h"
#include "sound.h"
#include "util.h"

/*----------------------------------------------------------------------------
-- Variables
Expand Down Expand Up @@ -1718,9 +1719,23 @@ void ImageTextField::drawBorder(gcn::Graphics *graphics)
}

/*----------------------------------------------------------------------------
-- LuaListModel
-- StringListModel
----------------------------------------------------------------------------*/

int StringListModel::getIdxOfElement(std::string_view element)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int StringListModel::getIdxOfElement(std::string_view element)
int StringListModel::getIdxOfElement(std::string_view element) const

{
auto result = ranges::find(this->list, element);
if (result != this->list.end()) {
return result - this->list.begin();
} else {
return -1;
}

}

/*----------------------------------------------------------------------------
-- LuaListModel
----------------------------------------------------------------------------*/

/**
** Set the list
Expand All @@ -1735,6 +1750,16 @@ void LuaListModel::setList(lua_State *lua, lua_Object *lo)
}
}

int LuaListModel::getIdxOfElement(std::string_view element)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int LuaListModel::getIdxOfElement(std::string_view element)
int LuaListModel::getIdxOfElement(std::string_view element) const

{
auto result = ranges::find(this->list, element);
if (result != this->list.end()) {
return result - this->list.begin();
} else {
return -1;
}
}

/*----------------------------------------------------------------------------
-- ImageListBox
----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -2610,6 +2635,13 @@ int ImageDropDownWidget::getSelected()
return mListBox.getSelected();
}

std::string ImageDropDownWidget::getSelectedItem()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::string ImageDropDownWidget::getSelectedItem()
std::string ImageDropDownWidget::getSelectedItem() const

{
Assert(mScrollArea && mScrollArea->getContent() != nullptr);

return listmodel.getElementAt(mListBox.getSelected());
}

void ImageDropDownWidget::setSelected(int selected)
{
Assert(mScrollArea && mScrollArea->getContent() != nullptr);
Expand All @@ -2620,6 +2652,19 @@ void ImageDropDownWidget::setSelected(int selected)
}
}

int ImageDropDownWidget::setSelectedItem(lua_State *lua, lua_Object *lo)
{
Assert(mScrollArea && mScrollArea->getContent() != nullptr);

auto item = LuaToString(lua, *lo);
int idx = this->listmodel.getIdxOfElement(item);
if (idx >= 0)
{
this->setSelected(idx);
}
return idx;
}

void ImageDropDownWidget::adjustHeight()
{
Assert(mScrollArea && mScrollArea->getContent() != nullptr);
Expand Down