Fixed bandboxed entity preference code from relying on parsing template names

New rules for preferences:
 units
 defensive
 others

This was SVN commit r7857.
This commit is contained in:
WhiteTreePaladin 2010-08-06 00:29:58 +00:00
parent e2a736fc5a
commit 4f2d9ee128
12 changed files with 110 additions and 88 deletions

View File

@ -198,6 +198,38 @@ function tryPlaceBuilding(queued)
return true;
}
// Limits bandboxed selections to certain types of entities based on priority
function getPreferredEntities(ents)
{
var entStateList = [];
var preferredEnts = [];
// Get list of ent states
for (var i = 0; i < ents.length; i++)
{
var entState = Engine.GuiInterfaceCall("GetEntityState", ents[i]);
if (!entState)
continue;
entStateList.push(entState);
}
// Check if there are units in the selection
for (var i = 0; i < ents.length; i++)
if (isUnit(entStateList[i]))
preferredEnts.push(ents[i]);
if (!preferredEnts.length)
{
// Check if there are defensive entities in the selection
for (var i = 0; i < ents.length; i++)
if (isDefensive(entStateList[i]))
preferredEnts.push(ents[i]);
}
return preferredEnts;
}
function handleInputBeforeGui(ev)
{
// Capture mouse position so we can use it for displaying cursors,
@ -264,54 +296,12 @@ function handleInputBeforeGui(ev)
var bandbox = getGUIObjectByName("bandbox");
bandbox.hidden = true;
// Get list of entities limited to preferred entities
var ents = Engine.PickFriendlyEntitiesInRect(x0, y0, x1, y1, Engine.GetPlayerID());
var chosenEnts = [];
var templateNames = [];
// Check if there are units or animals in the selection and collect a list of template names
for (var i = 0; i < ents.length; i++)
{
var entState = Engine.GuiInterfaceCall("GetEntityState", ents[i]);
if (!entState)
continue;
templateNames.push(entState.template);
if (isUnit(templateNames[i]) || isAnimal(templateNames[i]))
chosenEnts.push(ents[i]);
}
var preferredEntities = getPreferredEntities(ents)
// If nothing was added to the list, then there were no units or animals
if (!chosenEnts.length)
{
var towers = [];
var structures = [];
var structureTemplateName;
for (var i = 0; i < ents.length; i++)
{
if (isTower(templateNames[i]))
{
towers.push(ents[i]);
}
else if (!towers.length && isStructure(templateNames[i]))
{
if (!structureTemplateName)
structureTemplateName = templateNames[i];
if (structureTemplateName == templateNames[i])
structures.push(ents[i]);
}
}
// Choose towers over other structures, and choose structures over other things
if (towers.length)
chosenEnts = towers;
else if (structures.length)
chosenEnts = structures;
}
// Set new entity list
if (chosenEnts.length)
ents = chosenEnts;
if (preferredEntities.length)
ents = preferredEntities;
// Remove entities if selection is too large
if (ents.length > MAX_SELECTION_SIZE)

View File

@ -37,7 +37,7 @@ function displayGeneralInfo(playerState, entState, template)
var iconTooltip = "";
// Rank Icon
// var rankId = getRankCellId(entState.template);
// var rankId = getRankCellId(entState);
// getGUIObjectByName("sdRankIcon").cell_id = rankId;
// Rank Title

View File

@ -180,32 +180,29 @@ function toTitleCase(string)
return string;
}
function isUnit(templateName)
function isUnit(entState)
{
if (templateName.substring(0, templateName.search("/")) == "units")
return true;
if (entState.identity)
{
var classes = entState.identity.classes;
if (classes && classes.length)
for (var i = 0; i < classes.length; i++)
if ((classes[i] == "Organic") || (classes[i] == "Mechanical"))
return true;
}
return false;
}
function isAnimal(templateName)
function isDefensive(entState)
{
if (templateName.substring(0, templateName.search("_")) == "gaia/fauna")
return true;
return false;
}
function isStructure(templateName)
{
if (templateName.substring(0, templateName.search("/")) == "structures")
return true;
return false;
}
function isTower(templateName)
{
if (templateName.length > 5)
if (templateName.substring(templateName.length-5, templateName.length) == "tower")
return true;
if (entState.identity)
{
var classes = entState.identity.classes;
if (classes && classes.length)
for (var i = 0; i < classes.length; i++)
if (classes[i] == "Defensive")
return true;
}
return false;
}
@ -247,16 +244,27 @@ function getCommandCellId(commandName)
}
}
function getRankCellId(templateName)
function getEntityCommandsList(entState)
{
var endsWith = templateName.substring(templateName.length-2, templateName.length);
if (isUnit(templateName))
var commands = [];
commands.push("delete");
return commands;
}
function getRankCellId(entState)
{
if (entState.template)
{
if (endsWith == "_e")
return 0;
else if (endsWith == "_a")
return 1;
var templateName = entState.template;
var endsWith = templateName.substring(templateName.length-2, templateName.length);
if (isUnit(entState))
{
if (endsWith == "_e")
return 0;
else if (endsWith == "_a")
return 1;
}
}
return -1;
}

