From 9ec9a2a77ce55322928216d20adfee6f6acee3b2 Mon Sep 17 00:00:00 2001 From: Octav Sandulescu Date: Wed, 30 Sep 2015 12:34:49 +0000 Subject: [PATCH] Add kill count sensor. Closes #29 --- evoGUI.lua | 1 + locale/en/locale.cfg | 10 +++++ value_sensors/kill_count.lua | 87 ++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 value_sensors/kill_count.lua diff --git a/evoGUI.lua b/evoGUI.lua index d0e853e..d2e4856 100644 --- a/evoGUI.lua +++ b/evoGUI.lua @@ -1,6 +1,7 @@ require "defines" require "value_sensors.day_time" require "value_sensors.evolution_factor" +require "value_sensors.kill_count" require "value_sensors.play_time" require "value_sensors.player_locations" require "value_sensors.pollution_around_player" diff --git a/locale/en/locale.cfg b/locale/en/locale.cfg index b885833..199de50 100644 --- a/locale/en/locale.cfg +++ b/locale/en/locale.cfg @@ -32,6 +32,16 @@ sensor.player_locations.settings.show_direction=Show direction to walk to reach sensor.pollution_around_player.name=Nearby pollution level sensor.pollution_around_player.format=Pollution: __1__ PU. +sensor.kill_count.name=Kill count (for your team) +sensor.kill_count.format=Kills: __1__, __2__, __3__. +sensor.kill_count.biter_fragment_single=1 biter +sensor.kill_count.biter_fragment_multiple=__1__ biters +sensor.kill_count.spawner_fragment_single=1 spawner +sensor.kill_count.spawner_fragment_multiple=__1__ spawners +sensor.kill_count.other_fragment_single=1 other +sensor.kill_count.other_fragment_multiple=__1__ others + + settings_title=EvoGUI settings settings.core_settings.title=Core settings (shared) diff --git a/value_sensors/kill_count.lua b/value_sensors/kill_count.lua new file mode 100644 index 0000000..5bf2715 --- /dev/null +++ b/value_sensors/kill_count.lua @@ -0,0 +1,87 @@ +require "template" + +-- Preload known vanilla types +local entity_types = { + ["unit"] = { + ["small-biter"] = true, + ["medium-biter"] = true, + ["big-biter"] = true, + ["behemoth-biter"] = true, + ["small-spitter"] = true, + ["medium-spitter"] = true, + ["big-spitter"] = true, + ["behemoth-spitter"] = true, + }, + ["unit-spawner"] = { + ["biter-spawner"] = true, + ["spitter-spawner"] = true, + }, +} + +local function is_entity_type(what_type, entity_name) + if not entity_types[what_type] then entity_types[what_type] = {} end + local type_cache = entity_types[what_type] + + if type_cache[entity_name] ~= nil then + return type_cache[entity_name] + end + + local prototype = game.entity_prototypes[entity_name] + if prototype and prototype.type == what_type then + type_cache[entity_name] = true + else + type_cache[entity_name] = false + end + + return type_cache[entity_name] +end + + +local function is_biter(entity_name) + return is_entity_type("unit", entity_name) +end + + +local function is_spawner(entity_name) + return is_entity_type("unit-spawner", entity_name) +end + + +local sensor = ValueSensor.new("kill_count") + +function sensor:update_ui(owner) + local player = game.get_player(owner.player_index) + + local biter_count = 0 + local spawner_count = 0 + local other_count = 0 + for entity_name, kill_count in pairs(player.force.kill_counts) do + if is_biter(entity_name) then + biter_count = biter_count + kill_count + elseif is_spawner(entity_name) then + spawner_count = spawner_count + kill_count + else + other_count = other_count + kill_count + end + end + + local biter_kills = {"sensor.kill_count.biter_fragment_single"} + if biter_count ~= 1 then + biter_kills = {"sensor.kill_count.biter_fragment_multiple", biter_count} + end + + local spawner_kills = {"sensor.kill_count.spawner_fragment_single"} + if spawner_count ~= 1 then + spawner_kills = {"sensor.kill_count.spawner_fragment_multiple", spawner_count} + end + + local other_kills = {"sensor.kill_count.other_fragment_single"} + if other_count ~= 1 then + other_kills = {"sensor.kill_count.other_fragment_multiple", other_count} + end + + owner[self.name].caption = {self.format_key, biter_kills, spawner_kills, other_kills} +end + +ValueSensor.register(sensor) +