Skip to content

Commit

Permalink
lua: Make unit.__tostring report units on recall lists
Browse files Browse the repository at this point in the history
  • Loading branch information
jostephd committed Oct 16, 2019
1 parent a67b9d0 commit 8caf887
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/scripting/lua_unit.cpp
Expand Up @@ -43,7 +43,7 @@ lua_unit::~lua_unit()
{
}

unit* lua_unit::get()
unit* lua_unit::get() const
{
if (ptr) return ptr.get();
if (c_ptr) return c_ptr;
Expand All @@ -54,7 +54,7 @@ unit* lua_unit::get()
if (!ui.valid()) return nullptr;
return ui.get_shared_ptr().get(); //&*ui would not be legal, must get new shared_ptr by copy ctor because the unit_map itself is holding a boost shared pointer.
}
unit_ptr lua_unit::get_shared()
unit_ptr lua_unit::get_shared() const
{
if (ptr) return ptr;
if (side) {
Expand Down Expand Up @@ -244,7 +244,8 @@ static int impl_unit_equality(lua_State* L)
*/
static int impl_unit_tostring(lua_State* L)
{
const unit& u = luaW_checkunit(L, 1);
const lua_unit* lu = luaW_tounit_ref(L, 1);
unit &u = *lu->get();
std::ostringstream str;

str << "unit: <";
Expand All @@ -253,8 +254,11 @@ static int impl_unit_tostring(lua_State* L)
} else {
str << u.type_id() << " ";
}
// This will say 0,0 (or -999,-999?) for units on the recall list
str << "at (" << u.get_location() << ")";
if(int side = lu->on_recall_list()) {
str << "at (side " << side << " recall list)";
} else {
str << "at (" << u.get_location() << ")";
}
str << '>';

lua_push(L, str.str());
Expand Down
9 changes: 5 additions & 4 deletions src/scripting/lua_unit.hpp
Expand Up @@ -97,13 +97,14 @@ class lua_unit
~lua_unit();

bool on_map() const { return !ptr && side == 0; }
/// @return If this unit is on any side's recall list, the number of that side (1-based). Otherwise, return 0.
int on_recall_list() const { return side; }

unit* get();
unit_ptr get_shared();
unit* get() const;
unit_ptr get_shared() const;

unit* operator->() {return get();}
unit& operator*() {return *get();}
unit* operator->() const {return get();}
unit& operator*() const {return *get();}

void clear_ref() { uid = 0; ptr = unit_ptr(); side = 0; c_ptr = nullptr; }
// Clobbers loc
Expand Down

0 comments on commit 8caf887

Please sign in to comment.