Skip to content

Commit

Permalink
added buffer entities and random minimum distance buffers from both f…
Browse files Browse the repository at this point in the history
…ake and real entities
  • Loading branch information
dcolestock committed Jun 25, 2018
1 parent 054560a commit 07f0747
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 54 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Daedeross
Copyright (c) 2016 Anythingapplied

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion config/buildinfo.json
Expand Up @@ -8,6 +8,6 @@
"author": "Anythingapplied",
"homepage": "https://github.com/anythingapplied/WhistleStop",
"contact": "n/a",
"description": "Spawns massive furnaces and assembly machines around the map to make the game all about trains.",
"description": "Spawns massive furnaces and assembly machines around the map to make the game all building trains to connect them",
"dependencies": ["base >= 0.16"] }
}
45 changes: 23 additions & 22 deletions src/control.lua
Expand Up @@ -8,22 +8,34 @@ require("scripts.controlSpawnEvent")
--TODO: Balancing: Crafting speed, power, pollution, recipe factor
--TODO: Balancing: Rarer as distance gets further? Multiple types of machines based on distance? Tech to enhance machines?
--TODO: helmod doesn't recognize productivity modules as valid for big iron recipe
--TODO: Algorithm improvement to distance check to improve performance

local DEBUG = false --used for debug, users should not enable

-- Stat Tracking
global.whistlestats = {furnace_count=0, assembly_count=0, valid_chunk_count=0}
global.whistlelocations = {{x=0,y=0}} -- fake location at spawn to prevent ones close to starting area
global.whistlestats = {}
global.whistlestats["big-furnace"] = 0
global.whistlestats["big-assembly"] = 0
global.whistlestats.valid_chunk_count = 0

-- Tracks location of big factory and or buffer locations, starting with a buffer location at spawn
global.whistlelocations = {{x=0, y=0, mindist=2 * settings.global["whistle-min-distance"].value}}

-- Registers a new location of a big factory or buffer location with a random minimum distance threshhold
function addPoint(center)
local mindist = settings.global["whistle-min-distance"].value
table.insert(global.whistlelocations, {x=center.x, y=center.y, mindist=math.random(mindist, 2*mindist)})
end

-- Function that will return true 'percent' of the time.
function probability(percent)
return math.random() <= percent
end

