Back to the H-World scripting manual.

Example usage: quaffing a potion

------------------------------------------------------------------
-- quaff a potion
--
-- Determine potion type, subtype, calculate effect and apply effect to user
--
-- author: Hj. Malthaner
-- date:   20-Mar-2002
-- update: 24-Mar-2002
-- update: 12-May-2004 - Hj. Malthaner: handle non-thirsty beings correctly
-- update: 23-May-2004 - Hj. Malthaner: added inventory parameter
-- update: 30-May-2004 - Hj. Malthaner: poison is now cumulative
--
-- param user:       type thing_t, 
--                   the one who quaffs the potion (player or other)
-- param inventory:  type inventory_t, 
--                   the inventory that stores the potion (may be nil)
-- param potion:     type thing_t, the potion to be quaffed
-- return:           time delay
------------------------------------------------------------------

function quaff (user, inventory, potion)
	_ALERT("quaff called\n")

	local is_player = thing_get_value(user, "is_player")

	if (is_player) then
		translate("You are quaffing a " .. thing_get_value(potion, "ident") .. ".")
	end

	local hp = thing_get_value(user, "HPcurr")
	local maxhp = thing_get_value(user, "HPmax")
	local power = thing_get_value(potion, "power")
	local subtype = thing_get_value(potion, "subtype")


	-- process healing potions
	
	if subtype == "heal" then
		_ALERT("  Healing: hp=".. hp .."\n")

		local rb = rng_get_int(power)+power/3

		-- restore hit point up to max

		if hp+rb > maxhp then
			hp = maxhp
		else
			hp = hp+rb
		end

		-- set values

		thing_set_value(user, "HPcurr", hp)

		-- tell user about effect

		if (is_player) then
			if rb < 10 then
				translate("You feel little better.")
			elseif rb < 20 then
				translate("You feel better.")
			elseif rb < 30 then
				translate("You feel much better.")
			else
				translate("You feel much better!")
			end

		else
			translate("The " .. thing_get_ident(user) .. " looks healthier.")
		end
	end


	-- process poisoning potions

	if subtype == "poison" then
		_ALERT("  Poison")

		local rb = rng_get_int(power)+power/3

		-- set values

		local prev = thing_get_value(user, "effect.poison")
		thing_set_value(user, "effect.poison", prev + rb)

		-- tell user about effect

		if (is_player) then
			translate("You feel very sick.")
		else 
			translate("The " .. thing_get_ident(user) .. " gets somewhat green.")
		end
		
	end


	-- process antidotes

	if subtype == "antidote" then
		_ALERT("  Antidote")

		local rb = rng_get_int(power)+power/3

		local poison = thing_get_value(user, "effect.poison") or 0

		if (is_player) then
			if rb >= poison then
				poison = 0
				translate("You feel perfectly well.")
			else
				poison = poison - rb
				translate("You feel less poisoned.")
			end
		else
			translate("The " .. thing_get_ident(user) .. " looks better.")
		end

		thing_set_value(user, "effect.poison", poison)
	end


	-- process blinding potions

	if subtype == "blind" then
		_ALERT("  blind")

		local rb = rng_get_int(power)+power/3

		-- set values

		local prev = thing_get_value(user, "effect.blind") or 0
		thing_set_value(user, "effect.blind", prev + rb)

		-- tell user about effect

		if (is_player) then
			translate("You are blind!")
		else 
			translate("The " .. thing_get_ident(user) .. " stumbles around.")
		end
	end


	-- modify bottle
	
	local bottle = thing_create("empty_bottle", 0, 0)
	if bottle ~= nil then
		replace_item(user, inventory, potion, bottle)
	end


	-- reduce thirst

	local thirst = thing_get_value(user, "thirst")

	-- only handle thirsty beings

	if(thirst) then
		thirst = thirst - power

		if thirst < 0 then 
			thirst = 0
		end

		thing_set_value(user, "thirst", thirst)

		-- tell user about effect
		if(is_player) then
			translate("You feel less thirsty.")
		end
	end


	return 4
end

Back to the H-World scripting manual.
Hansjörg Malthaner