From ba071468dc42fd2098aeb2200fcc7a48b405b855 Mon Sep 17 00:00:00 2001 From: Warmist Date: Fri, 2 Nov 2012 00:28:16 +0200 Subject: [PATCH] New way of doing things! Now using a class for menus, also no (non script) way to use bin-plugins. --- plugins/Dfusion/luafiles/adv_tools/init.lua | 133 -------------------- plugins/Dfusion/luafiles/common.lua | 32 +---- plugins/lua/dfusion.lua | 35 ++++++ plugins/lua/dfusion/adv_tools.lua | 52 ++++++++ scripts/dfusion.lua | 47 ++----- 5 files changed, 96 insertions(+), 203 deletions(-) delete mode 100644 plugins/Dfusion/luafiles/adv_tools/init.lua create mode 100644 plugins/lua/dfusion/adv_tools.lua diff --git a/plugins/Dfusion/luafiles/adv_tools/init.lua b/plugins/Dfusion/luafiles/adv_tools/init.lua deleted file mode 100644 index 566484f01..000000000 --- a/plugins/Dfusion/luafiles/adv_tools/init.lua +++ /dev/null @@ -1,133 +0,0 @@ -adv_tools= {} -adv_tools.menu=MakeMenu() ---TODO make every tool generic (work for both modes) -function adv_tools.reincarnate(swap_soul) --only for adventurer i guess - if swap_soul==nil then - swap_soul=true - end - local adv=df.global.world.units.active[0] - if adv.flags1.dead==false then - error("You are not dead (yet)!") - end - local hist_fig=getNemesis(adv).figure - if hist_fig==nil then - error("No historical figure for adventurer...") - end - local events=df.global.world.history.events - local trg_hist_fig - for i=#events-1,0,-1 do -- reverse search because almost always it will be last entry - if df.history_event_hist_figure_diedst:is_instance(events[i]) then - --print("is instance:"..i) - if events[i].victim==hist_fig.id then - --print("Is same id:"..i) - trg_hist_fig=events[i].slayer - if trg_hist_fig then - trg_hist_fig=df.historical_figure.find(trg_hist_fig) - end - break - end - end - end - if trg_hist_fig ==nil then - qerror("Slayer not found") - end - - local trg_unit=trg_hist_fig.unit_id - if trg_unit==nil then - qerror("Unit id not found!") - end - local trg_unit_final=df.unit.find(trg_unit) - - tools.change_adv(trg_unit_final) - if swap_soul then --actually add a soul... - t_soul=adv.status.current_soul - adv.status.current_soul=df.NULL - adv.status.souls:resize(0) - trg_unit_final.status.current_soul=t_soul - trg_unit_final.status.souls:insert(#trg_unit_final.status.souls,t_soul) - end -end -adv_tools.menu:add("Reincarnate",adv_tools.reincarnate) -function adv_tools.ressurect() - - v2=engine.peek(vector:getval(indx),ptr_Creature.hurt1) - for i=0,v2:size()-1 do - v2:setval(i,0) - end - v2=engine.peek(vector:getval(indx),ptr_Creature.hurt2) - v2.type=DWORD - for i=0,v2:size()-1 do - v2:setval(i,0) - end - engine.poke(vector:getval(indx),ptr_Creature.bloodlvl,60000) --give blood - engine.poke(vector:getval(indx),ptr_Creature.bleedlvl,0) --stop some bleeding... - local flg=engine.peek(vector:getval(indx),ptr_Creature.flags) - flg:set(1,false) --ALIVE - flg:set(39,false) -- leave body yet again - flg:set(37,false) -- something todo with wounds- lets you walk again. - flg:set(58,true) -- makes them able to breathe - flg:set(61,true) -- gives them sight - engine.poke(vector:getval(indx),ptr_Creature.flags,flg) -end - -function adv_tools.wagonmode() --by rumrusher - --first three lines same as before (because we will need an offset of creature at location x,y,z) - myoff=offsets.getEx("AdvCreatureVec") - vector=engine.peek(myoff,ptr_vector) - indx=GetCreatureAtPos(getxyz()) - --indx=0 - --print(string.format("%x",vector:getval(indx))) - flg=engine.peek(vector:getval(indx),ptr_Creature.flags) --get flags - flg:set(1,false) - flg:set(74,false) - engine.poke(vector:getval(indx),ptr_Creature.flags,flg) - print("To stay normal press y, else hit Enter turn Wagon mode on.") - r=io.stdin:read() -- repeat for it too work... also creature will be dead. - if r== "y" then - flg=engine.peek(vector:getval(indx),ptr_Creature.flags) - flg:set(1,false) - engine.poke(vector:getval(indx),ptr_Creature.flags,flg) - else - flg=engine.peek(vector:getval(indx),ptr_Creature.flags) - flg:set(1,false) - flg:flip(74) - engine.poke(vector:getval(indx),ptr_Creature.flags,flg) - end -end -function selectall() - local retvec={} --return vector (or a list) - myoff=offsets.getEx("AdvCreatureVec") - vector=engine.peek(myoff,ptr_vector) --standart start - for i=0,vector:size()-1 do --check all creatures - local off - off=vector:getval(i) - local flags=engine.peek(off,ptr_Creature.flags) - if flags:get(1)==true then --if dead ... - table.insert(retvec,off)--... add it to return vector - end - end - return retvec --return the "return vector" :) -end -function adv_tools.hostilate() - vector=engine.peek(offsets.getEx("AdvCreatureVec"),ptr_vector) - id=GetCreatureAtPos(getxyz()) - print(string.format("Vec:%d cr:%d",vector:size(),id)) - off=vector:getval(id) - crciv=engine.peek(vector:getval(id),ptr_Creature.civ) - curciv=engine.peek(vector:getval(0),ptr_Creature.civ) - - if curciv==crciv then - print("Friendly-making enemy") - engine.poke(off,ptr_Creature.civ,-1) - flg=engine.peek(off,ptr_Creature.flags) - flg:set(17,true) - engine.poke(off,ptr_Creature.flags,flg) - else - print("Enemy- making friendly") - engine.poke(off,ptr_Creature.civ,curciv) - flg=engine.peek(off,ptr_Creature.flags) - flg:set(17,false) - flg:set(19,false) - engine.poke(off,ptr_Creature.flags,flg) - end -end diff --git a/plugins/Dfusion/luafiles/common.lua b/plugins/Dfusion/luafiles/common.lua index a6781b385..bdf3a2bc8 100644 --- a/plugins/Dfusion/luafiles/common.lua +++ b/plugins/Dfusion/luafiles/common.lua @@ -242,37 +242,7 @@ function engine.installMod(file,name,bonussize) return T end -it_menu={} -it_menu.__index=it_menu -function it_menu:add(name,func) - table.insert(self.items,{func,name}) -end -function it_menu:display() - print("Select choice (q exits):") - for p,c in pairs(self.items) do - print(string.format("%3d).%s",p,c[2])) - end - local ans - repeat - local r - r=getline("") - if r==nil then return end - if r=='q' then return end - ans=tonumber(r) - - if ans==nil or not(ans<=#self.items and ans>0) then - print("incorrect choice") - end - - until ans~=nil and (ans<=#self.items and ans>0) - self.items[ans][1]() -end -function MakeMenu() - local ret={} - ret.items={} - setmetatable(ret,it_menu) - return ret -end + function PrintPattern(loadedpattern) for k,v in pairs(loadedpattern) do diff --git a/plugins/lua/dfusion.lua b/plugins/lua/dfusion.lua index 053c486cf..c7bd9bef3 100644 --- a/plugins/lua/dfusion.lua +++ b/plugins/lua/dfusion.lua @@ -202,4 +202,39 @@ function BinaryPlugin:__gc() end self.data:delete() end +-- a Menu for some stuff. Maybe add a posibility of it working as a gui, or a gui adaptor? +-- Todo add hints, and parse them to make a "smart" choice of parameters to pass +SimpleMenu=defclass(SimpleMenu) +SimpleMenu.ATTRS{title=DEFAULT_NIL} +function SimpleMenu:init(args) + self.items={} +end +function SimpleMenu:add(name,entry,hints) + table.insert(self.items,{entry,name,hints}) +end +function SimpleMenu:display() + print("Select choice (q exits):") + for p,c in pairs(self.items) do + print(string.format("%3d).%s",p,c[2])) + end + local ans + repeat + local r + r=io.stdin:read() + if r==nil then return end + if r=='q' then return end + ans=tonumber(r) + + if ans==nil or not(ans<=#self.items and ans>0) then + print("Invalid choice.") + end + + until ans~=nil and (ans<=#self.items and ans>0) + if type(self.items[ans][1])=="function" then + self.items[ans][1]() + else + self.items[ans][1]:display() + end +end + return _ENV \ No newline at end of file diff --git a/plugins/lua/dfusion/adv_tools.lua b/plugins/lua/dfusion/adv_tools.lua new file mode 100644 index 000000000..4440a4b2f --- /dev/null +++ b/plugins/lua/dfusion/adv_tools.lua @@ -0,0 +1,52 @@ +local _ENV = mkmodule('plugins.dfusion.adv_tools') +local dfu=require("plugins.dfusion") +menu=dfu.SimpleMenu() +function Reincarnate(trg_unit,swap_soul) --only for adventurer i guess + if swap_soul==nil then + swap_soul=true + end + local adv=trg_unit or df.global.world.units.active[0] + if adv.flags1.dead==false then + qerror("You are not dead (yet)!") + end + local hist_fig=getNemesis(adv).figure + if hist_fig==nil then + qerror("No historical figure for adventurer...") + end + local events=df.global.world.history.events + local trg_hist_fig + for i=#events-1,0,-1 do -- reverse search because almost always it will be last entry + if df.history_event_hist_figure_diedst:is_instance(events[i]) then + --print("is instance:"..i) + if events[i].victim==hist_fig.id then + --print("Is same id:"..i) + trg_hist_fig=events[i].slayer + if trg_hist_fig then + trg_hist_fig=df.historical_figure.find(trg_hist_fig) + end + break + end + end + end + if trg_hist_fig ==nil then + qerror("Slayer not found") + end + + local trg_unit=trg_hist_fig.unit_id + if trg_unit==nil then + qerror("Unit id not found!") + end + local trg_unit_final=df.unit.find(trg_unit) + + tools.change_adv(trg_unit_final) + if swap_soul then --actually add a soul... + t_soul=adv.status.current_soul + adv.status.current_soul=df.NULL + adv.status.souls:resize(0) + trg_unit_final.status.current_soul=t_soul + trg_unit_final.status.souls:insert(#trg_unit_final.status.souls,t_soul) + end +end +menu:add("Reincarnate",Reincarnate,{{df.unit,"optional"}})-- bool, optional + +return _ENV \ No newline at end of file diff --git a/scripts/dfusion.lua b/scripts/dfusion.lua index 63a530454..4fee0438e 100644 --- a/scripts/dfusion.lua +++ b/scripts/dfusion.lua @@ -1,42 +1,11 @@ --- a binary hack/plugin collection for df +-- a collection of misc lua scripts local dfu=require("plugins.dfusion") local myos=dfhack.getOSType() - ---some imports go here -local plugins={ - require("plugins.dfusion.embark").CustomEmbark -} ---show a table of all the statuses -function status(plug) - if dfu.plugins[plug.name]==nil then - return plug.class_status - else - return dfu.plugins[plug.name]:status() - end +args={...} +mainmenu=dfu.SimpleMenu() +function runsave() + print("doing file:"..df.global.world.cur_savegame.save_dir) end -function printPlugs() - local endthis=false - print("current:") - while(not endthis) do - for k,v in pairs(plugins) do - if v then - print(string.format("%2d. %15s-%s",k,v.name,status(v))) - end - end - print("e-edit and load, u-unload,c-cancel and then number to manipulate:") - local choice=io.stdin:read() - local num=tonumber(io.stdin:read()) - if num then - local plg=dfu.plugins[plugins[num].name] or plugins[num]() - if choice=='e' then - plg:edit() - elseif choice=='u' then - plg:uninstall() - elseif choice=='c' then - endthis=true - end - end - end -end - -printPlugs() \ No newline at end of file +mainmenu:add("Run save script",runsave) +mainmenu:add("Adventurer tools",require("plugins.dfusion.adv_tools").menu) +mainmenu:display() \ No newline at end of file