1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-03-11 14:49:24 +02:00

more modules and tweaks

This commit is contained in:
Gerkiz 2020-10-30 17:32:40 +01:00
parent d586bddd54
commit 75ebb4a3b2
13 changed files with 1180 additions and 434 deletions

View File

@ -28,6 +28,7 @@ shutdown_game=*** Soft-reset is disabled! Server will shutdown. Most likely beca
notify_shutdown=Soft-reset is disabled! Server will shutdown. Most likely because of updates.
train_taking_damage=[color=blue]Comfylatron:[/color]\nTrain is taking heavy damage.\nDeploying defense mechanisms.
entity_limit_reached=__1__ limit reached. Purchase more slots at the market!
found_car=__1__ has found a car underneath the rumble!
[gui]

View File

@ -28,6 +28,7 @@ shutdown_game=*** Soft-reset is disabled! Server will shutdown. Most likely beca
notify_shutdown=Soft-reset is disabled! Server will shutdown. Most likely because of updates.
train_taking_damage=[color=blue]Comfylatron:[/color]\nTrain is taking heavy damage.\nDeploying defense mechanisms.
entity_limit_reached=__1__ limit reached. Purchase more slots at the market!
found_car=__1__ has found a car underneath the rumble!
[gui]

View File

@ -28,6 +28,7 @@ shutdown_game=*** Soft-reset is disabled! Server will shutdown. Most likely beca
notify_shutdown=Soft-reset is disabled! Server will shutdown. Most likely because of updates.
train_taking_damage=[color=blue]Comfylatron:[/color]\nTrain is taking heavy damage.\nDeploying defense mechanisms.
entity_limit_reached=__1__ limit reached. Purchase more slots at the market!
found_car=__1__ has found a car underneath the rumble!
[gui]

View File

