Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -300,13 +300,14 @@ static int impl_unit_get(lua_State *L) | |
|
||
if(strcmp(m, "upkeep") == 0) { | ||
unit::upkeep_t upkeep = u.upkeep_raw(); | ||
if(boost::get<unit::upkeep_full>(&upkeep) != nullptr){ | ||
lua_pushstring(L, "full"); | ||
} else if(boost::get<unit::upkeep_loyal>(&upkeep) != nullptr){ | ||
lua_pushstring(L, "loyal"); | ||
|
||
if(int* v = boost::get<int>(&upkeep)) { | ||
lua_push(L, *v); | ||
} else { | ||
lua_push(L, boost::get<int>(upkeep)); | ||
const std::string type = boost::apply_visitor(unit::upkeep_type_visitor(), upkeep); | ||
lua_pushstring(L, type.c_str()); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gfgtdf
Contributor
|
||
} | ||
|
||
return 1; | ||
} | ||
if(strcmp(m, "advancements") == 0) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -740,10 +740,58 @@ class unit | |
*/ | ||
int upkeep() const; | ||
|
||
// TODO: look for a cleaner solution than these structs | ||
struct upkeep_full {}; | ||
struct upkeep_loyal {}; | ||
using upkeep_t = boost::variant<upkeep_full, upkeep_loyal, int>; | ||
struct upkeep_full | ||
{ | ||
static std::string type() { static std::string v = "full"; return v; } | ||
This comment has been minimized.
Sorry, something went wrong.
CelticMinstrel
Member
|
||
}; | ||
|
||
struct upkeep_loyal | ||
{ | ||
static std::string type() { static std::string v = "loyal"; return v; } | ||
}; | ||
|
||
/** Visitor helper class to fetch the appropriate upkeep value. */ | ||
class upkeep_value_visitor : public boost::static_visitor<int> | ||
{ | ||
public: | ||
explicit upkeep_value_visitor(const unit& unit) : u_(unit) {} | ||
|
||
int operator()(const upkeep_full&) const | ||
{ | ||
return u_.level(); | ||
} | ||
|
||
int operator()(const upkeep_loyal&) const | ||
{ | ||
return 0; | ||
} | ||
|
||
int operator()(int v) const | ||
{ | ||
return v; | ||
} | ||
|
||
private: | ||
const unit& u_; | ||
}; | ||
|
||
/** Visitor helper struct to fetch the upkeep type flag if applicable, or the the value otherwise. */ | ||
struct upkeep_type_visitor : public boost::static_visitor<std::string> | ||
{ | ||
template<typename T> | ||
typename std::enable_if<!std::is_same<int, T>::value, std::string>::type | ||
operator()(T& v) const | ||
{ | ||
return v.type(); | ||
} | ||
|
||
std::string operator()(int v) const | ||
{ | ||
return std::to_string(v); | ||
} | ||
}; | ||
|
||
using upkeep_t = boost::variant<upkeep_full, upkeep_loyal, int>; | ||
|
||
/** | ||
* Gets the raw variant controlling the upkeep value. | ||
|
@@ -755,7 +803,7 @@ class unit | |
return upkeep_; | ||
} | ||
|
||
/** Sets the upkeep value to a specific numeric value. */ | ||
/** Sets the upkeep value to a specific value value. Does not necessarily need to be numeric */ | ||
void set_upkeep(upkeep_t v) | ||
{ | ||
upkeep_ = v; | ||
|
lua_pushstring
is for pushing C strings. Uselua_pushlstring
for C++ strings.