Skip to content

Instantly share code, notes, and snippets.

@thistleknot
Last active December 20, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thistleknot/6153673 to your computer and use it in GitHub Desktop.
Save thistleknot/6153673 to your computer and use it in GitHub Desktop.
-- Bugs
-- --------------------------------------------------------------------------
-- Functions
function boostAttributes(selectedUnitCaste, unit)
--[[ these were not easy to get, see code...
df.physical_attribute_type
0 STRENGTH
1 AGILITY
2 TOUGHNESS
3 ENDURANCE
4 RECUPERATION
5 DISEASE_RESISTANCE
df.mental_attribute_type
0 ANALYTICAL_ABILITY
1 FOCUS
2 WILLPOWER
3 CREATIVITY
4 INTUITION
5 PATIENCE
6 MEMORY
7 LINGUISTIC_ABILITY
8 SPATIAL_SENSE
9 MUSICALITY
10 KINESTHETIC_SENSE
11 EMPATHY
12 SOCIAL_AWARENESS
]]
handleBoostType("Physical", selectedUnitCaste, unit)
handleBoostType("Mental", selectedUnitCaste, unit)
end
function handleBoostType(string_type, selectedUnitCaste, unit)
attributeBin = {}
if string_type == "Physical" then
print (string_type)
numberOfAttr = #selected_unit_caste.attributes.phys_att_range
else
print (string_type)
numberOfAttr = #selected_unit_caste.attributes.ment_att_range
end
print ("Number of Attributes: ", numberOfAttr)
-- cycle through attributes
-- for i,j in ipairs(df.physical_attribute_type) do print(i,j) end
for attrNumber = 1, numberOfAttr do
if string_type == "Physical" then
numberOfBins = #selected_unit_caste.attributes.phys_att_range[attrNumber-1]
attribute_name = df.physical_attribute_type[attrNumber-1]
else
numberOfBins = #selected_unit_caste.attributes.ment_att_range[attrNumber-1]
attribute_name = df.mental_attribute_type[attrNumber-1]
end
print("Attribute Name: ", attribute_name)
print("Number of bins: ", numberOfBins)
-- cycle through bins, print each bin
for line = 1, numberOfBins do
if string_type == "Physical" then
attributeBin[line] = selected_unit_caste.attributes.phys_att_range[attrNumber-1][line-1]
else
attributeBin[line] = selected_unit_caste.attributes.ment_att_range[attrNumber-1][line-1]
end
print("Bin: " , line, attributeBin[line])
end
-- Figure out Percent
if string_type == "Physical" then
rawValue = unit.body.physical_attrs[attrNumber-1].value
else
rawValue = unit.status.current_soul.mental_attrs[attrNumber-1].value
end
if string_type == "Physical" then
print("Raw Value: ", unit.body.physical_attrs[attrNumber-1].value)
else
print("Raw Value: ", unit.status.current_soul.mental_attrs[attrNumber-1].value)
end
-- If value is above final bin, skip
if rawValue >= attributeBin[#attributeBin] then
print("Value meets or exceeds max embark value, not attempting")
else
percent = calcPercent(rawValue, attributeBin)
print ("Percent: ", percent)
-- Boost this attributes value
if string_type == "Physical" then
boostValue(attrNumber, rawValue, percent, attributeBin, unit, type_string, "Physical")
else
boostValue(attrNumber, rawValue, percent, attributeBin, unit, type_string, "Mental")
end
end
end
end
function figureValueUL_Limit (rawValue, attributeBin)
local counter = #attributeBin
while rawValue < attributeBin[counter] do
l_limit = attributeBin[counter-1]
u_limit = attributeBin[counter]
counter = counter-1
end
return l_limit, u_limit, counter
end
function calcPercent(rawValue, attributeBin)
local l_limit, u_limit, counter = figureValueUL_Limit(rawValue, attributeBin)
print ("l_limit", l_limit)
print ("u_limit", u_limit)
print("counter: ", counter)
local value = (rawValue - l_limit)
value = value / (u_limit - l_limit)
value = value * ( 1/6 )
value = value + ( ( counter - 1 ) * 1/6)
return value
end
--would like to clean this up, don't like type_string, should be able to assign something to a var
function boostValue(attrNumber, rawValue, percent, attributeBin, unit, line, type_string)
-- Check if we're going to boost the value
local randomRoll = math.random()
print("RNG: ", randomRoll)
if randomRoll >= percent then
print ("Boosting!")
local l_limit, u_limit, counter = figureValueUL_Limit(rawValue, attributeBin)
local binsLeft = #attributeBin - counter
print ("Bins Left: ", binsLeft)
percentWithinBin = (rawValue - l_limit) / (u_limit - l_limit)
print("Percent within bin: ", percentWithinBin)
binPercents = {}
for position = 1, binsLeft do
if position == 1 then
binPercents[position] = percentWithinBin
else
binPercents[position] = 1
end
end
-- get sum
sumOfPercents = sum(binPercents)
print("Sum of Percents: ", sumOfPercents)
for position = 1, #binPercents do
binPercents[position] = binPercents[position]/sumOfPercents
end
-- last = 0 ; for ... ; last = last + value ; binPc[i] = last ; end
-- cumulative sum array
for position = 1, #binPercents do
if position == 1 then
else
binPercents[position] = binPercents[position-1]+binPercents[position]
end
end
printall(binPercents)
-- do a second RNG to determine what bin we're going to land in
secondRoll = math.random()
-- found flag
local found = 0
local binPosition = 1
-- search for matching range
while found == 0 do
if secondRoll > binPercents[binPosition] then
binPosition = binPosition + 1
else
found = 1
end
end
print("secondRoll: ", secondRoll)
binP = findBinPosition (l_limit, attributeBin)
l_limit = attributeBin[binP + binPosition - 1]
u_limit = attributeBin[binP + binPosition]
print("new l_limit: ", l_limit)
print("new u_limit: ", u_limit)
-- gen new value!
if rawValue > l_limit then
l_limit = rawValue + 1
end
range = u_limit - l_limit - 1
thirdRoll = math.random()
newValue = (range * thirdRoll ) + l_limit
print("New Value: ", newValue)
-- need to specify whether physical or mental here.
if type_string == "Physical" then
unit.body.physical_attrs[attrNumber-1].value = round(newValue,0)
if (unit.body.physical_attrs[attrNumber-1].value * 2) > 5000 then
unit.body.physical_attrs[attrNumber-1].max_value = 5000
else
unit.body.physical_attrs[attrNumber-1].max_value = (unit.body.physical_attrs[attrNumber-1].value * 2)
end
else
unit.status.current_soul.mental_attrs[attrNumber-1].value = round(newValue,0)
if (unit.status.current_soul.mental_attrs[attrNumber-1].value * 2) > 5000 then
unit.status.current_soul.mental_attrs[attrNumber-1].max_value = 5000
else
unit.status.current_soul.mental_attrs[attrNumber-1].max_value = (unit.status.current_soul.mental_attrs[attrNumber-1].value * 2)
end
end
else
print ("Value was determined too high on this pass")
end
end
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
function findBinPosition (binValue, attributeBin)
for tempValue = 1, #attributeBin do
if binValue == attributeBin[tempValue] then
return tempValue
end
end
end
function sum(t)
local sum = 0
for k,v in pairs(t) do
sum = sum + v
end
return sum
end
function getCitizen()
local ret={}
local units={}
local vs = dfhack.gui.getCurViewscreen()
if df.viewscreen_setupdwarfgamest:is_instance(vs) then
print("Embark!")
units=df.global.world.units.all
else
print("Not Embark!")
units=df.global.world.units.active
end
for num,unit in ipairs(units) do
if dfhack.units.isCitizen(unit) then
table.insert(ret,unit)
end
end
return ret
end
function calculateTraits()
end
-- --------------------------------------------------------------------------
-- --------------------------------------------------------------------------
-- Main
-- Initilization
local opt = ...
-- unit = dfhack.gui.getSelectedUnit()
if opt == nil then
print("Options: ")
print("boost_dwarf (SU)Selected Unit, (AU)All Units")
else if opt == "AU" then
if df.global.world.units.all == nil then
print("No Units! Aborting!")
else
print("Units, we got go!")
-- get all citizens!
citizens = getCitizen()
for num, unit in ipairs(citizens) do
race = unit.race
caste = unit.caste
selected_unit_caste = df.global.world.raws.creatures.all[race].caste[caste]
-- Attributes
boostAttributes(selectedUnitCaste, unit)
-- Traits
-- Skills
end
end
else if opt == "SU" then
if dfhack.gui.getSelectedUnit() == nil then
print("No Unit selected! Aborting!")
else
unit = dfhack.gui.getSelectedUnit()
race = unit.race
caste = unit.caste
selected_unit_caste = df.global.world.raws.creatures.all[race].caste[caste]
-- Attributes
boostAttributes(selectedUnitCaste, unit)
-- Traits
-- Skills
end
end
end
end
-- --------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment