Arena properties can now be tweaked in-game, both via editor and via chat

This commit is contained in:
Zughy 2020-09-03 15:29:09 +00:00
parent b695c388e9
commit 97094fdc55
6 changed files with 159 additions and 39 deletions

View File

@ -66,7 +66,11 @@ If you don't want to rely on the hotbar, or you want both the editor and the com
##### 1.2.2.2 Enabling/Disabling teams
`arena_lib.toggle_teams_per_arena(sender, mod, arena_name, enable)` enables/disables teams per single arena. `enable` is an int, where 0 disables teams and 1 enables them.
##### 1.2.2.3 Spawners
##### 1.2.2.3 Arenas properties
Properties are explained down below, but essentially they allow you to create additional attributes specifically suited for what you have in mind (e.g. a score to reach to win the game).
`arena_lib.change_arena_properties(sender, mod, arena_name, property, new_value)` changes the specified arena property with `new_value`. Keep in mind you can't change a property type (a number must remain a number, a string a string etc), and strings need quotes surrounding them - so `false` is a boolean, but `"false"` is a string.
##### 1.2.2.4 Spawners
`arena_lib.set_spawner(sender, mod, arena_name, <teamID_or_name>, <param>, <ID>)` creates a spawner where the sender is standing, so be sure to stand where you want the spawn point to be.
* `teamID_or_name` can be both a string and a number. It must be specified if your arena uses teams
* `param` is a string, specifically "overwrite", "delete" or "deleteall". "deleteall" aside, the other ones need an ID after them. Also, if a team is specified with deleteall, it will only delete the spawners belonging to that team
@ -102,7 +106,7 @@ ChatCmdBuilder.new("NAMEOFYOURCOMMAND", function(cmd)
[etc.]
```
##### 1.2.2.4 Signs
##### 1.2.2.5 Signs
`arena_lib.set_sign(sender, <pos, remove>, <mod, arena_name>)` via chat uses `sender`, `mod` and `arena_name`, while the editor `pos` and `remove` (hence the weird subdivision). When used via chat, it takes the block the player is pointing at in a 5 blocks radius. If the block is a sign, it then creates (or remove if already set) the "arena sign".
#### 1.2.3 Enabling an arena

View File

@ -1,12 +1,13 @@
local S = minetest.get_translator("arena_lib")
local FS = minetest.formspec_escape
local function get_rename_formspec() end
local function get_properties_formspec() end
local function property_to_string() end
local settings_tools = {
"arena_lib:settings_rename",
--"arena_lib:settings_properties",
"",
"arena_lib:settings_properties",
"",
"",
"",
@ -15,6 +16,7 @@ local settings_tools = {
"arena_lib:editor_quit",
}
local sel_property_attr = {} --KEY: p_name; VALUE: {id = idx, name = property_name}
@ -38,14 +40,19 @@ minetest.register_tool("arena_lib:settings_rename", {
minetest.register_tool("arena_lib:settings_properties", {
description = S("Arena properties"),
inventory_image = "arenalib_tool_settings_editor.png",
inventory_image = "arenalib_tool_properties.png",
groups = {not_in_creative_inventory = 1, oddly_breakable_by_hand = "2"},
on_place = function() end,
on_drop = function() end,
on_use = function(itemstack, user, pointed_thing)
local p_name = user:get_player_name()
minetest.show_formspec(p_name, "arena_lib:settings_properties", get_properties_formspec(p_name))
local mod = user:get_meta():get_string("arena_lib_editor.mod")
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
local id, arena = arena_lib.get_arena_by_name(mod, arena_name)
minetest.show_formspec(p_name, "arena_lib:settings_properties", get_properties_formspec(p_name, mod, arena, 1))
end
})
@ -63,14 +70,45 @@ end
---------------FUNZIONI LOCALI----------------
----------------------------------------------
function get_properties_formspec(p_name)
function get_properties_formspec(p_name, mod, arena, sel_idx)
local mod_ref = arena_lib.mods[mod]
local properties = ""
local properties_by_idx = {}
local sel_property = ""
local sel_property_value = ""
local i = 1
-- ottengo una stringa con tutte le proprietà
for property, v in pairs(mod_ref.properties) do
properties = properties .. property .. " = " .. FS(property_to_string(arena[property])) .. ","
properties_by_idx[i] = property
i = i + 1
end
-- ottengo il nome della proprietà selezionata
if not sel_idx then
sel_property = properties_by_idx[1]
else
sel_property = properties_by_idx[sel_idx]
end
-- e assegno il valore
sel_property_attr[p_name] = {id = sel_idx, name = sel_property}
sel_property_value = FS(property_to_string(arena[sel_property]))
properties = properties:sub(1,-2)
local formspec = {
"size[6,4]"
"size[6.25,3.7]",
"hypertext[0,0;6.25,1;properties_title;<global halign=center>Arena properties]",
"textlist[0,0.5;6,2.5;arena_properties;" .. properties .. ";" .. sel_idx .. ";false]",
"field[0.3,3.3;4.7,1;sel_property_value;;" .. sel_property_value .. "]",
"button[4.72,2.983;1.5,1;property_overwrite;" .. S("Overwrite") .. "]",
"field_close_on_enter[sel_property_value;false]"
}
return table.concat(formspec, "")
end
@ -82,10 +120,73 @@ function get_rename_formspec(p_name)
"no_prepend[]",
"bgcolor[;neither]",
"field[0.2,0.25;4,1;rename;;]",
"button[3.8,-0.05;1.5,1;rename_confirm;Rename Arena]",
"button[3.8,-0.05;1.5,1;rename_confirm;" .. S("Rename Arena") .. "]",
"field_close_on_enter[rename;false]"
}
return table.concat(formspec, "")
end
function property_to_string(property)
if type(property) == "string" then
return "\"" .. property .. "\""
elseif type(property) == "table" then
return tostring(dump(property)):gsub("\n", "")
else
return tostring(property)
end
end
----------------------------------------------
---------------GESTIONE CAMPI-----------------
----------------------------------------------
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "arena_lib:settings_rename" and formname ~= "arena_lib:settings_properties" then return end
local p_name = player:get_player_name()
local mod = player:get_meta():get_string("arena_lib_editor.mod")
local arena_name = player:get_meta():get_string("arena_lib_editor.arena")
-- GUI per rinominare arena
if formname == "arena_lib:settings_rename" then
if fields.rename_confirm or fields.key_enter then
if arena_lib.rename_arena(p_name, mod, arena_name, fields.rename, true) then
player:get_meta():set_string("arena_lib_editor.arena", fields.rename)
minetest.close_formspec(p_name, formname)
end
end
-- GUI per modificare proprietà
else
local id, arena = arena_lib.get_arena_by_name(mod, arena_name)
-- se clicco sulla lista
if fields.arena_properties then
local expl = minetest.explode_textlist_event(fields.arena_properties)
if expl.type == "DCL" or expl.type == "CHG" then
minetest.show_formspec(p_name, "arena_lib:settings_properties", get_properties_formspec(p_name, mod, arena, expl.index))
end
-- se premo per sovrascrivere
elseif fields.property_overwrite or fields.key_enter then
arena_lib.change_arena_properties(p_name, mod, arena_name, sel_property_attr[p_name].name, fields.sel_property_value, true)
minetest.show_formspec(p_name, "arena_lib:settings_properties", get_properties_formspec(p_name, mod, arena, sel_property_attr[p_name].id))
end
end
end)

View File

@ -17,7 +17,7 @@ function arena_lib.show_waypoints(p_name, arena)
local caption = "#" .. ID
-- se ci sono team, lo specifico nel nome
if #arena.teams > 1 then
if arena.teams_enabled then
caption = caption .. ", " .. arena.teams[spawn.teamID].name
end

42
api.lua
View File

@ -355,6 +355,48 @@ end
function arena_lib.change_arena_properties(sender, mod, arena_name, property, new_value, in_editor)
local id, arena = arena_lib.get_arena_by_name(mod, arena_name)
if not in_editor then
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
end
-- se la proprietà non esiste
if arena[property] == nil then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", S("[!] Parameters don't seem right!")))
return end
local func, error_msg = loadstring("return (" .. new_value .. ")")
-- se non ritorna una sintassi corretta
if not func then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", "[!] " .. error_msg))
return end
setfenv(func, {})
local good, result = pcall(func)
-- se le operazioni della funzione causano errori
if not good then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", "[!] " .. result))
return end
-- se il tipo è diverso dal precedente
if type(arena[property]) ~= type(result) then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", S("[!] You can't change property type!")))
return end
arena[property] = result
update_storage(false, mod, id, arena)
minetest.chat_send_player(sender, S("Property @1 successfully overwritten", property))
end
function arena_lib.change_players_amount(sender, mod, arena_name, min_players, max_players, in_editor)
local id, arena = arena_lib.get_arena_by_name(mod, arena_name)

View File

@ -115,30 +115,3 @@ minetest.register_on_respawnplayer(function(player)
return true
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "arena_lib:settings_rename" and formname ~= "arena_lib:settings_editor" then return end
local p_name = player:get_player_name()
local mod = player:get_meta():get_string("arena_lib_editor.mod")
local arena_name = player:get_meta():get_string("arena_lib_editor.arena")
-- GUI per rinominare arena
if formname == "arena_lib:settings_rename" then
if fields.rename_confirm or fields.key_enter then
if arena_lib.rename_arena(p_name, mod, arena_name, fields.rename, true) then
player:get_meta():set_string("arena_lib_editor.arena", fields.rename)
minetest.close_formspec(p_name, formname)
end
end
-- GUI per modificare proprietà
else
end
end)

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B