I have several questions, but need to work out what they are first... :-/ More to follow. --TheFalcon
- Well, to start: Does an object have to exist for me to get its name? i.e. if I have tval and sval, can I get out the name of the object they would describe, without having to access an actual object_kind? (if that makes sense...)
KhymChanur: If you're talking about 3.0.0, easily get at least a partial name from a tval and sval. In Lua, you can do something like:
local k_idx = lookup_kind(tval, sval)
local object_kind = k_info[k_idx + 1]
local name = object_kind.name
But this will give you the string "light" for a Scroll of Light, and "flying" for a Ring of Flying. It will also still include any "&", "~" or "#" embedded in the name. engine/wish.lua has code for getting an objects name from its k_idx. Slightly modified, it goes like this:
if object_desc_tvals[item.tval] and not is_artifact(item) then
name.aware = 1
name.known = 1
if item.flags[FLAG_PARSE_WISH_STR] then
-- The item kind might have a weird way of parsing
-- the item description
local func = get_func_from_flag(item.flags,
FLAG_PARSE_WISH_STR)
local success, base = func(wish_str, item)
if success == wish.GRANTED then
name["base"] = base
else
name["base"] = ""
end
elseif tag(object_desc_tvals[item.tval]) == TAG_FUNCTION then
object_desc_tvals[item.tval](item, name)
else
for k, e in object_desc_tvals[item.tval] do
name[k] = e
end
end
if name["base"] then
name = name["base"]
name = gsub(name, "#", item.name)
else
name = item.name
end
else
name = item.name
end -- Extract the object's base description
name = gsub(name, "& ", "")
name = gsub(name, "~", "")
That really should be broken off from the wishing code and put into engine/objects.lua, so that it can be re-used.
KhymChanur: Also, for some items, like wands, its impossible to turn get the items name just from its tval/sval/k_idx, because one item kind is handling multiple flavors. For instance, there's three different item kinds for wands, one for Lesser Wands, one for Wands, and one for Greater Wands. You could try creating a wand object so it can be passed to object_desc(), but then you'd get back something like "Lesser Wand of Fire Bolt" or "Greater Wand of Cold Storm" or something.
TheFalcon: Sorry, I should have made it clear I was talking about 2.3.x, but this is still all helpful.
I tried doing this anyway:
local k_idx = lookup_kind(tval, sval)
local object_kind = k_info[k_idx + 1]
local name = object_kind.name
but when I msg_print(name) it it gives me a number...
[later...] object.pkg reveals that the name is a u32b. How do I convert that to a string?
DarkGod: Either you are out of luck or I made a function for this purpose, but I'm afraid I do not recall making it(and I do not have the 2.X sources at work to check it)
TheFalcon: Meh? surely there must be a function that does this somewhere, otherwise the game wouldn't be able to print an object name ever...
DarkGod: In C yes .. well you can make an object out of the k_info entry and call object_desc on it
NeilStevens: Simple question: What exactly are you trying to accomplish, that you think you want an object name without having a complete, generated object to name? I don't think that concept even makes sense.
TheFalcon: I'm trying to write a quest for Theme, such that the player is asked to bring back random (ego) items of a particular tval. Obviously the item doesn't exist when the player is told to fetch it, but I still need a name for the item, so I can tell the player what to fetch... I can't tell them to bring back an object with tval 23 and sval 24, aka "6760", I need to be able to tell them to bring back an espadon. Thus I need to convert "6760" to a string. I assume that this will also be the case with egos. I can handle sticking the name together if I can just get them in string form. Could this not be done with lua?
Would it be possible to create an ego object of specific tval (not necessarily sval), get the game to describe it, and then delete the object again?
NeilStevens: Why so focused on 'tval' and 'sval'? Wouldn't just be easier just to generate random objects until you get one that meets certain criteria (not an artifact, etc.), store the final object, display it to the user, then compare the brought-back objects with the stored object to determine whether the quest is completed? You could even skip the internal stats entirely, and do a simple string comparison on the object names!
That'd seem to avoid all the problems you're having from being altogether too focused on particular internal variables, and how well or incompletely they describe an object.
Note that if you're looking to compare 'base' types, rather than the specific egos or whatnot, you can always clone the object, set it unidenfied, and get the description of *that* in order to get the string for comparison.
TheFalcon: Ok. Sorry for the basic question, but how do I generate a random object?
[later...] Oh wait, I've just found create_object(tv,sv). No idea where it is in the source though, I can't find it in object.pkg. Hmmm *experiments*.
NeilStevens: You want make_object(object, good, great, theme) followed by apply_magic(object, level, okay, good, great)
A temporary object variable you can use for generating an item whose name you'll save for later is obj_forge. So to get a random great combat item at level 25, I think you'd do this:
theme_forge.treasure = 0 theme_forge.combat = 100 theme_forge.magic = 0 theme_forge.tools = 0 make_object(obj_forge, true, true, theme_forge) apply_magic(obj_forge, 25, true, true, true)
TheFalcon: Yep, that works nicely. Thanks.
ToME Wiki