Added diplomacy

This commit is contained in:
Billy S 2019-04-28 19:32:43 -04:00
parent 8b0bdb8eb9
commit 3e16a8fb8c
4 changed files with 165 additions and 24 deletions

View File

@ -31,21 +31,25 @@ function kingdoms.set_gui(pname, tab)
fs = fs:sub(1, -2) .. "]"
minetest.show_formspec(pname, "kingdoms:gui_news", fs)
elseif tab == "apls" then
-- Check if a gui entry exists
-- Check for a gui entry
local doadd = false
if kingdoms_gui[pname].apls == nil then
kingdoms_gui[pname].apls = {}
kingdoms_gui[pname].apls.apls = {}
kingdoms_gui[pname].apls.index = 0
doadd = true
end
-- Get info
local idx = kingdoms_gui[pname].apls.index
-- Generate fs
-- Generate textlist
fs = fs .. "textlist[0,1;7.8,7;aplslist;Kingdom Applicants:,"
for n,k in pairs(kingdoms.pending) do
if k == kingdom then
fs = fs .. n .. ","
-- Add applicant to table
table.insert(kingdoms_gui[pname].apls.apls, n)
if doadd then
table.insert(kingdoms_gui[pname].apls.apls, n)
end
end
end
fs = fs:sub(1, -2) .. ";" .. tostring(kingdoms_gui[pname].apls.index) .. ";false]"
@ -54,13 +58,74 @@ function kingdoms.set_gui(pname, tab)
minetest.log(minetest.serialize(kingdoms_gui[pname].apls.apls))
if idx > 1 and kingdoms.player_has_priv(pname, "recruiter") then -- Idx 1 == "Kingdom Applications:"
local victim = kingdoms_gui[pname].apls.apls[idx - 1]
fs = fs .. "button[0,8.3;2,1;acpt_" .. victim .. ";Accept]"
fs = fs .. "button[2,8.3;2,1;rejc_" .. victim .. ";Reject]"
fs = fs .. "button[0,8.3;2,1;acpt;Accept]"
fs = fs .. "button[2,8.3;2,1;rejc;Reject]"
end
-- Present
minetest.show_formspec(pname, "kingdoms:gui_apls", fs)
elseif tab == "diplo" then
fs = fs .. "label[0,1;Diplomacy]"
-- Check for a gui entry
local doadd = false
if kingdoms_gui[pname].diplo == nil then
kingdoms_gui[pname].diplo = {}
kingdoms_gui[pname].diplo.kds = {}
kingdoms_gui[pname].diplo.index = 0
doadd = true
end
-- Generate textlist
fs = fs .. "textlist[0,1;7.8,7;klist;Kingdoms:,"
local pkingdom = kingdoms.members[pname].kingdom
for n,k in pairs(kingdoms.kingdoms) do
if n ~= pkingdom then
local r = kingdoms.get_relation(pkingdom, n)
if r.id == kingdoms.relations.war then
fs = fs .. "#FF0000" .. n .. ","
elseif r.id == kingdoms.relations.peace then
if r.pending then
fs = fs .. "#FFA500" .. n .. ","
else
fs = fs .. "#FFFFFF" .. n .. ","
end
elseif r.id == kingdoms.relations.alliance then
if r.pending then
fs = fs .. "#90EE90" .. n .. ","
else
fs = fs .. "#00FF00" .. n .. ","
end
end
if doadd then
table.insert(kingdoms_gui[pname].diplo.kds, n)
end
end
end
fs = fs:sub(1, -2) .. ";" .. tostring(kingdoms_gui[pname].diplo.index) .. ";false]"
-- Add elements for selected kingdom
if kingdoms_gui[pname].diplo.index > 1 and kingdoms.player_has_priv(pname, "diplomat") then
local kname = kingdoms_gui[pname].diplo.kds[kingdoms_gui[pname].diplo.index - 1]
local r = kingdoms.get_relation(pkingdom, kname)
if r.id == kingdoms.relations.war then
fs = fs .. "button[0,8.3;3,1;reqp;Request Peace]"
elseif r.id == kingdoms.relations.peace then
if r.pending == pkingdom then
fs = fs .. "button[0,8.3;3,1;acpp;Accept Peace Request]"
fs = fs .. "button[3,8.3;3,1;dclp;Decline Peace Request]"
elseif r.pending == kname then
fs = fs .. "button[0,8.3;3,1;cnclpr;Cancel Peace Request]"
else
fs = fs .. "button[0,8.3;3,1;decw;Declare War]"
fs = fs .. "button[3,8.3;3,1;reqa;Request Alliance]"
end
elseif r.id == kingdoms.relations.alliance then
if r.pending == pkingdom then
fs = fs .. "button[0,8.3;3,1;acpar;Accept Alliance Request]"
fs = fs .. "button[3,8.3;3,1;dclar;Decline Alliance Request]"
elseif r.pending == kname then
fs = fs .. "button[0,8.3;3,1;cnclar;Cancel Alliance Request]"
else
fs = fs .. "button[0,8.3;3,1;cncla;Cancel Alliance]"
end
end
end
-- Present
minetest.show_formspec(pname, "kingdoms:gui_diplo", fs)
end
@ -81,6 +146,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
kingdoms.set_gui(pname, "diplo")
return
end
-- Get kingdom
local pkingdom = kingdoms.members[pname].kingdom
-- Check if selection changed / button pushed
if formname == "kingdoms:gui_apls" then
-- If a player is not selected, selected = nil
@ -95,20 +162,92 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
kingdoms_gui[pname].apls.index = e.index
kingdoms.set_gui(pname, "apls")
end
-- Accept button was pushed
elseif selected and fields["acpt_" .. selected] then
return
end
-- Buttons were pushed
if selected == nil then return end
if fields["acpt"] then -- Accepted
kingdoms_gui[pname].apls = nil
kingdoms.add_player_to_kingdom(kingdoms.members[pname].kingdom, selected)
kingdoms.pending[selected] = nil
kingdoms.helpers.save()
kingdoms.set_gui(pname, "apls")
-- Reject button was pushed
elseif selected and fields["rejc_" .. selected] then
elseif fields["rejc"] then -- Rejected
kingdoms_gui[pname].apls = nil
kingdoms.pending[selected] = nil
kingdoms.helpers.save()
kingdoms.set_gui(pname, "apls")
end
elseif formname == "kingdoms:gui_diplo" then
-- If a kingdom is not selected, selected = nil
local selected
if kingdoms_gui[pname].diplo.index > 1 then
selected = kingdoms_gui[pname].diplo.kds[kingdoms_gui[pname].diplo.index - 1]
end
-- Different kingdom was selected
if fields["klist"] ~= nil then
local e = minetest.explode_textlist_event(fields["klist"])
if e.type == "CHG" then
kingdoms_gui[pname].diplo.index = e.index
kingdoms.set_gui(pname, "diplo")
end
return
end
-- Buttons were pushed
if selected == nil then return end
if fields["reqp"] then -- Peace requested
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.peace,
pending = selected
})
kingdoms.set_gui(pname, "diplo")
elseif fields["acpp"] then -- Peace accepted
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.peace
})
kingdoms.set_gui(pname, "diplo")
elseif fields["dclp"] then -- Peace denied
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.war
})
kingdoms.set_gui(pname, "diplo")
elseif fields["cnclpr"] then -- Peace canceled
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.war
})
kingdoms.set_gui(pname, "diplo")
elseif fields["decw"] then -- Declare war
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.war
})
kingdoms.set_gui(pname, "diplo")
elseif fields["reqa"] then -- Request alliance
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.alliance,
pending = selected
})
kingdoms.set_gui(pname, "diplo")
elseif fields["acpar"] then -- Accept alliance request
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.alliance
})
kingdoms.set_gui(pname, "diplo")
elseif fields["dclar"] then -- Decline alliance request
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.peace
})
kingdoms.set_gui(pname, "diplo")
elseif fields["cnclar"] then -- Cancel alliance request
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.peace
})
kingdoms.set_gui(pname, "diplo")
elseif fields["cncla"] then -- Cancel alliance
kingdoms.set_relation(pkingdom, selected, {
id = kingdoms.relations.peace
})
kingdoms.set_gui(pname, "diplo")
end
end
end)

