Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 75 additions & 90 deletions bobclasses/control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,13 @@ function minime_exchanged_characters(event)
local player_index = event.player_index
local old_character = event.old_character
local new_character = event.new_character
local editor = event.editor_mode
local god = event.god_mode

-- Switch to another character
if new_character and not (editor or god) then
if new_character and not god then
wlog("Calling refresh_avatar_gui(" .. player_index .. ")")
refresh_avatar_gui(player_index)

-- Enter editor mode
elseif editor then
wlog("Calling switch_to_editor(" .. player_index .. ", " .. tostring(old_character and old_character.name) .. ")")
switch_to_editor(player_index, old_character)
-- Enter god mode
elseif god then
wlog("Calling switch_to_god(" .. player_index .. ", " .. tostring(old_character and old_character.name) .. ")")
Expand Down Expand Up @@ -277,6 +272,9 @@ function init()
if not storage.respawn_inventory then
storage.respawn_inventory = {}
end
if not storage.god_inventories then
storage.god_inventories = {}
end

storage.classes["bob-ballanced"] = nil
storage.classes["bob-balanced"] = nil
Expand Down Expand Up @@ -592,6 +590,15 @@ end)

script.on_event(defines.events.on_player_created, function(event)
wlog("Entered event handler on_player_created(" .. serpent.line(event) .. ")")

if not storage.god_start then
storage.god_start = false
end

if game.players[event.player_index].controller_type == defines.controllers.god then
storage.god_start = true
end

create_button(event.player_index)
storage.players[event.player_index] = { respawn = false, first_character = true }
class_select(event.player_index)
Expand Down Expand Up @@ -669,7 +676,6 @@ script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
if event.setting_type == "runtime-global" then
if
event.setting == "bobmods-classes-god-mode"
or event.setting == "bobmods-classes-editor-mode"
or event.setting == "bobmods-classes-class-select"
or event.setting == "bobmods-classes-class-select-user"
then
Expand Down Expand Up @@ -759,9 +765,6 @@ script.on_event(defines.events.on_gui_click, function(event)
if event.element.valid and event.element.name == "bob_avatar_god" then
switch_to_god(event.player_index)
end
if event.element.valid and event.element.name == "bob_avatar_editor" then
switch_to_editor(event.player_index)
end
if event.element.valid and event.element.name == "bob_avatar_class_select" then
draw_class_gui(event.player_index)
end
Expand Down Expand Up @@ -1061,7 +1064,7 @@ function draw_buttons_row(player_index)
end

if not game.is_multiplayer or player.admin then
if settings.global["bobmods-classes-god-mode"].value then
if settings.global["bobmods-classes-god-mode"].value or storage.god_start == true then
gui.add({
type = "button",
name = "bob_avatar_god",
Expand All @@ -1070,15 +1073,6 @@ function draw_buttons_row(player_index)
})
gui.bob_avatar_god.style.minimal_width = 56
end
if settings.global["bobmods-classes-editor-mode"].value then
gui.add({
type = "button",
name = "bob_avatar_editor",
caption = { "gui.bob-avatar-editor-mode" },
tooltip = { "gui.bob-avatar-editor-mode-tooltip" },
})
gui.bob_avatar_editor.style.minimal_width = 56
end
end
if
((not game.is_multiplayer or player.admin) and settings.global["bobmods-classes-class-select"].value)
Expand Down Expand Up @@ -1393,14 +1387,24 @@ function create_character(player_index, class)

if storage.players[player_index].first_character then
for i, item in pairs(storage.starting_inventory) do
player.insert(item)
if prototypes.item[item.name] then
player.insert(item)
end
end
if class.starting_inventory then
for i, item in pairs(class.starting_inventory.add) do
player.insert(item)
if prototypes.item[item.name] then
player.insert(item)
end
end
for i, item in pairs(class.starting_inventory.replace) do
if item.remove and item.add and player.get_item_count(item.remove.name) >= item.remove.count then
if
item.remove
and item.add
and prototypes.item[item.remove.name]
and prototypes.item[item.add.name]
and player.get_item_count(item.remove.name) >= item.remove.count
then
player.remove_item(item.remove)
player.insert(item.add)
end
Expand All @@ -1409,14 +1413,24 @@ function create_character(player_index, class)
storage.players[player_index].first_character = false
elseif storage.players[player_index].respawn then
for i, item in pairs(storage.respawn_inventory) do
player.insert(item)
if prototypes.item[item.name] then
player.insert(item)
end
end
if class.respawn_inventory.replace then
for i, item in pairs(class.respawn_inventory.add) do
player.insert(item)
if prototypes.item[item.name] then
player.insert(item)
end
end
for i, item in pairs(class.respawn_inventory.replace) do
if item.remove and item.add and player.get_item_count(item.remove.name) >= item.remove.count then
if
item.remove
and item.add
and prototypes.item[item.remove.name]
and prototypes.item[item.add.name]
and player.get_item_count(item.remove.name) >= item.remove.count
then
player.remove_item(item.remove)
player.insert(item.add)
end
Expand Down Expand Up @@ -1459,6 +1473,9 @@ function switch_character(player_index, new_character)
if player.controller_type == defines.controllers.editor then
player.toggle_map_editor() --safety restores the character entity.
end
if player.controller_type == defines.controllers.god then
storage.god_inventories[player.name] = player.get_inventory(defines.inventory.god_main).get_contents() --Saves god controller's inventory in a table labeled with the player's user name to avoid multiplayer issues
end
if remote.interfaces["space-exploration"] then
remote.call("space-exploration", "remote_view_stop", { player = player }) --Attempt to safe exit SatNav mode
end
Expand Down Expand Up @@ -1522,74 +1539,42 @@ end
function switch_to_god(player_index, old_char)
wlog("Entered function switch_to_god(" .. player_index .. ")")

--~ local old_character = game.players[player_index].character
local old_character = old_char or game.players[player_index].character
game.players[player_index].set_controller({ type = defines.controllers.god })
if old_character then
game.players[player_index].associate_character(old_character)
end
if game.players[player_index].controller_type ~= defines.controllers.god then --Do nothing if already using god controller. Avoids issues with inventory reset.

-- Skip this if called with old_char, i.e. if this function is called in response
-- to changes by another mod. If another mod toggled editor/god mode, it's their
-- responsibility to announce the change.
if not old_char then
-- Notify other mods that a character has been exchanged
--~ announce("minime", {old = old_character})
announce("minime", {
old_character = old_character,
old_unit_number = old_character and old_character.unit_number,
player_index = player_index,
god_mode = true,
})
announce("jetpack", {
old_character = old_character,
old_character_unit_number = old_character and old_character.unit_number,
})
end
--~ local old_character = game.players[player_index].character
local old_character = old_char or game.players[player_index].character
game.players[player_index].set_controller({ type = defines.controllers.god })
if old_character then
game.players[player_index].associate_character(old_character)
end

refresh_avatar_gui(player_index)
end
-- Skip this if called with old_char, i.e. if this function is called in response
-- to changes by another mod. If another mod toggled editor/god mode, it's their
-- responsibility to announce the change.
if not old_char then
-- Notify other mods that a character has been exchanged
--~ announce("minime", {old = old_character})
announce("minime", {
old_character = old_character,
old_unit_number = old_character and old_character.unit_number,
player_index = player_index,
god_mode = true,
})
announce("jetpack", {
old_character = old_character,
old_character_unit_number = old_character and old_character.unit_number,
})
end

--Switches to the editor controller, then adds the connected character to the (end of the) association list.
-- Added optional argument old_char. Per default, the player's current character will be used, but
-- if this function is called from a remote function, this allows to pass on a different character.
--~ function switch_to_editor(player_index)
function switch_to_editor(player_index, old_char)
wlog("Entered function switch_to_editor(" .. player_index .. ", " .. tostring(old_char and old_char.name) .. ")")
wlog(
string.format("Controller of player %s: %s", player_index, controllers[game.players[player_index].controller_type])
)
if storage.god_inventories[game.players[player_index].name] then
for i, items in pairs(storage.god_inventories[game.players[player_index].name]) do
if prototypes.item[items.name] then
game.players[player_index].insert(items)
end
end
end

--~ local old_character = game.players[player_index].character
local old_character = old_char or game.players[player_index].character
game.players[player_index].set_controller({ type = defines.controllers.editor })
wlog(
string.format(
"Controller of player %s after change: %s",
player_index,
controllers[game.players[player_index].controller_type]
)
)
if old_character then
game.players[player_index].associate_character(old_character)
end
refresh_avatar_gui(player_index)

-- Skip this if called with old_char, i.e. if this function is called in response
-- to changes by another mod. If another mod toggled editor/god mode, it's their
-- responsibility to announce the change.
if not old_char then
-- Notify other mods that a character has been exchanged
announce("minime", {
old_character = old_character,
old_unit_number = old_character and old_character.unit_number,
player_index = player_index,
editor_mode = true,
})
announce("jetpack", {
old_character = old_character,
old_character_unit_number = old_character and old_character.unit_number,
})
end

refresh_avatar_gui(player_index)
end
6 changes: 2 additions & 4 deletions bobclasses/locale/en/bobclasses.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ bob-avatar-minimap=Opens a minimap of this character's location.
bob-avatar-seticon=Click here while holding an item to set it as a character's icon

bob-avatar-god-mode=God mode
bob-avatar-god-mode-tooltip=God mode is where you have free camera movement without controlling a character.\nWarning: God mode inventory is lost when switching to another mode.\nWarning: if your body is killed while you are in god mode, it won't respawn.
bob-avatar-god-mode-tooltip=God mode is where you have free camera movement without controlling a character.\nWarning: if your body is killed while you are in god mode, it won't respawn.
bob-avatar-editor-mode=Editor mode
bob-avatar-editor-mode-tooltip=In editor mode, you are disconnected from a character and have full scenario editor controls.\nWarning: if your body is killed while you are in editor mode, it won't respawn.
bob-avatar-editor-mode-tooltip=In editor mode, you are disconnected from a character and have full scenario editor controls.
bob-avatar-class-select=Select class
bob-avatar-class-select-tooltip=Opens the class select window.

Expand All @@ -135,13 +135,11 @@ bob-current-character=Current:

[mod-setting-name]
bobmods-classes-god-mode=Admin button "God mode"
bobmods-classes-editor-mode=Admin button "Editor mode"
bobmods-classes-class-select=Admin button "Class select"
bobmods-classes-class-select-user=User button "Class select"


[mod-setting-description]
bobmods-classes-god-mode=If enabled (requires being an admin if you're in multiplayer) then the button "God mode" will show on the Avatar switcher window
bobmods-classes-editor-mode=If enabled (requires being an admin if you're in multiplayer) then the button "Editor mode" will show on the Avatar switcher window
bobmods-classes-class-select=If enabled (requires being an admin if you're in multiplayer) then the button "Class select" will show on the Avatar switcher window
bobmods-classes-class-select-user=If enabled (all users) then the button "Class select" will show on the Avatar switcher window
6 changes: 0 additions & 6 deletions bobclasses/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ data:extend({
setting_type = "runtime-global",
default_value = false,
},
{
type = "bool-setting",
name = "bobmods-classes-editor-mode",
setting_type = "runtime-global",
default_value = false,
},
{
type = "bool-setting",
name = "bobmods-classes-class-select",
Expand Down