diff --git a/README.md b/README.md index 421c00d..7038f90 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ### Description -Adds alerts when a car or gun turret is out of ammo or has low ammo. The amount of ammo considered low can be configured. Alerts for cars and turrets can be disabled separately. +Adds alerts when a car or gun turret is out of ammo or has low ammo. The amount of ammo considered low can be configured. Alerts for cars and turrets can be disabled separately. Behaviour on cars with multiple ammo slots can be configured globally. ### Locale If you want to contribute by translating this mod, you can view the existing translations [here](https://github.com/Wyrrrd/Gun_Turret_Alerts/tree/master/locale). I'd be happy to add your language and credits to the next release. diff --git a/changelog.txt b/changelog.txt index fc453ea..1b3d16b 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,13 @@ --------------------------------------------------------------------------------------------------- +Version: 1.1.5 +Date: 2021-04-19 + Features: + - Added config option to change behaviour on multiple ammo slots (for cars) + Bugfixes: + - Fixed only checking for first ammo slot in cars + Optimisations: + - Theoretical performance improvement by not scanning surfaces twice anymore (once for turrets and once for cars) +--------------------------------------------------------------------------------------------------- Version: 1.1.4 Date: 2021-04-02 Bugfixes: diff --git a/control.lua b/control.lua index beba8a5..079a7c1 100644 --- a/control.lua +++ b/control.lua @@ -1,23 +1,74 @@ --control.lua --This mod scans the map for cars and gun-turrets and places alerts when they are low. +local get_ammo_flags = { + ["added"] = function (inventory) + local no,low = false + if inventory.is_empty() then + no = true + else + local ammo_count = 0 + for i=1, #inventory do + ammo_count = ammo_count + inventory[i].count + end + if ammo_count == 0 then + no = true + elseif ammo_count < player_threshold then + low = true + end + end + return no, low + end, + + ["individually"] = function (inventory) + local no,low = false + if inventory.is_empty() then + no = true + else + for i=1, #inventory do + if inventory[i].count == 0 then + no = true + elseif inventory[i].count < player_threshold then + low = true + end + end + end + return no, low + end, + + ["selected"] = function (inventory,gun_index) + local no,low = false + if inventory.is_empty() then + no = true + else + if not gun_index then + gun_index = 1 + end + if inventory[gun_index].count == 0 then + no = true + elseif inventory[gun_index].count < player_threshold then + low = true + end + end + return no, low + end +} + + script.on_init(function (event) -- index init - global.turret_entities = {} - global.car_entities = {} + global.ammo_entities = {} end) script.on_configuration_changed(function (event) -- index init fix - global.turret_entities = {} - global.car_entities = {} + global.ammo_entities = {} end) script.on_nth_tick(3600, function (event) --Every minute the surface is rescanned for car and ammo-turret type entities. This is stored in two global tables. for index,surface in pairs(game.surfaces) do - global.turret_entities[index] = surface.find_entities_filtered{type = "ammo-turret"} - global.car_entities[index] = surface.find_entities_filtered{type = "car"} + global.ammo_entities[index] = surface.find_entities_filtered{type = {"ammo-turret","car"}} end end) @@ -25,38 +76,38 @@ script.on_nth_tick(600, function (event) --Every 10 seconds recheck and give alerts to players for car and ammo-turret entities on the same force as them. for _,player in pairs(game.connected_players) do - GTA_turret_enabled = player.mod_settings["gun-turret-alerts-enabled"].value - GTA_car_enabled = player.mod_settings["gun-turret-alerts-car-enabled"].value + turret_enabled = player.mod_settings["gun-turret-alerts-enabled"].value + car_enabled = player.mod_settings["gun-turret-alerts-car-enabled"].value + mode = player.mod_settings["gun-turret-alerts-mode"].value player_threshold = player.mod_settings["gun-turret-alerts-threshold"].value - turret_entities = global.turret_entities[player.surface.name] - car_entities = global.car_entities[player.surface.name] + ammo_entities = global.ammo_entities[player.surface.name] - if GTA_turret_enabled and turret_entities then - for _,turret_entity in pairs(turret_entities) do - if turret_entity.valid and turret_entity.force == player.force then - inv_var = turret_entity.get_inventory(defines.inventory.turret_ammo) - if inv_var.is_empty() then - -- no ammo alert - player.add_custom_alert(turret_entity, {type = "virtual", name = "ammo-icon-red"}, {"gun-turret-alerts.message-empty", turret_entity.localised_name}, true) - elseif inv_var[1].count < player_threshold then - -- low ammo alert - player.add_custom_alert(turret_entity, {type = "virtual", name = "ammo-icon-yellow"}, {"gun-turret-alerts.message-low", turret_entity.localised_name}, true) + if ammo_entities then + for _,entity in pairs(ammo_entities) do + if entity.valid and entity.force == player.force then + + local inventory + if entity.type == "ammo-turret" and turret_enabled then + inventory = entity.get_inventory(defines.inventory.turret_ammo) + elseif entity.type == "car" and car_enabled then + inventory = entity.get_inventory(defines.inventory.car_ammo) end - end - end - end - - if GTA_car_enabled and car_entities then - for _,car_entity in pairs(car_entities) do - -- extra check if car has gun - if car_entity.valid and car_entity.force == player.force and car_entity.selected_gun_index then - inv_var = car_entity.get_inventory(defines.inventory.car_ammo) - if inv_var.is_empty() then + + local no, low + if inventory and get_ammo_flags[mode] then + if entity.type == "ammo-turret" then + no, low = get_ammo_flags[mode](inventory) + elseif entity.type == "car" then + no, low = get_ammo_flags[mode](inventory,entity.selected_gun_index) + end + end + + if no then -- no ammo alert - player.add_custom_alert(car_entity, {type = "virtual", name = "ammo-icon-red"}, {"gun-turret-alerts.message-empty", car_entity.localised_name}, true) - elseif inv_var[1].count < player_threshold then + player.add_custom_alert(entity, {type = "virtual", name = "ammo-icon-red"}, {"gun-turret-alerts.message-empty", entity.localised_name}, true) + elseif low then -- low ammo alert - player.add_custom_alert(car_entity, {type = "virtual", name = "ammo-icon-yellow"}, {"gun-turret-alerts.message-low", car_entity.localised_name}, true) + player.add_custom_alert(entity, {type = "virtual", name = "ammo-icon-yellow"}, {"gun-turret-alerts.message-low", entity.localised_name}, true) end end end diff --git a/info.json b/info.json index c8316b9..22dabf9 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "Gun_Turret_Alerts", - "version": "1.1.4", + "version": "1.1.5", "title": "Ammo Alerts", "author": "Wyrrrd", "factorio_version": "1.1", diff --git a/locale/de/config.cfg b/locale/de/config.cfg index a178a00..3708ba2 100644 --- a/locale/de/config.cfg +++ b/locale/de/config.cfg @@ -1,13 +1,20 @@ [mod-setting-name] gun-turret-alerts-enabled=Alarme für Geschützturmmunition aktiviert gun-turret-alerts-car-enabled=Alarme für Fahrzeugmunition aktiviert +gun-turret-alerts-mode=Mehrplatz-Modus gun-turret-alerts-threshold=Grenze für wenig Munition [mod-setting-description] gun-turret-alerts-enabled=Aktiviert Alarme für Geschützturmmunition für deinen Spieler gun-turret-alerts-car-enabled=Aktiviert Alarme für Fahrzeugmunition für deinen Spieler +gun-turret-alerts-mode=Bestimmt, ob die Munitionsplätze aufaddiert, jeder Platz einzeln oder nur der ausgewählte Platz gezählt wird.\n(Greift nur bei mehr als einem Munitionsplatz, zum Beispiel in Fahrzeugen.) gun-turret-alerts-threshold=Wenn ein Fahrzeug oder Geschützturm weniger als diese Anzahl Munition hat, wird ein Alarm zu deinem Spieler hinzugefügt +[string-mod-setting] +autofilter_mode-added=Aufaddiert +autofilter_mode-individually=Einzeln +autofilter_mode-selected=Auswahl + [gun-turret-alerts] message-empty=__1__ hat keine Munition message-low=__1__ hat wenig Munition \ No newline at end of file diff --git a/locale/en/config.cfg b/locale/en/config.cfg index c66ff26..44e8814 100644 --- a/locale/en/config.cfg +++ b/locale/en/config.cfg @@ -1,12 +1,19 @@ [mod-setting-name] gun-turret-alerts-enabled=Turret ammo alerts enabled gun-turret-alerts-car-enabled=Car ammo alerts enabled +gun-turret-alerts-mode=Multi slot mode gun-turret-alerts-threshold=Low ammo threshold [mod-setting-description] -gun-turret-alerts-enabled=Enables turret ammo alerts to be added to your player -gun-turret-alerts-car-enabled=Enables car ammo alerts to be added to your player -gun-turret-alerts-threshold=If a car or turret has less than this much ammo an alert will be added to your player +gun-turret-alerts-enabled=Enables turret ammo alerts to be added to your player. +gun-turret-alerts-car-enabled=Enables car ammo alerts to be added to your player. +gun-turret-alerts-mode=Controls, if the ammo slots are added up, every slot is counted individually, or just the selected slot is counted.\n(Only applies for more than one ammo slot, for example in vehicles.) +gun-turret-alerts-threshold=If a car or turret has less than this much ammo an alert will be added to your player. + +[string-mod-setting] +autofilter_mode-added=Added up +autofilter_mode-individually=Individually +autofilter_mode-selected=Selected [gun-turret-alerts] message-empty=__1__ out of ammo diff --git a/settings.lua b/settings.lua index 9e7fcb8..02ae43f 100644 --- a/settings.lua +++ b/settings.lua @@ -13,6 +13,13 @@ data:extend({ name = "gun-turret-alerts-car-enabled", setting_type = "runtime-per-user", default_value = true + }, + { + type = "string-setting", + name = "gun-turret-alerts-mode", + setting_type = "runtime-per-user", + allowed_values = {"added","individually","selected"}, + default_value = "selected" }, { type = "int-setting", @@ -20,7 +27,5 @@ data:extend({ setting_type = "runtime-per-user", default_value = 8, minimum_value = 0 - - } - + }, }) \ No newline at end of file