Ruby: use Gui module to get selected unit

This allows "exterminate it" to work from command-prompt, among other things.

Fixes #1100
develop
lethosor 2017-06-06 23:34:34 -04:00
parent 6be805f509
commit c3d5663320
2 changed files with 11 additions and 38 deletions

@ -5,9 +5,11 @@
#include "PluginManager.h"
#include "VersionInfo.h"
#include "MemAccess.h"
#include "DataDefs.h"
#include "modules/Gui.h"
#include "df/global_objects.h"
#include "df/unit.h"
#include "tinythread.h"
@ -627,6 +629,12 @@ static VALUE rb_dfget_vtable_ptr(VALUE self, VALUE objptr)
return rb_uint2inum(*(uintptr_t*)rb_num2ulong(objptr));
}
static VALUE rb_dfget_selected_unit_id(VALUE self)
{
df::unit *u = Gui::getAnyUnit(Core::getTopViewscreen());
return rb_int2inum(u ? u->id : -1);
}
// run a dfhack command, as if typed from the dfhack console
static VALUE rb_dfhack_run(VALUE self, VALUE cmd)
{
@ -1138,6 +1146,7 @@ static void ruby_bind_dfhack(void) {
rb_define_singleton_method(rb_cDFHack, "get_vtable", RUBY_METHOD_FUNC(rb_dfget_vtable), 1);
rb_define_singleton_method(rb_cDFHack, "get_rtti_classname", RUBY_METHOD_FUNC(rb_dfget_rtti_classname), 1);
rb_define_singleton_method(rb_cDFHack, "get_vtable_ptr", RUBY_METHOD_FUNC(rb_dfget_vtable_ptr), 1);
rb_define_singleton_method(rb_cDFHack, "get_selected_unit_id", RUBY_METHOD_FUNC(rb_dfget_selected_unit_id), 0);
rb_define_singleton_method(rb_cDFHack, "dfhack_run", RUBY_METHOD_FUNC(rb_dfhack_run), 1);
rb_define_singleton_method(rb_cDFHack, "print_str", RUBY_METHOD_FUNC(rb_dfprint_str), 1);
rb_define_singleton_method(rb_cDFHack, "print_color", RUBY_METHOD_FUNC(rb_dfprint_color), 2);

@ -6,43 +6,7 @@ module DFHack
# with an argument that respond to x/y/z (eg cursor), find first unit at this position
def unit_find(what=:selected, y=nil, z=nil)
if what == :selected
case curview._rtti_classname
when :viewscreen_itemst
ref = curview.entry_ref[curview.cursor_pos]
ref.unit_tg if ref.kind_of?(GeneralRefUnit)
when :viewscreen_unitlistst
v = curview
v.units[v.page][v.cursor_pos[v.page]]
when :viewscreen_petst
v = curview
case v.mode
when :List
v.animal[v.cursor].unit if !v.is_vermin[v.cursor]
when :SelectTrainer
v.trainer_unit[v.trainer_cursor]
end
when :viewscreen_dwarfmodest
case ui.main.mode
when :ViewUnits
# nobody selected => idx == 0
v = world.units.active[ui_selected_unit]
v if v and v.pos.z == cursor.z
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.unit if k.type == :Unit
else
ui.follow_unit_tg if ui.follow_unit != -1
end
when :viewscreen_dungeonmodest
case ui_advmode.menu
when :Default
world.units.active[0]
else
unit_find(cursor) # XXX
end
when :viewscreen_dungeon_monsterstatusst
curview.unit
end
return world.units.all.binsearch(df.get_selected_unit_id)
elsif what.kind_of?(Integer)
# search by id
return world.units.all.binsearch(what) if not z