diff --git a/.vscode/settings.json b/.vscode/settings.json index adeef5e..07c219e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,7 +11,10 @@ "global", "script", "rendering", - "worm_shoot_shiftings" + "worm_shoot_shiftings", + "mods", + "remote", + "informatron_make_image" ], "Lua.diagnostics.disable": [ "lowercase-global" diff --git a/changelog.txt b/changelog.txt index 4d04ed4..7198669 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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 diff --git a/control.lua b/control.lua index 7996eb9..9d477bb 100644 --- a/control.lua +++ b/control.lua @@ -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 diff --git a/info.json b/info.json index 13952d6..2349646 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "nauvis-melange", - "version": "0.8.3", + "version": "0.9.0", "title": "Nauvis Melange", "author": "Zetabite", "factorio_version": "0.18", diff --git a/scripts/config.lua b/scripts/config.lua index a8430df..20378a6 100644 --- a/scripts/config.lua +++ b/scripts/config.lua @@ -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, @@ -44,4 +50,5 @@ config.spice_effects_blacklist_default = { }, name = {} } + return config diff --git a/scripts/control/aliens.lua b/scripts/control/aliens.lua index 054770d..7088b03 100644 --- a/scripts/control/aliens.lua +++ b/scripts/control/aliens.lua @@ -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 @@ -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 @@ -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 @@ -93,31 +94,31 @@ 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) @@ -125,14 +126,16 @@ function evolve_alien(alien, 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 @@ -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 @@ -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 diff --git a/scripts/control/forces.lua b/scripts/control/forces.lua index d2592dd..696a092 100644 --- a/scripts/control/forces.lua +++ b/scripts/control/forces.lua @@ -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) @@ -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 @@ -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 diff --git a/scripts/control/machines.lua b/scripts/control/machines.lua index 3b369a8..2c742dc 100644 --- a/scripts/control/machines.lua +++ b/scripts/control/machines.lua @@ -1,3 +1,7 @@ +local config = require('scripts.config') + +local WATER_INJECTOR_CHECK_TICK = config.WATER_INJECTOR_CHECK_TICK + function built_entity(event) local entity = event.created_entity if(entity) then @@ -41,6 +45,23 @@ function rocket_launch_ordered(event) silo.active = (#spacing_guilds >= #silos) end +function check_water_injectors() + local surface = game.surfaces[1] + local water_injectors = surface.find_entities_filtered({name = 'water-injector'}) + for _, entity in pairs(water_injectors) do + local capacity = entity.fluidbox.get_capacity(1) + local fluid_count = entity.get_fluid_count('water') + if (capacity - fluid_count) <= 1000 then + -- do explosion, create pre spice mass + local pos = entity.position + local deposit = surface.find_entity('worm-hole', pos) + deposit.destroy() + surface.create_entity({ name = 'explosive-rocket', position = pos, target = entity , force = 'neutral', speed = 0.5 }) + surface.create_entity({ name = 'pre-spice-mass-ore', position = pos, force = 'neutral' }) + end + end +end + -- lib local lib = {} @@ -51,21 +72,8 @@ lib.events = { } lib.on_nth_tick = { - [60] = function() - local surface = game.surfaces[1] - local water_injectors = surface.find_entities_filtered({name = 'water-injector'}) - for _, entity in pairs(water_injectors) do - local capacity = entity.fluidbox.get_capacity(1) - local fluid_count = entity.get_fluid_count('water') - if (capacity - fluid_count) <= 1000 then - -- do explosion, create pre spice mass - local pos = entity.position - local deposit = surface.find_entity('worm-hole', pos) - deposit.destroy() - surface.create_entity({ name = 'explosive-rocket', position = pos, target = entity , force = 'neutral', speed = 0.5 }) - surface.create_entity({ name = 'pre-spice-mass-ore', position = pos, force = 'neutral' }) - end - end + [WATER_INJECTOR_CHECK_TICK] = function() + check_water_injectors() end } diff --git a/scripts/control/players.lua b/scripts/control/players.lua index f2f3cfb..13e13e5 100644 --- a/scripts/control/players.lua +++ b/scripts/control/players.lua @@ -1,25 +1,78 @@ -local players_table, spice_effects_blacklist, render_table local config = require('scripts.config') -local SPICE_DURATION = config.SPICE_DURATION -local SPICE_COOLDOWN = config.SPICE_COOLDOWN -local OVERLAY_REFRESH = config.OVERLAY_REFRESH + +local PLAYER_CHECK_TICK = config.PLAYER_CHECK_TICK +local RENDER_REFRESH_TICK = config.RENDER_REFRESH_TICK local OVERLAY_TIMER = config.OVERLAY_TIMER +local SPICE_DURATION = config.SPICE_DURATION +local effect_table = config.effect_table remote.add_interface('nauvis_melange_player', { consume_spice = function(player_index, factor, consequence, pre_consumption) return consume_spice(player_index, factor, consequence, pre_consumption) end, onZoomFactorChanged = function(event) - if global.zoom_table[event.playerIndex] then - global.zoom_table[event.playerIndex] = event.zoomFactor - end + global.players[event.playerIndex].zoom_factor = event.zoomFactor + end, + spice_influence_changed_add = function(interface_name, function_name) + global.callbacks = global.callbacks or {} + global.callbacks.spice_influence_changed = global.callbacks.spice_influence_changed or {} + + local id = interface_name..'-'..function_name + local entry = { + interface_name = interface_name, + function_name = function_name, + } + global.callbacks.spice_influence_changed[id] = entry + end, + spice_influence_changed_remove = function(interface_name, function_name) + global.callbacks = global.callbacks or {} + global.callbacks.spice_influence_changed = global.callbacks.spice_influence_changed or {} + local id = interface_name..'-'..function_name + global.callbacks.spice_influence_changed[id] = nil + end, + has_spice_influence = function(player_index) + return has_spice_influence(player_index) end }) +function has_spice_influence(player_index) + local entry = global.players[player_index] + if entry.radar.tick or entry.craft_mod.tick then + return true + end + return false +end + +function has_bad_trip(player_index) + if global.players[player_index].bad_trip.tick then + return true + end + return false +end + +function spice_influence_changed_raise(player_index) + global.callbacks = global.callbacks or {} + global.callbacks.spice_effects_changed = global.callbacks.spice_effects_changed or {} + if has_spice_influence(player_index) then + game.get_player(player_index).print("Spice status: on") + else + game.get_player(player_index).print("Spice status: off") + end + local event = { + player_index = player_index, + on_spice = has_spice_influence(player_index) + } + for _, entry in pairs(global.callbacks.spice_effects_changed) do + if entry then + remote.call(entry.interface_name, entry.function_name, event) + end + end +end + function apply_spice_to_vehicle(player) if (player.vehicle) then local vehicle = player.vehicle - if vehicle.type == 'car' and not spice_effects_blacklist['name'][vehicle.name] then + if vehicle.type == 'car' and not global.spice_effects_blacklist['name'][vehicle.name] then if not has_spice_effects(vehicle) then local surface = vehicle.surface local position = vehicle.position @@ -36,21 +89,68 @@ function vehicle_interaction(event) apply_spice_to_vehicle(player) end -function has_spice_effects(entity) - if (entity.stickers) then - for _, sticker in pairs(entity.stickers) do - if sticker.name == 'spice-applied-sticker' then - return true +-- This is where the spice effects for the player are applied +local manage = { + radar = { + enable = function(player_index) + local player = game.get_player(player_index) + local radar = global.players[player_index].radar + local character = player.character + local surface = character.surface + local position = character.position + local reference = surface.create_entity({name = 'spice-radar', position = position, force = character.force}) + radar.reference = reference + radar.tick = game.tick + end, + disable = function(player_index) + local radar = global.players[player_index].radar + if radar.reference then + radar.reference.destroy() + end + radar.reference = false + radar.tick = false + end, + }, + + craft_mod = { + enable = function(player_index) + local player = game.get_player(player_index) + local craft_modifier = player.character_crafting_speed_modifier + player.character_crafting_speed_modifier = craft_modifier + 2 + craft_modifier = player.character_crafting_speed_modifier + global.players[player_index].craft_mod.tick = game.tick + end, + disable = function(player_index) + local player = game.get_player(player_index) + local craft_modifier = player.character_crafting_speed_modifier + player.character_crafting_speed_modifier = craft_modifier - 2 + global.players[player_index].craft_mod.tick = false + end, + }, + + bad_trip = { + enable = function(player_index) + if not settings.global['Kux-Running_Enable'].value then + local player = game.get_player(player_index) + local speed_modifier = player.character_running_speed_modifier + player.character_running_speed_modifier = speed_modifier * 0.8 end + global.players[player_index].bad_trip.tick = game.tick + end, + disable = function(player_index) + if not settings.global['Kux-Running_Enable'].value then + local player = game.get_player(player_index) + local speed_modifier = player.character_running_speed_modifier + player.character_running_speed_modifier = speed_modifier * 0.5 + end + global.players[player_index].bad_trip.tick = false end - end - return false -end + } +} --- This is where the spice effects for the player are applied function used_capsule(event) local capsule = event.item - if string.match(capsule.name, 'spice') then + if capsule.name == 'spice' then local player_index = event.player_index local player = game.get_player(player_index) if (player.character) then @@ -69,11 +169,14 @@ end function consume_spice(player_index, factor, consequence, pre_consumed) local player = game.get_player(player_index) local character = player.character - local addiction_level = players_table[player_index].addiction_level + local addiction_level = global.players[player_index].addiction_level local inventory = character.get_main_inventory() - local needed = ((1 + addiction_level * addiction_level) * factor) - if pre_consumed and pre_consumed > 0 then - needed = needed - pre_consumed + local needed = 0 + if factor > 0 then + needed = ((1 + addiction_level * addiction_level) * factor) + if pre_consumed and pre_consumed > 0 then + needed = needed - pre_consumed + end end if needed > 0 then local item = {name = 'spice', count = needed} @@ -84,12 +187,7 @@ function consume_spice(player_index, factor, consequence, pre_consumed) if consequence == 'no_effect' or consequence == 'bad_trip' then inventory.remove(item) if consequence == 'bad_trip' then - players_table[player_index].bad_trip.active = true - players_table[player_index].bad_trip.remaining_ticks = SPICE_DURATION - if not settings.global['Kux-Running_Enable'].value then - local speed_modifier = player.character_running_speed_modifier - player.character_running_speed_modifier = speed_modifier * 0.8 - end + manage.bad_trip.enable(player_index) character.damage(50 * (needed - actual), 'enemy') end elseif consequence == 'no_consumption' then @@ -103,111 +201,88 @@ function consume_spice(player_index, factor, consequence, pre_consumed) end inventory.remove(item) end - if not has_spice_effects(character) then + if not has_spice_influence(player_index) then apply_spice_to_player(player) end return true end function apply_spice_to_player(player) + local players_table = global.players local character = player.character local surface = character.surface local position = character.position - local addiction_level = players_table[player.index].addiction_level - players_table[player.index].addiction_level = addiction_level + 1 + local player_index = player.index + local addiction_level = players_table[player_index].addiction_level + players_table[player_index].addiction_level = addiction_level + 1 -- Create entities/effects - local radar = surface.create_entity({name = 'spice-radar', position = position, force = character.force}) if not settings.global['Kux-Running_Enable'].value then surface.create_entity({ name = 'spice-speed-sticker', target = character, position = position }) end surface.create_entity({ name = 'spice-regen-sticker', target = character, position = position }) - surface.create_entity({ name = 'spice-applied-sticker', target = character, position = position }) - players_table[player.index].radar.reference = radar - local craft_modifier = player.character_crafting_speed_modifier - player.character_crafting_speed_modifier = craft_modifier + 2 - craft_modifier = player.character_crafting_speed_modifier -- Table changes - players_table[player.index].under_influence = true - players_table[player.index].radar.active = true - players_table[player.index].craft_mod.active = true - players_table[player.index].radar.remaining_ticks = SPICE_COOLDOWN - players_table[player.index].craft_mod.remaining_ticks = SPICE_COOLDOWN - render_table.spice_overlay[player.index] = true + global.render_table.spice_overlay[player_index] = true + manage.radar.enable(player_index) + manage.craft_mod.enable(player_index) apply_spice_to_vehicle(player) + spice_influence_changed_raise(player_index) end function player_joined(event) + local players_table = global.players local player_index = event.player_index - if not (players_table[player_index]) then - players_table[player_index] = config.players_default + players_table[player_index] = players_table[player_index] or config.default.players + if has_spice_influence(player_index) then + global.render_table.spice_overlay[player_index] = true end end function player_died(event) local player_index = event.player_index - if players_table[player_index].radar.reference then - players_table[player_index].radar.reference.destroy() - end - players_table[player_index] = config.players_default - if render_table.spice_overlay[player_index] then - table.remove(render_table.spice_overlay, player_index) - end + try_to_remove_spice_effects(player_index, true) end function remove_addiction() for _, player in pairs(game.connected_players) do if player.character then + local players_table = global.players local addiction_level = players_table[player.index].addiction_level - if math.random(0, 100) > 75 and addiction_level > 0 then + if addiction_level > 0 and math.random(0, 100) > 90 then players_table[player.index].addiction_level = addiction_level - 1 end end end end -function remove_spice_effects() - if (players_table) then - for player_index, entry in pairs(players_table) do - local player = game.get_player(player_index) - if player.character then - if entry.radar.reference then - if entry.radar.remaining_ticks > 0 then - entry.radar.remaining_ticks = entry.radar.remaining_ticks - SPICE_DURATION - end - if entry.radar.remaining_ticks <= 0 then - entry.radar.reference.destroy() - entry.radar.reference = false - entry.radar.active = false - end - end - if entry.craft_mod.active then - if entry.craft_mod.remaining_ticks > 0 then - entry.craft_mod.remaining_ticks = entry.craft_mod.remaining_ticks - SPICE_DURATION - end - if entry.craft_mod.remaining_ticks <= 0 then - local craft_modifier = player.character_crafting_speed_modifier - player.character_crafting_speed_modifier = craft_modifier - 2 - entry.craft_mod.active = false - end - end - if entry.bad_trip.active then - if entry.bad_trip.remaining_ticks > 0 then - entry.bad_trip.remaining_ticks = entry.bad_trip.remaining_ticks - SPICE_DURATION - end - if entry.bad_trip.remaining_ticks <= 0 then - if not settings.global['Kux-Running_Enable'].value then - local speed_modifier = player.character_running_speed_modifier - player.character_running_speed_modifier = speed_modifier * 1.25 - end - entry.bad_trip.active = false - end - end - if render_table.spice_overlay[player_index] then - table.remove(render_table.spice_overlay, player_index) +function try_to_remove_spice_effects(player_index, ignore) + local players_table = global.players or {} + local player = game.get_player(player_index) + if player.character or ignore then + local entry = players_table[player_index] + for _, key in pairs(effect_table) do + local value = entry[key] + if value.tick then + if value.tick <= (game.tick - value.duration) or ignore then + manage[key].disable(player_index) end - players_table[player.index].under_influence = false end end + if not has_spice_influence(player_index) then + local render_table = global.render_table + if render_table.spice_overlay[player_index] then + render_table.spice_overlay[player_index] = nil + end + spice_influence_changed_raise(player_index) + end + end +end + +function player_check() + for _, player in pairs(game.connected_players) do + local player_index = player.index + if has_spice_influence(player_index) or has_bad_trip(player_index) then + try_to_remove_spice_effects(player_index, false) + end end end @@ -216,28 +291,44 @@ function player_moved(event) local player = game.get_player(player_index) if (player.character) then local character = player.character - if has_spice_effects(character) then + if has_spice_influence(player_index) then + local radar = global.players[player_index].radar local surface = character.surface - local radar = surface.create_entity({name = 'spice-radar', position = character.position, force = character.force}) - if players_table[player_index].radar.reference then - players_table[player_index].radar.reference.destroy() + local reference = surface.create_entity({name = 'spice-radar', position = character.position, force = character.force}) + if radar.reference then + radar.reference.destroy() end - players_table[player_index].radar.reference = radar + radar.reference = reference end end end function player_removed(event) - table.remove(players_table, event.player_index) - if render_table.spice_overlay[event.player_index] then - table.remove(render_table.spice_overlay, event.player_index) + local player_index = event.player_index + global.players[player_index] = nil + global.render_table.spice_overlay[player_index] = nil +end + +function player_left(event) + local player_index = event.player_index + global.render_table.spice_overlay[player_index] = nil +end + +function has_spice_effects(entity) + if (entity.stickers) then + for _, sticker in pairs(entity.stickers) do + if sticker.name == 'spice-applied-sticker' then + return true + end + end end + return false end -function overlay_refresh() - for player_index, _ in pairs(render_table.spice_overlay) do +function render_refresh() + for player_index, _ in pairs(global.render_table.spice_overlay) do local player = game.get_player(player_index) - local zoom_factor = players_table[player_index].zoom_factor + local zoom_factor = global.players[player_index].zoom_factor rendering.draw_sprite({ sprite = 'item.spice-overlay', x_scale = 160 * zoom_factor, @@ -251,14 +342,6 @@ function overlay_refresh() end end --- Util stuff - -function load_globals() - players_table = global.players - spice_effects_blacklist = global.spice_effects_blacklist - render_table = global.render_table -end - function check_and_call_kux_zooming() if script.active_mods['Kux-Zooming'] then if remote.interfaces['Kux-Zooming'] and remote.interfaces['Kux-Zooming']['onZoomFactorChanged'] then @@ -278,30 +361,31 @@ lib.events = { [defines.events.on_player_changed_position] = player_moved, [defines.events.on_player_changed_surface] = player_moved, [defines.events.on_player_removed] = player_removed, + [defines.events.on_player_left_game] = player_left, + [defines.events.on_player_kicked] = player_left, } lib.on_init = function() - load_globals() check_and_call_kux_zooming() end lib.on_configuration_changed = function() - load_globals() check_and_call_kux_zooming() end lib.on_load = function () - load_globals() check_and_call_kux_zooming() end lib.on_nth_tick = { [SPICE_DURATION] = function() remove_addiction() - remove_spice_effects() end, - [OVERLAY_REFRESH] = function() - overlay_refresh() + [PLAYER_CHECK_TICK] = function() + player_check() + end, + [RENDER_REFRESH_TICK] = function() + render_refresh() end } diff --git a/scripts/control/resources.lua b/scripts/control/resources.lua deleted file mode 100644 index 7ea4c5a..0000000 --- a/scripts/control/resources.lua +++ /dev/null @@ -1,27 +0,0 @@ -function destroyed_entity(event) - local entity = event.entity - if (entity) then - if entity.type == 'turret' and string.match(entity.name, 'worm') then - create_worm_hole(entity) - end - end -end - -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 - --- lib -local lib = {} - -lib.events = { - [defines.events.script_raised_destroy] = destroyed_entity, - [defines.events.on_entity_died] = destroyed_entity, -} - -return lib diff --git a/scripts/control/setup.lua b/scripts/control/setup.lua index fffad4e..06b0175 100644 --- a/scripts/control/setup.lua +++ b/scripts/control/setup.lua @@ -10,105 +10,26 @@ remote.add_interface('nauvis_melange_functions', { end }) --- Render table -function render_table_init() - global.render_table = config.render_default -end - --- Player table -function players_table_init() - global.players = {} - for _, player in pairs(game.connected_players) do - global.players[player.index] = config.forces_default - end -end - -function players_table_configuration_changed() - -- For older version - if global.addiction_system then - players_table_init() - local players = {} - for player_name, entry in pairs(global.addiction_system) do - if #entry <= 0 then - for subkey, value in pairs(config.players_default) do - if not entry[subkey] then - entry[subkey] = value - end - end - end - local player_index = game.get_player(player_name).index - players[player_index] = entry +-- Inits +init = { + players = function() + global.players = {} + for _, player in pairs(game.connected_players) do + global.players[player.index] = config.default.players end - global.players = players - end - -- For current version - if global.players then - local players = {} - for player_index, entry in pairs(global.players) do - players[player_index] = {} - for subkey, value in pairs(config.players_default) do - if not entry[subkey] then - entry[subkey] = value - else - if subkey == 'radar' and type(entry['radar']) ~= 'table' then - local radar_reference = entry['radar'] - local remaining_ticks = entry['radar_tick'] - entry['radar'] = { - active = false, - reference = false, - remaining_ticks = 0, - } - if radar_reference then - entry['radar'] = {} - entry['radar'].active = true - entry['radar'].reference = radar_reference - entry['radar'].remaining_ticks = remaining_ticks - end - table.remove(entry, 'radar_tick') - elseif subkey == 'counter' then - entry['addiction_level'] = entry['counter'] - table.remove(entry, 'counter') - end - end - end - players[player_index] = entry - end - global.players = players - else - players_table_init() - end -end - --- Forces table for winning condition -function forces_table_init() - global.forces = {} - for _, force in pairs(game.forces) do - global.forces[force.index] = config.forces_default - end -end + end, -function forces_table_configuration_changed() - if global.forces then - local forces = {} - for force_index, entry in pairs(global.forces) do - if #entry <= 0 then - for subkey, value in pairs(config.forces_default) do - if not entry[subkey] then - entry[subkey] = value - end - end - end - forces[force_index] = entry + -- Forces table for winning condition + forces = function() + global.forces = {} + for _, force in pairs(game.forces) do + global.forces[force.index] = config.default.forces end - global.forces = forces - else - forces_table_init() - end -end + end, --- Aliens Table -function aliens_table_init() - global.aliens = {} + -- Aliens Table + aliens = function() + global.aliens = {} -- Creating enums from mods local enum_aliens = { ['biter'] = {}, @@ -159,48 +80,99 @@ function aliens_table_init() enum_aliens_reverse[type_name] = reverse end + global.aliens.dict = enum_aliens global.aliens.names = enum_alien_names global.aliens.collector = enum_alien_collector global.aliens.melee = enum_alien_melee global.aliens.reverse = enum_aliens_reverse -end - --- Competitor spice table -function competitor_spice_table_init() - global.competitor_spice = {} - if script.active_mods['space-exploration'] then - local spice_name = 'se-vitamelange' - local spice_suffix = { '', '-spice', '-nugget', '-roast', '-extract'} - for _, suffix in pairs(spice_suffix) do - table.insert(global.competitor_spice, (spice_name..suffix)) + end, + competitor_spice = function() + global.competitor_spice = {} + if script.active_mods['space-exploration'] then + local spice_name = 'se-vitamelange' + local spice_suffix = { '', '-spice', '-nugget', '-roast', '-extract'} + for _, suffix in pairs(spice_suffix) do + table.insert(global.competitor_spice, (spice_name..suffix)) + end end end -end - --- Spice effects blacklist -function spice_effects_blacklist_init() - global.spice_effects_blacklist = config.spice_effects_blacklist_default -end +} + +-- Inits +configuration_changed = { + players = function() + -- Change for next version in case of change + local players_table = global.players + if players_table then + for player_index,_ in pairs(players_table) do + local default = config.default.players + if players_table[player_index].under_influence ~= nil then + players_table[player_index] = default + else + for key, entry in pairs(default) do + if not players_table[player_index][key] then + players_table[player_index][key] = entry + end + if type(default[key]) == 'table' then + for subkey, subentry in pairs(default[key]) do + if not entry[subkey] then + entry[subkey] = subentry + end + end + end + end + end + end + else + init.players() + end + end, + -- Forces table for winning condition + forces = function() + if global.forces then + local forces = {} + for force_index, entry in pairs(global.forces) do + if #entry <= 0 then + for subkey, value in pairs(config.default.forces) do + if not entry[subkey] then + entry[subkey] = value + end + end + end + forces[force_index] = entry + end + global.forces = forces + else + init.forces() + end + end, + aliens = function() + init.aliens() + end, + competitor_spice = function() + init.competitor_spice() + end +} -- lib local lib = {} lib.on_init = function() - players_table_init() - aliens_table_init() - competitor_spice_table_init() - forces_table_init() - spice_effects_blacklist_init() - render_table_init() + global.render_table = config.default.render + global.spice_effects_blacklist = config.default.spice_effects_blacklist + init.aliens() + init.players() + init.forces() + init.competitor_spice() end lib.on_configuration_changed = function() - players_table_configuration_changed() - aliens_table_init() - competitor_spice_table_init() - forces_table_configuration_changed() - spice_effects_blacklist_init() - render_table_init() + global.render_table = global.render_table or config.default.render + global.spice_effects_blacklist = global.spice_effects_blacklist or config.default.spice_effects_blacklist + configuration_changed.aliens() + configuration_changed.players() + configuration_changed.forces() + configuration_changed.competitor_spice() end return lib