Ruby: use core methods for finding selected item and building

Similar to c3d566332, the core methods have become more complicated (and are also checked at compile-time). This will give a more consistent experience across scripts.

Fixes #1563
develop
lethosor 2020-05-01 22:50:02 -04:00
parent 24cebbda58
commit 1dc59d3772
3 changed files with 18 additions and 45 deletions

@ -2,16 +2,7 @@ module DFHack
class << self
def building_find(what=:selected, y=nil, z=nil)
if what == :selected
case ui.main.mode
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.building if k.type == :Building
when :BuildingItems, :QueryBuilding
world.selected_building
when :Zones, :ZonesPenInfo, :ZonesPitInfo, :ZonesHospitalInfo
ui_sidebar_menus.zone.selected
end
return world.buildings.all.binsearch(df.get_selected_building_id)
elsif what.kind_of?(Integer)
# search by building.id
return world.buildings.all.binsearch(what) if not z

@ -4,41 +4,7 @@ module DFHack
# arg similar to unit.rb/unit_find; no arg = 'k' menu
def item_find(what=:selected, y=nil, z=nil)
if what == :selected
case curview._rtti_classname
when :viewscreen_itemst
if ref = curview.entry_ref[curview.cursor_pos]
ref.item_tg if ref.kind_of?(GeneralRefItem)
else
# not a container
curview.item
end
when :viewscreen_storesst # z/stocks
if curview.in_group_mode == 0 and curview.in_right_list == 1
curview.items[curview.item_cursor]
end
else
case ui.main.mode
when :LookAround
k = ui_look_list.items[ui_look_cursor]
case k.type
when :Item
k.item
when :Building
# hilight a constructed bed/coffer
mats = k.building.contained_items.find_all { |i| i.use_mode == 2 }
mats[0].item if mats.length == 1
end
when :BuildingItems
bld = world.selected_building
bld.contained_items[ui_building_item_cursor].item if bld
when :ViewUnits
u = world.units.active[ui_selected_unit]
u.inventory[ui_look_cursor].item if u and u.pos.z == cursor.z and
ui_unit_view_mode.value == :Inventory and u.inventory[ui_look_cursor]
else
ui.follow_item_tg if ui.follow_item != -1
end
end
return world.items.all.binsearch(df.get_selected_item_id)
elsif what.kind_of?(Integer)
# search by id
return world.items.all.binsearch(what) if not z

@ -9,6 +9,8 @@
#include "modules/Gui.h"
#include "df/global_objects.h"
#include "df/building.h"
#include "df/item.h"
#include "df/unit.h"
#include "tinythread.h"
@ -630,6 +632,18 @@ static VALUE rb_dfget_vtable_ptr(VALUE self, VALUE objptr)
return rb_uint2inum(*(uintptr_t*)rb_num2ulong(objptr));
}
static VALUE rb_dfget_selected_building_id(VALUE self)
{
df::building *b = Gui::getAnyBuilding(Core::getTopViewscreen());
return rb_int2inum(b ? b->id : -1);
}
static VALUE rb_dfget_selected_item_id(VALUE self)
{
df::item *i = Gui::getAnyItem(Core::getTopViewscreen());
return rb_int2inum(i ? i->id : -1);
}
static VALUE rb_dfget_selected_unit_id(VALUE self)
{
df::unit *u = Gui::getAnyUnit(Core::getTopViewscreen());
@ -1147,6 +1161,8 @@ 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_building_id", RUBY_METHOD_FUNC(rb_dfget_selected_building_id), 0);
rb_define_singleton_method(rb_cDFHack, "get_selected_item_id", RUBY_METHOD_FUNC(rb_dfget_selected_item_id), 0);
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);