From 1dc59d377275f893d568bc5782107ef3ff609806 Mon Sep 17 00:00:00 2001 From: lethosor Date: Fri, 1 May 2020 22:50:02 -0400 Subject: [PATCH 1/4] 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 --- plugins/ruby/building.rb | 11 +---------- plugins/ruby/item.rb | 36 +----------------------------------- plugins/ruby/ruby.cpp | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 45 deletions(-) diff --git a/plugins/ruby/building.rb b/plugins/ruby/building.rb index 7a85e9314..dae1d455b 100644 --- a/plugins/ruby/building.rb +++ b/plugins/ruby/building.rb @@ -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 diff --git a/plugins/ruby/item.rb b/plugins/ruby/item.rb index 394e545c6..59477c382 100644 --- a/plugins/ruby/item.rb +++ b/plugins/ruby/item.rb @@ -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 diff --git a/plugins/ruby/ruby.cpp b/plugins/ruby/ruby.cpp index 240a67cb2..0209806d1 100644 --- a/plugins/ruby/ruby.cpp +++ b/plugins/ruby/ruby.cpp @@ -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); From 217535decbc38709cb0870530cf796a9e438b52f Mon Sep 17 00:00:00 2001 From: lethosor Date: Fri, 1 May 2020 22:52:35 -0400 Subject: [PATCH 2/4] Update xml --- library/xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/xml b/library/xml index 0792fc020..7941e0ffa 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit 0792fc0202fb6a04bfdaa262bc36a3b14c8581e5 +Subproject commit 7941e0ffa7e355cd2363367cdfbb3e085ac4a941 From 947d0db182832c931e26b30eb13daba94219a1cc Mon Sep 17 00:00:00 2001 From: Ben Lubar Date: Sat, 2 May 2020 01:54:38 -0500 Subject: [PATCH 3/4] update structures --- library/xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/xml b/library/xml index 7941e0ffa..0231cce29 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit 7941e0ffa7e355cd2363367cdfbb3e085ac4a941 +Subproject commit 0231cce29e6014367c0af122aed4f7ba21292b2b From 1c57aa7bb4f103b482c3dabba9bcfcb0bdb47e90 Mon Sep 17 00:00:00 2001 From: Ben Lubar Date: Sat, 2 May 2020 02:01:32 -0500 Subject: [PATCH 4/4] update structures --- library/xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/xml b/library/xml index 0231cce29..b4dfab6d0 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit 0231cce29e6014367c0af122aed4f7ba21292b2b +Subproject commit b4dfab6d08635da8b01504a289e8cf4e62c859c5