sorcery  Diff

Differences From Artifact [59f5727496]:

To Artifact [c255af4d4e]:


   316    316   			if i <= 2 then minetest.remove_node(pos) else
   317    317   				minetest.set_node(pos, {name='sorcery:air_flash_1'})
   318    318   				return true
   319    319   			end
   320    320   		end
   321    321   	});
   322    322   end
          323  +
          324  +local function enchpwrhud(user, flash, fac)
          325  +	-- this function displays or changes the HUD element
          326  +	-- that shows how much energy is left in an enchanted
          327  +	-- item. it is called by the dig handler and sequences
          328  +	-- callbacks to remove the HUD from the screen once
          329  +	-- its timeleft property has been used up. timeleft is
          330  +	-- reset if the 'flash' argument, which indicates
          331  +	-- whether the enchantment has just been used, is set
          332  +	-- to true; otherwise, it is left alone.
          333  +	--
          334  +	-- this whole thing is really unfriendly and without
          335  +	-- FP tricks it would have been intolerably painful
          336  +	-- to implement. minetest needs better primitives.
          337  +	local frame = math.ceil(16 * (1-math.min(1,fac)))
          338  +	if tostring(frame) == '-0' then frame = '0' -- ??????
          339  +		else frame = tostring(frame) end  
          340  +	local tex = 'sorcery_ui_manaring_' .. (flash and 'flash_' or '') .. frame .. '.png';
          341  +	local c = sorcery.ctx.get(user)
          342  +	if not c.hud_ench then
          343  +		local hid = user:hud_add {
          344  +			name = 'sorcery:manaring';
          345  +			hud_elem_type = 'image';
          346  +			text = tex;
          347  +			position = { x = 0.5, y = 0.5 };
          348  +			offset   = { x = 0,   y = 0  };
          349  +			scale    = { x = 2.0, y = 2.0 };
          350  +			z_index  = 0;
          351  +		}
          352  +		c.hud_ench = {
          353  +			id = hid;
          354  +			timeleft = 2.0;
          355  +			fn = false;
          356  +			fac = fac;
          357  +		}
          358  +		c = c.hud_ench
          359  +	else
          360  +		c = c.hud_ench
          361  +		c.fac = fac
          362  +		user:hud_change(c.id,'text',tex)
          363  +		if flash then c.timeleft = 2.0 end
          364  +	end
          365  +	if c.fn == false then
          366  +		c.fn = true
          367  +		local delta = 0.10
          368  +		-- tried making Δ conditional on 'flash' but it
          369  +		-- turns out that causes the flash not to always
          370  +		-- disappear in a timely manner. solving this
          371  +		-- efficiently would be a major, complex headache
          372  +		-- so i'm just compromising and setting delta to a
          373  +		-- constant :/
          374  +		minetest.after(delta, function()
          375  +			if not sorcery.ctx.stat(user) then return end
          376  +			local u = sorcery.ctx.get(user)
          377  +			local h = u.hud_ench
          378  +			if not h then return end
          379  +			print('timeleft,delta',h.timeleft,delta)
          380  +			if h.timeleft - delta <= 0 then
          381  +				user:hud_remove(h.id)
          382  +				u.hud_ench = nil
          383  +			else
          384  +				h.timeleft = h.timeleft - delta
          385  +				h.fn = false
          386  +				enchpwrhud(user, false, h.fac)
          387  +			end
          388  +		end)
          389  +	end
          390  +end
   323    391   
   324    392   minetest.register_on_dignode(function(pos, node, puncher)
   325    393   	if puncher == nil then return end -- i don't know why
   326    394   	-- this is necessary but you get rare crashes without it
          395  +
          396  +	-- perform leyline checks and call notify if necessary
          397  +	if minetest.get_item_group(node.name, 'sorcery_ley_device') ~= 0 then
          398  +		sorcery.lib.node.notifyneighbors(pos)
          399  +	end
   327    400   
   328    401   	-- we're goint to do something VERY evil here and
   329    402   	-- replace the air with a "glow-air" that removes
   330    403   	-- itself after a short period of time, to create
   331    404   	-- a flash of light when an enchanted tool's used
   332    405   	-- to dig out a node
   333    406   	local tool = puncher:get_wielded_item()
................................................................................
   363    436   		if ch > sp.reliability then goto skip end
   364    437   		sparks[#sparks + 1] = {
   365    438   			color = sorcery.lib.color(data.tone):brighten(1.1);
   366    439   			count = strength * 7;
   367    440   		}
   368    441   	::skip::end end
   369    442   	if totalcost > 0 then
   370         -		local conservation = sorcery.enchant.strength(tool,'conserve') * 0.5
   371         -		totalcost = totalcost - (totalcost * conservation)
   372         -		ench.energy = math.max(0,ench.energy - (totalcost - (material.data.energysource or 0)))
          443  +		local conservation = math.floor(sorcery.enchant.strength(tool,'conserve') * 6)
          444  +		-- totalcost = totalcost - (totalcost * conservation)
          445  +		if conservation == 0 or math.random(conservation) == 1 then
          446  +			ench.energy = math.max(0,ench.energy - (totalcost - (material.data.energysource or 0)))
          447  +		end
   373    448   	end
   374    449   	if #sparks == 0 then return end
   375    450   	if math.random(5) == 1 then
   376    451   		minetest.set_node(pos, {name='sorcery:air_flash_' .. tostring(math.random(10))})
   377    452   	end
   378    453   	local range = function(min, max)
   379    454   		local span = max - min
................................................................................
   418    493   		ench.spells = {}
   419    494   		sorcery.enchant.set(tool,ench)
   420    495   	else
   421    496   		sorcery.enchant.set(tool,ench,true)
   422    497   	end
   423    498   	puncher:set_wielded_item(tool)
   424    499   
   425         -	-- perform leyline checks and call notify if necessary
   426         -	if minetest.get_item_group(node.name, 'sorcery_ley_device') ~= 0 then
   427         -		sorcery.lib.node.notifyneighbors(pos)
   428         -	end
          500  +	local epct = ench.energy / material.data.maxenergy
          501  +	enchpwrhud(puncher, true, epct)
   429    502   end)
   430         -
   431         -minetest.register_chatcommand('enchants', {
   432         -	description = 'Log information about the currently held object\'s enchantment';
   433         -	privs = { server = true };
   434         -	func = function(caller,params)
   435         -		local tool = minetest.get_player_by_name(caller):get_wielded_item()
   436         -		minetest.chat_send_player(caller, dump(sorcery.enchant.get(tool)))
   437         -		local binary = tool:get_meta():get_string('sorcery_enchantment_recs')
   438         -		local dumpout = ''
   439         -		for i=1,string.len(binary) do
   440         -			dumpout = dumpout .. string.format('%x%s',string.byte(binary,i),(i%16==0 and '\n') or ' ') 
   441         -		end
   442         -		print(dumpout)
   443         -	end;
   444         -})