Emulating RTC's COMBINE

This forum is for the Lua scriptable clone of DM/CSB called Dungeon Strikes Back by Sophia. Use DSB to build your own highly customised games.

Moderator: Sophia

Forum rules
Please read the Forum rules and policies before posting. You may Image to help finance the hosting costs of this forum.
Post Reply
User avatar
Gambit37
Should eat more pies
Posts: 13772
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Emulating RTC's COMBINE

Post 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?
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: Emuating RTC's COMBINE

Post 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
User avatar
Gambit37
Should eat more pies
Posts: 13772
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: Emulating RTC's COMBINE

Post by Gambit37 »

Mmmm... very interesting! Thanks, I think I can work with that. Might have more questions though :-)
User avatar
Gambit37
Should eat more pies
Posts: 13772
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: Emulating RTC's COMBINE

Post 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".
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: Emulating RTC's COMBINE

Post by Sophia »

Ok, fixed.
Post Reply