-- Returns true of no big structures within whistle-min-distances
-- Returns true if no big structures within minimum distance threshholds
function distanceOkay(a)
for k,v in pairs(global.whistlelocations) do
if (a.x-v.x)^2+(a.y-v.y)^2 < settings.global["whistle-min-distance"].value^2 then
if (a.x-v.x)^2 + (a.y-v.y)^2 < v.mindist^2 then
return false
end
end
Expand All @@ -35,7 +47,7 @@ script.on_event({defines.events.on_chunk_generated},
if probability(0.005) then -- Initial probability filter to give the map a more random spread
return
end

-- Chunk center plus random variance so they aren't always chunk aligned
local center = {
x=(e.area.left_top.x+e.area.right_bottom.x)/2 + math.random(-8,8),
Expand All @@ -46,25 +58,14 @@ script.on_event({defines.events.on_chunk_generated},
end

global.whistlestats.valid_chunk_count = global.whistlestats.valid_chunk_count + 1


local assembly_to_furnace_ratio = 1.2 -- How many assembly machines you want per furnace spawn

if probability(1/(1+assembly_to_furnace_ratio)) then
-- Spawn big furnace
if DEBUG then
game.print("A big furnace spawn attempt at " .. center.x .. "," .. center.y .. " (" .. global.whistlestats.furnace_count .. "/" .. global.whistlestats.valid_chunk_count .. ")")
end

spawn(center, e.surface, "big-furnace")
else
-- Spawn big assembly machine
if DEBUG then
game.print("A big assembly machine spawn attempt at " .. center.x .. "," .. center.y .. " (" .. global.whistlestats.assembly_count .. "/" .. global.whistlestats.valid_chunk_count .. ")")
end

spawn(center, e.surface, "big-assembly")
if probability(0.3) then -- Insert fake big factory placeholder to cause more spreading out
addPoint(center)
return
end

spawn(center, e.surface)

end
)

Expand Down
22 changes: 11 additions & 11 deletions src/prototypes/bigassembly.lua
Expand Up @@ -10,6 +10,17 @@ bigassembly.minable = nil
bigassembly.fast_replaceable_group = nil
bigassembly.dying_explosion = "big-explosion"

bigassembly.collision_box = {{-8.1, -8.1}, {8.1, 8.1}}
bigassembly.selection_box = {{-8.8, -9}, {8.8, 9}}
bigassembly.drawing_box = {{-8.8, -8.8}, {8.8, 8.8}}

bigassembly.crafting_categories = {"big-recipe"}
bigassembly.crafting_speed = 40

bigassembly.energy_usage = "1500kW"
bigassembly.ingredient_count = 10
bigassembly.module_specification.module_slots = 5

local function fluidBox(type, position)
retvalue = {
production_type = type,
Expand All @@ -35,17 +46,6 @@ bigassembly.fluid_boxes = {
off_when_no_fluid_recipe = true
}

bigassembly.collision_box = {{-8.1, -8.1}, {8.1, 8.1}}
bigassembly.selection_box = {{-8.8, -9}, {8.8, 9}}
bigassembly.drawing_box = {{-8.8, -8.8}, {8.8, 8.8}}

bigassembly.crafting_categories = {"big-recipe"}
bigassembly.crafting_speed = 40

bigassembly.energy_usage = "1500kW"
bigassembly.ingredient_count = 10
bigassembly.module_specification.module_slots = 5

-- Scale graphics by a factor and correct animation speed
local function bumpUp(animation, factor)
animation.scale = factor
Expand Down
1 change: 1 addition & 0 deletions src/scripts/controlSpawnEvent.lua
Expand Up @@ -8,6 +8,7 @@ local function placeLoader(entity, position, type, direction)
end

local function on_built_event(event)
local entity = event.created_entity
local center = entity.position

if entity.name == "big-furnace" then
Expand Down
31 changes: 13 additions & 18 deletions src/scripts/spawnFactory.lua
Expand Up @@ -26,28 +26,23 @@ function clearArea(center, surface)
return true
end

function spawn(center, surface, itemname)
function spawn(center, surface)
local ce = surface.create_entity --save typing
local force = game.forces.player
local assembly_to_furnace_ratio = 1.2 -- How many assembly machines you want per furnace spawn

if clearArea(center, surface) then
table.insert(global.whistlelocations, center)
if itemname == "big-furnace" then
global.whistlestats.furnace_count = global.whistlestats.furnace_count + 1
local en = ce{name = "big-furnace", position = {center.x, center.y}, force = force}
local event = {created_entity=en, player_index=1}
script.raise_event(defines.events.on_built_entity, event)
en.minable = false
--en.destructable = false
elseif itemname == "big-assembly" then
global.whistlestats.assembly_count = global.whistlestats.assembly_count + 1
local en = ce{name = "big-assembly", position = {center.x, center.y}, force = force}
local event = {created_entity=en, player_index=1}
script.raise_event(defines.events.on_built_entity, event)
en.minable = false
--en.destructable = false
elseif DEBUG then
game.print("Error: itemname not recognized")
addPoint(center)

if probability(1/(1+assembly_to_furnace_ratio)) then
entityname = "big-furnace"
else
entityname = "big-assembly"
end

global.whistlestats[entityname] = global.whistlestats[entityname] + 1
local en = ce{name = entityname, position = {center.x, center.y}, force = force}
local event = {created_entity=en, player_index=1}
script.raise_event(defines.events.on_built_entity, event)
end
end
2 changes: 1 addition & 1 deletion src/settings.lua
Expand Up @@ -3,7 +3,7 @@ data:extend({
type = "int-setting",
name = "whistle-min-distance",
setting_type = "runtime-global",
default_value = 200,
default_value = 180,
order = "a",
}
})

0 comments on commit 07f0747

Please sign in to comment.