Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: Natural efficient stacking script?  (Read 2377 times)

yaymeh

  • Bay Watcher
    • View Profile
Natural efficient stacking script?
« on: March 15, 2014, 09:10:08 am »

Hi!


I'm having a hard time getting started with dfhack. Trying to get stacks to combine

( because my dorfs are filling every available container with "stacks of 2 meat" or "stacks of 2 [distillable plant]" while they're also slowly dying of dehydration because there are no containers left for the still even with my constantly growing super-stockpile-of-epic-lag and I'm sick of the constant barrel micromana... HEY DON'T PUT THOSE 100 STACKS OF ONE ARROW INTO THOSE BINS, THOSE ARE FOR THE CARAVAN!!!) *eeeerm-back-to-topic-sorrry*


... this is my first idea about how this could be achieved in a natural looking and possibly efficient manner, but I didn't get far - well, didn't really get started... -.-

pseydo-pseydocode:
Code: [Select]
while true do

c=get_random_container()
ia=get_random_item(c)
ib=get_random_item(c)

sleeponsuccess = 1000 + 10000 / amount_of_stackable_items_in_fortress_or_something
sleeponfail = 100 + 100 * $failed;

if stack_stacks(a, b) then
 { sleep $sleeponsuccess; failed=0 }
else
 { sleep $sleeponfail; failed++ }
done;


function stack_stacks(item1, item2)
{
if (item1 == item2 || item1.type != item2.type || item1.material != item2.material || item1.weight + item2.weight > 200) do return false
else do {
item1.stack_size += item2.stack_size;
item1.age = (item1.age + item2.age) / 2
item1.temperature = (item1.temperature + item2.temperature) /2
item1.therestofallthoseproperties = dostuff(item1.thoseproperties, item2thoseproperties)

item2.delete_dat_thing;

return "yay!"
}

So far so "good"... love pseydocode... could write or read it all day... well... and that's how far I actually got for real:
Code: [Select]
df.world.items.all.each do |c|
if c.flags.container then
c.general_refs.each do |id|
if id.respond_to?(:item_id=) then

i= df.world.items.all.binsearch(id.item_id)
puts "mat #{i.mat_type} #{i.mat_index}"
puts "stack #{i.stack_size}"
puts i

# bwahahaha i hate ruby
# new hash-thingy["#{i.pos.x}#{i.pos.y}#{i.pos.z}][
end
end
end
end
# puts df.world.items.inspect

I CAN'T FIND SH*T!
( neither documentation about dfhack nor -out how ruby works T_T )

Uhm... HELP!!!!?
:D
« Last Edit: March 15, 2014, 09:11:54 am by yaymeh »
Logged

yaymeh

  • Bay Watcher
    • View Profile
Re: Natural efficient stacking script?
« Reply #1 on: March 15, 2014, 11:49:36 am »

Ok, that seems to do... uhm... something:
Code: [Select]
df.onupdate_register('test', 4200) { bruteforce }

def bruteforce
countc=0
failed=0;
df.world.items.all.each do |c|
if c.flags.container=true then
# puts c.class
if (c.class==DFHack::ItemBarrelst || c.class==DFHack::ItemToolst) then
stuff = Array.new
c.general_refs.each do |id|
if id.respond_to?(:item_id) then
i= df.world.items.all.binsearch(id.item_id)
stuff.push(i)
# puts "#{i.flags.weight_computed} #{i.class} #{i.weight} #{i.weight_fraction}"
end
if stuff.size > 1 then stuff.each { |a|
stuff.each { |b|
if (a == b || a.class != b.class || a.mat_type != b.mat_type || a.mat_index != b.mat_index) then failed=failed+1
else
(not a.flags.weight_computed ? a.calculateWeight() : false)
(not b.flags.weight_computed ? b.calculateWeight() : false)
if (a.weight+b.weight < 50 && a.stack_size > 0 && b.stack_size > 0)then
# puts c.class
puts "A item: #{a.type} weighs #{a.weight} stack #{a.stack_size} mat #{a.mat_type} #{a.mat_index}"
puts "B item: #{b.type} weighs #{b.weight} stack #{b.stack_size} mat #{b.mat_type} #{b.mat_index}"
a.weight = a.weight + b.weight
a.stack_size=a.stack_size+b.stack_size
b.stack_size=0
b.boiling_point=1
end
end
}
}
end end
end
end
end
#
# puts df.world.items.inspect
end
... but it's sooo far away from how I'd like i to work -.-


Those are my main problems:
- Couldn't figure out an efficient way of getting a random container... or 2 random items from a container.
- Also I don't know how to delete items except by letting something boil away.
- The bruteforce loop and the weight calculations are sort of resource heavy... and also can't do it slowly / carefully like this...
- I don't dare try bins - most things in there aren't stackable and df makes a habit of swallowing whole stacks of things to make one item... no idea how to test if an item is stackable without any silly business like scanning for stack>1 and putting all the item id's in a hash... urks...
- Also the code sucks T_T
« Last Edit: March 15, 2014, 04:43:33 pm by yaymeh »
Logged

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: Natural efficient stacking script?
« Reply #2 on: March 17, 2014, 06:31:55 pm »

DFHack apperas to be totaly lacking in documentation, I finally figured out that if I read the xml files that come with the source I could (mostly) figure out what to do to get stuff working in Lua, not sure about ruby...

I sugest you look at any and all other scripts you can get your hands on, a little copy-paste-edit can get you a long way when first starting out.
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: Natural efficient stacking script?
« Reply #3 on: March 17, 2014, 08:03:42 pm »

DFHack apperas to be totaly lacking in documentation, I finally figured out that if I read the xml files that come with the source I could (mostly) figure out what to do to get stuff working in Lua, not sure about ruby...

I sugest you look at any and all other scripts you can get your hands on, a little copy-paste-edit can get you a long way when first starting out.

The Lua API is easily the most documented thing there.

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: Natural efficient stacking script?
« Reply #4 on: March 18, 2014, 05:11:16 am »

I sugest you look at any and all other scripts you can get your hands on, a little copy-paste-edit can get you a long way when first starting out.
I have found that looking at relevant .cpp source files can help a lot as well.

Sometimes you can find the logic you need in a function that isn't exported for invocation from lua.
Logged

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: Natural efficient stacking script?
« Reply #5 on: March 19, 2014, 04:32:03 pm »

The Lua API may be "the most documented" but that documentaion is VERY spotty (it only covers the "global" functions). If you are generating the C++ header files and Lua bindings automaticly anyway shouldn't it be fairly easy to generate basic docs as well?
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: Natural efficient stacking script?
« Reply #6 on: March 19, 2014, 04:39:57 pm »

?


What exactly do you think is missing?

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: Natural efficient stacking script?
« Reply #7 on: March 19, 2014, 04:58:25 pm »

Something a little more readable than the raw xml?
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: Natural efficient stacking script?
« Reply #8 on: March 19, 2014, 05:29:24 pm »

...xml is specifically designed to be readable, and that's pretty damn readable. What kind of documentation are you thinking about for the structures, then?

Urist Da Vinci

  • Bay Watcher
  • [NATURAL_SKILL: ENGINEER:4]
    • View Profile
Re: Natural efficient stacking script?
« Reply #9 on: March 19, 2014, 08:05:37 pm »

An item, stackable or not, can have unique information associated with it. For example, contaminants coating the item, or kills made with the item. A job may require a specific item. How do you plan on handling situations like these?

yaymeh

  • Bay Watcher
    • View Profile
Re: Natural efficient stacking script?
« Reply #10 on: March 20, 2014, 03:38:25 am »

I didn'd see anything particularly problematic there at a first glance.

In most cases (especially barrels / pots where stack size + amount don't seem to be statistically confounded with properties), even just ignoring those properties would be a good solution as far as I can tell, at least as long as stack size is used to determines the chance for which one of the 2 stacks is being used as the enlarged/swallowed stack - all impact on gameplay should simply randomize itself away over a period of game time sufficiently small not to be noticeable. And if variance caused by this was too high, some manual extra independent randomizing should be able to solve that problem (average properties that have numeric values / randomize booleans still weighted by stack size but independent from each others and possibly with extra weighting for special properties like "always retain 'on fire' etc").

Guess of all stackable items, that would only leave ammunition to require extra attention? The problem here could result from potentially huge amount of stacks with size "1" that have in common that it's already been used which could lead to extreme spikes in prevalence of certain properties associated with that due to small stacks getting "swallowed" by big stacks of fresh ammunition most of the time until one of the small stacks is "very lucky" and suddenly dupes its properties to a whole big stack. The only problem that I can see with that would be auto-syndrome / poison stuff... those problems might simply be avoidable by checking the contaminant and stacking only ammunition with the same one. ( Or by just pretending that this reflects... sort of... semi-realistic... spreading of contaminants. :D )

Am I missing anything?
Logged

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: Natural efficient stacking script?
« Reply #11 on: March 20, 2014, 12:40:18 pm »

Well the best case would be just a simple html or text file where everything is listed in a TOC at the top and all the stuff that maters to the code generator but not users is stripped.
I suppose I could try to write such a tool :)
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: Natural efficient stacking script?
« Reply #12 on: March 20, 2014, 12:56:21 pm »

1. The fact that they're neatly organized into files and the fact that XML is really easily searched through regular expressions means that a table of contents is pretty unnecessary.

2. Can you give me an example of something that matters to the code generator but not users?
« Last Edit: March 20, 2014, 01:22:29 pm by Putnam »
Logged

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: Natural efficient stacking script?
« Reply #13 on: March 20, 2014, 01:20:04 pm »

I think it'll be easier for me to just write a simple document generator, I need a project anyway :p

When I'm done (in a week or so, maybe less), I'll just start a new thread and post it.
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: Natural efficient stacking script?
« Reply #14 on: March 21, 2014, 01:22:34 am »

Technically you could use xslt to generate html from xml(s) (like how docbook works). Practically the language is INSANE! I don't know one person who would use it (though i tried to make boost-book based on docbook with some success).
Pages: [1] 2