Add script function GetDefinitionGroupPath

console-destruction
Sven Eberhardt 2016-09-04 21:45:09 -04:00
parent 34058e13f9
commit 41dde5e7d0
4 changed files with 80 additions and 1 deletions

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE funcs
SYSTEM '../../../clonk.dtd'>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<funcs>
<func>
<title>GetDefinitionGroupPath</title>
<category>Objects</category>
<version>8.0 OC</version>
<syntax>
<rtype>string</rtype>
<params>
<param>
<type>id</type>
<name>definition</name>
<desc>Definition to get the path of.</desc>
</param>
</params>
</syntax>
<desc>Returns the editor group path of a definition. This is the path at which the definition is shown in the creator window and can be used to gorup definitions in large dropdown enumerations.</desc>
<examples>
<example>
<code>var index = 0, weapon_def, weapon_list = [];
while (weapon_def = GetDefinition(index))
{
if (<funclink>WildcardMatch</funclink>(GetDefinitionGroupPath(weapon_def), "*/Weapons")
weapon_list[GetLength(weapon_list)] = weapon_def;
index++;
}
var spawn_weapon = weapon_list[<funclink>Random</funclink>(<funclink>GetLength</funclink>(weapon_list))];
<funclink>CreateObject</funclink>(spawn_weapon,<funclink>AbsX</funclink>(<funclink>Random</funclink>(<funclink>LandscapeWidth</funclink>())),0);</code>
<text>Searches through all loaded definitions and all that are placed directly in a group folder named "Weapons" are put into an array. After that, a random item of these defintions is selected to spawn at a random position in the landscape.</text>
</example>
</examples>
<related>
<funclink>GetDefinition</funclink>
</related>
</func>
<author>Sven2</author><date>2016-09</date>
</funcs>

View File

@ -118,6 +118,36 @@ void C4ConsoleQtDefinitionListModel::ReInit()
new_root = root.get();
}
root->parent = nullptr;
// Copy group path names into definitions for later lookup by script
QStringList group_names;
DefListNode *node = root.get();
while (node)
{
if (node->def)
{
node->def->ConsoleGroupPath.Copy(group_names.join('/').toUtf8());
}
// Walk over tree. Remember groups in group_names string list.
if (!node->items.empty())
{
if (node != root.get()) group_names.append(node->name.getData());
node = node->items[0].get();
}
else
{
int32_t idx = node->idx + 1;
while ((node = node->parent))
{
if (node->items.size() > idx)
{
node = node->items[idx].get();
break;
}
if (group_names.size()) group_names.pop_back();
idx = node->idx + 1;
}
}
}
// Sort everything by display name (recursively)
root->SortByName();
// Model reset to invalidate all indexes
@ -194,7 +224,6 @@ QModelIndex C4ConsoleQtDefinitionListModel::parent(const QModelIndex &index) con
return createIndex(parent_node->idx, index.column(), parent_node);
}
QModelIndex C4ConsoleQtDefinitionListModel::GetModelIndexByItem(C4Def *def) const
{
// Just search tree

View File

@ -987,6 +987,14 @@ static C4Def * FnGetDefinition(C4PropList * _this, long iIndex)
return ::Definitions.GetDef(iIndex);
}
static C4String * FnGetDefinitionGroupPath(C4PropList * _this, C4ID id)
{
// Resolve definition
C4Def *def = C4Id2Def(id);
if (!def) return nullptr;
return ::Strings.RegString(def->ConsoleGroupPath.getData());
}
static C4Value FnGetBaseMaterial(C4PropList * _this, int iPlr, C4ID id, int iIndex, int dwCategory)
{
if (!ValidPlr(iPlr)) return C4VBool(false);
@ -2685,6 +2693,7 @@ void InitGameFunctionMap(C4AulScriptEngine *pEngine)
F(GetX);
F(GetY);
F(GetDefinition);
F(GetDefinitionGroupPath);
F(GetPlayerName);
F(GetPlayerType);
F(GetPlayerColor);

View File

@ -178,6 +178,7 @@ public:
~C4Def();
public:
char Filename[_MAX_FNAME+1];
StdCopyStrBuf ConsoleGroupPath; // file path as used in the definition list viewer in the console. Only initialized in editor mode.
int32_t Creation;
int32_t Count; // number of instanciations