@ -2,65 +2,38 @@ local Event = require 'utils.event'
local Server = require 'utils.server'
local Global = require 'utils.global'
local insert = table.insert
local max = math.max
local this = {
difficulties = {
[1] = {
name = 'Peaceful',
name = "I'm too young to die",
index = 1,
value = 0.25,
color = {r = 0.00, g = 0.45, b = 0.00},
print_color = {r = 0.00, g = 0.8, b = 0.00},
count = 0
},
[2] = {
name = 'Piece of cake',
index = 2,
value = 0.5,
color = {r = 0.00, g = 0.35, b = 0.00},
print_color = {r = 0.00, g = 0.6, b = 0.00},
count = 0
},
[3] = {
name = 'Easy',
index = 3,
value = 0.75,
color = {r = 0.00, g = 0.25, b = 0.00},
print_color = {r = 0.00, g = 0.4, b = 0.00},
count = 0
},
[4] = {
name = 'Normal',
[2] = {
name = 'Hurt me plenty',
index = 2,
value = 1,
index = 4,
color = {r = 0.00, g = 0.00, b = 0.25},
print_color = {r = 0.0, g = 0.0, b = 0.5},
count = 0
},
[5] = {
name = 'Hard',
index = 5,
[3] = {
name = 'Ultra-violence',
index = 3,
value = 1.5,
color = {r = 0.25, g = 0.00, b = 0.00},
print_color = {r = 0.4, g = 0.0, b = 0.00},
color = {r = 255, g = 128, b = 0.00},
print_color = {r = 255, g = 128, b = 0.00},
count = 0
},
[6] = {
[4] = {
name = 'Nightmare',
index = 6,
index = 4,
value = 3,
color = {r = 0.35, g = 0.00, b = 0.00},
print_color = {r = 0.6, g = 0.0, b = 0.00},
count = 0
},
[7] = {
name = 'Impossible',
index = 7,
value = 5,
color = {r = 0.45, g = 0.00, b = 0.00},
print_color = {r = 0.8, g = 0.0, b = 0.00},
color = {r = 255, g = 0.00, b = 0.00},
print_color = {r = 255, g = 0.0, b = 0.00},
count = 0
}
},
@ -78,7 +51,7 @@ local this = {
difficulty_poll_closing_timeout = 54000,
difficulty_player_votes = {},
gui_width = 108,
name = 'Easy',
name = "I'm too young to die",
button_tooltip = nil
}

View File

@ -4,21 +4,19 @@ local Event = require 'utils.event'
local Server = require 'utils.server'
local Map_score = require 'comfy_panel.map_score'
local BiterRolls = require 'modules.wave_defense.biter_rolls'
local BuriedEnemies = require 'modules.wave_defense.buried_enemies'
local Loot = require 'maps.mountain_fortress_v3.loot'
local Pets = require 'maps.mountain_fortress_v3.biter_pets'
local RPG_Settings = require 'modules.rpg.table'
local Functions = require 'modules.rpg.functions'
local Mining = require 'maps.mountain_fortress_v3.mining'
local Terrain = require 'maps.mountain_fortress_v3.terrain'
local BiterHealthBooster = require 'modules.biter_health_booster'
local Difficulty = require 'maps.mountain_fortress_v3.difficulty_vote'
local Traps = require 'maps.mountain_fortress_v3.traps'
local Locomotive = require 'maps.mountain_fortress_v3.locomotive'
local ExplosiveBullets = require 'maps.mountain_fortress_v3.explosive_gun_bullets'
local Alert = require 'utils.alert'
local Task = require 'utils.task'
local Token = require 'utils.token'
--local HD = require 'modules.hidden_dimension.main'
-- tables
local WPT = require 'maps.mountain_fortress_v3.table'
@ -32,6 +30,21 @@ local abs = math.abs
local sqrt = math.sqrt
local round = math.round
local chests = {
'wooden-chest',
'iron-chest',
'steel-chest',
'crash-site-chest-1',
'crash-site-chest-2',
'crash-site-spaceship-wreck-big-1',
'crash-site-spaceship-wreck-big-2',
'crash-site-spaceship-wreck-medium-1',
'crash-site-spaceship-wreck-medium-2',
'crash-site-spaceship-wreck-medium-3'
}
local size_chests = #chests
local treasure_chest_messages = {
({'entity.treasure_1'}),
({'entity.treasure_2'}),
@ -91,6 +104,50 @@ local reset_game =
end
)
local function get_random_weighted(weighted_table, item_index, weight_index)
local total_weight = 0
item_index = item_index or 1
weight_index = weight_index or 2
for _, w in pairs(weighted_table) do
total_weight = total_weight + w[weight_index]
end
local index = random() * total_weight
local weight_sum = 0
for _, w in pairs(weighted_table) do
weight_sum = weight_sum + w[weight_index]
if weight_sum >= index then
return w[item_index]
end
end
end
local function on_entity_removed(data)
local entity = data.entity
local this = data.this
local upg = this.upgrades
local built = {
['land-mine'] = upg.landmine.built,
['flamethrower-turret'] = upg.flame_turret.built
}
local validator = {
['land-mine'] = 'landmine',
['flamethrower-turret'] = 'flame_turret'
}
local name = validator[entity.name]
if built[entity.name] and entity.force.index == 1 then
this.upgrades[name].built = this.upgrades[name].built - 1
if this.upgrades[name].built <= 0 then
this.upgrades[name].built = 0
end
end
end
local function set_objective_health(final_damage_amount)
local this = WPT.get()
if final_damage_amount == 0 then
@ -174,104 +231,36 @@ local function protect_entities(event)
end
end
local function hidden_biter(entity)
local surface = entity.surface
local h = floor(abs(entity.position.y))
local m = 1 / Terrain.level_depth
local count = floor(random(0, h + Terrain.level_depth) * m) + 1
local position = surface.find_non_colliding_position('small-biter', entity.position, 16, 0.5)
if not position then
position = entity.position
end
local biters = WPT.get('biters')
if biters.amount >= biters.limit then
local function hidden_biter_pet(player, entity)
if random(1, 1024) ~= 1 then
return
end
BiterRolls.wave_defense_set_unit_raffle(h * 0.20)
local pos = entity.position
for _ = 1, count, 1 do
local unit
if random(1, 3) == 1 then
unit = surface.create_entity({name = BiterRolls.wave_defense_roll_spitter_name(), position = position})
biters.amount = biters.amount + 1
else
unit = surface.create_entity({name = BiterRolls.wave_defense_roll_biter_name(), position = position})
biters.amount = biters.amount + 1
end
if random(1, 64) == 1 then
BiterHealthBooster.add_boss_unit(unit, m * h * 5 + 1, 0.38)
biters.amount = biters.amount + 1
end
end
end
local function hidden_worm(entity)
local biters = WPT.get('biters')
if biters.amount >= biters.limit then
return
end
BiterRolls.wave_defense_set_worm_raffle(sqrt(entity.position.x ^ 2 + entity.position.y ^ 2) * 0.20)
entity.surface.create_entity({name = BiterRolls.wave_defense_roll_worm_name(), position = entity.position})
biters.amount = biters.amount + 1
end
local function hidden_biter_pet(event)
if random(1, 2048) ~= 1 then
return
end
BiterRolls.wave_defense_set_unit_raffle(sqrt(event.entity.position.x ^ 2 + event.entity.position.y ^ 2) * 0.25)
BiterRolls.wave_defense_set_unit_raffle(sqrt(pos.x ^ 2 + pos.y ^ 2) * 0.25)
local unit
if random(1, 3) == 1 then
unit =
event.entity.surface.create_entity(
{name = BiterRolls.wave_defense_roll_spitter_name(), position = event.entity.position}
)
unit = entity.surface.create_entity({name = BiterRolls.wave_defense_roll_spitter_name(), position = pos})
else
unit =
event.entity.surface.create_entity(
{name = BiterRolls.wave_defense_roll_biter_name(), position = event.entity.position}
)
unit = entity.surface.create_entity({name = BiterRolls.wave_defense_roll_biter_name(), position = pos})
end
Pets.biter_pets_tame_unit(game.players[event.player_index], unit, true)
Pets.biter_pets_tame_unit(game.players[player.index], unit, true)
end
local function hidden_treasure(event)
local player = game.players[event.player_index]
local function hidden_treasure(player, entity)
local rpg = RPG_Settings.get('rpg_t')
local magic = rpg[player.index].magicka
local name = Difficulty.get('name')
if name == 'Easy' then
if random(1, 220) ~= 1 then
return
end
elseif name == 'Normal' then
if random(1, 320) ~= 1 then
return
end
elseif name == 'Hard' then
if random(1, 420) ~= 1 then
return
end
elseif name == 'Insane' then
if random(1, 520) ~= 1 then
return
end
end
if magic > 50 then
local msg = rare_treasure_chest_messages[random(1, #rare_treasure_chest_messages)]
Alert.alert_player(player, 5, msg)
Loot.add_rare(event.entity.surface, event.entity.position, 'wooden-chest', magic)
Loot.add_rare(entity.surface, entity.position, 'wooden-chest', magic)
return
end
local msg = treasure_chest_messages[random(1, #treasure_chest_messages)]
Alert.alert_player(player, 5, msg)
Loot.add(event.entity.surface, event.entity.position, 'wooden-chest')
Alert.alert_player(player, 5, msg, nil, nil, 0.3)
Loot.add(entity.surface, entity.position, chests[random(1, size_chests)])
end
local function biters_chew_rocks_faster(event)
@ -292,6 +281,7 @@ local function biters_chew_rocks_faster(event)
end
local projectiles = {'grenade', 'explosive-rocket', 'grenade', 'explosive-rocket', 'explosive-cannon-projectile'}
local function angry_tree(entity, cause)
if entity.type ~= 'tree' then
return
@ -301,10 +291,10 @@ local function angry_tree(entity, cause)
return
end
if random(1, 4) == 1 then
hidden_biter(entity)
BuriedEnemies.buried_biter(entity.surface, entity.position)
end
if random(1, 8) == 1 then
hidden_worm(entity)
BuriedEnemies.buried_worm(entity.surface, entity.position)
end
if random(1, 32) ~= 1 then
return
@ -333,12 +323,244 @@ local function angry_tree(entity, cause)
end
local function give_coin(player)
local coin_amount = WPT.get('coin_amount')
if coin_amount >= 1 then
player.insert({name = 'coin', count = coin_amount})
local breached_wall = WPT.get('breached_wall')
local coin = breached_wall
if breached_wall >= 10 then
coin = random(5, 10)
end
player.insert({name = 'coin', count = coin})
end
local mining_events = {
{
function()
end,
300000,
'Nothing'
},
{
function()
end,
16384,
'Nothing'
},
{
function()
end,
16384,
'Nothing'
},
{
function()
end,
8192,
'Nothing'
},
{
function()
end,
4096,
'Nothing'
},
{
function(entity)
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
BuriedEnemies.buried_biter(entity.surface, entity.position, 1)
entity.destroy()
end,
4096,
'Angry Biter #2'
},
{
function()
end,
2048,
'Nothing'
},
{
function(entity)
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
BuriedEnemies.buried_biter(entity.surface, entity.position)
entity.destroy()
end,
512,
'Angry Biter #2'
},
{
function(entity)
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
BuriedEnemies.buried_worm(entity.surface, entity.position)
entity.destroy()
end,
2048,
'Angry Worm'
},
{
function(entity)
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
Traps(entity.surface, entity.position)
entity.destroy()
end,
2048,
'Dangerous Trap'
},
{
function(entity, index)
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
local player = game.get_player(index)
if entity.type == 'tree' then
angry_tree(entity, player.character)
entity.destroy()
end
end,
1024,
'Angry Tree'
},
{
function(entity, index)
local player = game.get_player(index)
hidden_treasure(player, entity)
end,
1024,
'Treasure_Tier_2'
},
{
function(entity, index)
local player = game.get_player(index)
hidden_treasure(player, entity)
end,
512,
'Treasure_Tier_3'
},
{
function(entity, index)
local player = game.get_player(index)
hidden_treasure(player, entity)
end,
256,
'Treasure_Tier_4'
},
{
function(entity, index)
local player = game.get_player(index)
hidden_treasure(player, entity)
end,
128,
'Treasure_Tier_5'
},
{
function(entity, index)
local player = game.get_player(index)
hidden_treasure(player, entity)
end,
64,
'Treasure_Tier_6'
},
{
function(entity, index)
local player = game.get_player(index)
hidden_treasure(player, entity)
end,
32,
'Treasure_Tier_7'
},
{
function(entity, index)
local player = game.get_player(index)
hidden_treasure(player, entity)
end,
16,
'Treasure_Tier_8'
},
{
function(entity, index)
local player = game.get_player(index)
Public.unstuck_player(index)
hidden_biter_pet(player, entity)
end,
256,
'Pet'
},
{
function(entity, index)
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
local position = entity.position
local surface = entity.surface
surface.create_entity({name = 'biter-spawner', position = position, force = 'enemy'})
Public.unstuck_player(index)
end,
512,
'Nest'
},
{
function(entity)
local position = entity.position
local surface = entity.surface
surface.create_entity({name = 'compilatron', position = position, force = 'player'})
end,
64,
'Friendly Compilatron'
},
{
function(entity)
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
local position = entity.position
local surface = entity.surface
surface.create_entity({name = 'compilatron', position = position, force = 'enemy'})
end,
128,
'Enemy Compilatron'
},
{
function(entity, index)
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
local position = entity.position
local surface = entity.surface
surface.create_entity({name = 'car', position = position, force = 'player'})
Public.unstuck_player(index)
local player = game.players[index]
local msg = ({'entity.found_car', player.name})
Alert.alert_player(player, 15, msg)
end,
32,
'Car'
}
}
local function on_player_mined_entity(event)
local this = WPT.get()
local entity = event.entity
@ -358,26 +580,12 @@ local function on_player_mined_entity(event)
return
end
local upg = this.upgrades
local built = {
['land-mine'] = upg.landmine.built,
['flamethrower-turret'] = upg.flame_turret.built
local d = {
entity = entity,
this = this
}
local validator = {
['land-mine'] = 'landmine',
['flamethrower-turret'] = 'flame_turret'
}
local name = validator[entity.name]
if built[entity.name] and entity.force.index == 1 then
this.upgrades[name].built = this.upgrades[name].built - 1
if this.upgrades[name].built <= 0 then
this.upgrades[name].built = 0
end
end
on_entity_removed(d)
if disabled_threats[entity.name] then
return
@ -387,7 +595,7 @@ local function on_player_mined_entity(event)
this.mined_scrap = this.mined_scrap + 1
Mining.on_player_mined_entity(event)
if entity.type == 'tree' then
if random(1, 2) == 1 then
if random(1, 3) == 1 then
give_coin(player)
end
else
@ -396,31 +604,9 @@ local function on_player_mined_entity(event)
if rpg_char.stone_path then
entity.surface.set_tiles({{name = 'stone-path', position = entity.position}}, true)
end
if Locomotive.is_around_train(entity) then
entity.destroy()
return
end
hidden_biter_pet(event)
hidden_treasure(event)
if random(1, 32) == 1 then
hidden_biter(event.entity)
entity.destroy()
return
end
if random(1, 512) == 1 then
hidden_worm(event.entity)
entity.destroy()
return
end
if random(1, 512) == 1 then
Traps(entity.surface, entity.position)
return
end
angry_tree(event.entity, game.players[event.player_index].character)
entity.destroy()
local func = get_random_weighted(mining_events)
func(entity, player.index)
end
end
@ -438,26 +624,12 @@ local function on_robot_mined_entity(event)
return
end
local upg = this.upgrades
local built = {
['land-mine'] = upg.landmine.built,
['flamethrower-turret'] = upg.flame_turret.built
local d = {
entity = entity,
this = this
}
local validator = {
['land-mine'] = 'landmine',
['flamethrower-turret'] = 'flame_turret'
}
local name = validator[entity.name]
if built[entity.name] and entity.force.index == 1 then
this.upgrades[name].built = this.upgrades[name].built - 1
if this.upgrades[name].built <= 0 then
this.upgrades[name].built = 0
end
end
on_entity_removed(d)
end
local function get_damage(event)
@ -641,26 +813,12 @@ local function on_entity_died(event)
return
end
local upg = this.upgrades
local built = {
['land-mine'] = upg.landmine.built,
['flamethrower-turret'] = upg.flame_turret.built
local d = {
entity = entity,
this = this
}
local validator = {
['land-mine'] = 'landmine',
['flamethrower-turret'] = 'flame_turret'
}
local name = validator[entity.name]
if built[entity.name] and entity.force.index == 1 then
this.upgrades[name].built = this.upgrades[name].built - 1
if this.upgrades[name].built <= 0 then
this.upgrades[name].built = 0
end
end
on_entity_removed(d)
if event.cause then
if event.cause.valid then
@ -727,13 +885,13 @@ local function on_entity_died(event)
return
end
if random(1, 32) == 1 then
hidden_biter(entity)
BuriedEnemies.buried_biter(entity.surface, entity.position)
Mining.entity_died_randomness(data)
entity.destroy()
return
end
if random(1, 64) == 1 then
hidden_worm(entity)
BuriedEnemies.buried_worm(entity.surface, entity.position)
Mining.entity_died_randomness(data)
entity.destroy()
return
@ -766,6 +924,16 @@ function Public.set_scores()
end
end
function Public.unstuck_player(index)
local player = game.get_player(index)
local surface = player.surface
local position = surface.find_non_colliding_position('character', player.position, 32, 0.5)
if not position then
return
end
player.teleport(position, surface)
end
function Public.loco_died()
local this = WPT.get()
local surface = game.surfaces[this.active_surface_index]

View File

@ -4,6 +4,7 @@ local Public = {}
local random = math.random
local abs = math.abs
local floor = math.floor
local sqrt = math.sqrt
local blacklist = {
['atomic-bomb'] = true,
@ -15,9 +16,15 @@ local blacklist = {
['artillery-wagon'] = true,
['artillery-turret'] = true,
['landfill'] = true,
['fluid-wagon'] = true
['fluid-wagon'] = true,
['pistol'] = true
}
function Public.get_distance(position)
local difficulty = sqrt(position.x ^ 2 + position.y ^ 2) * 0.0001
return difficulty
end
function Public.add(surface, position, chest)
local budget = 48 + abs(position.y) * 1.75
budget = budget * random(25, 175) * 0.01
@ -33,7 +40,16 @@ function Public.add(surface, position, chest)
budget = floor(budget) + 1
local item_stacks = LootRaffle.roll(budget, 8, blacklist)
local amount = random(1, 5)
local base_amount = 12 * amount
local distance_mod = Public.get_distance(position)
local result = base_amount + budget + distance_mod
local c = game.entity_prototypes[chest]
local slots = c.get_inventory_size(defines.inventory.chest)
local item_stacks = LootRaffle.roll(result, slots, blacklist)
local container = surface.create_entity({name = chest, position = position, force = 'neutral'})
for _, item_stack in pairs(item_stacks) do
container.insert(item_stack)
@ -62,9 +78,18 @@ function Public.add_rare(surface, position, chest, magic)
chest = 'crash-site-chest-' .. random(1, 2)
end
local amount = random(1, 5)
local base_amount = 12 * amount
local distance_mod = Public.get_distance(position)
budget = floor(budget) + 1
local item_stacks = LootRaffle.roll(budget, 8, blacklist)
local result = base_amount + budget + distance_mod
local c = game.entity_prototypes[chest]
local slots = c.get_inventory_size(defines.inventory.chest)
local item_stacks = LootRaffle.roll(result, slots, blacklist)
local container = surface.create_entity({name = chest, position = position, force = 'neutral'})
for _, item_stack in pairs(item_stacks) do
container.insert(item_stack)

View File

@ -10,7 +10,6 @@ require 'modules.rocks_yield_ore_veins'
require 'modules.spawners_contain_biters'
require 'modules.biters_yield_coins'
require 'modules.wave_defense.main'
require 'modules.mineable_wreckage_yields_scrap'
require 'modules.charging_station'
local IC = require 'maps.mountain_fortress_v3.ic.table'
@ -155,7 +154,7 @@ local set_difficulty = function()
if wave_defense_table.threat <= 0 then
wave_defense_table.wave_interval = 1000
end
if name == 'Insane' then
if name == 'Nightmare' then
wave_defense_table.wave_interval = 1800
else
wave_defense_table.wave_interval = 3600 - player_count * 60
@ -169,7 +168,7 @@ local set_difficulty = function()
return
end
if name == 'Insane' then
if name == 'Nightmare' then
Collapse.set_amount(10)
elseif collapse_amount then
Collapse.set_amount(collapse_amount)
@ -642,7 +641,7 @@ local boost_difficulty = function()
local force = game.forces.player
if name == 'Easy' then
if name == "I'm too young to die" then
rpg_extra.difficulty = 1
force.manual_mining_speed_modifier = force.manual_mining_speed_modifier + 1
force.character_running_speed_modifier = 0.2
@ -657,7 +656,7 @@ local boost_difficulty = function()
WPT.set().spidertron_unlocked_at_wave = 11
WPT.set().difficulty_set = true
WD.set_biter_health_boost(1.2)
elseif name == 'Normal' then
elseif name == 'Hurt me plenty' then
rpg_extra.difficulty = 0.5
force.manual_mining_speed_modifier = force.manual_mining_speed_modifier + 0.5
force.character_running_speed_modifier = 0.1
@ -672,7 +671,7 @@ local boost_difficulty = function()
WPT.set().spidertron_unlocked_at_wave = 16
WPT.set().difficulty_set = true
WD.set_biter_health_boost(1.7)
elseif name == 'Hard' then
elseif name == 'Ultra-violence' then
rpg_extra.difficulty = 0
force.character_running_speed_modifier = 0
force.manual_crafting_speed_modifier = 0
@ -686,7 +685,7 @@ local boost_difficulty = function()
WPT.set().spidertron_unlocked_at_wave = 21
WPT.set().difficulty_set = true
WD.set_biter_health_boost(2.2)
elseif name == 'Insane' then
elseif name == 'Nightmare' then
rpg_extra.difficulty = 0
force.character_running_speed_modifier = 0
force.manual_crafting_speed_modifier = 0
@ -762,13 +761,13 @@ local collapse_after_wave_100 = function()
local name = difficulty.difficulties[difficulty.difficulty_vote_index].name
local difficulty_set = WPT.get('difficulty_set')
if not difficulty_set and name == 'Insane' then
if not difficulty_set and name == 'Nightmare' then
return
end
local wave_number = WD.get_wave()
if wave_number >= 100 or name == 'Insane' then
if wave_number >= 100 or name == 'Nightmare' then
Collapse.start_now(true)
local data = {
position = Collapse.get_position()
@ -819,41 +818,6 @@ local on_init = function()
local this = WPT.get()
Public.reset_map()
local difficulties = {
[1] = {
name = 'Easy',
index = 1,
value = 0.75,
color = {r = 0.00, g = 0.25, b = 0.00},
print_color = {r = 0.00, g = 0.4, b = 0.00},
count = 0
},
[2] = {
name = 'Normal',
index = 2,
value = 1,
color = {r = 0.00, g = 0.00, b = 0.25},
print_color = {r = 0.0, g = 0.0, b = 0.5},
count = 0
},
[3] = {
name = 'Hard',
index = 3,
value = 1.5,
color = {r = 0.25, g = 0.25, b = 0.00},
print_color = {r = 0.4, g = 0.0, b = 0.00},
count = 0
},
[4] = {
name = 'Insane',
index = 4,
value = 3,
color = {r = 0.25, g = 0.00, b = 0.00},
print_color = {r = 0.4, g = 0.0, b = 0.00},
count = 0
}
}
local tooltip = {
[1] = ({'main.diff_tooltip', '1', '1.5', '0.2', '0.4', '2', '25', '100', '15000', '100%', '20', '10'}),
[2] = ({'main.diff_tooltip', '0.5', '1', '0.1', '0.2', '1', '10', '50', '10000', '75%', '15', '15'}),
@ -861,12 +825,11 @@ local on_init = function()
[4] = ({'main.diff_tooltip', '0', '0', '0', '0', '1', '0', '0', '1000', '25%', '5', '25'})
}
Difficulty.set_difficulties(difficulties)
Difficulty.set_tooltip(tooltip)
this.rocks_yield_ore_maximum_amount = 500
this.type_modifier = 1
this.rocks_yield_ore_base_amount = 100
this.rocks_yield_ore_base_amount = 50
this.rocks_yield_ore_distance_modifier = 0.025
local T = Map.Pop_info()

View File

@ -8,6 +8,110 @@ local sqrt = math.sqrt
local max_spill = 60
local mining_chance_weights = {
{name = 'iron-plate', chance = 1000},
{name = 'iron-gear-wheel', chance = 750},
{name = 'copper-plate', chance = 750},
{name = 'copper-cable', chance = 500},
{name = 'electronic-circuit', chance = 300},
{name = 'steel-plate', chance = 200},
{name = 'solid-fuel', chance = 150},
{name = 'pipe', chance = 100},
{name = 'iron-stick', chance = 50},
{name = 'battery', chance = 20},
{name = 'empty-barrel', chance = 10},
{name = 'crude-oil-barrel', chance = 30},
{name = 'lubricant-barrel', chance = 20},
{name = 'petroleum-gas-barrel', chance = 15},
{name = 'sulfuric-acid-barrel', chance = 15},
{name = 'heavy-oil-barrel', chance = 15},
{name = 'light-oil-barrel', chance = 15},
{name = 'water-barrel', chance = 10},
{name = 'green-wire', chance = 10},
{name = 'red-wire', chance = 10},
{name = 'explosives', chance = 5},
{name = 'advanced-circuit', chance = 5},
{name = 'nuclear-fuel', chance = 1},
{name = 'pipe-to-ground', chance = 10},
{name = 'plastic-bar', chance = 5},
{name = 'processing-unit', chance = 2},
{name = 'used-up-uranium-fuel-cell', chance = 1},
{name = 'uranium-fuel-cell', chance = 1},
{name = 'rocket-fuel', chance = 3},
{name = 'rocket-control-unit', chance = 1},
{name = 'low-density-structure', chance = 1},
{name = 'heat-pipe', chance = 1},
{name = 'engine-unit', chance = 4},
{name = 'electric-engine-unit', chance = 2},
{name = 'logistic-robot', chance = 1},
{name = 'construction-robot', chance = 1},
{name = 'land-mine', chance = 3},
{name = 'grenade', chance = 10},
{name = 'rocket', chance = 3},
{name = 'explosive-rocket', chance = 3},
{name = 'cannon-shell', chance = 2},
{name = 'explosive-cannon-shell', chance = 2},
{name = 'uranium-cannon-shell', chance = 1},
{name = 'explosive-uranium-cannon-shell', chance = 1},
{name = 'artillery-shell', chance = 1},
{name = 'cluster-grenade', chance = 2},
{name = 'defender-capsule', chance = 5},
{name = 'destroyer-capsule', chance = 1},
{name = 'distractor-capsule', chance = 2}
}
local scrap_yield_amounts = {
['iron-plate'] = 16,
['iron-gear-wheel'] = 8,
['iron-stick'] = 16,
['copper-plate'] = 16,
['copper-cable'] = 24,
['electronic-circuit'] = 8,
['steel-plate'] = 4,
['pipe'] = 8,
['solid-fuel'] = 4,
['empty-barrel'] = 3,
['crude-oil-barrel'] = 3,
['lubricant-barrel'] = 3,
['petroleum-gas-barrel'] = 3,
['sulfuric-acid-barrel'] = 3,
['heavy-oil-barrel'] = 3,
['light-oil-barrel'] = 3,
['water-barrel'] = 3,
['battery'] = 2,
['explosives'] = 4,
['advanced-circuit'] = 2,
['nuclear-fuel'] = 0.1,
['pipe-to-ground'] = 1,
['plastic-bar'] = 4,
['processing-unit'] = 1,
['used-up-uranium-fuel-cell'] = 1,
['uranium-fuel-cell'] = 0.3,
['rocket-fuel'] = 0.3,
['rocket-control-unit'] = 0.3,
['low-density-structure'] = 0.3,
['heat-pipe'] = 1,
['green-wire'] = 8,
['red-wire'] = 8,
['engine-unit'] = 2,
['electric-engine-unit'] = 2,
['logistic-robot'] = 0.3,
['construction-robot'] = 0.3,
['land-mine'] = 1,
['grenade'] = 2,
['rocket'] = 2,
['explosive-rocket'] = 2,
['cannon-shell'] = 2,
['explosive-cannon-shell'] = 2,
['uranium-cannon-shell'] = 2,
['explosive-uranium-cannon-shell'] = 2,
['artillery-shell'] = 0.3,
['cluster-grenade'] = 0.3,
['defender-capsule'] = 2,
['destroyer-capsule'] = 0.3,
['distractor-capsule'] = 0.3
}
local valid_rocks = {
['sand-rock-big'] = true,
['rock-big'] = true,
@ -102,6 +206,15 @@ end
local size_of_ore_raffle = #harvest_raffle_ores
local scrap_raffle = {}
for _, t in pairs(mining_chance_weights) do
for _ = 1, t.chance, 1 do
scrap_raffle[#scrap_raffle + 1] = t.name
end
end
local size_of_scrap_raffle = #scrap_raffle
local function get_amount(data)
local entity = data.entity
local this = data.this
@ -130,6 +243,10 @@ local function get_amount(data)
type_modifier = rock_yield[entity.name] or type_modifier
if data.size then
base_amount = data.size
end
amount = base_amount + (distance_to_center * distance_modifier)
second_amount = floor((second_base_amount + (distance_to_center * distance_modifier)) / 3)
if amount > maximum_amount then
@ -193,8 +310,57 @@ local function randomness(data)
{
name = 'flying-text',
position = position,
text = '+' .. harvest_amount .. ' [img=item/' .. harvest .. ']',
color = {r = 188, g = 201, b = 63}
text = '+' .. harvest_amount .. ' [img=item/' .. harvest .. ']',
color = {r = 200, g = 160, b = 30}
}
)
if harvest_amount > max_spill then
if this.spill_items_to_surface then
player.surface.spill_item_stack(position, {name = harvest, count = max_spill}, true)
else
player.insert({name = harvest, count = max_spill})
end
harvest_amount = harvest_amount - max_spill
local inserted_count = player.insert({name = harvest, count = harvest_amount})
harvest_amount = harvest_amount - inserted_count
if harvest_amount > 0 then
if this.spill_items_to_surface then
player.surface.spill_item_stack(position, {name = harvest, count = harvest_amount}, true)
else
player.insert({name = harvest, count = harvest_amount})
end
end
else
if this.spill_items_to_surface then
player.surface.spill_item_stack(position, {name = harvest, count = harvest_amount}, true)
else
player.insert({name = harvest, count = harvest_amount})
end
end
local particle = particles[harvest]
create_particles(player.surface, particle, position, 64, {x = player.position.x, y = player.position.y})
end
local function randomness_scrap(data)
local entity = data.entity
local player = data.player
local this = data.this
local harvest_amount
data.size = 15
harvest_amount = get_amount(data)
local harvest = scrap_raffle[random(1, size_of_scrap_raffle)]
local position = {x = entity.position.x, y = entity.position.y}
player.surface.create_entity(
{
name = 'flying-text',
position = position,
text = '+' .. harvest_amount .. ' [img=item/' .. harvest .. ']',
color = {r = 200, g = 160, b = 30}
}
)
@ -240,13 +406,17 @@ function Public.on_player_mined_entity(event)
if valid_rocks[entity.name] or valid_trees[entity.name] then
event.buffer.clear()
local data = {
this = this,
entity = entity,
player = player
}
randomness(data)
if this.breached_wall == 6 then
randomness_scrap(data)
else
randomness(data)
end
end
end

View File

@ -3,7 +3,7 @@ local Biters = require 'modules.wave_defense.biter_rolls'
local Functions = require 'maps.mountain_fortress_v3.functions'
local Generate_resources = require 'maps.mountain_fortress_v3.resource_generator'
local WPT = require 'maps.mountain_fortress_v3.table'
local get_noise = require 'utils.get_noise'
local get_perlin = require 'utils.get_perlin'
local Public = {}
local random = math.random
@ -255,8 +255,8 @@ local function wall(data)
local seed = data.seed
local p = {x = x + data.top_x, y = y + data.top_y}
local small_caves = get_noise('small_caves', p, seed + 300000)
local cave_ponds = get_noise('cave_rivers', p, seed + 150000)
local small_caves = get_perlin('small_caves', p, seed + 300000)
local cave_ponds = get_perlin('cave_rivers', p, seed + 150000)
if y > 9 + cave_ponds * 6 and y < 23 + small_caves * 6 then
if small_caves > 0.02 or cave_ponds > 0.02 then
if small_caves > 0.005 then
@ -397,9 +397,9 @@ local function process_level_14_position(x, y, data)
local buildings = data.buildings
local treasure = data.treasure
local small_caves = get_noise('small_caves', p, seed)
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 40000)
local small_caves = get_perlin('small_caves', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 40000)
--Resource Spots
if smol_areas < -0.71 then
@ -472,9 +472,9 @@ local function process_level_13_position(x, y, data)
local buildings = data.buildings
local treasure = data.treasure
local small_caves = get_noise('small_caves', p, seed)
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 70000)
local small_caves = get_perlin('small_caves', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 70000)
--Resource Spots
if smol_areas < -0.72 then
@ -548,9 +548,9 @@ local function process_level_12_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local noise_1 = get_noise('small_caves', p, seed)
local noise_2 = get_noise('no_rocks_2', p, seed + 20000)
local smol_areas = get_noise('smol_areas', p, seed + 60000)
local noise_1 = get_perlin('small_caves', p, seed)
local noise_2 = get_perlin('no_rocks_2', p, seed + 20000)
local smol_areas = get_perlin('smol_areas', p, seed + 60000)
--Resource Spots
if smol_areas < -0.72 then
@ -634,9 +634,9 @@ local function process_level_11_position(x, y, data)
local markets = data.markets
local treasure = data.treasure
local noise_1 = get_noise('small_caves', p, seed)
local noise_2 = get_noise('no_rocks_2', p, seed + 10000)
local smol_areas = get_noise('smol_areas', p, seed + 50000)
local noise_1 = get_perlin('small_caves', p, seed)
local noise_2 = get_perlin('no_rocks_2', p, seed + 10000)
local smol_areas = get_perlin('smol_areas', p, seed + 50000)
if noise_1 > 0.7 then
tiles[#tiles + 1] = {name = 'water', position = p}
@ -693,7 +693,7 @@ local function process_level_11_position(x, y, data)
return
end
local noise_forest_location = get_noise('forest_location', p, seed)
local noise_forest_location = get_perlin('forest_location', p, seed)
if noise_forest_location > 0.095 then
if noise_forest_location > 0.6 then
if random(1, 100) > 42 then
@ -729,8 +729,8 @@ local function process_level_10_position(x, y, data)
local buildings = data.buildings
local treasure = data.treasure
local scrapyard = get_noise('scrapyard', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 45000)
local scrapyard = get_perlin('scrapyard', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 45000)
if scrapyard < -0.70 or scrapyard > 0.70 then
tiles[#tiles + 1] = {name = 'grass-3', position = p}
@ -774,7 +774,7 @@ local function process_level_10_position(x, y, data)
tiles[#tiles + 1] = {name = 'water-shallow', position = p}
return
end
local noise_forest_location = get_noise('forest_location', p, seed)
local noise_forest_location = get_perlin('forest_location', p, seed)
if scrapyard > -0.15 and scrapyard < 0.15 then
if noise_forest_location > 0.095 then
if random(1, 256) == 1 then
@ -833,12 +833,12 @@ local function process_level_9_position(x, y, data)
local treasure = data.treasure
local maze_p = {x = floor(p.x - p.x % 10), y = floor(p.y - p.y % 10)}
local maze_noise = get_noise('no_rocks_2', maze_p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 40000)
local maze_noise = get_perlin('no_rocks_2', maze_p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 40000)
if maze_noise > -0.35 and maze_noise < 0.35 then
tiles[#tiles + 1] = {name = 'dirt-7', position = p}
local no_rocks_2 = get_noise('no_rocks_2', p, seed)
local no_rocks_2 = get_perlin('no_rocks_2', p, seed)
if random(1, 2) == 1 and no_rocks_2 > -0.5 then
entities[#entities + 1] = {name = rock_raffle[random(1, size_of_rock_raffle)], position = p}
end
@ -898,12 +898,12 @@ local function process_level_8_position(x, y, data, void_or_lab)
local entities = data.entities
local buildings = data.buildings
local scrapyard = get_noise('scrapyard', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 35000)
local scrapyard = get_perlin('scrapyard', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 35000)
--Chasms
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local small_caves = get_noise('small_caves', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local small_caves = get_perlin('small_caves', p, seed)
if noise_cave_ponds < 0.15 and noise_cave_ponds > -0.15 then
if small_caves > 0.35 then
tiles[#tiles + 1] = {name = void_or_lab, position = p}
@ -965,7 +965,7 @@ local function process_level_8_position(x, y, data, void_or_lab)
return
end
local cave_ponds = get_noise('cave_ponds', p, seed)
local cave_ponds = get_perlin('cave_ponds', p, seed)
if cave_ponds < -0.6 and scrapyard > -0.2 and scrapyard < 0.2 then
tiles[#tiles + 1] = {name = 'deepwater-green', position = p}
if random(1, 128) == 1 then
@ -981,7 +981,7 @@ local function process_level_8_position(x, y, data, void_or_lab)
end
end
local large_caves = get_noise('large_caves', p, seed)
local large_caves = get_perlin('large_caves', p, seed)
if scrapyard > -0.15 and scrapyard < 0.15 then
if floor(large_caves * 10) % 4 < 3 then
tiles[#tiles + 1] = {name = 'dirt-7', position = p}
@ -1011,10 +1011,10 @@ local function process_level_7_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local cave_rivers_3 = get_noise('cave_rivers_3', p, seed)
local cave_rivers_4 = get_noise('cave_rivers_4', p, seed + 50000)
local no_rocks_2 = get_noise('no_rocks_2', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 30000)
local cave_rivers_3 = get_perlin('cave_rivers_3', p, seed)
local cave_rivers_4 = get_perlin('cave_rivers_4', p, seed + 50000)
local no_rocks_2 = get_perlin('no_rocks_2', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 30000)
if cave_rivers_3 > -0.025 and cave_rivers_3 < 0.025 and no_rocks_2 > -0.6 then
tiles[#tiles + 1] = {name = 'water', position = p}
@ -1032,7 +1032,7 @@ local function process_level_7_position(x, y, data, void_or_lab)
return
end
local noise_ores = get_noise('no_rocks_2', p, seed + 25000)
local noise_ores = get_perlin('no_rocks_2', p, seed + 25000)
if cave_rivers_3 > -0.20 and cave_rivers_3 < 0.20 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(cave_rivers_3 * 32) % 3 + 1, position = p}
@ -1087,8 +1087,8 @@ local function process_level_7_position(x, y, data, void_or_lab)
end
--Chasms
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local small_caves = get_noise('small_caves', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local small_caves = get_perlin('small_caves', p, seed)
if noise_cave_ponds < 0.25 and noise_cave_ponds > -0.25 then
if small_caves > 0.55 then
tiles[#tiles + 1] = {name = void_or_lab, position = p}
@ -1127,13 +1127,13 @@ local function process_level_6_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local large_caves = get_noise('large_caves', p, seed)
local cave_rivers = get_noise('cave_rivers', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 25000)
local large_caves = get_perlin('large_caves', p, seed)
local cave_rivers = get_perlin('cave_rivers', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 25000)
--Chasms
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local small_caves = get_noise('small_caves', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local small_caves = get_perlin('small_caves', p, seed)
if noise_cave_ponds < 0.45 and noise_cave_ponds > -0.45 then
if small_caves > 0.45 then
tiles[#tiles + 1] = {name = void_or_lab, position = p}
@ -1160,7 +1160,7 @@ local function process_level_6_position(x, y, data, void_or_lab)
Generate_resources(buildings, p, Public.level_depth)
end
end
local noise_forest_location = get_noise('forest_location', p, seed)
local noise_forest_location = get_perlin('forest_location', p, seed)
if cave_rivers > -0.1 and cave_rivers < 0.1 then
local success = place_wagon(data)
if success then
@ -1241,9 +1241,9 @@ local function process_level_5_position(x, y, data, void_or_lab)
local buildings = data.buildings
local treasure = data.treasure
local small_caves = get_noise('small_caves', p, seed)
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 20000)
local small_caves = get_perlin('small_caves', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 20000)
if small_caves > -0.24 and small_caves < 0.24 then
tiles[#tiles + 1] = {name = 'dirt-7', position = p}
@ -1316,10 +1316,10 @@ local function process_level_4_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local noise_large_caves = get_noise('large_caves', p, seed)
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local small_caves = get_noise('dungeons', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 15000)
local noise_large_caves = get_perlin('large_caves', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local small_caves = get_perlin('dungeons', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 15000)
if abs(noise_large_caves) > 0.7 then
tiles[#tiles + 1] = {name = 'water', position = p}
@ -1398,7 +1398,7 @@ local function process_level_4_position(x, y, data, void_or_lab)
if noise_large_caves > -0.2 and noise_large_caves < 0.2 then
--Main Rock Terrain
local no_rocks_2 = get_noise('no_rocks_2', p, seed + 75000)
local no_rocks_2 = get_perlin('no_rocks_2', p, seed + 75000)
if no_rocks_2 > 0.80 or no_rocks_2 < -0.80 then
tiles[#tiles + 1] = {name = 'dirt-' .. floor(no_rocks_2 * 8) % 2 + 5, position = p}
if random(1, 512) == 1 then
@ -1429,11 +1429,11 @@ local function process_level_3_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local small_caves = get_noise('dungeons', p, seed + 50000)
local small_caves_2 = get_noise('small_caves_2', p, seed + 70000)
local noise_large_caves = get_noise('large_caves', p, seed + 60000)
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 60000)
local small_caves = get_perlin('dungeons', p, seed + 50000)
local small_caves_2 = get_perlin('small_caves_2', p, seed + 70000)
local noise_large_caves = get_perlin('large_caves', p, seed + 60000)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 60000)
--Resource Spots
if smol_areas < -0.72 then
@ -1485,7 +1485,7 @@ local function process_level_3_position(x, y, data, void_or_lab)
end
--Rivers
local cave_rivers = get_noise('cave_rivers', p, seed + 100000)
local cave_rivers = get_perlin('cave_rivers', p, seed + 100000)
if cave_rivers < 0.024 and cave_rivers > -0.024 then
if noise_cave_ponds > 0.2 then
tiles[#tiles + 1] = {name = 'water-shallow', position = p}
@ -1495,7 +1495,7 @@ local function process_level_3_position(x, y, data, void_or_lab)
return
end
end
local cave_rivers_2 = get_noise('cave_rivers_2', p, seed)
local cave_rivers_2 = get_perlin('cave_rivers_2', p, seed)
if cave_rivers_2 < 0.024 and cave_rivers_2 > -0.024 then
if noise_cave_ponds < 0.4 then
tiles[#tiles + 1] = {name = 'water-shallow', position = p}
@ -1511,7 +1511,7 @@ local function process_level_3_position(x, y, data, void_or_lab)
return
end
local no_rocks = get_noise('no_rocks', p, seed + 25000)
local no_rocks = get_perlin('no_rocks', p, seed + 25000)
--Worm oil Zones
if no_rocks < 0.20 and no_rocks > -0.20 then
if small_caves > 0.35 then
@ -1541,7 +1541,7 @@ local function process_level_3_position(x, y, data, void_or_lab)
end
--Main Rock Terrain
local no_rocks_2 = get_noise('no_rocks_2', p, seed + 75000)
local no_rocks_2 = get_perlin('no_rocks_2', p, seed + 75000)
if no_rocks_2 > 0.80 or no_rocks_2 < -0.80 then
local success = place_wagon(data)
if success then
@ -1576,9 +1576,9 @@ local function process_level_2_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local small_caves = get_noise('dungeons', p, seed)
local noise_large_caves = get_noise('large_caves', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 15000)
local small_caves = get_perlin('dungeons', p, seed)
local noise_large_caves = get_perlin('large_caves', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 15000)
--Resource Spots
if smol_areas < -0.72 then
@ -1588,7 +1588,7 @@ local function process_level_2_position(x, y, data, void_or_lab)
end
if noise_large_caves > -0.75 and noise_large_caves < 0.75 then
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
--Chasms
if noise_cave_ponds < 0.15 and noise_cave_ponds > -0.15 then
@ -1613,7 +1613,7 @@ local function process_level_2_position(x, y, data, void_or_lab)
end
--Rivers
local cave_rivers = get_noise('cave_rivers', p, seed + 100000)
local cave_rivers = get_perlin('cave_rivers', p, seed + 100000)
if cave_rivers < 0.037 and cave_rivers > -0.037 then
if noise_cave_ponds < 0.1 then
tiles[#tiles + 1] = {name = 'water-shallow', position = p}
@ -1641,7 +1641,7 @@ local function process_level_2_position(x, y, data, void_or_lab)
return
end
local no_rocks = get_noise('no_rocks', p, seed + 25000)
local no_rocks = get_perlin('no_rocks', p, seed + 25000)
--Worm oil Zones
if no_rocks < 0.20 and no_rocks > -0.20 then
if small_caves > 0.30 then
@ -1671,7 +1671,7 @@ local function process_level_2_position(x, y, data, void_or_lab)
end
--Main Rock Terrain
local no_rocks_2 = get_noise('no_rocks_2', p, seed + 75000)
local no_rocks_2 = get_perlin('no_rocks_2', p, seed + 75000)
if no_rocks_2 > 0.80 or no_rocks_2 < -0.80 then
local success = place_wagon(data)
if success then
@ -1706,9 +1706,9 @@ local function process_level_1_2_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local small_caves = get_noise('dungeons', p, seed + 33322)
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 33333)
local small_caves = get_perlin('dungeons', p, seed + 33322)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed + 33333)
--Resource Spots
if smol_areas < -0.71 then
@ -1730,7 +1730,17 @@ local function process_level_1_2_position(x, y, data, void_or_lab)
end
--Water Ponds
if noise_cave_ponds > 0.790 then
if noise_cave_ponds > 0.670 then
if noise_cave_ponds > 0.750 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
if random(1, 4) == 1 then
markets[#markets + 1] = p
end
if random(1, 4) == 1 then
entities[#entities + 1] = {name = tree_raffle[random(1, size_of_tree_raffle)], position = p}
end
return
end
tiles[#tiles + 1] = {name = 'deepwater', position = p}
if random(1, 16) == 1 then
entities[#entities + 1] = {name = 'fish', position = p}
@ -1739,7 +1749,7 @@ local function process_level_1_2_position(x, y, data, void_or_lab)
end
--Rivers
local cave_rivers = get_noise('cave_rivers', p, seed + 200000)
local cave_rivers = get_perlin('cave_rivers', p, seed + 200000)
if cave_rivers < 0.041 and cave_rivers > -0.042 then
if noise_cave_ponds > 0 then
tiles[#tiles + 1] = {name = 'water-shallow', position = p}
@ -1762,19 +1772,7 @@ local function process_level_1_2_position(x, y, data, void_or_lab)
return
end
--Market Spots
if noise_cave_ponds < -0.74 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
if random(1, 32) == 1 then
markets[#markets + 1] = p
end
if random(1, 32) == 1 then
entities[#entities + 1] = {name = tree_raffle[random(1, size_of_tree_raffle)], position = p}
end
return
end
local no_rocks = get_noise('no_rocks', p, seed + 30000)
local no_rocks = get_perlin('no_rocks', p, seed + 30000)
--Worm oil Zones
if p.y < -64 + noise_cave_ponds * 10 then
if no_rocks < 0.11 and no_rocks > -0.11 then
@ -1804,7 +1802,7 @@ local function process_level_1_2_position(x, y, data, void_or_lab)
end
--Main Rock Terrain
local no_rocks_2 = get_noise('no_rocks_2', p, seed + 85000)
local no_rocks_2 = get_perlin('no_rocks_2', p, seed + 5000)
if no_rocks_2 > 0.64 or no_rocks_2 < -0.64 then
local success = place_wagon(data)
if success then
@ -1824,7 +1822,7 @@ local function process_level_1_2_position(x, y, data, void_or_lab)
treasure[#treasure + 1] = {position = p, chest = 'iron-chest'}
end
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
local noise_forest_location = get_noise('forest_location', p, seed)
local noise_forest_location = get_perlin('forest_location', p, seed)
if noise_forest_location > 0.095 then
if random(1, 256) == 1 then
Biters.wave_defense_set_worm_raffle(abs(p.y) * worm_level_modifier)
@ -1877,9 +1875,9 @@ local function process_level_1_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local small_caves = get_noise('dungeons', p, seed)
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local smol_areas = get_noise('smol_areas', p, seed)
local small_caves = get_perlin('dungeons', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed)
--Resource Spots
if smol_areas < -0.72 then
@ -1901,7 +1899,17 @@ local function process_level_1_position(x, y, data, void_or_lab)
end
--Water Ponds
if noise_cave_ponds > 0.810 then
if noise_cave_ponds > 0.670 then
if noise_cave_ponds > 0.750 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
if random(1, 4) == 1 then
markets[#markets + 1] = p
end
if random(1, 4) == 1 then
entities[#entities + 1] = {name = tree_raffle[random(1, size_of_tree_raffle)], position = p}
end
return
end
tiles[#tiles + 1] = {name = 'deepwater', position = p}
if random(1, 16) == 1 then
entities[#entities + 1] = {name = 'fish', position = p}
@ -1910,7 +1918,7 @@ local function process_level_1_position(x, y, data, void_or_lab)
end
--Rivers
local cave_rivers = get_noise('cave_rivers', p, seed + 300000)
local cave_rivers = get_perlin('cave_rivers', p, seed + 300000)
if cave_rivers < 0.042 and cave_rivers > -0.042 then
if noise_cave_ponds > 0 then
tiles[#tiles + 1] = {name = 'water-shallow', position = p}
@ -1933,19 +1941,7 @@ local function process_level_1_position(x, y, data, void_or_lab)
return
end
--Market Spots
if noise_cave_ponds < -0.74 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
if random(1, 32) == 1 then
markets[#markets + 1] = p
end
if random(1, 32) == 1 then
entities[#entities + 1] = {name = 'tree-0' .. random(1, 9), position = p}
end
return
end
local no_rocks = get_noise('no_rocks', p, seed + 50000)
local no_rocks = get_perlin('no_rocks', p, seed + 50000)
--Worm oil Zones
if p.y < -64 + noise_cave_ponds * 10 then
if no_rocks < 0.12 and no_rocks > -0.12 then
@ -1975,7 +1971,7 @@ local function process_level_1_position(x, y, data, void_or_lab)
end
--Main Rock Terrain
local no_rocks_2 = get_noise('no_rocks_2', p, seed + 75000)
local no_rocks_2 = get_perlin('no_rocks_2', p, seed + 75000)
if no_rocks_2 > 0.66 or no_rocks_2 < -0.66 then
local success = place_wagon(data)
if success then
@ -1994,7 +1990,7 @@ local function process_level_1_position(x, y, data, void_or_lab)
if random(1, 2048) == 1 then
treasure[#treasure + 1] = {position = p, chest = 'iron-chest'}
end
local random_tiles = get_noise('forest_location', p, seed)
local random_tiles = get_perlin('forest_location', p, seed)
if random_tiles > 0.095 then
if random_tiles > 0.6 then
if random(1, 100) > 42 then
@ -2032,31 +2028,55 @@ local function process_level_0_position(x, y, data, void_or_lab)
local markets = data.markets
local treasure = data.treasure
local small_caves = get_noise('dungeons', p, seed + 24244)
local noise_cave_ponds = get_noise('cave_ponds', p, seed)
local smol_areas = get_noise('smol_areas', p, seed + 23444)
local small_caves = get_perlin('dungeons', p, seed)
local noise_cave_ponds = get_perlin('cave_ponds', p, seed)
local smol_areas = get_perlin('smol_areas', p, seed)
local no_rocks_2 = get_perlin('no_rocks_2', p, seed)
local cave_rivers = get_perlin('cave_rivers', p, seed)
local no_rocks = get_perlin('no_rocks', p, seed)
--Resource Spots
if smol_areas < -0.71 then
-- if random(1, 8) == 1 then
Generate_resources(buildings, p, Public.level_depth)
-- end
if smol_areas > 0.750 then
tiles[#tiles + 1] = {name = 'stone-path', position = p}
if random(1, 4) == 1 then
Generate_resources(buildings, p, Public.level_depth)
end
if random(1, 4) == 1 then
Biters.wave_defense_set_worm_raffle(abs(p.y) * worm_level_modifier)
entities[#entities + 1] = {
name = Biters.wave_defense_roll_worm_name(),
position = p,
force = 'enemy'
}
end
return
end
end
--Chasms
if noise_cave_ponds < 0.099 and noise_cave_ponds > -0.102 then
if small_caves > 0.52 then
if noise_cave_ponds < 0.111 and noise_cave_ponds > -0.112 then
if small_caves > 0.53 then
tiles[#tiles + 1] = {name = void_or_lab, position = p}
return
end
if small_caves < -0.52 then
if small_caves < -0.53 then
tiles[#tiles + 1] = {name = void_or_lab, position = p}
return
end
end
--Water Ponds
if noise_cave_ponds > 0.770 then
if noise_cave_ponds > 0.670 then
if noise_cave_ponds > 0.750 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
if random(1, 4) == 1 then
markets[#markets + 1] = p
end
if random(1, 4) == 1 then
entities[#entities + 1] = {name = tree_raffle[random(1, size_of_tree_raffle)], position = p}
end
return
end
tiles[#tiles + 1] = {name = 'deepwater', position = p}
if random(1, 16) == 1 then
entities[#entities + 1] = {name = 'fish', position = p}
@ -2065,9 +2085,8 @@ local function process_level_0_position(x, y, data, void_or_lab)
end
--Rivers
local cave_rivers = get_noise('cave_rivers', p, seed + 200000)
if cave_rivers < 0.031 and cave_rivers > -0.032 then
if noise_cave_ponds > 0 then
if cave_rivers < 0.044 and cave_rivers > -0.062 then
if noise_cave_ponds > 0.1 then
tiles[#tiles + 1] = {name = 'water-shallow', position = p}
if random(1, 64) == 1 then
entities[#entities + 1] = {name = 'fish', position = p}
@ -2076,11 +2095,11 @@ local function process_level_0_position(x, y, data, void_or_lab)
end
end
if noise_cave_ponds > 0.72 then
tiles[#tiles + 1] = {name = 'grass-' .. random(1, 4), position = p}
tiles[#tiles + 1] = {name = 'grass-1', position = p}
if cave_rivers < -0.502 then
tiles[#tiles + 1] = {name = 'refined-hazard-concrete-right', position = p}
if noise_cave_ponds > 0.632 then
if noise_cave_ponds > 0.542 then
if cave_rivers > -0.302 then
tiles[#tiles + 1] = {name = 'refined-hazard-concrete-right', position = p}
end
end
if random(1, 64) == 1 then
entities[#entities + 1] = {name = tree_raffle[random(1, size_of_tree_raffle)], position = p}
@ -2088,50 +2107,34 @@ local function process_level_0_position(x, y, data, void_or_lab)
return
end
--Market Spots
if noise_cave_ponds < -0.72 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
if random(1, 32) == 1 then
markets[#markets + 1] = p
end
if random(1, 32) == 1 then
entities[#entities + 1] = {name = tree_raffle[random(1, size_of_tree_raffle)], position = p}
end
return
end
local no_rocks = get_noise('no_rocks', p, seed + 35000)
--Worm oil Zones
if p.y < -64 + noise_cave_ponds * 10 then
if no_rocks < 0.11 and no_rocks > -0.11 then
if small_caves > 0.31 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
if random(1, 450) == 1 then
entities[#entities + 1] = {name = 'crude-oil', position = p, amount = get_oil_amount(p)}
end
if random(1, 96) == 1 then
Biters.wave_defense_set_worm_raffle(abs(p.y) * worm_level_modifier)
entities[#entities + 1] = {
name = Biters.wave_defense_roll_worm_name(),
position = p,
force = 'enemy'
}
end
if random(1, 1024) == 1 then
treasure[#treasure + 1] = {position = p, chest = 'iron-chest'}
end
if random(1, 64) == 1 then
entities[#entities + 1] = {name = 'tree-0' .. random(1, 9), position = p}
end
return
if no_rocks < 0.031 and no_rocks > -0.141 then
if small_caves > 0.081 then
tiles[#tiles + 1] = {name = 'grass-' .. floor(noise_cave_ponds * 32) % 3 + 1, position = p}
if random(1, 250) == 1 then
entities[#entities + 1] = {name = 'crude-oil', position = p, amount = get_oil_amount(p)}
end
if random(1, 96) == 1 then
Biters.wave_defense_set_worm_raffle(abs(p.y) * worm_level_modifier)
entities[#entities + 1] = {
name = Biters.wave_defense_roll_worm_name(),
position = p,
force = 'enemy'
}
end
if random(1, 1024) == 1 then
treasure[#treasure + 1] = {position = p, chest = 'iron-chest'}
end
if random(1, 64) == 1 then
entities[#entities + 1] = {name = 'tree-0' .. random(1, 9), position = p}
end
return
end
end
--Main Rock Terrain
local no_rocks_2 = get_noise('no_rocks_2', p, seed + 85000)
if no_rocks_2 > 0.64 or no_rocks_2 < -0.64 then
if no_rocks_2 > 0.334 and no_rocks_2 < 0.544 then
local success = place_wagon(data)
if success then
return

View File

@ -0,0 +1,206 @@
local Event = require 'utils.event'
local Global = require 'utils.global'
local BiterRolls = require 'modules.wave_defense.biter_rolls'
local BiterHealthBooster = require 'modules.biter_health_booster'
local traps = {}
Global.register(
traps,
function(t)
traps = t
end
)
local Public = {}
local floor = math.floor
local random = math.random
local abs = math.abs
local sqrt = math.sqrt
local spawn_amount_rolls = {}
for a = 48, 1, -1 do
spawn_amount_rolls[#spawn_amount_rolls + 1] = floor(a ^ 5)
end
local random_particles = {
'dirt-2-stone-particle-medium',
'dirt-4-dust-particle',
'coal-particle'
}
local s_random_particles = #random_particles
local function create_particles(data)
local surface = data.surface
local position = data.position
local amount = data.amount
if not surface or not surface.valid then
return
end
for i = 1, amount, 1 do
local m = random(6, 12)
local m2 = m * 0.005
surface.create_particle(
{
name = random_particles[random(1, s_random_particles)],
position = position,
frame_speed = 0.1,
vertical_speed = 0.1,
height = 0.1,
movement = {m2 - (random(0, m) * 0.01), m2 - (random(0, m) * 0.01)}
}
)
end
end
local function spawn_biters(data)
local surface = data.surface
local position = data.position
local h = floor(abs(position.y))
if not position then
position = surface.find_non_colliding_position('small-biter', position, 10, 1)
if not position then
return
end
end
BiterRolls.wave_defense_set_unit_raffle(h * 0.20)
local unit
if random(1, 2) == 1 then
unit = surface.create_entity({name = BiterRolls.wave_defense_roll_spitter_name(), position = position})
else
unit = surface.create_entity({name = BiterRolls.wave_defense_roll_biter_name(), position = position})
end
if random(1, 128) == 1 then
BiterHealthBooster.add_boss_unit(unit, h * 5 + 1, 0.38)
end
end
local function spawn_worms(data)
local surface = data.surface
local position = data.position
BiterRolls.wave_defense_set_worm_raffle(sqrt(position.x ^ 2 + position.y ^ 2) * 0.20)
surface.create_entity({name = BiterRolls.wave_defense_roll_worm_name(), position = position})
end
function Public.buried_biter(surface, position, max)
if not surface then
return
end
if not surface.valid then
return
end
if not position then
return
end
if not position.x then
return
end
if not position.y then
return
end
local amount = 8
local a = 0
max = max or random(4, 6)
local ticks = amount * 30
ticks = ticks + 90
for t = 1, ticks, 1 do
if not traps[game.tick + t] then
traps[game.tick + t] = {}
end
traps[game.tick + t][#traps[game.tick + t] + 1] = {
callback = 'create_particles',
data = {surface = surface, position = {x = position.x, y = position.y}, amount = 4}
}
if t > 90 then
if t % 30 == 29 then
a = a + 1
traps[game.tick + t][#traps[game.tick + t] + 1] = {
callback = 'spawn_biters',
data = {surface = surface, position = {x = position.x, y = position.y}}
}
if a >= max then
break
end
end
end
end
end
function Public.buried_worm(surface, position)
if not surface then
return
end
if not surface.valid then
return
end
if not position then
return
end
if not position.x then
return
end
if not position.y then
return
end
local amount = 8
local ticks = amount * 30
ticks = ticks + 90
local a = false
for t = 1, ticks, 1 do
if not traps[game.tick + t] then
traps[game.tick + t] = {}
end
traps[game.tick + t][#traps[game.tick + t] + 1] = {
callback = 'create_particles',
data = {surface = surface, position = {x = position.x, y = position.y}, amount = 4}
}
if not a then
traps[game.tick + t][#traps[game.tick + t] + 1] = {
callback = 'spawn_worms',
data = {surface = surface, position = {x = position.x, y = position.y}}
}
a = true
end
end
end
local callbacks = {
['create_particles'] = create_particles,
['spawn_biters'] = spawn_biters,
['spawn_worms'] = spawn_worms
}
local function on_tick()
local t = game.tick
if not traps[t] then
return
end
for _, token in pairs(traps[t]) do
local callback = token.callback
local data = token.data
local cbl = callbacks[callback]
if callbacks[callback] then
cbl(data)
end
end
traps[t] = nil
end
Event.add(defines.events.on_tick, on_tick)
return Public

View File

@ -1,6 +1,13 @@
local perlin = require 'utils.perlin_noise'.noise
local simplex_noise = require 'utils.simplex_noise'.d2
--add or use noise templates from here
local noises = {
['bb_biterland'] = {
{modifier = 0.001, weight = 1},
{modifier = 0.01, weight = 0.35},
{modifier = 0.1, weight = 0.015}
},
['bb_ore'] = {{modifier = 0.0042, weight = 1}, {modifier = 0.031, weight = 0.08}, {modifier = 0.1, weight = 0.025}},
['cave_ponds'] = {{modifier = 0.01, weight = 1}, {modifier = 0.1, weight = 0.06}},
['smol_areas'] = {{modifier = 0.01, weight = 1}, {modifier = 0.1, weight = 0.02}, {modifier = 0.1, weight = 0.03}},
['cave_worms'] = {{modifier = 0.001, weight = 1}, {modifier = 0.1, weight = 0.06}},
@ -102,6 +109,12 @@ local noises = {
{modifier = 0.006, weight = 1},
{modifier = 0.02, weight = 0.15},
{modifier = 0.25, weight = 0.025}
},
['cm_ponds'] = {{modifier = 0.025, weight = 1}, {modifier = 0.05, weight = 0.25}, {modifier = 0.1, weight = 0.05}},
['cm_ocean'] = {
{modifier = 0.002, weight = 1},
{modifier = 0.004, weight = 1},
{modifier = 0.02, weight = 0.05}
}
}
@ -111,9 +124,9 @@ local function get_noise(name, pos, seed)
local d = 0
for i = 1, #noises[name] do
local mod = noises[name]
noise = noise + perlin(pos.x * mod[i].modifier, pos.y * mod[i].modifier, seed) * mod[i].weight
noise = noise + simplex_noise(pos.x * mod[i].modifier, pos.y * mod[i].modifier, seed, 0xF) * mod[i].weight
d = d + mod[i].weight
seed = seed + 10000
seed = seed + seed / seed
end
noise = noise / d
return noise

View File

@ -107,8 +107,8 @@ local dot_product = {
return -y - z
end
}
local function grad(hash, x, y, z)
return dot_product[band(hash, 0xF)](x, y, z)
local function grad(hash, x, y, z, bit)
return dot_product[band(hash, bit)](x, y, z)
end
-- Fade function is used to smooth final output
@ -121,7 +121,7 @@ local function lerp(t, a, b)
end
-- Return range: [-1, 1]
function Perlin.noise(x, y, z)
function Perlin.noise(x, y, z, bit)
y = y or 0
z = z or 0
@ -168,13 +168,13 @@ function Perlin.noise(x, y, z)
w,
lerp(
v,
lerp(u, grad(AAA, x, y, z), grad(BAA, x - 1, y, z)),
lerp(u, grad(ABA, x, y - 1, z), grad(BBA, x - 1, y - 1, z))
lerp(u, grad(AAA, x, y, z, bit), grad(BAA, x - 1, y, z, bit)),
lerp(u, grad(ABA, x, y - 1, z), grad(BBA, x - 1, y - 1, z, bit))
),
lerp(
v,
lerp(u, grad(AAB, x, y, z - 1), grad(BAB, x - 1, y, z - 1)),
lerp(u, grad(ABB, x, y - 1, z - 1), grad(BBB, x - 1, y - 1, z - 1))
lerp(u, grad(AAB, x, y, z - 1, bit), grad(BAB, x - 1, y, z - 1)),
lerp(u, grad(ABB, x, y - 1, z - 1, bit), grad(BBB, x - 1, y - 1, z - 1, bit))
)
)
end

222
utils/profiler.lua Normal file
View File

@ -0,0 +1,222 @@
local table_sort = table.sort
local string_rep = string.rep
local string_format = string.format
local string_len = string.len
local string_sub = string.sub
local string_gsub = string.gsub
local debug_getinfo = debug.getinfo
-- Call
-- name (string)
-- calls (int)
-- profiler (LuaProfiler)
-- next (Array of Call)
local Profiler =
{
-- Call
CallTree = nil,
IsRunning = false,
}
local ignoredFunctions =
{
[debug.sethook] = true
}
local namedSources =
{
["[string \"local n, v = \"serpent\", \"0.30\" -- (C) 2012-17...\"]"] = "serpent",
}
local function startCommand(command)
Profiler.Start(command.parameter ~= nil)
end
local function stopCommand(command)
Profiler.Stop(command.parameter ~= nil, nil)
end
ignoredFunctions[startCommand] = true
ignoredFunctions[stopCommand] = true
commands.add_command("startProfiler", "Starts profiling", startCommand)
commands.add_command("stopProfiler", "Stops profiling", stopCommand)
--local assert_raw = assert
--function assert(expr, ...)
-- if not expr then
-- Profiler.Stop(false, "Assertion failed")
-- end
-- assert_raw(expr, ...)
--end
local error_raw = error
function error(...)
Profiler.Stop(false, "Error raised")
error_raw(...)
end
function Profiler.Start(excludeCalledMs)
if Profiler.IsRunning then
return
end
local create_profiler = game.create_profiler
Profiler.IsRunning = true
Profiler.CallTree =
{
name = "root",
calls = 0,
profiler = create_profiler(),
next = { },
}
-- Array of Call
local stack = { [0] = Profiler.CallTree }
local stack_count = 0
debug.sethook(function(event)
local info = debug_getinfo(2, "nSf")
if ignoredFunctions[info.func] then
return
end
if event == "call" or event == "tail call" then
local prevCall = stack[stack_count]
if excludeCalledMs then
prevCall.profiler.stop()
end
local what = info.what
local name
if what == "C" then
name = string_format("C function %q", info.name or "anonymous")
else
local source = info.short_src
local namedSource = namedSources[source]
if namedSource ~= nil then
source = namedSource
elseif string.sub(source, 1, 1) == "@" then
source = string.sub(source, 1)
end
name = string_format("%q in %q, line %d", info.name or "anonymous", source, info.linedefined)
end
local prevCall_next = prevCall.next
if prevCall_next == nil then
prevCall_next = { }
prevCall.next = prevCall_next
end
local currCall = prevCall_next[name]
local profilerStartFunc
if currCall == nil then
local prof = create_profiler()
currCall =
{
name = name,
calls = 1,
profiler = prof,
}
prevCall_next[name] = currCall
profilerStartFunc = prof.reset
else
currCall.calls = currCall.calls + 1
profilerStartFunc = currCall.profiler.restart
end
stack_count = stack_count + 1
stack[stack_count] = currCall
profilerStartFunc()
end
if event == "return" or event == "tail call" then
if stack_count > 0 then
stack[stack_count].profiler.stop()
stack[stack_count] = nil
stack_count = stack_count - 1
if excludeCalledMs then
stack[stack_count].profiler.restart()
end
end
end
end, "cr")
end
ignoredFunctions[Profiler.Start] = true
local function DumpTree(averageMs)
local function sort_Call(a, b)
return a.calls > b.calls
end
local fullStr = { "" }
local str = fullStr
local line = 1
local function recurse(curr, depth)
local sort = { }
local i = 1
for k, v in pairs(curr) do
sort[i] = v
i = i + 1
end
table_sort(sort, sort_Call)
for i = 1, #sort do
local call = sort[i]
if line >= 19 then --Localised string can only have up to 20 parameters
local newStr = { "" } --So nest them!
str[line + 1] = newStr
str = newStr
line = 1
end
if averageMs then
call.profiler.divide(call.calls)
end
str[line + 1] = string_format("\n%s%dx %s. %s ", string_rep("\t", depth), call.calls, call.name, averageMs and "Average" or "Total")
str[line + 2] = call.profiler
line = line + 2
local next = call.next
if next ~= nil then
recurse(next, depth + 1)
end
end
end
if Profiler.CallTree.next ~= nil then
recurse(Profiler.CallTree.next, 0)
return fullStr
end
return "No calls"
end
function Profiler.Stop(averageMs, message)
if not Profiler.IsRunning then
return
end
debug.sethook()
local text = { "", "\n\n----------PROFILER DUMP----------\n", DumpTree(averageMs), "\n\n----------PROFILER STOPPED----------\n" }
if message ~= nil then
text[#text + 1] = string.format("Reason: %s\n", message)
end
log(text)
Profiler.CallTree = nil
Profiler.IsRunning = false
end
ignoredFunctions[Profiler.Stop] = true
return Profiler