Skip to content

Commit

Permalink
Overhaul of codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetabite committed Oct 15, 2020
1 parent aae3b8f commit 3d6a928
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 398 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"global",
"script",
"rendering",
"worm_shoot_shiftings"
"worm_shoot_shiftings",
"mods",
"remote",
"informatron_make_image"
],
"Lua.diagnostics.disable": [
"lowercase-global"
Expand Down
13 changes: 13 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
---------------------------------------------------------------------------------------------------
Version: 0.9.0
Date: 15.10.2020
Changes:
- Added interface for getting a certain players spice status
- Rewrote most of the player regarding code, you can still play in existing worlds, but the addiction system has been resetted
- Optimised codebase, should be more performant and accurate now
Bugfixes:
- Fixed crash due to an empty table when killed by alien
- Fixed crash due to not adding a table to global.aliens
- Fixed crash when checking for spice in inventory after being killed
- Aliens now evolve when a character dies near them, this was already in the mod, but didnt work properly till now

---------------------------------------------------------------------------------------------------
Version: 0.8.3
Date: 13.10.2020
Expand Down
1 change: 0 additions & 1 deletion control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ handler.add_lib(require('scripts.control.setup'))
handler.add_lib(require('scripts.control.aliens'))
handler.add_lib(require('scripts.control.machines'))
handler.add_lib(require('scripts.control.players'))
handler.add_lib(require('scripts.control.resources'))
handler.add_lib(require('scripts.control.forces'))

if script.active_mods['informatron'] then
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nauvis-melange",
"version": "0.8.3",
"version": "0.9.0",
"title": "Nauvis Melange",
"author": "Zetabite",
"factorio_version": "0.18",
Expand Down
37 changes: 22 additions & 15 deletions scripts/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,46 @@ local config = {}
-- Constants
config.SPICE_DURATION = 3600
config.SPICE_COOLDOWN = config.SPICE_DURATION
config.OVERLAY_REFRESH = 30
config.OVERLAY_TIMER = config.OVERLAY_REFRESH + 1
config.PLAYER_CHECK_TICK = 300
config.RENDER_REFRESH_TICK = 30
config.OVERLAY_TIMER = config.RENDER_REFRESH_TICK + 1
config.WATER_INJECTOR_CHECK_TICK = 60
config.VICTORY_CHECK_TICK = 120

config.ZOOM_FACTOR = 3.0
config.VICTORY_SPICE_AMOUNT = 10000

config.effect_table = {'radar', 'craft_mod', 'bad_trip'}

-- Defaults
config.players_default = {
config.default = {}
config.default.players = {
addiction_level = 0,
zoom_factor = config.ZOOM_FACTOR,
under_influence = false,
radar = {
active = false,
reference = false,
remaining_ticks = 0,
},
craft_mod = {
active = false,
remaining_ticks = 0,
tick = false,
duration = config.SPICE_DURATION
},
bad_trip = {
active = false,
remaining_ticks = 0,
tick = false,
duration = config.SPICE_DURATION
},
craft_mod = {
tick = false,
duration = config.SPICE_DURATION
}
}

config.forces_default = {
config.default.forces = {
spice_shipped = 0
}

config.render_default = {
config.default.render = {
spice_overlay = {}
}

config.spice_effects_blacklist_default = {
config.default.spice_effects_blacklist = {
type = {
['locomotive'] = true,
['artillery-wagon'] = true,
Expand All @@ -44,4 +50,5 @@ config.spice_effects_blacklist_default = {
},
name = {}
}

return config
134 changes: 63 additions & 71 deletions scripts/control/aliens.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
local aliens_table
local spice_evolution_factor = settings.global['spice-evolution-factor'].value
local spice_direct_evolution_level = settings.global['spice-direct-evolution-level'].value
local spice_evolve_neighbours = settings.global['spice-evolve-neighbours'].value
local spice_evolve_neighbours_radius = settings.global['spice-evolve-neighbours-radius'].value

function update_vars(event)
if event.setting == 'spice-evolution-factor' then
spice_evolution_factor = settings.global['spice-evolution-factor'].value
elseif event.setting == 'spice-direct-evolution-level' then
spice_direct_evolution_level = settings.global['spice-direct-evolution-level'].value
elseif event.setting == 'spice-evolve-neighbours' then
spice_evolve_neighbours = settings.global['spice-evolve-neighbours'].value
elseif event.setting == 'spice-evolve-neighbours-radius' then
spice_evolve_neighbours_radius = settings.global['spice-evolve-neighbours-radius'].value
end
end

function created_entity(event)
local entity = event.entity
if entity.name == 'alien-probe-proxy' then
Expand All @@ -24,6 +6,7 @@ function created_entity(event)
local radius = 5
local alien = surface.find_entities_filtered({type = 'unit', position = position, radius = radius, force = 'enemy', limit = 1})
if alien[1] then
local aliens_table = global.aliens
alien = alien[1]
local sample = nil
if aliens_table.reverse['biter'][alien.name] then
Expand All @@ -47,40 +30,58 @@ function created_entity(event)
end

function destroyed_entity(event)
if (event.cause) then
local cause = event.cause
if (event.entity) then
spice_effects(cause, event.entity, false)
elseif (event.player_index) then
spice_effects(cause, game.get_player(event.player_index), event.player_index)
local cause = event.cause
local entity = event.entity
if (cause) then
if (entity) then
if entity.type == 'turret' and string.match(entity.name, 'worm') then
create_worm_hole(entity)
else
spice_effects(cause, entity)
end
end
end
end

function spice_effects(cause, victim, player_index)
local spice_collector = is_spice_collector(cause, victim)
if spice_collector then
function create_worm_hole(entity)
local surface = entity.surface
local pos = entity.position
pos = surface.find_non_colliding_position('worm-hole', pos, 2, 0.5, true)
if (pos) then
surface.create_entity({ name = 'worm-hole', position = pos })
end
end

function spice_effects(cause, victim)
cause = is_spice_collector(cause, victim)
if cause then
-- in case we later want to scale effect based on this
local spice_amount = has_spice_in_fluidbox(victim, player_index) or has_spice_in_inventory(cause, victim, player_index)
local spice_amount = has_spice_in_fluidbox(victim) or has_spice_in_inventory(victim)
if spice_amount then
local spice_settings = {
['spice-evolution-factor'] = settings.global['spice-evolution-factor'].value,
['spice-direct-evolution-level'] = settings.global['spice-direct-evolution-level'].value,
['spice-evolve-neighbours'] = settings.global['spice-evolve-neighbours'].value,
['spice-evolve-neighbours-radius'] = settings.global['spice-evolve-neighbours-radius'].value
}
local force = cause.force
force.evolution_factor = force.evolution_factor * spice_evolution_factor
apply_spice_to_alien(spice_collector)
cause = evolve_alien(cause, spice_direct_evolution_level)
if spice_evolve_neighbours then
force.evolution_factor = force.evolution_factor * spice_settings['spice-evolution-factor']
cause = evolve_alien(cause, spice_settings['spice-direct-evolution-level'])
apply_spice_to_alien(cause)
if spice_settings['spice-evolve-neighbours'] then
local surface = cause.surface
local position = cause.position
local aliens = surface.find_entities_filtered({ name = aliens_table.names, position = position, radius = spice_evolve_neighbours_radius, force = 'enemy'})
local aliens = surface.find_entities_filtered({ name = global.aliens.names, position = position, radius = spice_settings['spice-evolve-neighbours-radius'], force = 'enemy'})
for _, alien in pairs(aliens) do
if alien ~= cause then evolve_alien(alien, spice_direct_evolution_level - 1) end
if alien ~= cause then evolve_alien(alien, spice_settings['spice-direct-evolution-level'] - 1) end
end
end
end
end
end

function has_spice_in_fluidbox(victim, player_index)
if not player_index then
function has_spice_in_fluidbox(victim)
if victim.type ~= 'character' then
if (victim.fluidbox) then
local fluidbox = victim.fluidbox
for i = 1, #fluidbox, 1 do
Expand All @@ -93,46 +94,48 @@ function has_spice_in_fluidbox(victim, player_index)
return false
end

function has_spice_in_inventory(collector, victim, player_index)
local amount = 0
function has_spice_in_inventory(victim)
local inventory = nil
if player_index then
local surface = collector.surface
local position = collector.position
local corpses = surface.find_entities_filtered({type = 'character-corpse', position = position, radius = 2})
for _, corpse in pairs(corpses) do
if corpse.character_corpse_player_index == player_index then
local corpse_inv = corpse.get_inventory(defines.inventory.character_corpse)
if corpse_inv.get_item_count('spice') > 0 then inventory = corpse_inv break end
end
if victim.type == 'car' then
inventory = victim.get_inventory(defines.inventory.car_trunk)
elseif victim.type == 'cargo-wagon' then
inventory = victim.get_inventory(defines.inventory.cargo_wagon)
elseif victim.type == 'character' then
local surface = victim.surface
local position = victim.position
local corpse = surface.find_entities_filtered({type = 'character-corpse', position = position, radius = 2, limit = 1})
if corpse[1] then
corpse = corpse[1]
inventory = corpse.get_inventory(defines.inventory.character_corpse)
end
else
if victim.type == 'car' then inventory = victim.get_inventory(defines.inventory.car_trunk)
elseif victim.type == 'cargo-wagon' then inventory = victim.get_inventory(defines.inventory.cargo_wagon)
else return false end
end
amount = inventory.get_item_count('spice')
if amount > 0 then
inventory.remove({name = 'spice', count = amount})
return amount
else
return false
end
if inventory then
local amount = inventory.get_item_count('spice')
if amount > 0 then
inventory.remove({name = 'spice', count = amount})
return amount
end
end
return false
end

function evolve_alien(alien, steps)
local next_level = get_next_level(alien.name, steps)
if next_level ~= alien.name then
local surface = alien.surface
local position = alien.position
local force = alien.force
alien.destroy()
alien = surface.create_entity({ name = next_level, position = position, force = 'enemy'})
alien = surface.create_entity({ name = next_level, position = position, force = force})
end
return alien
end

function get_next_level(name, steps)
for type_name, entry in pairs(aliens_table) do
local aliens_table = global.aliens
for type_name, entry in pairs(aliens_table.dict) do
if aliens_table.reverse[type_name][name] then
local tier = aliens_table.reverse[type_name][name]
local next_tier = tier + steps
Expand All @@ -153,6 +156,7 @@ end

-- only allows melee units to collect spice
function is_spice_collector(cause, victim)
local aliens_table = global.aliens
if aliens_table.melee[cause.name] then
return cause
else
Expand All @@ -170,20 +174,8 @@ local lib = {}
lib.events = {
[defines.events.on_player_died] = destroyed_entity,
[defines.events.on_entity_died] = destroyed_entity,
[defines.events.script_raised_destroy] = destroyed_entity,
[defines.events.on_trigger_created_entity] = created_entity,
[defines.events.on_runtime_mod_setting_changed] = update_vars,
}

lib.on_init = function()
aliens_table = global.aliens
end

lib.on_configuration_changed = function()
aliens_table = global.aliens
end

lib.on_load = function ()
aliens_table = global.aliens
end

return lib
40 changes: 14 additions & 26 deletions scripts/control/forces.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
local forces_table, competitor_spice_table
local config = require('scripts.config')

local VICTORY_SPICE_AMOUNT = config.VICTORY_SPICE_AMOUNT
local VICTORY_CHECK_TICK = config.VICTORY_CHECK_TICK

function force_created(event)
local force_index = event.force.index
forces_table[force_index] = config.forces_default
global.forces[force_index] = config.default.forces
end

function forces_merged(event)
local force_table = global.forces
local source_index = event.source_index
local force_index = event.destination.index
local spice_count = forces_table[source_index].spice_shipped
spice_count = spice_count + forces_table[force_index].spice_shipped
forces_table[force_index].spice_shipped = spice_count
table.remove(forces_table, source_index)
local spice_count = force_table[source_index].spice_shipped
spice_count = spice_count + force_table[force_index].spice_shipped
force_table[force_index].spice_shipped = spice_count
force_table[source_index] = nil
end

function force_reset(event)
forces_table[event.force.index] = config.forces_default
global.forces[event.force.index] = config.default.forces
end

function checkSpiceVictory()
for force_index, entry in pairs(forces_table) do
for force_index, entry in pairs(global.forces) do
if entry.spice_shipped >= VICTORY_SPICE_AMOUNT then
if remote.interfaces['kr-intergalactic-transceiver'] and remote.interfaces['kr-intergalactic-transceiver']['set_no_victory'] then
remote.call('kr-intergalactic-transceiver', 'set_no_victory', true)
Expand All @@ -33,10 +35,11 @@ function checkSpiceVictory()
end

function rocket_launched(event)
local force_table = global.forces
local force_index = event.rocket.force.index
local spice_count = event.rocket.get_inventory(defines.inventory.rocket).get_item_count('spice')
spice_count = spice_count + forces_table[force_index].spice_shipped
forces_table[force_index].spice_shipped = spice_count
spice_count = spice_count + force_table[force_index].spice_shipped
force_table[force_index].spice_shipped = spice_count
end

-- lib
Expand All @@ -50,24 +53,9 @@ lib.events = {
}

lib.on_nth_tick = {
[120] = function()
[VICTORY_CHECK_TICK] = function()
checkSpiceVictory()
end
}

lib.on_init = function()
forces_table = global.forces
competitor_spice_table = global.competitor_spice
end

lib.on_configuration_changed = function()
forces_table = global.forces
competitor_spice_table = global.competitor_spice
end

lib.on_load = function ()
forces_table = global.forces
competitor_spice_table = global.competitor_spice
end

return lib
Loading

0 comments on commit 3d6a928

Please sign in to comment.