Skip to content

Commit 7e92be9

Browse files
committed
Treasures: Add start of new mod (requires balancing)
1 parent 8dd78b3 commit 7e92be9

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

treasures_0.16.0/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Simon Forsberg
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

treasures_0.16.0/control.lua

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

treasures_0.16.0/info.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "treasures",
3+
"version": "0.16.0",
4+
"title": "Treasures",
5+
"author": "Zomis",
6+
"contact": "zomis2k@hotmail.com",
7+
"homepage": "https://github.com/Zomis/FactorioMods",
8+
"factorio_version": "0.16",
9+
"dependencies": ["base >= 0.16"],
10+
"description": "Encourage exploration even more by placing random chests of treasures on the map"
11+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
local function possible_treasures()
2+
return {
3+
{
4+
name = "start",
5+
frequency = 1000,
6+
distanceFrequencyBonus = -20,
7+
minimumDistance = 0,
8+
items = {
9+
{ name = "lab", min = 1, max = 3 },
10+
{ name = "science-pack-1", probability = 0.42, min = 10, max = 100 },
11+
{ name = "science-pack-2", probability = 0.42, min = 10, max = 100 }
12+
}
13+
},
14+
{
15+
name = "basic",
16+
frequency = 10,
17+
distanceFrequencyBonus = 0.001,
18+
minimumDistance = 2,
19+
items = {
20+
{ name = "iron-plate", min = 10, max = 20 },
21+
{ name = "copper-plate", probability = 0.42, min = 10, max = 100 },
22+
{ name = "iron-plate", probability = 0.1, min = 1, max = 3, multiplier = 3 },
23+
{ name = "transport-belt", probability = 0.2, min = 1, max = 3, multiplier = 3 }
24+
}
25+
},
26+
{
27+
name = "level 2",
28+
frequency = 10,
29+
minimumDistance = 2,
30+
items = {
31+
{ name = "steel-plate", min = 10, max = 20 },
32+
{ name = "stone", probability = 0.42, min = 10, max = 100 },
33+
}
34+
}
35+
}
36+
end
37+
--
38+
--
39+
--
40+
--
41+
--[[
42+
package:
43+
- contains multiple items
44+
- rarity
45+
- minimum-distance
46+
47+
item:
48+
- probability
49+
- min amount, max amount
50+
- name
51+
52+
53+
iron-plate
54+
transport-belt, inserter, fast-inserter
55+
straight-rail, locomotive, cargo-wagon
56+
assembling-machine-2
57+
lab, science-pack-1, science-pack-2, science-pack-3...
58+
fast-transport-belt
59+
60+
]]
61+
62+
63+
return { possible_treasures = possible_treasures }

0 commit comments

Comments
 (0)