first iteration of the toggle code

This commit is contained in:
Peter 2019-03-11 20:28:38 +08:00
parent 8f3af2d358
commit 2c634396bf
4 changed files with 136 additions and 34 deletions

35
Shortcuts/changelog.txt Normal file
View File

@ -0,0 +1,35 @@
---------------------------------------------------------------------------------------------------
Version: 0.4.0
Date: 10. 03. 2019
Features:
-
Bugfixes:
- Toggle buttons visually work properly
- Using the lamp toggle on a god controller no longer crashes the game
---------------------------------------------------------------------------------------------------
Version: 0.3.0
Date: 10. 03. 2019
Features:
- Added distress beacon and ultra zoom button
Bugfixes:
- Fix another equipment grid issue found in playtesting
---------------------------------------------------------------------------------------------------
Version: 0.2.2
Date: 10. 03. 2019
Bugfixes:
- Fixed issue with gridless armor
---------------------------------------------------------------------------------------------------
Version: 0.2.1
Date: 09. 03. 2019
Bugfixes:
- Fixed issue with the disabled item equipment generation
---------------------------------------------------------------------------------------------------
Version: 0.2.0
Date: 09. 03. 2019
Features:
- Added toggle shortcuts for night-vision-equipment, belt-immunity-equipment and the flashlight
---------------------------------------------------------------------------------------------------
Version: 0.1.0
Date: 08. 03. 2019
Features:
- Added shortcut for the artillery-targeting-remote

View File

