|
| 1 | +-- Load array of possible Treasures once |
| 2 | +-- When exploring new chunk, generate number - compare with global config for treasure probability |
| 3 | +-- Calculate distance from start |
| 4 | +-- Pick treasure: Sum up the frequency of treasures, after checking for minimumDistance and distanceFrequencyBonus, generate random number based on frequency |
| 5 | +-- For each item in treasure: |
| 6 | + -- Random number, check probability |
| 7 | + -- Random number between min and max |
| 8 | + -- Multiply by multiplier |
| 9 | + -- Add item to treasure |
| 10 | + |
| 11 | +-- FUTURE: Add guarded treasures (enemy laser turrets or similar) |
| 12 | +-- FUTURE: Add random buildings/factories placed in the world (both friendly and enemy) |
| 13 | + |
| 14 | +local treasureConfig = require "treasure-config" |
| 15 | + |
| 16 | +local PROBABILITY_OF_TREASURE = 0.1 |
| 17 | + |
| 18 | +local function out(txt) |
| 19 | + local debug = true |
| 20 | + if debug then |
| 21 | + game.print(txt) |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +local function txtpos(pos) |
| 26 | + return "{" .. pos["x"] .. ", " .. pos["y"] .."}" |
| 27 | +end |
| 28 | + |
| 29 | +local function chooseTreasure(possible_treasures, distance, generator) |
| 30 | + local sum = 0 |
| 31 | + for _, treasure in ipairs(possible_treasures) do |
| 32 | + if distance >= treasure.minimumDistance then |
| 33 | + local treasureScore = treasure.frequency |
| 34 | + if treasure.distanceFrequencyBonus then |
| 35 | + treasureScore = treasureScore + treasure.distanceFrequencyBonus * distance |
| 36 | + end |
| 37 | + if treasureScore > 0 then |
| 38 | + sum = sum + treasureScore |
| 39 | + out("Sum is now " .. sum .. " after adding " .. treasure.name) |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + local chosenValue = generator() * sum |
| 45 | + out("Chosen value is " .. chosenValue .. " where sum was " .. sum .. " and distance " .. distance) |
| 46 | + |
| 47 | + for _, treasure in ipairs(possible_treasures) do |
| 48 | + if distance >= treasure.minimumDistance then |
| 49 | + local treasureScore = treasure.frequency |
| 50 | + if treasure.distanceFrequencyBonus then |
| 51 | + treasureScore = treasureScore + treasure.distanceFrequencyBonus * distance |
| 52 | + end |
| 53 | + if treasureScore > 0 then |
| 54 | + out("Decreasing " .. chosenValue .. "with " .. treasureScore) |
| 55 | + chosenValue = chosenValue - treasureScore |
| 56 | + end |
| 57 | + if chosenValue < 0 then |
| 58 | + out("Chosen treasure is " .. treasure.name) |
| 59 | + return treasure |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + return nil |
| 64 | +end |
| 65 | + |
| 66 | +local function chooseItems(treasure, generator) |
| 67 | + local chosen = {} |
| 68 | + for _, itemConfig in ipairs(treasure.items) do |
| 69 | + local add = not itemConfig.probability or generator() < itemConfig.probability |
| 70 | + if add then |
| 71 | + local multiplier = itemConfig.multiplier or 1 |
| 72 | + local count = generator(itemConfig.min, itemConfig.max) * multiplier |
| 73 | + table.insert(chosen, { name = itemConfig.name, count = count }) |
| 74 | + end |
| 75 | + end |
| 76 | + return chosen |
| 77 | +end |
| 78 | + |
| 79 | +local function placeTreasure(values) |
| 80 | + local chest = values.surface.create_entity({ name = values.container, position = values.position, force = values.force }) |
| 81 | + local inv = chest.get_inventory(defines.inventory.chest) |
| 82 | + |
| 83 | + for _, item in ipairs(values.items) do |
| 84 | + out("placed " .. item.count .. " " .. item.name .. " at " .. txtpos(values.position)) |
| 85 | + inv.insert(item) |
| 86 | + end |
| 87 | + return chest |
| 88 | +end |
| 89 | + |
| 90 | +local function treasure(surface, force, position) |
| 91 | + local placeable = surface.can_place_entity({ name = "iron-chest", position = position, force = force }) |
| 92 | + if not placeable then |
| 93 | + -- out("Not placeable at " .. txtpos(position)) |
| 94 | + return false |
| 95 | + end |
| 96 | + |
| 97 | + local allTreasures = treasureConfig.possible_treasures() |
| 98 | + local distanceFromStart = math.sqrt(position.x * position.x + position.y * position.y) |
| 99 | + game.print("Distance from start is " .. distanceFromStart) |
| 100 | + |
| 101 | + local chosenTreasure = chooseTreasure(allTreasures, distanceFromStart, global.generator) |
| 102 | + if not chosenTreasure then |
| 103 | + game.print("[Treasures] No treasure chosen at " .. txtpos(position) .. ", is the config working properly?") |
| 104 | + return |
| 105 | + end |
| 106 | + local chosenItems = chooseItems(chosenTreasure, global.generator) |
| 107 | + placeTreasure({ container = "iron-chest", treasure = chosenTreasure, surface = surface, items = chosenItems, |
| 108 | + position = position, force = force }) |
| 109 | +end |
| 110 | + |
| 111 | +local function onChunkGenerated(event) |
| 112 | + if event.surface.name ~= "nauvis" then |
| 113 | + return |
| 114 | + end |
| 115 | + |
| 116 | + -- Forces existing by default are "player", "enemy", "neutral" |
| 117 | + local force_name = "player" |
| 118 | + local chosen_force = game.forces[force_name] |
| 119 | + if not chosen_force then |
| 120 | + game.print("[Treasures] Unable to place treasure: There is no force named '" .. force_name .. "'") |
| 121 | + return |
| 122 | + end |
| 123 | + |
| 124 | + if global.generator() >= PROBABILITY_OF_TREASURE then |
| 125 | + -- out("Bad luck, no treasure at " .. txtpos(event.area.left_top) .. "/" .. txtpos(event.area.right_bottom)) |
| 126 | + return |
| 127 | + end |
| 128 | + |
| 129 | + for i = 1, 5 do |
| 130 | + local x = global.generator(event.area.left_top.x, event.area.right_bottom.x) + 0.5 |
| 131 | + local y = global.generator(event.area.left_top.y, event.area.right_bottom.y) + 0.5 |
| 132 | + x = (event.area.right_bottom.x - event.area.left_top.x) / 2 + event.area.left_top.x |
| 133 | + y = (event.area.right_bottom.y - event.area.left_top.y) / 2 + event.area.left_top.y |
| 134 | + local placed = treasure(event.surface, chosen_force, { x=x, y=y }) |
| 135 | + if placed then |
| 136 | + return |
| 137 | + end |
| 138 | + end |
| 139 | + out("No treasure in chunk " .. txtpos(event.area.left_top) .. " to " .. txtpos(event.area.right_bottom)) |
| 140 | +end |
| 141 | + |
| 142 | +script.on_init(function(event) |
| 143 | + global.generator = game.create_random_generator() |
| 144 | +end) |
| 145 | +script.on_event(defines.events.on_chunk_generated, onChunkGenerated) |
| 146 | +-- on_sector_scanned |
0 commit comments