Skip to content

Commit

Permalink
Merge branch 'streak' of github.com:farooqkz/capturetheflag into streak
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqkz committed May 29, 2024
2 parents 7521b1e + 049266a commit f77d978
Show file tree
Hide file tree
Showing 21 changed files with 387 additions and 290 deletions.
5 changes: 4 additions & 1 deletion docs/ctf-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ ctf_settings.register("my_setting", {

---
# mods/ctf/
TODO
TODO, below is a collection of quick notes for later

## ctf_teams
* https://modern.ircdocs.horse/formatting.html#colors-16-98

---
# mods/mtg/
Expand Down
6 changes: 3 additions & 3 deletions mods/apis/ctf_gui/dev.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local unset_function = "[f]\nreturn "
local unset_function = "return "

function ctf_gui.show_formspec_dev(player, formname, formspec, formcontext)
local filepath = minetest.get_worldpath().."/ctf_gui/"
Expand All @@ -20,8 +20,8 @@ function ctf_gui.show_formspec_dev(player, formname, formspec, formcontext)
local function interval()
if type(formspec) == "function" then
ctf_gui.show_formspec(player, formname, formspec(formcontext))
elseif formspec:sub(1, 3) == "[f]" then
local result, form = pcall((loadstring(formspec:sub(4)) or function() return function() end end)(), formcontext)
elseif formspec:match("^%s*return") then
local result, form = pcall((loadstring(formspec) or function() return function() end end)(), formcontext)

ctf_gui.show_formspec(player, formname,
result and form or "size[10,10]hypertext[0,0;10,10;err;"..minetest.formspec_escape(form or "").."]"
Expand Down
2 changes: 1 addition & 1 deletion mods/apis/ctf_settings/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function ctf_settings.get(player, setting)
local value = player:get_meta():get_string("ctf_settings:"..setting)
local info = ctf_settings.settings[setting]

return value == "" and info.default or value
return value == "" and (info and info.default) or value
end

-- This Function MIT by Rubenwardy
Expand Down
4 changes: 2 additions & 2 deletions mods/ctf/ctf_combat/ctf_ranged/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ local function process_ray(ray, user, look_dir, def)
end
end
elseif hitpoint.type == "object" then
hitpoint.ref:punch(user, 1, {
full_punch_interval = 1,
hitpoint.ref:punch(user, def.fire_interval or 0.1, {
full_punch_interval = def.fire_interval or 0.1,
damage_groups = {ranged = 1, [def.type] = 1, fleshy = def.damage}
}, look_dir)
end
Expand Down
50 changes: 26 additions & 24 deletions mods/ctf/ctf_map/ctf_traps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,31 @@ minetest.register_node("ctf_map:spike", {
})

for _, team in ipairs(ctf_teams.teamlist) do
local spikecolor = ctf_teams.team[team].color

minetest.register_node("ctf_map:spike_"..team, {
description = HumanReadable(team).." Team Spike",
drawtype = "plantlike",
tiles = {"ctf_map_spike.png^[colorize:"..spikecolor..":150"},
inventory_image = "ctf_map_spike.png^[colorize:"..spikecolor..":150",
use_texture_alpha = "clip",
paramtype = "light",
paramtype2 = "meshoptions",
sunlight_propagates = true,
walkable = false,
damage_per_second = 7,
groups = {cracky=1, level=2},
drop = "ctf_map:spike",
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
},
on_place = function(itemstack, placer, pointed_thing)
return minetest.item_place(itemstack, placer, pointed_thing, 34)
end
})
if not ctf_teams.team[team].not_playing then
local spikecolor = ctf_teams.team[team].color

minetest.register_node("ctf_map:spike_"..team, {
description = HumanReadable(team).." Team Spike",
drawtype = "plantlike",
tiles = {"ctf_map_spike.png^[colorize:"..spikecolor..":150"},
inventory_image = "ctf_map_spike.png^[colorize:"..spikecolor..":150",
use_texture_alpha = "clip",
paramtype = "light",
paramtype2 = "meshoptions",
sunlight_propagates = true,
walkable = false,
damage_per_second = 7,
groups = {cracky=1, level=2},
drop = "ctf_map:spike",
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
},
on_place = function(itemstack, placer, pointed_thing)
return minetest.item_place(itemstack, placer, pointed_thing, 34)
end
})
end
end

minetest.register_on_player_hpchange(function(player, hp_change, reason)
Expand Down Expand Up @@ -129,7 +131,7 @@ local function damage_cobble_dig(pos, node, digger)
local placerobj = minetest.get_player_by_name(placer_name)

if placerobj then
digger:punch(placerobj, 10, {
digger:punch(placerobj, 1, {
damage_groups = {
fleshy = 7,
damage_cobble = 1,
Expand Down
6 changes: 4 additions & 2 deletions mods/ctf/ctf_map/map_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,10 @@ local function prepare_nodes(pos1, pos2, data, team_chest_items, blacklisted_nod
end

for _, team in ipairs(ctf_teams.teamlist) do
local node = "ctf_teams:chest_" .. team
nodes[minetest.get_content_id(node)] = minetest.registered_nodes[node]
if not ctf_teams.team[team].not_playing then
local node = "ctf_teams:chest_" .. team
nodes[minetest.get_content_id(node)] = minetest.registered_nodes[node]
end
end

for i, v in ipairs(data) do
Expand Down
15 changes: 9 additions & 6 deletions mods/ctf/ctf_modebase/build_timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ local function timer_func(time_left)
end

local pteam = ctf_teams.get(player)
if pteam and not ctf_core.pos_inside(player:get_pos(), ctf_teams.get_team_territory(pteam)) then
local tpos1, tpos2 = ctf_teams.get_team_territory(pteam)
if pteam and tpos1 and not ctf_core.pos_inside(player:get_pos(), tpos1, tpos2) then
hud_events.new(player, {
quick = true,
text = "You can't cross the barrier until build time is over!",
Expand All @@ -47,6 +48,13 @@ local function timer_func(time_left)
timer = minetest.after(1, timer_func, time_left - 1)
end

function ctf_modebase.build_timer.start(build_time)
local time = build_time or ctf_modebase:get_current_mode().build_timer or DEFAULT_BUILD_TIME

if time > 0 then
timer = timer_func(time)
end
end

function ctf_modebase.build_timer.finish()
if timer == nil then return end
Expand Down Expand Up @@ -76,10 +84,6 @@ function ctf_modebase.build_timer.finish()
end
end

ctf_api.register_on_new_match(function()
timer = minetest.after(1, timer_func, ctf_modebase:get_current_mode().build_timer or DEFAULT_BUILD_TIME)
end)

ctf_api.register_on_match_end(function()
if timer == nil then return end
timer:cancel()
Expand Down Expand Up @@ -119,4 +123,3 @@ minetest.register_chatcommand("ctf_start", {
return true, "Build time ended"
end,
})

76 changes: 48 additions & 28 deletions mods/ctf/ctf_modebase/features.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ctf_core.testing = {
end
}

local hud = mhud.init()
local mapload_huds = mhud.init()
local LOADING_SCREEN_TARGET_TIME = 7
local loading_screen_time

Expand Down Expand Up @@ -121,25 +121,27 @@ local old_announce = ctf_modebase.map_chosen
function ctf_modebase.map_chosen(map, ...)
set_playertags_state(PLAYERTAGS_OFF)

mapload_huds:clear_all()

for _, p in pairs(minetest.get_connected_players()) do
if ctf_teams.get(p) then
hud:add(p, "loading_screen", {
mapload_huds:add(p, "loading_screen", {
hud_elem_type = "image",
position = {x = 0.5, y = 0.5},
image_scale = -100,
z_index = 1000,
texture = "[combine:1x1^[invert:rgba^[opacity:1^[colorize:#141523:255"
})

hud:add(p, "map_image", {
mapload_huds:add(p, "map_image", {
hud_elem_type = "image",
position = {x = 0.5, y = 0.5},
image_scale = -100,
z_index = 1001,
texture = map.dirname.."_screenshot.png^[opacity:30",
})

hud:add(p, "loading_text", {
mapload_huds:add(p, "loading_text", {
hud_elem_type = "text",
position = {x = 0.5, y = 0.5},
alignment = {x = "center", y = "up"},
Expand All @@ -148,7 +150,7 @@ function ctf_modebase.map_chosen(map, ...)
color = 0x7ec5ff,
z_index = 1002,
})
hud:add(p, {
mapload_huds:add(p, {
hud_elem_type = "text",
position = {x = 0.5, y = 0.75},
alignment = {x = "center", y = "center"},
Expand Down Expand Up @@ -252,14 +254,22 @@ local function tp_player_near_flag(player)
local tname = ctf_teams.get(player)
if not tname then return end

local pos = vector.offset(ctf_map.current_map.teams[tname].flag_pos,
math.random(-1, 1),
0.5,
math.random(-1, 1)
)
local rotation_y = vector.dir_to_rotation(
vector.direction(pos, ctf_map.current_map.teams[tname].look_pos or ctf_map.current_map.flag_center)
).y
local rotation_y
local pos

if ctf_map.current_map.teams[tname] then
pos = vector.offset(ctf_map.current_map.teams[tname].flag_pos,
math.random(-1, 1),
0.5,
math.random(-1, 1)
)
rotation_y = vector.dir_to_rotation(
vector.direction(pos, ctf_map.current_map.teams[tname].look_pos or ctf_map.current_map.flag_center)
).y
else
pos = vector.add(ctf_map.current_map.pos1, vector.divide(ctf_map.current_map.size, 2))
rotation_y = player:get_look_horizontal()
end

local function apply()
player:set_pos(pos)
Expand Down Expand Up @@ -424,6 +434,8 @@ local delete_queue = {}
local team_switch_after_capture = false

return {
tp_player_near_flag = tp_player_near_flag,

on_new_match = function()
team_list = {}
for tname in pairs(ctf_map.current_map.teams) do
Expand Down Expand Up @@ -467,8 +479,10 @@ return {
local total_time = (minetest.get_us_time() - loading_screen_time) / 1e6

minetest.after(math.max(0, LOADING_SCREEN_TARGET_TIME - total_time), function()
hud:clear_all()
mapload_huds:clear_all()
set_playertags_state(PLAYERTAGS_ON)

ctf_modebase.build_timer.start()
end)
end
end,
Expand All @@ -480,6 +494,8 @@ return {
delete_queue = {ctf_map.current_map.pos1, ctf_map.current_map.pos2}
end
end,
-- If you set this in a mode def it will replace the call to ctf_teams.allocate_teams() in match.lua
-- allocate_teams = function()
team_allocator = function(player)
player = PlayerName(player)

Expand Down Expand Up @@ -671,15 +687,7 @@ return {

celebrate_team(pteam)

local text = " has captured the flag"
if many_teams then
text = " has captured the flag of team(s) " .. HumanReadable(teamnames)
minetest.chat_send_all(
minetest.colorize(tcolor, pname) ..
minetest.colorize(FLAG_MESSAGE_COLOR, text)
)
end
ctf_modebase.announce(string.format("Player %s (team %s)%s", pname, pteam, text))


ctf_modebase.flag_huds.untrack_capturer(pname)

Expand All @@ -690,7 +698,19 @@ return {
score = math.max(75, math.min(500, score))
capture_reward = capture_reward + score
end

local text = " has captured the flag"
if many_teams then
text = string.format(
" has captured the flag of team(s) %s and got %d points",
HumanReadable(teamnames),
capture_reward
)
minetest.chat_send_all(
minetest.colorize(tcolor, pname) ..
minetest.colorize(FLAG_MESSAGE_COLOR, text)
)
end
ctf_modebase.announce(string.format("Player %s (team %s)%s and got %d points", pname, pteam, text, capture_reward))
local team_score = team_scores[pteam].score
for teammate in pairs(ctf_teams.online_players[pteam].players) do
if teammate ~= pname then
Expand All @@ -705,12 +725,12 @@ return {
teams_left = teams_left - #teamnames

if teams_left <= 1 then
local capture_text = "Player %s captured"
local capture_text = "Player %s captured and got %d points"
if many_teams then
capture_text = "Player %s captured the last flag"
capture_text = "Player %s captured the last flag and got %d points"
end

ctf_modebase.summary.set_winner(string.format(capture_text, minetest.colorize(tcolor, pname)))
ctf_modebase.summary.set_winner(string.format(capture_text, minetest.colorize(tcolor, pname), capture_reward))

local win_text = HumanReadable(pteam) .. " Team Wins!"

Expand All @@ -730,7 +750,7 @@ return {

for lost_player in pairs(ctf_teams.online_players[lost_team].players) do
team_switch_after_capture = true
ctf_teams.allocate_player(lost_player)
ctf_teams.allocate_player(lost_player)
team_switch_after_capture = false
end
end
Expand Down
19 changes: 11 additions & 8 deletions mods/ctf/ctf_modebase/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ ctf_modebase = {
-- Table containing all registered modes and their definitions
modes = {}, ---@type table

-- Same as ctf_modebase.modes but in list form
-- Same as ctf_modebase.modes but in list form.
-- Exception: Disabled modes that show up in ctf_modebase.modes won't show up in the modelist
modelist = {}, ---@type list

-- Name of the mode currently being played. On server start this will be false
Expand Down Expand Up @@ -108,13 +109,15 @@ minetest.register_on_mods_loaded(function()
end

for _, name in pairs(ctf_modebase.modelist) do
ctf_settings.register("ctf_modebase:default_vote_"..name, {
type = "list",
description = "Match count vote for the mode '"..HumanReadable(name).."'",
list = {HumanReadable(name).." - Ask", "0", "1", "2", "3", "4", "5"},
_list_map = {"ask", 0, 1, 2, 3, 4, 5},
default = "1", -- "Ask"
})
if not ctf_modebase.modes[name].rounds then
ctf_settings.register("ctf_modebase:default_vote_"..name, {
type = "list",
description = "Match count vote for the mode '"..HumanReadable(name).."'",
list = {HumanReadable(name).." - Ask", "0", "1", "2", "3", "4", "5"},
_list_map = {"ask", 0, 1, 2, 3, 4, 5},
default = "1", -- "Ask"
})
end
end
end)

Expand Down
5 changes: 5 additions & 0 deletions mods/ctf/ctf_modebase/map_catalog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function ctf_modebase.map_catalog.select_map(filter)
end

local selected = maps[math.random(1, #maps)]

if not selected then
selected = ctf_modebase.map_catalog.map_dirnames["plains"]
end

ctf_modebase.map_catalog.current_map = maps_pool[selected]

if map_repeat_interval > 0 then
Expand Down
8 changes: 7 additions & 1 deletion mods/ctf/ctf_modebase/match.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ function ctf_modebase.start_match_after_vote()
ctf_modebase.on_new_match()

ctf_modebase.in_game = true
ctf_teams.allocate_teams(ctf_map.current_map.teams)
local team_alloc = ctf_modebase:get_current_mode().allocate_teams

if team_alloc then
team_alloc(ctf_map.current_map.teams)
else
ctf_teams.allocate_teams(ctf_map.current_map.teams)
end

ctf_modebase.current_mode_matches_played = ctf_modebase.current_mode_matches_played + 1

Expand Down
Loading

0 comments on commit f77d978

Please sign in to comment.