Skip to content

Commit

Permalink
Merge pull request Wargus#581 from Wargus/ipochto/highground-fixes
Browse files Browse the repository at this point in the history
Ipochto/highground fixes
  • Loading branch information
Jarod42 authored Nov 28, 2023
2 parents 7a9d154 + dc9cc18 commit e44db04
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 59 deletions.
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);
}

#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());
}
}

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;
}

/**
** <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)
{
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)
{
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()
{
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

0 comments on commit e44db04

Please sign in to comment.