View File

@ -47,6 +47,13 @@ kingdoms.colors = {
Brown = "#8B4513"
}
-- Define relations
kingdoms.relations = {
war = 1,
peace = 2,
alliance = 3,
}
-- Load news
local nStr = storage:get_string("news")
if nStr == "" then

View File

@ -1,10 +1,3 @@
-- Constants
local kingdom_relations = {
war = 1,
peace = 2,
alliance = 3
}
-- Get info about members
function kingdoms.player_in_any_kingdoms(name)
if kingdoms.members[name] ~= nil then
@ -243,13 +236,9 @@ function kingdoms.set_relation(kingdom1, kingdom2, relation)
if kingdoms.kingdoms[kingdom2] == nil then
return false, "Kingdom " .. kingdom2 .. " does not exist"
end
-- Check if relation is valid
if kingdom_relations[relation] == nil then
return false, "Invalid relation " .. relation
end
-- Set relation
kingdoms.kingdoms[kingdom1].relations[kingdom2] = kingdom_relations[relation]
kingdoms.kingdoms[kingdom2].relations[kingdom1] = kingdom_relations[relation]
kingdoms.kingdoms[kingdom1].relations[kingdom2] = relation
kingdoms.kingdoms[kingdom2].relations[kingdom1] = relation
-- Save
kingdoms.helpers.save()
return true, ""
@ -265,7 +254,7 @@ function kingdoms.get_relation(kingdom1, kingdom2)
return false, "Kingdom " .. kingdom2 .. " does not exist"
end
-- Get relation
return true, kingdoms.kingdoms[kingdom1].relations[kingdom2] or kingdom_relations.peace
return kingdoms.kingdoms[kingdom1].relations[kingdom2] or {id = kingdoms.relations.peace}
end
function kingdoms.toggle_restricted(name)

View File

@ -142,6 +142,12 @@ for c,v in pairs(kingdoms.colors) do
minetest.chat_send_player(pname, "This marker already belongs to your kingdom")
return
end
-- Check if puncher is at peace with the kingdom
if kingdoms.get_relation(marker.kingdom, member.kingdom).id ~= kingdoms.relations.war then
minetest.node_punch(pos, node, puncher, pointed_thing)
minetest.chat_send_player(pname, "You are not at war with this kingdom")
return
end
-- Check if marker is already under attack
local meta = minetest.get_meta(pos)
local attackers = meta:get_string("attackers")