2020-12-05 20:03:01 -08:00
|
|
|
-- API
|
|
|
|
|
2020-12-06 20:10:41 -08:00
|
|
|
function adv_core.load_pouch(name)
|
|
|
|
local pouch = minetest.deserialize(adv_core.mod_storage:get_string(tostring(name) .. "pouch"))
|
2020-12-06 10:57:53 -08:00
|
|
|
|
|
|
|
--init if nil
|
|
|
|
if pouch == nil then
|
|
|
|
pouch = {
|
|
|
|
fire = 0,
|
|
|
|
water = 0,
|
|
|
|
earth = 0,
|
|
|
|
air = 0,
|
|
|
|
}
|
|
|
|
--store it back
|
2020-12-06 20:10:41 -08:00
|
|
|
adv_core.mod_storage:set_string(name .. "pouch", minetest.serialize(pouch))
|
2020-12-06 10:57:53 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
return pouch
|
|
|
|
end
|
2020-12-05 20:03:01 -08:00
|
|
|
|
2020-12-06 10:57:53 -08:00
|
|
|
function adv_core.player_can_afford_object(name, object_name)
|
2020-12-20 20:30:29 -08:00
|
|
|
local objectTable = adv_core.objectTable or {}
|
|
|
|
if objectTable[object_name] == nil then
|
2020-12-06 10:57:53 -08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--load player pouch
|
2020-12-06 20:10:41 -08:00
|
|
|
local player_pouch = adv_core.load_pouch(name)
|
2020-12-06 10:57:53 -08:00
|
|
|
|
2020-12-20 20:30:29 -08:00
|
|
|
local fire_ok = player_pouch.fire >= objectTable[object_name].fire
|
|
|
|
local water_ok = player_pouch.water >= objectTable[object_name].water
|
|
|
|
local earth_ok = player_pouch.earth >= objectTable[object_name].earth
|
|
|
|
local air_ok = player_pouch.air >= objectTable[object_name].air
|
|
|
|
|
2020-12-06 10:57:53 -08:00
|
|
|
if fire_ok and water_ok and earth_ok and air_ok then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2020-12-05 20:03:01 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function adv_core.player_can_afford(name, fire, water, earth, air)
|
2020-12-06 10:57:53 -08:00
|
|
|
--load player pouch
|
2020-12-06 20:10:41 -08:00
|
|
|
local player_pouch = adv_core.load_pouch(name)
|
2020-12-06 10:57:53 -08:00
|
|
|
|
|
|
|
local fire_ok = player_pouch.fire >= fire
|
|
|
|
local water_ok = player_pouch.water >= water
|
|
|
|
local earth_ok = player_pouch.earth >= earth
|
|
|
|
local water_ok = player_pouch.air >= air
|
|
|
|
|
|
|
|
if fire_ok and water_ok and earth_ok and air_ok then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2020-12-05 20:03:01 -08:00
|
|
|
end
|
|
|
|
-- (reward player with elements, pay with elements, register nodes to purchase for shop)
|
|
|
|
|
2020-12-06 10:57:53 -08:00
|
|
|
function adv_core.reward_player(name, fire, water, earth, air, notify)
|
2020-12-06 20:10:41 -08:00
|
|
|
local player_pouch = adv_core.load_pouch(name)
|
2020-12-06 10:57:53 -08:00
|
|
|
|
|
|
|
player_pouch.fire = player_pouch.fire + fire
|
|
|
|
player_pouch.water = player_pouch.water + water
|
|
|
|
player_pouch.earth = player_pouch.earth + earth
|
|
|
|
player_pouch.air = player_pouch.air + air
|
2020-12-05 20:03:01 -08:00
|
|
|
|
2020-12-06 10:57:53 -08:00
|
|
|
if notify then
|
|
|
|
--notify player:
|
|
|
|
minetest.chat_send_player(player_name,
|
|
|
|
"You've been rewarded elemental essence!"
|
|
|
|
)
|
|
|
|
|
|
|
|
minetest.chat_send_player(player_name,
|
|
|
|
minetest.get_color_escape_sequence("red") .. "Fire: " .. player_pouch.fire ..
|
|
|
|
minetest.get_color_escape_sequence("blue") .. " Water: " .. player_pouch.water ..
|
|
|
|
minetest.get_color_escape_sequence("green") .. " Earth: " .. player_pouch.earth ..
|
|
|
|
minetest.get_color_escape_sequence("yellow") .. " Air: " .. player_pouch.air
|
|
|
|
)
|
|
|
|
end
|
2020-12-05 20:03:01 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
function adv_core.take_from_player(name, fire, water, earth, air)
|
2020-12-06 20:10:41 -08:00
|
|
|
local player_pouch = adv_core.load_pouch(name)
|
2020-12-06 10:57:53 -08:00
|
|
|
|
|
|
|
--Don't reduce below zero, that's silly
|
|
|
|
|
|
|
|
player_pouch.fire = math.max(player_pouch.fire - fire , 0)
|
|
|
|
player_pouch.water = math.max(player_pouch.water - water, 0)
|
|
|
|
player_pouch.earth = math.max(player_pouch.earth - earth, 0)
|
|
|
|
player_pouch.air = math.max(player_pouch.air - air , 0)
|
2020-12-05 20:03:01 -08:00
|
|
|
|
2020-12-06 10:57:53 -08:00
|
|
|
if notify then
|
|
|
|
--notify player:
|
|
|
|
minetest.chat_send_player(player_name,
|
|
|
|
"Elemental essence taken!"
|
|
|
|
)
|
|
|
|
|
|
|
|
minetest.chat_send_player(player_name,
|
|
|
|
minetest.get_color_escape_sequence("red") .. "Fire: " .. player_pouch.fire ..
|
|
|
|
minetest.get_color_escape_sequence("blue") .. " Water: " .. player_pouch.water ..
|
|
|
|
minetest.get_color_escape_sequence("green") .. " Earth: " .. player_pouch.earth ..
|
|
|
|
minetest.get_color_escape_sequence("yellow") .. " Air: " .. player_pouch.air
|
|
|
|
)
|
|
|
|
end
|
2020-12-05 20:03:01 -08:00
|
|
|
end
|
|
|
|
|
2020-12-20 20:30:29 -08:00
|
|
|
adv_core.num_objects = 0
|
|
|
|
adv_core.objectTable = {}
|
2020-12-06 10:57:53 -08:00
|
|
|
function adv_core.register_object(object_name, lfire, lwater, learth, lair)
|
2020-12-05 20:03:01 -08:00
|
|
|
--if object name is taken, Error out
|
2020-12-20 20:30:29 -08:00
|
|
|
if adv_core.objectTable[object_name] ~= nil then
|
|
|
|
minetest.log("error","Unable to add " .. object_name .. " to adventure core object list, name already taken")
|
2020-12-06 10:57:53 -08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--add to table
|
2020-12-20 20:30:29 -08:00
|
|
|
adv_core.objectTable[object_name] = {
|
2020-12-06 10:57:53 -08:00
|
|
|
fire = lfire,
|
|
|
|
water = lwater,
|
|
|
|
earth = learth,
|
|
|
|
air = lair,
|
|
|
|
}
|
2020-12-20 20:30:29 -08:00
|
|
|
adv_core.num_objects = adv_core.num_objects+1
|
2020-12-06 10:57:53 -08:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--"fire","water","earth","air"
|
|
|
|
function adv_core.spawn_element(element_type, pos)
|
|
|
|
if element_type == "fire" then
|
|
|
|
minetest.add_entity(pos, "adventure_core:fire_element", nil)
|
|
|
|
elseif element_type == "water" then
|
|
|
|
minetest.add_entity(pos, "adventure_core:water_element", nil)
|
|
|
|
elseif element_type == "earth" then
|
|
|
|
minetest.add_entity(pos, "adventure_core:earth_element", nil)
|
|
|
|
elseif element_type == "air" then
|
|
|
|
minetest.add_entity(pos, "adventure_core:air_element", nil)
|
|
|
|
end
|
2020-12-20 20:30:29 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
--Credit to Unified Inventory for how to fix this:
|
|
|
|
function adv_core.escape_for_formspec(item_name)
|
|
|
|
return string.gsub(item_name, "([^A-Za-z0-9])", function (c) return string.format("_%d_", string.byte(c)) end)
|
|
|
|
end
|
|
|
|
function adv_core.remove_formspec_escapes(item_name)
|
|
|
|
return string.gsub(item_name, "_([0-9]+)_", function (v) return string.char(v) end)
|
|
|
|
end
|