Page 1 of 1

Emulating RTC's COMBINE

Posted: Sun Mar 04, 2012 6:19 pm
by Gambit37
How can I do something like RTCs COMBINE -- where you have two items in the players hands and you can combine them into one new item?

Re: Emuating RTC's COMBINE

Posted: Mon Mar 05, 2012 5:09 am
by Sophia
While DSB has no directly equivalent function, it's not that hard to write equivalent code. For example, let's look at create_potion, which is called when you cast a spell that, not surprisingly, creates a potion.

Code: Select all

function create_potion(atype, ppos, who, pow, skill, delay)
	local flask = find_arch_in_hand(who, "flask", needs_an_empty_flask)
	
	if (flask) then
		dsb_swap(flask, atype.potion)
		exvar[flask] = { power = pow }
	else
		return true
	end
	
	return false, flask
end
A lot of this code is superfluous to this explanation, but the basic function that is important to note is find_arch_in_hand. It takes three parameters: the index of the character to search, the name of the arch to search for, and a function to execute or text to print if the search is not successful. This third parameter is not optional in DSB 0.51, though I think it should be-- see the "Important Note" below.

This means a rudimentary combine is something of the form:
(Assuming the fix in the "Important Note" has been applied)

Code: Select all

obj1 = find_arch_in_hand(who, "first_arch")
obj2 = find_arch_in_hand(who, "second_arch")
if (obj1 and obj2) then
   dsb_swap(obj1, "something_else")
   dsb_delete(obj2)
end

Important Note:
Due to a design oversight, find_arch_in_hand as written needs a third parameter. This is suboptimal and I will fix this in the next version of DSB, but for the time being, you can fix it yourself by editing base/magic.lua. Find the find_arch_in_hand function around line 55. Delete the entire function and replace it with this version:

Code: Select all

function find_arch_in_hand(who, archname, failmsg)
	local look = dsb_fetch(CHARACTER, who, INV_L_HAND, 0)
	if (not look or dsb_find_arch(look) ~= obj[archname]) then
		look = dsb_fetch(CHARACTER, who, INV_R_HAND, 0)
		if (not look or dsb_find_arch(look) ~= obj[archname]) then
			if (failmsg) then
				if (type(failmsg) == "function") then
					failmsg(dsb_get_charname(who), archame)
				else
					dsb_write(system_color, failmsg)
				end
			end
			return nil
		end
	end	
	return look
end

Re: Emulating RTC's COMBINE

Posted: Thu Mar 08, 2012 10:14 pm
by Gambit37
Mmmm... very interesting! Thanks, I think I can work with that. Might have more questions though :-)

Re: Emulating RTC's COMBINE

Posted: Sat Nov 17, 2012 12:37 am
by Gambit37
Just discovered a bug in the code you posted above, which is in v0.54:

Code: Select all

            if (type(failmsg) == "function") then
               failmsg(dsb_get_charname(who), archame)
            else
               dsb_write(system_color, failmsg)
            end
Typo on the second line, "archame" should be "archname".

Re: Emulating RTC's COMBINE

Posted: Sat Nov 17, 2012 1:01 am
by Sophia
Ok, fixed.