@ -1,11 +1,15 @@
local function update_armor(event)
local player = game.players[event.player_index]
power_armor = player.get_inventory(defines.inventory.player_armor)
if power_armor and power_armor.valid then
local power_armor = player.get_inventory(defines.inventory.player_armor)
if power_armor and power_armor ~= nil and power_armor.valid then
if power_armor[1].valid_for_read then
if power_armor[1].grid and power_armor[1].grid.valid then
if power_armor[1].grid and power_armor[1].grid.valid and power_armor[1].grid.width > 0 then
global.shortcuts_armor[player.index] = power_armor[1].grid
else
table.remove(global.shortcuts_armor,player.index)
end
else
table.remove(global.shortcuts_armor,player.index)
end
end
end
@ -25,12 +29,14 @@ local function update_state(event, equipment_type)
if new_equipment and new_equipment.valid then
new_equipment.energy = energy
end
game.players[event.player_index].set_shortcut_toggled(equipment_type, false)
elseif (string.sub(equipment.name,1,9) == "disabled-") then
grid.take(equipment)
local new_equipment = grid.put{name=(string.sub(name,10,#name)), position=position}
if new_equipment and new_equipment.valid then
new_equipment.energy = energy
end
end
game.players[event.player_index].set_shortcut_toggled(equipment_type, true)
end
end
end
@ -38,23 +44,29 @@ local function update_state(event, equipment_type)
end
local function toggle_light(player)
if global.shortcuts_light[player.index] == nil then
global.shortcuts_light[player.index] = true
end
if global.shortcuts_light[player.index] == true then
player.character.disable_flashlight()
global.shortcuts_light[player.index] = false
elseif global.shortcuts_light[player.index] == false then
player.character.enable_flashlight()
global.shortcuts_light[player.index] = true
if player.character then
if global.shortcuts_light[player.index] == nil then
global.shortcuts_light[player.index] = true
end
if global.shortcuts_light[player.index] == true then
player.character.disable_flashlight()
global.shortcuts_light[player.index] = false
player.set_shortcut_toggled("flashlight-toggle", false)
elseif global.shortcuts_light[player.index] == false then
player.character.enable_flashlight()
global.shortcuts_light[player.index] = true
player.set_shortcut_toggled("flashlight-toggle", true)
end
else
player.print({"", {"error.error-message-box-title"}, ": ", {"player-doesnt-exist", {"gui.character"}}, " (", {"controller.god"}, "): ", {"entity-name.small-lamp"}, " ", {"gui-mod-info.status-disabled"}})
end
end
local function shortcut_type(event)
local prototype_name = event.prototype_name
if prototype_name == "night-vision-toggle" then
if prototype_name == "night-vision-equipment" then
update_state(event, "night-vision-equipment")
elseif prototype_name == "belt-immunity-toggle" then
elseif prototype_name == "belt-immunity-equipment" then
update_state(event, "belt-immunity-equipment")
elseif prototype_name == "flashlight-toggle" then
toggle_light(game.players[event.player_index])
@ -75,10 +87,44 @@ local function shortcut_type(event)
end
end
local function remove_remote(event)
local trash = game.players[event.player_index].get_inventory(defines.inventory.player_main)
if trash and trash.valid then
trash.remove("artillery-targeting-remote")
local function update_inventory(event)
local inventory = game.players[event.player_index].get_inventory(defines.inventory.player_main)
if inventory and inventory.valid then
inventory.remove("artillery-targeting-remote")
end
end
local function reset_state(event)
update_armor(event)
local player = game.players[event.player_index]
local grid = global.shortcuts_armor[game.players[event.player_index].index]
if grid then
for _, equipment in pairs(grid.equipment) do
if equipment.type == "night-vision-equipment" then
player.set_shortcut_available("night-vision-equipment", true)
player.set_shortcut_toggled("night-vision-equipment", true)
elseif equipment.type == "belt-immunity-equipment" then
player.set_shortcut_available("belt-immunity-equipment", true)
player.set_shortcut_toggled("belt-immunity-equipment", true)
end
if equipment.valid and equipment.type == "night-vision-equipment" or equipment.type == "belt-immunity-equipment" then
local name = equipment.name
local position = equipment.position
local energy = equipment.energy
if (string.sub(equipment.name,1,9) == "disabled-") then
grid.take(equipment)
local new_equipment = grid.put{name=(string.sub(name,10,#name)), position=position}
if new_equipment and new_equipment.valid then
new_equipment.energy = energy
end
end
end
end
else
player.set_shortcut_available("night-vision-equipment", false)
player.set_shortcut_available("belt-immunity-equipment", false)
player.set_shortcut_toggled("night-vision-equipment", false)
player.set_shortcut_toggled("belt-immunity-equipment", false)
end
end
@ -91,13 +137,20 @@ local function initialize()
end
end
script.on_event(defines.events.on_player_armor_inventory_changed, update_armor)
script.on_event(defines.events.on_player_placed_equipment, update_armor)
script.on_event(defines.events.on_player_removed_equipment, update_armor)
script.on_event(defines.events.on_player_armor_inventory_changed, reset_state)
script.on_event(defines.events.on_player_placed_equipment, reset_state)
script.on_event(defines.events.on_player_removed_equipment, reset_state)
script.on_event(defines.events.on_lua_shortcut, shortcut_type)
script.on_event(defines.events.on_player_main_inventory_changed, remove_remote)
script.on_event(defines.events.on_player_main_inventory_changed, update_inventory)
script.on_event(defines.events.on_player_created, function(event)
local player = game.players[event.player_index]
player.set_shortcut_toggled("flashlight-toggle", true)
player.set_shortcut_available("night-vision-equipment", false)
player.set_shortcut_available("belt-immunity-equipment", false)
end)
script.on_init(initialize)
script.on_configuration_changed(initialize)

View File

@ -20,31 +20,43 @@ for i=1,(#equipment_list) do
for _, equipment in pairs(data.raw[equipment_list[i]]) do
local i = #disabled_equipment+1
disabled_equipment[i] = util.table.deepcopy(equipment)
disabled_equipment_item[i] = util.table.deepcopy(data.raw["item"][equipment_list[i]])
local name = disabled_equipment[i].name
local newname = "disabled-" .. name
disabled_equipment[i].name = newname
disabled_equipment[i].name = "disabled-" .. name
disabled_equipment[i].localised_name = {"", {"equipment-name." .. name}, " (", {"gui-constant.off"}, ")"}
disabled_equipment[i].energy_input = "0kW"
disabled_equipment[i].take_result = name
if equipment_list[i] == "belt-immunity-equipment" then
disabled_equipment[i].energy_source.input_flow_limit = "0kW"
disabled_equipment[i].energy_source.buffer_capacity = "0kJ"
disabled_equipment[i].energy_source.drain = "1kW"
end
if not data.raw["item"][equipment_list[i]] then --for mods which have a different item name compared to equipment name
disabled_equipment_item[i] = util.table.deepcopy(data.raw["item"]["night-vision-equipment"])
disabled_equipment_item[i].name = "disabled-" .. name
disabled_equipment_item[i].hidden = true
disabled_equipment_item[i].placed_as_equipment_result = name
end
disabled_equipment_item[i] = util.table.deepcopy(data.raw["item"][equipment_list[i]]) -- LEGACY ITEM (Disable for release)
if not disabled_equipment_item[i] then
disabled_equipment_item[i] = util.table.deepcopy(data.raw["item"]["night-vision-equipment"])
end
disabled_equipment_item[i].name = newname
disabled_equipment_item[i].name = "disabled-" .. name
disabled_equipment_item[i].hidden = true
disabled_equipment_item[i].localised_name = {"", {"equipment-name." .. name}, " (", {"gui-constant.off"}, ")"}
-- disabled_equipment_item[i].localised_name = {"", {"equipment-name." .. name}, " (", {"gui-constant.off"}, ")"}
disabled_equipment_item[i].localised_description = {"item-description." .. name}
disabled_equipment_item[i].placed_as_equipment_result = newname
disabled_equipment_item[i].placed_as_equipment_result = name
end
end
for i=1,(#disabled_equipment),1 do
data:extend({disabled_equipment_item[i]})
data:extend({disabled_equipment[i]})
if disabled_equipment_item[i] then
data:extend({disabled_equipment_item[i]})
end
end
local warning = {

View File

@ -36,8 +36,8 @@ data:extend(
},
{
type = "shortcut",
name = "night-vision-toggle",
order = "a[night-vision-toggle]",
name = "night-vision-equipment",
order = "a[night-vision-equipment]",
action = "lua",
localised_name = {"equipment-name.night-vision-equipment"},
technology_to_unlock = "night-vision-equipment",
@ -69,8 +69,8 @@ data:extend(
},
{
type = "shortcut",
name = "belt-immunity-toggle",
order = "a[belt-immunity-toggle]",
name = "belt-immunity-equipment",
order = "a[belt-immunity-equipment]",
action = "lua",
localised_name = {"item-name.belt-immunity-equipment"},
technology_to_unlock = "belt-immunity-equipment",
@ -104,6 +104,7 @@ data:extend(
type = "shortcut",
name = "flashlight-toggle",
order = "a[flashlight-toggle]",
technology_to_unlock = "electronics",
action = "lua",
localised_name = {"entity-name.small-lamp"},
toggleable = true,
@ -170,6 +171,7 @@ data:extend(
type = "shortcut",
name = "signal-flare",
order = "a[signal-flare]",
technology_to_unlock = "electronics",
action = "lua",
localised_name = {"", {"technology-name.military"}, " ", {"entity-name.beacon"}, " (", {"description.force"}, " ", {"deconstruction-tile-mode.only"}, ")"},
toggleable = true,