Last active
December 25, 2015 18:39
-
-
Save Putnam3145/7021958 to your computer and use it in GitHub Desktop.
Gives a choice of creatures to transform into.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- transforms unit (by number) into another creature, choice given to user. Syntax is: unitID tickamount maxsize namefilter. A size of 0 is ignored. A length of 0 is also ignored. If no filter, all units will be sorted. A filter of ALL will also work with all units. | |
local dialog = require('gui.dialogs') | |
local script = require('gui.script') | |
function transform(target,race,caste,length) | |
if target==nil then | |
qerror("Not a valid target") | |
end | |
local defaultRace = target.enemy.normal_race | |
local defaultCaste = target.enemy.normal_caste | |
target.enemy.normal_race = race | |
target.enemy.normal_caste = caste | |
target.enemy.were_race = race | |
target.enemy.were_caste=caste | |
if length~='forever' and length>0 then dfhack.timeout(length,'ticks',function() target.enemy.normal_race = defaultRace target.enemy.normal_caste = defaultCaste target.enemy.were_race=defaultRace target.enemy.were_caste = defaultCaste end) end | |
end | |
function getBodySize(caste) | |
return caste.body_size_1[#caste.body_size_1-1] | |
end | |
function selectCreature(unitID,length,size,filter) --taken straight from here, but edited so I can understand it better: https://gist.github.com/warmist/4061959/... again. Also edited for syndromeTrigger, but in a completely different way. | |
size = size or 0 | |
filter = filter or "all" | |
length = length or 2400 | |
local creatures=df.global.world.raws.creatures.all | |
local tbl={} | |
local tunit=df.unit.find(unitID) | |
for cr_k,creature in ipairs(creatures) do | |
for ca_k,caste in ipairs(creature.caste) do | |
local name=caste.caste_name[0] | |
if name=="" then name="?" end | |
if (not filter or string.find(name,filter) or string.lower(filter)=="all") and (not size or size>getBodySize(caste) or size<1 and not creature.flags.DOES_NOT_EXIST) then table.insert(tbl,{name,nil,cr_k,ca_k}) end | |
end | |
end | |
table.sort(tbl,function(a,b) return a[1]<b[1] end) | |
local f=function(name,C) | |
transform(tunit,C[3],C[4],length) | |
end | |
script.start(function() | |
local ok = | |
script.showYesNoPrompt( | |
"Just checking","Do you want " | |
.. dfhack.TranslateName(dfhack.units.getVisibleName(tunit)) .. | |
" to transform into a creature of size below " ..NEWLINE.. | |
.. (not not size and size>1 and size or "infinity") .. | |
" (" | |
.. size/(getBodySize(df.creature_raw.find(tunit.race).caste[tunit.caste]))*100 .. | |
"% of current size) for " | |
..length.. | |
" ticks (" | |
..length/1200 .. | |
" days, ~" | |
..length/df.global.enabler.fps.. | |
" seconds)?", | |
COLOR_LIGHTRED) | |
if ok then dialog.showListPrompt("Creature Selection","Choose creature:",COLOR_WHITE,tbl,f) end | |
end) | |
end | |
local utils = require('utils') | |
validArgs = validArgs or utils.invert({ | |
'help', | |
'unit', | |
'duration', | |
'size', | |
'filter', | |
}) | |
local args = utils.processArgs({...},validArgs) | |
if args.help then | |
print([[scripts/modtools/transform-unit.lua | |
arguments | |
-help | |
print this help message | |
-unit id | |
set the target unit | |
-duration ticks | |
how long it should last, or "forever" | |
-size size | |
The largest size of creature shown in the menu. | |
-filter filter | |
Filter creatures based on name. Only creatures whose names contain the filter string will be shown. | |
]]) | |
return | |
end | |
selectCreature(tonumber(args.unit),tonumber(args.duration),tonumber(args.size),args.filter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment