From 63254df6dde62e32624abafe74e39dc6b2359598 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Tue, 3 Jun 2014 12:24:45 +0400 Subject: [PATCH] Fix obvious issues in stockflow. - Duplicate definition of a function now in uicommon.h - Assertion failure due to missing core suspend claim. - Incorrect way of accessing the civ entity. - Accessing nil objects in the lua module if filter matches nothing. - Lua module breaking on reload('plugins.stockflow'). --- plugins/lua/stockflow.lua | 28 +++++++++++++--------------- plugins/stockflow.cpp | 19 +------------------ 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/plugins/lua/stockflow.lua b/plugins/lua/stockflow.lua index ec52c3deb..489a72705 100644 --- a/plugins/lua/stockflow.lua +++ b/plugins/lua/stockflow.lua @@ -1,11 +1,11 @@ local _ENV = mkmodule('plugins.stockflow') -gui = require "gui" -utils = require "utils" +local gui = require "gui" +local utils = require "utils" -reaction_list = {} -saved_orders = {} -jobs_to_create = {} +reaction_list = reaction_list or {} +saved_orders = saved_orders or {} +jobs_to_create = jobs_to_create or {} triggers = { {filled = false, divisor = 1, name = "Per empty space"}, @@ -19,11 +19,7 @@ triggers = { {name = "Never"}, } --- There must be a better way get the value of an enum. -job_types = {} -for key, value in ipairs(df.job_type) do - job_types[value] = key -end +local job_types = df.job_type entry_ints = { stockpile_id = 1, @@ -205,7 +201,7 @@ function material_reactions(reactions, itemtypes, mat_info) end function clothing_reactions(reactions, mat_info, filter) - local resources = df.global.world.entities.all[df.global.ui.civ_id].resources + local resources = df.historical_entity.find(df.global.ui.civ_id).resources local itemdefs = df.global.world.raws.itemdefs resource_reactions(reactions, job_types.MakeArmor, mat_info, resources.armor_type, itemdefs.armor, {permissible = filter}) resource_reactions(reactions, job_types.MakePants, mat_info, resources.pants_type, itemdefs.pants, {permissible = filter}) @@ -372,7 +368,7 @@ function collect_reactions() -- Reactions defined in the raws. -- Not all reactions are allowed to the civilization. -- That includes "Make sharp rock" by default. - local entity = df.global.world.entities.all[df.global.ui.civ_id] + local entity = df.historical_entity.find(df.global.ui.civ_id) for _, reaction_id in ipairs(entity.entity_raw.workshops.permitted_reaction_id) do local reaction = df.global.world.raws.reactions[reaction_id] local name = string.gsub(reaction.name, "^.", string.upper) @@ -740,8 +736,10 @@ function screen:onInput(keys) self:dismiss() elseif keys.SELECT then self:dismiss() - local selected = self.reactions[self.position].index - store_order(self.stockpile, selected) + local selected = self.reactions[self.position] + if selected then + store_order(self.stockpile, selected.index) + end elseif keys.STANDARDSCROLL_UP then self.position = self.position - 1 elseif keys.STANDARDSCROLL_DOWN then @@ -838,10 +836,10 @@ function screen:refilter() local displayed = {} for n = 0, PageSize*2 - 1 do local item = filtered[start + n] - local name = item.name if not item then break end + local name = item.name local x = 1 local y = FirstRow + n diff --git a/plugins/stockflow.cpp b/plugins/stockflow.cpp index b96faa47f..adff25a04 100644 --- a/plugins/stockflow.cpp +++ b/plugins/stockflow.cpp @@ -51,20 +51,6 @@ const char *usage = ( "number of identical orders already in the queue.\n" ); -/* - * Stockpile Access - */ -static building_stockpilest *get_selected_stockpile() { - if (!Gui::dwarfmode_hotkey(Core::getTopViewscreen()) || - ui->main.mode != ui_sidebar_mode::QueryBuilding) - { - return nullptr; - } - - return virtual_cast(world->selected_building); -} - - /* * Lua interface. * Currently calls out to Lua functions, but never back in. @@ -134,10 +120,6 @@ public: bool stockpile_method(const char *method, building_stockpilest *sp) { // Combines the select_order and toggle_trigger method calls, // because they share the same signature. - - // Suspension is necessary for toggle_trigger, - // but may be overkill for select_order. - // Both are used from hooks, so CoreSuspender is prohibited. CoreSuspendClaimer suspend; auto L = Lua::Core::State; @@ -168,6 +150,7 @@ public: auto L = Lua::Core::State; color_ostream_proxy out(Core::getInstance().getConsole()); + CoreSuspendClaimer suspend; Lua::StackUnwinder top(L); if (!lua_checkstack(L, 2))