View File

@ -66,7 +66,7 @@ function setupUnitPanel(guiName, usedPanels, unitEntState, items, callback)
continue;
var tooltip = getEntityName(template);
tooltip += "[font=\"serif-bold-16\"]" + getRankTitle(getRankCellId(entState.template)) + "[/font]";
tooltip += "[font=\"serif-bold-16\"]" + getRankTitle(getRankCellId(entState)) + "[/font]";
// Hitpoints
if (entState.maxHitpoints != undefined)
@ -192,15 +192,14 @@ function updateUnitCommands(entState, commandsPanel, selection)
function (item) { removeFromTrainingQueue(entState.id, item.id); } );
/*
// HACK: This should be referenced in the entities template, rather than completely faked here
// HACK: displays all command buttons
var commands = [];
for (var i = 0; i < 15; i++)
commands.push("test"+i);
commands[4] = "delete";
*/
// Needs to have list provided by the entity
var commands = ["delete"];
var commands = getEntityCommandsList(entState);
if (commands.length)
setupUnitPanel("Command", usedPanels, entState, commands,
function (item) { performCommand(entState.id, item); } );

View File

@ -54,6 +54,14 @@ GuiInterface.prototype.GetEntityState = function(player, ent)
"template": template
}
var cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
if (cmpIdentity)
{
ret.identity = {
"classes": cmpIdentity.GetClassesList(),
};
}
var cmpPosition = Engine.QueryInterface(ent, IID_Position);
if (cmpPosition)
{

View File

@ -51,20 +51,22 @@ Identity.prototype.Schema =
"<list>" +
"<zeroOrMore>" +
"<choice>" +
"<value>Organic</value>" +
"<value>Foot</value>" +
"<value>Mounted</value>" +
"<value>Mechanical</value>" +
"<value>Organic</value>" +
"<value>Super</value>" +
"<value>Hero</value>" +
"<value>Civic</value>" +
"<value>Economic</value>" +
"<value>City</value>" +
"<value>Defensive</value>" +
"<value>Village</value>" +
"<value>Town</value>" +
"<value>City</value>" +
"<value a:help='Primary weapon type'>Bow</value>" + // TODO: what are these used for?
"<value a:help='Primary weapon type'>Javelin</value>" +
"<value a:help='Primary weapon type'>Spear</value>" +
"<value a:help='Primary weapon type'>Sword</value>" +
"<value>Hero</value>" +
"</choice>" +
"</zeroOrMore>" +
"</list>" +
@ -91,4 +93,17 @@ Identity.prototype.GetCiv = function()
return this.template.Civ;
};
Identity.prototype.GetClassesList = function()
{
if (this.template.Classes)
{
var string = this.template.Classes._string;
return string.split(/\s+/);
}
else
{
return [];
}
};
Engine.RegisterComponentType(IID_Identity, "Identity", Identity);

View File

@ -4,7 +4,7 @@
<GenericName>Civic Centre</GenericName>
<IconCell>47</IconCell>
<Tooltip>The heart of the player's empire. Trains Citizen-Soldiers and is used to capture territories by being built on settlements.</Tooltip>
<Classes datatype="tokens">Village</Classes>
<Classes datatype="tokens">Village Defensive</Classes>
</Identity>
<BuildRestrictions>
<PlacementType>settlement</PlacementType>

View File

@ -4,7 +4,7 @@
<GenericName>Outpost</GenericName>
<IconCell>53</IconCell>
<Tooltip>Provides reconnaissance and defense against attackers when garrisoned.</Tooltip>
<Classes datatype="tokens">Village</Classes>
<Classes datatype="tokens">Village Defensive</Classes>
</Identity>
<BuildRestrictions>
<Category>ScoutTower</Category>

View File

@ -4,7 +4,7 @@
<GenericName>Tower</GenericName>
<IconCell>55</IconCell>
<Tooltip>Towers can be garrisoned to defend a city wall against attackers.</Tooltip>
<Classes datatype="tokens">Village</Classes>
<Classes datatype="tokens">Village Defensive</Classes>
</Identity>
<Cost>
<Resources>

View File

@ -4,7 +4,7 @@
<GenericName>Fortress</GenericName>
<IconCell>61</IconCell>
<Tooltip>Trains Heroes and Siege Units. Researches some Special Technologies.</Tooltip>
<Classes datatype="tokens">City</Classes>
<Classes datatype="tokens">City Defensive</Classes>
</Identity>
<BuildRestrictions>
<Category>Fortress</Category>

View File

@ -2,6 +2,7 @@
<Entity parent="template_unit">
<Identity>
<GenericName>Fauna</GenericName>
<Classes datatype="tokens">Organic</Classes>
<IconSheet>snPortraitSheetAnimalGaia</IconSheet>
<IconCell>0</IconCell>
</Identity>

View File

@ -2,6 +2,7 @@
<Entity parent="template_unit">
<Identity>
<GenericName>Super Unit</GenericName>
<Classes datatype="tokens">Super Organic</Classes>
</Identity>
<Loot>
<xp>150</xp>