local target_node = function(ctx,tgt)
if not ctx.target or ctx.target.type ~= 'node' then return false end
local node = minetest.get_node(ctx.target.under)
if node.name ~= tgt then return false end
return node
end;
local get_enchanter = function(ctx)
local ench = target_node(ctx, 'sorcery:enchanter')
if not ench then return false end
return minetest.get_meta(ctx.target.under):get_inventory()
end
local cast_sparkle = function(ctx,color,strength,duration)
sorcery.vfx.cast_sparkle(ctx.caster,color,strength,duration)
end
local enchantment_sparkle = function(ctx,color)
sorcery.vfx.enchantment_sparkle(ctx.target,color)
end
local anchorwand = function(aff,uses,recipe)
local affcolor = sorcery.lib.color(sorcery.data.affinities[aff].color)
return {
name = aff .. ' anchor';
desc = 'With an enchanter, anchor ' .. aff .. ' spells into an object to enable it to produce preternatural effects';
uses = uses;
affinity = recipe;
color = affcolor;
sound = 'xdecor_enchanting'; -- FIXME make own
cast = function(ctx)
local node = target_node(ctx, 'sorcery:enchanter')
if not node then return false end
local inv = minetest.get_meta(ctx.target.under):get_inventory()
if inv:is_empty('item') then return false end
local subj = inv:get_stack('item',1)
-- now we have everything we need. this part is complex.
-- first, we need to check the item to see if it is in a
-- group eligible for enchantment. if so, we then retrieve
-- the item material and look up the slots offered by this
-- material. we iterate through the slot definitions to pick
-- out a candidate slot, criteria being that the slot
-- possesses the proper affinity, and that the corresponding
-- slot on the item enchantment record is empty, then pick
-- the final slot at random from this table if #candidates>0
-- (otherwise, we fail unceremoniously).
local current_enchant = sorcery.enchant.get(subj)
local material, eligible_spells = sorcery.enchant.getsubj(subj)
if eligible_spells == nil
or #eligible_spells == 0
or material == nil
or material.data.slots == nil
or #material.data.slots == 0
then return false end
-- determine the properties the enchantment will have
local power = 10
local energy = material.data.maxenergy
local reliability = 100
if ctx.base.gem == 'sapphire' then power = power + 5
elseif ctx.base.gem == 'amethyst' then
energy = energy * (math.random() * 0.7)
elseif ctx.base.gem == 'diamond' then
if math.random(5) == 1 then
power = power * 2
reliability = reliability - (reliability / 4)
else
power = power + 5
reliability = reliability - 10
end
end
local viable_slots = {}
for i,slot in pairs(material.data.slots) do
if sorcery.lib.tbl.has(slot.affinity,aff) then
for _,spell in pairs(current_enchant.spells) do
if spell.slot == i then goto nonviable end
end
viable_slots[#viable_slots + 1] = i
end
::nonviable::end
if #viable_slots == 0 then return false end
local target_slot = viable_slots[math.random(#viable_slots)]
-- once we have selected an appropriate slot, we iterate
-- through the list of known enchantments to check the
-- affinity of each against `aff`. if the affinity matches
-- the wand, we then check whether the 'recipe' matches.
-- if so, we've found the enchantment -- apply it to the
-- object and update its enchantment data. otherwise, we
-- move on to the next. if no matching recipe is found,
-- we give up and bail, returning 'false' to signify that
-- a spell was not cast.
local focus_match = function(spec,stack)
local default_mode
if spec.lens then
default_mode = 'dmg'
if minetest.get_item_group(stack:get_name(), 'sorcery_enchanting_lens') == 0
then return false end
local proto = stack:get_definition()._proto
if proto.kind ~= spec.lens or proto.gem ~= spec.gem
then return false end
elseif spec.item then
default_mode = 'consume'
if stack:get_name() ~= spec.item then
return false end
else
return false
end
local mode
if spec.dmg then mode = 'dmg'
elseif spec.consume then mode = 'consume'
else mode = default_mode end
if mode == 'dmg' then
stack:add_wear((spec.dmg or 1) * 1000)
return stack
elseif mode == 'consume' then
stack:take_item(spec.consume or 1)
return stack
end
end
for ench,data in pairs(sorcery.data.enchants) do
if data.affinity ~= aff or data.recipe == nil then goto skip end
local newinv = {}
for s,v in pairs(data.recipe) do
newinv[s] = focus_match(v,inv:get_stack('foci',s))
if newinv[s] == false then goto skip end
end
-- is the tool compatible with the chosen spell? we check its groups to
-- see if any of them grant it eligibility; if not, we skip to the next
-- spell. NOTE: this means the same recipe could mean different things
-- for different kinds of tools, if one were so inclined.
for g,v in pairs(subj:get_definition().groups) do
if v ~= 0 and sorcery.lib.tbl.has(data.groups, g)
then goto enchant end
end
goto skip
-- spell matches!
::enchant:: current_enchant.spells[#current_enchant.spells + 1] = {
id = ench;
slot = target_slot;
boost = power;
reliability = reliability;
}
current_enchant.energy = math.max(current_enchant.energy, energy)
sorcery.enchant.set(subj, current_enchant)
inv:set_stack('item',1,subj)
for i,v in pairs(newinv) do inv:set_stack('foci',i,v) end
sorcery.enchant.update_enchanter(ctx.target.under)
enchantment_sparkle(ctx,affcolor)
do return nil end
::skip::end
return false
end
};
end
-- note: this was written before terminology was standardized,
-- and "leytype" corresponds to what is otherwise known as an
-- "affinity"; "affinity" after this comment is widely misused
return {
flame = {
name = 'flamebolt';
color = {255,89,16};
uses = 32;
affinity = {'acacia','blazing'};
leytype = 'praxic';
desc = 'Conjure a gout of fire to scorch your foes with a flick of this wand';
cast = function(ctx)
local speed = 30 -- TODO maybe amethyst tip increases speed?
local radius
if ctx.base.gem == 'sapphire'
then radius = math.random(2,3)
else radius = math.random(1,2)
end
local heading = ctx.heading
heading.pos.y = heading.pos.y + heading.eyeheight*0.9
local bolt = minetest.add_entity(heading.pos,'sorcery:spell_projectile_flamebolt')
bolt:set_rotation(heading.yaw)
local vel = {
x = heading.yaw.x * speed;
y = heading.yaw.y * speed;
z = heading.yaw.z * speed;
};
bolt:get_luaentity()._blastradius = radius
bolt:set_velocity(vel)
end;
};
seal = {
name = 'sealing';
color = {255,238,16};
uses = 32;
desc = 'Bind an object to your spirit such that it will be rendered impregnable to others, or break a sealing created with this same wand';
leytype = 'imperic';
affinity = {'pine','dark'};
cast = function(ctx)
if ctx.target == nil or ctx.target.type ~= 'node' then return false end
local meta = minetest.get_meta(ctx.target.under)
-- first we need to check if the wand has an identifying 'key' yet,
-- and set one if not.
local modes = {
sapphire = 'lockdown';
diamond = 'steal';
}
local wandmode = modes[ctx.base.gem] or 'seal'
local keycode
if ctx.meta:contains('sorcery_wand_key') then
keycode = ctx.meta:get_string('sorcery_wand_key')
else
keycode = sorcery.lib.str.rand(32)
ctx.meta:set_string('sorcery_wand_key', keycode)
-- ctx.meta:mark_as_private('sorcery_wand_key')
end
if meta:contains('owner') then
-- owner is already set -- can we break the enchantment?
if wandmode == 'steal' then
if meta:get_string('owner') ~= ctx.caster:get_player_name() then
meta:set_string('owner',ctx.caster:get_player_name())
enchantment_sparkle(ctx,sorcery.lib.color(101,255,238))
end
return
end
if meta:get_string('sorcery_wand_key') == keycode then
meta:set_string('owner','')
meta:set_string('sorcery_wand_key','')
meta:set_string('sorcery_seal_mode','')
enchantment_sparkle(ctx,sorcery.lib.color(101,255,142))
else return false end
else
meta:set_string('sorcery_wand_key',keycode)
meta:mark_as_private('sorcery_wand_key')
meta:set_string('owner',ctx.caster:get_player_name())
if wandmode == 'lockdown' then
meta:set_string('sorcery_seal_mode','wand')
end
enchantment_sparkle(ctx,sorcery.lib.color(255,201,27))
end
end;
};
leyspark = {
name = 'Leyspark';
leytype = 'cognic';
color = {255,246,142}; -- bright yellow
affinity = {'apple','silent'};
uses = 128;
desc = 'Reveal the strength and affinities of the local leyline';
cast = function(ctx)
local color = ctx.base.gem == 'sapphire';
local duration = (ctx.base.gem == 'amethyst' and 4) or 2;
local ley = sorcery.ley.estimate(ctx.caster:get_pos())
local strength = ley.force
if color then
strength = strength / #ley.aff
for _,a in pairs(ley.aff) do
cast_sparkle(ctx,sorcery.lib.color(sorcery.data.affinities[a].color):brighten(1.3), strength, duration * strength)
end
else
cast_sparkle(ctx,sorcery.lib.color(250,255,185), strength, duration*strength)
end
end;
};
dowse = {
name = 'dowsing';
leytype = 'cognic';
color = {65,116,255};
affinity = {'acacia','dark','silent'};
uses = 176;
desc = 'Send up sparks of radia to indicate nearness or absence of the blocks whose presence the wand is attuned to';
};
verdant = {
name = 'verdant';
color = {16,29,255};
uses = 48;
leytype = 'imperic';
desc = 'Pour life-energy into the soil, causing flowers and trees to spring up at your command';
affinity = {'jungle','verdant'};
};
praxic = anchorwand('praxic', 16, {'pine','shimmering','blazing'});
counterpraxic = anchorwand('counterpraxic',23, {'pine','shimmering','silent'});
entropic = anchorwand('entropic', 8, {'jungle','dark'});
syncretic = anchorwand('syncretic', 12, {'aspen','verdant','shimmering','blazing'});
cognic = anchorwand('cognic', 32, {'acacia','verdant','dark'});
occlutic = anchorwand('occlutic', 15, {'apple','silent','dark'});
imperic = anchorwand('imperic', 8, {'apple','verdant','blazing'});
mandatic = anchorwand('mandatic', 18, {'jungle','verdant','silent'});
shape = {
name = 'shaping';
uses = 24;
color = {255,114,65};
leytype = 'praxic';
affinity = {'apple','blazing'};
desc = 'With an enchanter, physically alter the mundane qualities of an object';
};
attune = {
name = 'attunement';
uses = 38;
color = {255,65,207};
leytype = 'syncretic';
affinity = {'pine','verdant','dark'};
desc = 'Establish a connection between mystic mechanisms, like connecting two sides of a portal or impressing targets onto a dowsing wand in an enchanter';
cast = function(ctx)
if not (ctx.target and ctx.target.type == 'node') then
ctx.meta:set_string('source','')
cast_sparkle(ctx, sorcery.lib.color(234,45,100), 0.3)
return true
end
local n = minetest.registered_nodes[minetest.get_node(ctx.target.under).name]
if not (n and n._sorcery and n._sorcery.attune) then
return false end -- just in case anyone casts on an undefined node
local m = sorcery.lib.marshal
local encpos, decpos = m.transcoder {
x = m.t.s32, y = m.t.s32, z = m.t.s32;
id = m.t.str;
}
local props = n._sorcery.attune
local rec = ctx.meta:get_string('source')
local src
if rec then
local data = decpos(sorcery.lib.str.meta_dearmor(rec,true))
local srcpos = {x=data.x,y=data.y,z=data.z}
local srcnode = minetest.get_node(srcpos)
local srcdef = minetest.registered_nodes[srcnode.name]
if srcdef and srcdef._sorcery and srcdef._sorcery.attune then
if sorcery.attunement.nodeid(srcpos) == data.id then
src = {
pos = srcpos;
props = srcdef._sorcery.attune;
}
end
end
end
local color
if src and src.props.source and
src.props.class == props.accepts and props.target and
(not vector.equals(src.pos, ctx.target.under)) then
sorcery.attunement.pair(src.props.reciprocal or true, src.pos, ctx.target.under)
ctx.meta:set_string('source','')
color = sorcery.lib.color(255,130,75)
elseif props.source then
ctx.meta:set_string('source', sorcery.lib.str.meta_armor(encpos {
x = ctx.target.under.x;
y = ctx.target.under.y;
z = ctx.target.under.z;
id = sorcery.attunement.nodeid(ctx.target.under);
},true))
color = sorcery.lib.color(128,75,255)
end
if color then enchantment_sparkle(ctx,color)
else return false end
end;
};
meld = {
name = 'melding';
uses = 48;
leytype = 'syncretic';
color = {172,65,255};
affinity = {'apple','verdant'};
desc = 'Meld the properties of three balanced items on an enchanter to create a new one with special properties, but destroying the old ones and losing two thirds of the mass in the process. The precise outcome is not always predictable, and may vary with the moons and the stars.';
cast = function(ctx)
local e = get_enchanter(ctx)
if not e then return false end
for _,m in pairs(sorcery.data.resonance.meld) do
if m.restrict and not m.restrict(ctx)
then goto next_meld end
local g = {}
for i,set in ipairs(m.set) do
if type(set) == 'table' then
g[i] = set
else g[i] = { take = set; } end
local found = false
for j=1,e:get_size('foci') do
local match,res = sorcery.lib.item.groupmatch(g[i].take, e:get_stack('foci',j),false)
if match then
found = true
g[i].slot = j
g[i].leftover = res
break
end
end
if not found then goto next_meld end
end
-- we've made it past the tests; this meld
-- matches the spec
for _,t in pairs(g) do
if t.leftover and t.leftover:get_count() > 0 then
e:set_stack('foci',t.slot,t.leftover)
if t.replacement then
minetest.add_item(ctx.target.above, ItemStack(t.replacement))
end
else
e:set_stack('foci',t.slot,ItemStack(t.replacement))
end
end
local res
if type(m.results) == 'function' then
res = m.results(ctx)
elseif type(m.results) == 'table' and m.results[1] then -- haaaack
res = select(2,sorcery.lib.tbl.pick(m.results))
else
res = m.results
end
e:set_stack('item',1,ItemStack(res))
enchantment_sparkle(ctx,sorcery.lib.color(228,4,201))
::next_meld::end
end;
};
divide = {
name = 'division';
uses = 19;
leytype = 'syncretic';
color = {255,65,121};
affinity = {'apple','shimmering'};
desc = 'Shatter an item on an enchanter, dividing its essence equally into three parts and precipitating it into new items embodying various properties of the destroyed item. The outcome is not always predictable, and may vary with the moons and the stars.';
cast = function(ctx)
local e = get_enchanter(ctx)
if not e then return false end
local orig = e:get_stack('item',1)
local div = sorcery.data.resonance.divide[orig:get_name()]
if not div then return false end
local bitch = function(err)
sorcery.log('data/spells(divide)', err .. ' for ' .. orig:get_name())
return false
end
if not (div.mode and div.give) then
return bitch('improperly specified division')
end
if div.restrict and not div.restrict(ctx) then
return false
end
local dst
if div.mode == 'any' then
local lst = sorcery.lib.tbl.cshuf(div.give)
dst = function(i) return lst[i] end
elseif div.mode == 'random' then
dst = function() return sorcery.lib.tbl.pick(div.give) end
elseif div.mode == 'set' then
dst = function(i) return div.give[i] end
elseif div.mode == 'all' then
dst = function() return div.give end
elseif div.mode == 'fn' then
dst = function(i) return div.give(i,ctx) end
else return bitch('invalid division mode') end
for i=1,e:get_size('foci') do
e:set_stack('foci',i,ItemStack(dst(i)))
end
e:set_stack('item',1,ItemStack(div.replacement))
for _,color in pairs{{245,63,63},{63,245,178}} do
enchantment_sparkle(ctx, sorcery.lib.color(color))
end
end;
};
obliterate = {
name = 'obliteration';
uses = 129;
color = {175,6,212};
affinity = {'aspen','dark'};
leytype = 'occlutic';
desc = 'Incinerate all items on an enchanter, rendering them down to ash or obliterating them entirely.';
cast = function(ctx)
local tgt = target_node(ctx, 'sorcery:enchanter')
if not tgt then return false end
local inv = minetest.get_meta(ctx.target.under):get_inventory()
for _,name in pairs{'foci','item'} do
for i=1,inv:get_size(name) do
local stack = 'sorcery:ash'
if ctx.base.gem == 'sapphire' then
stack = nil
end
inv:set_stack(name,i,ItemStack(stack))
end
end
enchantment_sparkle(ctx,sorcery.lib.color(255,12,0))
enchantment_sparkle(ctx,sorcery.lib.color(85,18,35))
enchantment_sparkle(ctx,sorcery.lib.color(0,0,0))
end
};
sacrifice = {
name = 'sacrifice';
uses = 58;
color = {212,6,63};
affinity = {'aspen','blazing'};
leytype = 'syncretic';
desc = 'Transform the matter of one to three items on an enchanter into energy and empower the item on the center of the enchanter with it. Useful to recharge wands in areas with weak leylines.';
};
transfer = {
name = 'transfer';
uses = 65;
color = {6,212,121};
leytype = 'syncretic';
affinity = {'aspen','shimmering','silent'};
desc = 'Transfer ley-current from items on an enchanter into the item in the center, but at a 50% loss if they are of mismatched affinities. One third of maximum current is transferred, and when used on items with little power may destroy them or their enchantments';
};
transmute = {
name = 'transmutation';
uses = 7;
color = {255,90,18};
leytype = 'imperic';
affinity = {'aspen','shimmering','dark','blazing'};
desc = 'Transmute three ingots into one of a different metal, determined by chance, and influenced by configuration of the wand as well as the stars and the phase of the moon';
-- diamond = quantity varies between 1-3
};
disjoin = {
name = 'disjunction';
uses = 32;
color = {17,6,212};
leytype = 'occlutic';
affinity = {'jungle','silent'};
desc = 'With an enchanter, disjoin the anchor holding a spell into an object so a new spell can instead be bound in';
cast = function(ctx)
local ench = target_node(ctx, 'sorcery:enchanter')
if not ench then return false end
local ei = minetest.get_meta(ctx.target.under):get_inventory()
local item = ei:get_stack('item',1)
local e = sorcery.enchant.get(item)
if next(e.spells) == nil then return false end
if #e.spells == 1 then e = nil else
if ctx.base.gem == 'sapphire'
then e.spells = {} e.energy = 0
else table.remove(e.spells, math.random(#e.spells))
end
end
sorcery.enchant.set(item,e)
ei:set_stack('item',1,item)
enchantment_sparkle(ctx,sorcery.lib.color(255,154,44))
enchantment_sparkle(ctx,sorcery.lib.color(226,44,255))
end;
};
divine = {
name = 'divining';
desc = 'Steal away the secrets of the cosmos';
uses = 16;
color= {97,97,255};
sound = 'xdecor:enchanting';
leytype = 'cognic';
affinity = {'pine','shimmering','dark','verdant'};
cast = function(ctx)
local inks = {'black','red','white','violet','blue','green'}
local getcolor = function(stack)
if stack:is_empty() then return nil end
if minetest.get_item_group(stack:get_name(), 'dye') == 0 then return nil end
for _,ink in pairs(inks) do
if minetest.get_item_group(stack:get_name(), 'color_' ..ink) ~= 0
then return ink end
end
end
if not ctx.target or ctx.target.type ~= 'node' then return false end
local tgt = minetest.get_node(ctx.target.under)
if tgt.name == 'sorcery:enchanter' then
local meta = minetest.get_meta(ctx.target.under)
local inv = meta:get_inventory()
if inv:get_stack('item',1):get_name() == 'default:paper'
-- and inv:get_stack('item',1):get_count() == 1
and not inv:is_empty('foci') then
local ink1 = getcolor(inv:get_stack('foci',2))
local ink2 = getcolor(inv:get_stack('foci',3))
local restrict, kind, mod = {} do
local ms = inv:get_stack('foci',1)
if not ms:is_empty() then mod = ms:get_name() end
end
if ink1 == 'black' and ink2 == 'black' then kind = 'craft'
if mod then
if mod == sorcery.data.metals.cobalt.parts.powder then
restrict.group = 'sorcery_magitech'
elseif mod == sorcery.data.metals.vidrium.parts.powder then
restrict.group = 'sorcery_ley_device'
elseif mod == sorcery.data.metals.aluminum.parts.powder then
restrict.group = 'sorcery_metallurgy'
elseif mod == sorcery.data.metals.lithium.parts.powder then
restrict.group = 'sorcery_enchanting_lens'
elseif mod == sorcery.data.metals.iridium.parts.powder then
restrict.group = 'sorcery_worship'
elseif mod == sorcery.data.metals.gold.parts.powder then
restrict.group = 'sorcery_grease'
elseif mod == sorcery.data.metals.silver.parts.powder then
restrict.group = 'sorcery_oil'
elseif mod == sorcery.data.metals.electrum.parts.powder then
restrict.group = 'sorcery_extract'
elseif mod == sorcery.data.metals.steel.parts.powder then
restrict.group = 'crafttool'
elseif mod == 'farming:sugar' then
restrict.mod = 'farming'
else return false end
end
elseif ink1 == 'black' and ink2 == 'white' then kind = 'infuse'
if mod then
if mod == sorcery.data.metals.gold.parts.powder then
restrict.mod = 'sorcery_draught'
elseif mod == sorcery.data.metals.cobalt.parts.powder then
restrict.mod = 'sorcery_philter'
elseif mod == sorcery.data.metals.lithium.parts.powder then
restrict.mod = 'sorcery_elixirs'
else return false end
end
elseif ink1 == 'blue' and ink2 == 'violet' then kind = 'enchant'
if mod then
if mod == sorcery.data.metals.cobalt.parts.powder then
restrict.aff = 'praxic'
elseif mod == sorcery.data.metals.tungsten.parts.powder then
restrict.aff = 'counterpraxic'
elseif mod == sorcery.data.metals.aluminum.parts.powder then
restrict.aff = 'syncretic'
elseif mod == sorcery.data.metals.lithium.parts.powder then
-- restrict.aff = 'mandatic' -- no enchants yet, will cause infinite loop 🙃
elseif mod == sorcery.data.metals.iridium.parts.powder then
restrict.aff = 'entropic'
elseif mod == sorcery.data.metals.gold.parts.powder then
restrict.aff = 'cognic'
elseif mod == sorcery.data.metals.silver.parts.powder then
-- restrict.aff = 'occlutic'
elseif mod == sorcery.data.metals.electrum.parts.powder then
-- restrict.aff = 'imperic'
else return false end
end
elseif ink1 == 'red' and ink2 == 'yellow' then kind = 'cook';
-- elseif ink1 == 'red' and ink2 == 'orange' then kind = 'smelt';
end
if kind then
local rec = ItemStack('sorcery:recipe')
local m = rec:get_meta()
if ctx.base.gem == 'diamond' then
-- make recipe for thing in slot 1
else
sorcery.cookbook.setrecipe(rec,kind,nil,restrict)
end
local old = inv:get_stack('item',1)
-- TODO: detect hopper underneath and place
-- recipe into it instead of item slot
if old:get_count() == 1 then
inv:set_stack('item',1,rec)
else
old:take_item(1)
inv:set_stack('item',1,old)
minetest.add_item(ctx.target.above,rec)
end
for i=1,inv:get_size('foci') do
local f = inv:get_stack('foci',i)
f:take_item(1)
inv:set_stack('foci',i,f)
end
enchantment_sparkle(ctx,sorcery.lib.color(97,97,255))
return
end
end
end
return false
end;
};
luminate = {
name = 'lumination';
desc = 'Banish darkness all about you for a few moments';
uses = 40;
color = {244,255,157};
affinity = {'acacia','shimmering','blazing'};
leytype = 'cognic';
cast = function(ctx)
local center = ctx.heading.pos
local maxpower = 20
local power = (ctx.base.gem == 'sapphire' and maxpower) or maxpower/2
local range = (ctx.base.gem == 'emerald' and 6) or 3
local duration = (ctx.base.gem == 'amethyst' and 60) or 30
if ctx.base.gem == 'diamond' then
power = power * (math.random()*2)
range = range * (math.random()*2)
duration = duration * (math.random()*2)
end
local lum = math.ceil((power/maxpower) * minetest.LIGHT_MAX)
for i=1,power do
local pos = vector.add(center, {
x = math.random(-range,range);
z = math.random(-range,range);
y = math.random(0,range/2);
})
local delta = vector.subtract(pos,center)
local near = 1 - (delta.x^2 + delta.y^2 + delta.z^2)/(range^2)
if minetest.get_node(pos).name == 'air' then
minetest.set_node(pos,{name='sorcery:air_glimmer_' .. tostring(lum)})
do local lm = minetest.get_meta(pos)
lm:set_float('duration',duration)
lm:set_float('timeleft',duration)
lm:set_float('power',lum * near)
end
end
end
end;
};
}