Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add classes #389

Merged
merged 15 commits into from
Mar 14, 2020
Merged
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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@
tags
*.vim
debug.txt

## Files related to minetest development cycle
*.patch
3 changes: 2 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ globals = {
"crafting", "vector", "table", "minetest", "worldedit", "ctf", "ctf_flag",
"ctf_colors", "hudkit", "default", "treasurer", "ChatCmdBuilder", "ctf_map",
"ctf_match", "ctf_stats", "ctf_treasure", "ctf_playertag", "chatplus", "irc",
"armor", "vote", "give_initial_stuff", "hud_score", "physics", "tsm_chests"
"armor", "vote", "give_initial_stuff", "hud_score", "physics", "tsm_chests",
"armor", "shooter"
}

read_globals = {
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Created by [rubenwardy](https://rubenwardy.com/).
Code: LGPLv2.1+
Textures: CC-BY-SA 3.0

### Textures

* ctf_classes_skin_* found and edited by GreenDimond/GreenXenith

### Mods

Check out [mods/](mods/) to see all the installed mods and their respective licenses.
Expand Down
18 changes: 18 additions & 0 deletions docs/accurate_statbar.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
diff --git a/builtin/game/statbars.lua b/builtin/game/statbars.lua
index 46c947b6..f4372843 100644
--- builtin/game/statbars.lua
+++ builtin/game/statbars.lua
@@ -24,12 +24,14 @@ local breath_bar_definition = {
local hud_ids = {}

local function scaleToDefault(player, field)
+ return player["get_" .. field](player)
- -- Scale "hp" or "breath" to the default dimensions
- local current = player["get_" .. field](player)
- local nominal = core["PLAYER_MAX_".. field:upper() .. "_DEFAULT"]
- local max_display = math.max(nominal,
- math.max(player:get_properties()[field .. "_max"], current))
- return current / max_display * nominal
end

local function update_builtin_statbars(player)
4 changes: 2 additions & 2 deletions minetest.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ enable_pvp = true
mg_name = singlenode
vote.kick_vote = false
barrier = 106
regen_interval = 6
regen_amount = 1
hpregen.interval = 6
hpregen.amount = 1
random_messages_interval = 60
sprint_stamina = 10
enable_lavacooling = false
Expand Down
2 changes: 1 addition & 1 deletion mods/ctf/ctf_alloc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ function ctf_alloc.set_all()
give_initial_stuff(player)
end

player:set_hp(20)
player:set_hp(player:get_properties().hp_max)
end
end
2 changes: 1 addition & 1 deletion mods/ctf/ctf_bandages/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
local healing_limit = 15

minetest.register_craftitem("ctf_bandages:bandage", {
description = "Bandage, heals teammates for 3-4 HP until HP is equal to "..healing_limit,
description = "Bandage\n\nHeals teammates for 3-4 HP until HP is equal to "..healing_limit,
inventory_image = "ctf_bandages_bandage.png",
on_use = function(itemstack, player, pointed_thing)
if pointed_thing.type ~= "object" then
Expand Down
139 changes: 139 additions & 0 deletions mods/ctf/ctf_classes/api.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
function ctf_classes.register(cname, def)
assert(not ctf_classes.__classes[cname])
def.name = cname
ctf_classes.__classes[cname] = def
table.insert(ctf_classes.__classes_ordered, def)

def.pros = def.pros or {}
def.cons = def.cons or {}

def.properties = def.properties or {}
if def.properties.can_capture == nil then
def.properties.can_capture = true
end

def.properties.initial_stuff = def.properties.initial_stuff or {}

if not def.properties.item_blacklist then
def.properties.item_blacklist = {}
for i=1, #def.properties.initial_stuff do
table.insert(def.properties.item_blacklist, def.properties.initial_stuff[i])
end
end

if def.properties.additional_item_blacklist then
for i=1, #def.properties.additional_item_blacklist do
table.insert(def.properties.item_blacklist, def.properties.additional_item_blacklist[i])
end
end

def.properties.speed = def.properties.speed or 1
def.properties.max_hp = def.properties.max_hp or 20
end

local registered_on_changed = {}
function ctf_classes.register_on_changed(func)
table.insert(registered_on_changed, func)
end

function ctf_classes.set_skin(player, color, class)
player:set_properties({
textures = {"ctf_classes_skin_" .. class.name .. "_" .. (color or "blue") .. ".png"}
})
end

function ctf_classes.get(player)
if type(player) == "string" then
player = minetest.get_player_by_name(player)
end

local cname = player:get_meta():get("ctf_classes:class") or ctf_classes.default_class
return ctf_classes.__classes[cname]
end

function ctf_classes.set(player, new_name)
assert(type(new_name) == "string")
local new = ctf_classes.__classes[new_name]
assert(new)

local meta = player:get_meta()
local old_name = meta:get("ctf_classes:class")

meta:set_string("ctf_classes:class", new_name)
ctf_classes.update(player)

if old_name == nil or old_name ~= new_name then
local old = old_name and ctf_classes.__classes[old_name]
for i=1, #registered_on_changed do
registered_on_changed[i](player, old, new)
end
end
end

local function set_max_hp(player, max_hp)
local cur_hp = player:get_hp()
local old_max = player:get_properties().hp_max
local new_hp = cur_hp + max_hp - old_max
player:set_properties({
hp_max = max_hp
})

if new_hp > max_hp then
minetest.log("error", string.format("New hp %d is larger than new max %d, old max is %d", new_hp, max_hp, old_max))
new_hp = max_hp
end

if cur_hp > max_hp then
player:set_hp(max_hp)
elseif new_hp > cur_hp then
player:set_hp(new_hp)
end
end

function ctf_classes.update(player)
local name = player:get_player_name()

local class = ctf_classes.get(player)
local color = ctf_colors.get_color(ctf.player(name)).text

set_max_hp(player, class.properties.max_hp)
ctf_classes.set_skin(player, color, class)

local speed = class.properties.speed
if ctf_flag.has_flag(name) and speed > 0.9 then
speed = 0.9
end

physics.set(player:get_player_name(), "ctf_classes:speed", {
speed = speed,
})
end

local function sqdist(a, b)
ClobberXD marked this conversation as resolved.
Show resolved Hide resolved
local x = a.x - b.x
local y = a.y - b.y
local z = a.z - b.z
return x*x + y*y + z*z
end

local function get_flag_pos(player)
local tplayer = ctf.player(player:get_player_name())
if not tplayer or not tplayer.team then
return nil
end

local team = ctf.team(tplayer.team)
if team and team.flags[1] then
return vector.new(team.flags[1])
end
return nil
end

function ctf_classes.can_change(player)
local flag_pos = get_flag_pos(player)
if not flag_pos then
return false
end

return sqdist(player:get_pos(), flag_pos) < 25
end
97 changes: 97 additions & 0 deletions mods/ctf/ctf_classes/classes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
ctf_classes.default_class = "knight"

ctf_classes.register("knight", {
description = "Knight",
pros = { "+50% Health Points" },
cons = { "-10% speed" },
color = "#ccc",
properties = {
max_hp = 30,
speed = 0.90,

initial_stuff = {
"default:sword_steel",
},

allowed_guns = {
"shooter:pistol",
"shooter:smg",
"shooter:shotgun",
},
},
})

ctf_classes.register("shooter", {
description = "Sharp Shooter",
pros = { "+50% range", "+20% faster shooting" },
cons = {},
color = "#c60",
properties = {
allow_grapples = true,

initial_stuff = {
"shooter:rifle",
"shooter:grapple_gun_loaded",
},

additional_item_blacklist = {
"shooter:grapple_gun",
"shooter:grapple_hook",
},

allowed_guns = {
"shooter:pistol",
"shooter:rifle",
"shooter:smg",
"shooter:shotgun",
},

shooter_multipliers = {
range = 1.5,
full_punch_interval = 0.8,
},
},
})

ctf_classes.register("medic", {
description = "Medic",
pros = { "x2 regen for nearby friendlies" },
cons = {},
color = "#0af",
properties = {
nearby_hpregen = true,

initial_stuff = {
"ctf_bandages:bandage 20",
},

allowed_guns = {
"shooter:pistol",
"shooter:smg",
"shooter:shotgun",
},
},
})

ctf_classes.register("rocketeer", {
description = "Rocketeer",
pros = { "Can craft rockets" },
cons = {},
color = "#fa0",
properties = {
initial_stuff = {
"shooter:rocket_gun_loaded",
"shooter:rocket 4",
},

additional_item_blacklist = {
"shooter:rocket_gun",
},

allowed_guns = {
"shooter:pistol",
"shooter:smg",
"shooter:shotgun",
},
},
})
31 changes: 31 additions & 0 deletions mods/ctf/ctf_classes/flags.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
ctf_flag.register_on_pick_up(function(name)
ctf_classes.update(minetest.get_player_by_name(name))
end)

ctf_flag.register_on_drop(function(name)
ctf_classes.update(minetest.get_player_by_name(name))
end)

local old_func = ctf_flag.on_punch
local function on_punch(pos, node, player, ...)
local class = ctf_classes.get(player)
if not class.properties.can_capture then
local pname = player:get_player_name()
local flag = ctf_flag.get(pos)
local team = ctf.player(pname).team
if flag and flag.team and team and team ~= flag.team then
minetest.chat_send_player(pname,
"You need to change classes to capture the flag!")
return
end
end

return old_func(pos, node, player, ...)
end

local function show(_, _, player)
ctf_classes.show_gui(player:get_player_name(), player)
end

ctf_flag.on_rightclick = show
ctf_flag.on_punch = on_punch
Loading