# LootLib

by Ter13
 Jun 12, 9:10 pm (Edited on Jun 12, 9:24 pm) Multiple items can be generated with weightless nodes: ```[ {"type":"/obj/item/meat","prob":80,"roll":"1d2"}, {"type":"/obj/item/hide","prob":50,"amt":2}, {"type":"/obj/item/bones","prob":90,"range":"2 to 3"}, {"type":"/obj/item/sinew","prob":5} ] ``` The above generator has a chance to drop everything in it. This is because it has no weighting. Instead, it uses probabilities. An item will always drop if it has no weight, the item before it in the list has tried to drop, and its probability passes. Let's say we want to always drop meat, but only drop one of a skull, sinew, or a horn: ```[ {"type":"/obj/item/meat","roll":"1d2"}, {"type":"/obj/item/skull","weight":1}, {"type":"/obj/item/sinew","weight":1}, {"type":"/obj/item/horn","weight":1} ] ``` In this case, because meat has no weight, and it comes before weighted items, it will always generate. Only one of the weighted items will generate. In this case, because the total weight of this array is 3, the chance for each of these weighted outcomes is 1 in 3. However, if we were to move meat from the front of the list to the back of the list: ```[ {"type":"/obj/item/skull","weight":1}, {"type":"/obj/item/sinew","weight":1}, {"type":"/obj/item/horn","weight":1}, {"type":"/obj/item/meat","roll":"1d2"} ] ``` This version of the generator would only generate meat if the horn was picked. If we were to move it into the middle of the list: ```[ {"type":"/obj/item/skull","weight":1}, {"type":"/obj/item/sinew","weight":1}, {"type":"/obj/item/meat","roll":"1d2"}, {"type":"/obj/item/horn","weight":1} ] ``` In this case, it would only generate if the sinew was generated. So your loot would either be a 1) skull, 2) a horn, or 3) a sinew and 1 to 2 peices of meat. Now let's say we only want horns, sinew, and meat to generate 10% of the time: ```[ {"type":"/obj/item/meat","roll":"1d2"}, {"type":"/obj/item/skull","weight":1,"prob":10}, {"type":"/obj/item/sinew","weight":1,"prob":10}, {"type":"/obj/item/horn","weight":1,"prob":10} ] ``` ```[ {"type":"/obj/item/meat","roll":"1d2"}, {"table":[ {"type":"/obj/item/skull","weight":1}, {"type":"/obj/item/sinew","weight":1}, {"type":"/obj/item/horn","weight":1} ],"prob":10} ] ``` We can add a probability to each item, OR, we can add an unweighted table with a 10% probability. The outcome will be the same, however, the table method will not generate a random number between 1 and 3 unless the prob(10) roll succeds, whereas the inline method will rand(1,3) first, pick and item, and then check if prob(10) succeeds, so their impact on random number generation will differ slightly. With the table argument, you can pass an id instead of an array of nodes. The id will be looked up in the generator_cache passed to the generate_loot function first, and if not found, will be looked up in the global.generator_cache list. If not found at all, it will simply be skipped. So let's say we wanted every animal to have similar rules to how it drops loot, but vary in how it works. We could use local table ids for this: creatures table: ```[ {"table":"flesh"}, {"table":"rare_products","prob":10} ] ``` This generator would just be the structure for setting up the final generator. We'd pass it to generate_loot(), which would then use additional generators you pass into the generator cache argument: flesh table: ```[ {"type":"/obj/item/meat","roll":"1d3"} {"type":"/obj/item/hide","prob":80} ] ``` rare_products table: ```[ {"weight":1,"type":"/obj/item/skull"} {"weight":1,"type":"/obj/item/horn"} {"weight":1,"type":"/obj/item/sinew"} ] ``` If we passed references to these loot tables in with a cache, we could use the creatures table as a basis, and then have each monster have its own flesh and rare_products table reference. When we kill that monster, we call ```var/c = global.generator_cachegenerate_loot(c["creature table"],list("flesh"=c[src.flesh_loot],"rare_products"=c[src.rare_loot])) ``` The seed table can now be pointed to the local cache we just built, and is now a complete loot table combining both rule sets.