add magic, fix stamina bug

pull/2/head
Brandon 2014-05-11 20:49:35 -05:00
parent b8fecda6a3
commit 492845fab8
5 changed files with 131 additions and 2 deletions

View File

@ -119,6 +119,14 @@ minetest.register_on_respawnplayer(function (player)
player_lastpos[name] = player:getpos()
end)
minetest.register_on_joinplayer(function (player)
local name = player:get_player_name()
if player_stamina[name] == nil then
player_stamina[name] = 20
player_lastpos[name] = player:getpos()
end
end)
minetest.register_on_shutdown(function()
default.serialize_to_file(stamina_file,player_stamina)
end)

View File

@ -0,0 +1,85 @@
--[[ SPELL DEF
{
-- Parameters
id = Spell Identifier, used in /cast command (e.g /cast <id> <target>)
type = Cast, Wand
level = minimum magic level to use spell
-- Cast Specifics
on_cast (name,target) = function to call when using /cast <id>. Target can be nil
-- Wand Specific
on_use = function to call when wand is used
wand_texture = texture for the wand tool
}
]]
magic._spells = {}
function magic.register_spell(def)
if def.id == "" or def.id == nil then
minetest.log("error","Unable to register spell, no ID")
return
end
if def.type == "cast" then
if def.on_cast == nil then
minetest.log("error","Unable to register spell, missing on_cast function")
return
end
print("Cast spell good")
magic._spells[def.id] = def
end
if def.type == "wand" then
-- wand will automatically register the wand tool
if def.on_use == nil then
minetest.log("error","Unable to register spell, missing on_use function")
return
end
if def.wand_texture == nil then
minetest.log("error","Unable to register spell, missing wand_texture")
return
end
print("wand spell good")
-- register the wand tool
end
minetest.log("action","Registered spell "..tostring(def.desc))
end
function magic.cast(id,name,target)
if magic._spells[id] ~= nil then
if magic._spells[id].type == "cast" then
local sk = skills.get_skill(name,SKILL_MAGIC)
if sk.level >= magic._spells[id].level then
minetest.chat_send_player(name,"You cast "..magic._spells[id].desc)
magic._spells[id].on_cast(magic._spells[id],name,target)
else
minetest.chat_send_player(name,"You are not a high enough level to cast "..magic._spells[id].desc)
end
else
minetest.chat_send_player(name,"You cannot cast "..tostring(id))
end
else
minetest.chat_send_player(name,"Spell "..tostring(id).." does not exist")
end
end
minetest.register_chatcommand("cast",{
params = "<spell id> [target]",
desc = "Casts a spell at an optional target",
privs = {},
func = function(name, param)
print(param)
local id, target = string.match(param, "([^ ]+) (.+)")
if id == nil and target == nil then
id = param
end
print("'"..tostring(id).."'")
print("'"..tostring(name).."'")
print("'"..tostring(target).."'")
magic.cast(id,name,target)
end,
})

View File

@ -8,7 +8,7 @@ dofile(magicpath.."/api.lua")
function magic.update_magic(player,name)
local s = skills.get_skill(name,SKILL_MAGIC)
local baseAdj = 5
local baseAdj = 2
if magic.player_magic[name] ~= nil then
if default.player_get_animation(player) == "lay" then
baseAdj = baseAdj + 3
@ -33,4 +33,8 @@ end
minetest.register_on_shutdown(function()
default.serialize_to_file(magic_file,magic.player_magic)
end)
end)
-- load magic spells
dofile(magicpath.."/thunder.lua")

Binary file not shown.

32
mods/magic/thunder.lua Normal file
View File

@ -0,0 +1,32 @@
local thunder = {
id = "thunder",
type = "cast",
desc = "Thunder Strike",
level = 3,
max_mana = 13,
on_cast = function(spell, name, target)
local p = minetest.get_player_by_name(name)
local sk = skills.get_skill(name,SKILL_MAGIC)
local skb = skills.get_def(SKILL_MAGIC)
local rad = 15 * ( sk.level / 10 )
local damage = ( 25 * ( sk.level / skb.max_level ) )
local mana = spell.max_mana - ( ( (sk.level - 3) / skb.max_level ) * 10 )
if magic.player_magic[name] >= mana then
magic.player_magic[name] = magic.player_magic[name] - mana
minetest.sound_play("spells_thunder",{object=p})
for _,obj in ipairs(minetest.get_objects_inside_radius(p:getpos(), rad)) do
if p ~= obj then
obj:punch(p, 1.0, {
full_punch_interval=1.0,
damage_groups={fleshy=damage},
}, nil)
end
end
else
minetest.chat_send_player(name,"You don't have enough magic to cast "..spell.desc)
end
magic.update_magic(p,name)
end
}
magic.register_spell(thunder)