1
0
forked from 0ad/0ad

Remove trailing whitespace and simplify some length checks. Refs #252.

This was SVN commit r18214.
This commit is contained in:
elexis 2016-05-21 16:20:27 +00:00
parent a2bd82d171
commit 4b5f8dfca6
17 changed files with 91 additions and 92 deletions

View File

@ -30,7 +30,7 @@ function GetVisibleIdentityClasses(template)
* Check if the classes given in the identity template
* match a list of classes
* @param classes List of the classes to check against
* @param match Either a string in the form
* @param match Either a string in the form
* "Class1 Class2+Class3"
* where spaces are handled as OR and '+'-signs as AND,
* and ! is handled as NOT, thus Class1+!Class2 = Class1 AND NOT Class2
@ -253,7 +253,7 @@ function GetTemplateDataHelper(template, player, auraTemplates)
"GainMultiplier": func("Trader/GainMultiplier", +template.Trader.GainMultiplier, player, template)
};
}
if (template.WallSet)
{
ret.wallSet = {
@ -284,7 +284,7 @@ function GetTechnologyDataHelper(template, civ)
{
var ret = {};
// Get specific name for this civ or else the generic specific name
// Get specific name for this civ or else the generic specific name
var specific = undefined;
if (template.specificName)
{

View File

@ -488,7 +488,7 @@ function getPreferredEntities(ents)
for (var i = 0; i < filters.length; ++i)
{
preferredEnts = ents.filter(filters[i]);
if (preferredEnts.length > 0)
if (preferredEnts.length)
break;
}
return preferredEnts;

View File

@ -1,12 +1,12 @@
/**
* List of different actions units can execute,
* List of different actions units can execute,
* this is mostly used to determine which actions can be executed
*
* "execute" is meant to send the command to the engine
*
* The next functions will always return false
* The next functions will always return false
* in case you have to continue to seek
* (i.e. look at the next entity for getActionInfo, the next
* (i.e. look at the next entity for getActionInfo, the next
* possible action for the actionCheck ...)
* They will return an object when the searching is finished
*
@ -24,10 +24,10 @@
*
* "specificness" is used to determine how specific an action is,
* The lower the number, the more specific an action is, and the bigger
* the chance of selecting that action when multiple actions are possible
* the chance of selecting that action when multiple actions are possible
*/
var unitActions =
var unitActions =
{
"move":
{
@ -56,7 +56,7 @@ var unitActions =
"specificness": 12,
},
"attack-move":
"attack-move":
{
"execute": function(target, action, selection, queued)
{
@ -87,7 +87,7 @@ var unitActions =
"specificness": 30,
},
"capture":
"capture":
{
"execute": function(target, action, selection, queued)
{
@ -139,7 +139,7 @@ var unitActions =
"specificness": 10,
},
"heal":
"heal":
{
"execute": function(target, action, selection, queued)
{
@ -159,7 +159,7 @@ var unitActions =
// Healers can't heal themselves.
if (entState.id == targetState.id)
return false;
var unhealableClasses = entState.healer.unhealableClasses;
if (MatchesClassList(targetState.identity.classes, unhealableClasses))
return false;
@ -179,7 +179,7 @@ var unitActions =
"specificness": 7,
},
"build":
"build":
{
"execute": function(target, action, selection, queued)
{
@ -202,7 +202,7 @@ var unitActions =
"specificness": 3,
},
"repair":
"repair":
{
"execute": function(target, action, selection, queued)
{
@ -233,7 +233,7 @@ var unitActions =
"specificness": 11,
},
"gather":
"gather":
{
"execute": function(target, action, selection, queued)
{
@ -260,7 +260,7 @@ var unitActions =
"specificness": 1,
},
"returnresource":
"returnresource":
{
"execute": function(target, action, selection, queued)
{
@ -297,7 +297,7 @@ var unitActions =
"specificness": 2,
},
"setup-trade-route":
"setup-trade-route":
{
"execute": function(target, action, selection, queued)
{
@ -362,7 +362,7 @@ var unitActions =
"specificness": 0,
},
"garrison":
"garrison":
{
"execute": function(target, action, selection, queued)
{
@ -409,7 +409,7 @@ var unitActions =
"specificness": 20,
},
"guard":
"guard":
{
"execute": function(target, action, selection, queued)
{
@ -446,7 +446,7 @@ var unitActions =
"specificness": 40,
},
"remove-guard":
"remove-guard":
{
"execute": function(target, action, selection, queued)
{
@ -471,11 +471,11 @@ var unitActions =
"specificness": 41,
},
"set-rallypoint":
"set-rallypoint":
{
"execute": function(target, action, selection, queued)
{
// if there is a position set in the action then use this so that when setting a
// if there is a position set in the action then use this so that when setting a
// rally point on an entity it is centered on that entity
if (action.position)
target = action.position;
@ -584,7 +584,7 @@ var unitActions =
}
return {"possible": true, "data": data, "position": targetState.position, "cursor": cursor, "tooltip": tooltip};
},
"actionCheck": function(target, selection)
{
@ -604,7 +604,7 @@ var unitActions =
});
if (!haveRallyPoints)
return false;
var actionInfo = getActionInfo("set-rallypoint", target);
if (!actionInfo.possible)
return false;
@ -613,7 +613,7 @@ var unitActions =
"specificness": 6,
},
"unset-rallypoint":
"unset-rallypoint":
{
"execute": function(target, action, selection, queued)
{
@ -654,7 +654,7 @@ var unitActions =
"specificness": 11,
},
"none":
"none":
{
"execute": function(target, action, selection, queued)
{
@ -668,7 +668,7 @@ var unitActions =
* Info and actions for the entity commands
* Currently displayed in the bottom of the central panel
*/
var g_EntityCommands =
var g_EntityCommands =
{
// Unload
"unload-all": {
@ -712,7 +712,7 @@ var g_EntityCommands =
"tooltip": translate("You cannot destroy this entity as you own less than half the capture points"),
"icon": "kill_small_disabled.png"
};
return {
"tooltip": translate("Delete"),
@ -728,7 +728,7 @@ var g_EntityCommands =
return;
var selection = g_Selection.toList();
if (selection.length < 1)
if (!selection.length)
return;
if (!entState.resourceSupply || !entState.resourceSupply.killBeforeGather || g_DevSettings.controlAll)
openDeleteDialog(selection);
@ -748,7 +748,7 @@ var g_EntityCommands =
"execute": function(entState)
{
var selection = g_Selection.toList();
if (selection.length > 0)
if (selection.length)
stopUnits(selection);
},
},
@ -824,7 +824,7 @@ var g_EntityCommands =
focusTarget = entState.rallyPoint.position;
else if (entState.position)
focusTarget = entState.position;
if (focusTarget)
Engine.CameraMoveTo(focusTarget.x, focusTarget.z);
},
@ -944,13 +944,13 @@ var g_AllyEntityCommands =
{
var selectedEntState = GetEntityState(ent);
if (selectedEntState.garrisonHolder)
{
{
var player = Engine.GetPlayerID();
for (var entity of selectedEntState.garrisonHolder.entities)
{
var state = GetEntityState(entity);
if (state.player == player)
count++;
++count;
}
}
}

View File

@ -111,7 +111,7 @@ function getTradingTooltip(gain)
function getEntityOrHolder(ent)
{
var entState = GetEntityState(ent);
if (entState && !entState.position && entState.unitAI && entState.unitAI.orders.length > 0 &&
if (entState && !entState.position && entState.unitAI && entState.unitAI.orders.length &&
(entState.unitAI.orders[0].type == "Garrison" || entState.unitAI.orders[0].type == "Autogarrison"))
return entState.unitAI.orders[0].data.target;

View File

@ -208,7 +208,7 @@ function loadTechnology(techName)
break;
case "all":
if (req[0].length < 1)
if (!req[0].length)
tech.reqs.generic = req[1];
else
for (let r of req[0])

View File

@ -57,7 +57,7 @@ m.Template = m.Class({
return undefined;
return GetIdentityClasses(template);
},
requiredTech: function() {
return this.get("Identity/RequiredTechnology");
},
@ -375,7 +375,7 @@ m.Template = m.Class({
return +this.get("ResourceSupply/MaxGatherers");
return 0;
},
resourceGatherRates: function() {
if (!this.get("ResourceGatherer"))
return undefined;
@ -478,7 +478,7 @@ m.Template = m.Class({
return undefined;
return this.get("BuildRestrictions/Category");
},
buildTime: function() {
if (!this.get("Cost") || !this.get("Cost/BuildTime"))
return undefined;
@ -702,7 +702,7 @@ m.Entity = m.Class({
resourceCarrying: function() {
if (this._entity.resourceCarrying === undefined)
return undefined;
return this._entity.resourceCarrying;
return this._entity.resourceCarrying;
},
currentGatherRate: function() {

View File

@ -10,7 +10,7 @@ m.EntityCollection = function(sharedAI, entities = new Map(), filters = [])
for (var filter of this._filters)
if (filter.dynamicProperties.length)
this.dynamicProp = this.dynamicProp.concat(filter.dynamicProperties);
Object.defineProperty(this, "length", {
get: function () {
return this._entities.size;
@ -83,12 +83,12 @@ m.EntityCollection.prototype.filter = function(filter, thisp)
{
if (typeof filter === "function")
filter = {"func": filter, "dynamicProperties": []};
var ret = new Map();
for (let [id, ent] of this._entities)
if (filter.func.call(thisp, ent, id, this))
ret.set(id, ent);
return new m.EntityCollection(this._ai, ret, this._filters.concat([filter]));
};

View File

@ -88,7 +88,7 @@ m.AttackPlan = function(gameState, Config, uniqueID, type, data)
}
}
this.paused = false;
this.maxCompletingTime = 0;
this.maxCompletingTime = 0;
// priority of the queues we'll create.
var priority = 70;
@ -598,7 +598,7 @@ m.AttackPlan.prototype.assignUnits = function(gameState)
continue;
ent.setMetadata(PlayerID, "plan", plan);
this.unitCollection.updateEnt(ent);
added = true;
added = true;
}
return added;
}

View File

@ -16,7 +16,7 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID)
{
// we'll assume this defender is ours already.
// we'll also override any previous assignment
var ent = gameState.getEntityById(entID);
if (!ent || !ent.position())
return false;
@ -27,14 +27,14 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID)
var idMin;
var distMin;
var idMinAll;
var distMinAll;
var distMinAll;
for (let id of this.foeEntities)
{
let eEnt = gameState.getEntityById(id);
if (!eEnt || !eEnt.position()) // probably can't happen.
continue;
if (eEnt.hasClass("Unit") && eEnt.unitAIOrderData() && eEnt.unitAIOrderData().length &&
if (eEnt.hasClass("Unit") && eEnt.unitAIOrderData() && eEnt.unitAIOrderData().length &&
eEnt.unitAIOrderData()[0].target && eEnt.unitAIOrderData()[0].target == entID)
{ // being attacked >>> target the unit
idMin = id;
@ -87,10 +87,10 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID)
// TODO: this should return cleverer results ("needs anti-elephant"…)
m.DefenseArmy.prototype.needsDefenders = function (gameState)
{
// some preliminary checks because we don't update for tech so entStrength removed can be > entStrength added
// some preliminary checks because we don't update for tech so entStrength removed can be > entStrength added
if (this.foeStrength <= 0 || this.ownStrength <= 0)
this.recalculateStrengths(gameState);
if (this.foeStrength * this.defenseRatio <= this.ownStrength)
return false;
return this.foeStrength * this.defenseRatio - this.ownStrength;

View File

@ -108,9 +108,9 @@ m.returnResources = function(gameState, ent)
distmin = dist;
closestDropsite = dropsite;
});
if (!closestDropsite)
return false;
return false;
ent.returnResources(closestDropsite);
return true;
};
@ -177,7 +177,7 @@ m.getHolder = function(gameState, ent)
};
/**
* return true if it is not worth finishing this building (it would surely decay)
* return true if it is not worth finishing this building (it would surely decay)
* TODO implement the other conditions
*/
m.isNotWorthBuilding = function(gameState, ent)

View File

@ -36,7 +36,7 @@ m.ResearchPlan.prototype.start = function(gameState)
// Prefer training buildings with short queues
// (TODO: this should also account for units added to the queue by
// plans that have already been executed this turn)
if (trainers.length > 0)
if (trainers.length)
{
trainers.sort((a, b) => a.trainingQueueTime() - b.trainingQueueTime());
// drop anything in the queue if we rush it.

View File

@ -53,7 +53,7 @@ m.TrainingPlan.prototype.start = function(gameState)
// Prefer training buildings with short queues
// (TODO: this should also account for units added to the queue by
// plans that have already been executed this turn)
if (trainersColl.length > 0)
if (trainersColl.length)
{
let trainers = trainersColl.toEntityArray();
let wantedIndex;

View File

@ -51,9 +51,8 @@ m.TutorialAI.prototype.OnUpdate = function() {
return;
}
if (this.events.length > 0){
if (this.events.length)
this.savedEvents = this.savedEvents.concat(this.events);
}
Engine.ProfileStart("tutorialBot");

View File

@ -1,6 +1,6 @@
function Attack() {}
Attack.prototype.bonusesSchema =
Attack.prototype.bonusesSchema =
"<optional>" +
"<element name='Bonuses'>" +
"<zeroOrMore>" +
@ -253,7 +253,7 @@ Attack.prototype.CanAttack = function(target)
let heightDiff = Math.abs(cmpThisPosition.GetHeightOffset() - cmpTargetPosition.GetHeightOffset());
const cmpIdentity = Engine.QueryInterface(target, IID_Identity);
if (!cmpIdentity)
if (!cmpIdentity)
return undefined;
const targetClasses = cmpIdentity.GetClassesList();
@ -294,7 +294,7 @@ Attack.prototype.CanAttack = function(target)
Attack.prototype.GetPreference = function(target)
{
const cmpIdentity = Engine.QueryInterface(target, IID_Identity);
if (!cmpIdentity)
if (!cmpIdentity)
return undefined;
const targetClasses = cmpIdentity.GetClassesList();
@ -350,14 +350,14 @@ Attack.prototype.GetBestAttackAgainst = function(target, allowCapture)
}
let cmpIdentity = Engine.QueryInterface(target, IID_Identity);
if (!cmpIdentity)
if (!cmpIdentity)
return undefined;
let targetClasses = cmpIdentity.GetClassesList();
let isTargetClass = function (className) { return targetClasses.indexOf(className) != -1; };
// Always slaughter domestic animals instead of using a normal attack
if (isTargetClass("Domestic") && this.template.Slaughter)
if (isTargetClass("Domestic") && this.template.Slaughter)
return "Slaughter";
let attack = this;
@ -535,9 +535,9 @@ Attack.prototype.PerformAttack = function(type, target)
let horizDistance = targetPosition.horizDistanceTo(selfPosition);
// This is an approximation of the time ot the target, it assumes that the target has a constant radial
// velocity, but since units move in straight lines this is not true. The exact value would be more
// difficult to calculate and I think this is sufficiently accurate. (I tested and for cavalry it was
// This is an approximation of the time ot the target, it assumes that the target has a constant radial
// velocity, but since units move in straight lines this is not true. The exact value would be more
// difficult to calculate and I think this is sufficiently accurate. (I tested and for cavalry it was
// about 5% of the units radius out in the worst case)
let timeToTarget = horizDistance / (horizSpeed - radialSpeed);

View File

@ -54,7 +54,7 @@ GarrisonHolder.prototype.Schema =
/**
* Initialize GarrisonHolder Component
*
* Garrisoning when loading a map is set in the script of the map, by setting initGarrison
* Garrisoning when loading a map is set in the script of the map, by setting initGarrison
* which should contain the array of garrisoned entities
*/
GarrisonHolder.prototype.Init = function()
@ -111,7 +111,7 @@ GarrisonHolder.prototype.GetEntities = function()
/**
* Returns an array of unit classes which can be garrisoned inside this
* particualar entity. Obtained from the entity's template
* particualar entity. Obtained from the entity's template
*/
GarrisonHolder.prototype.GetAllowedClasses = function()
{
@ -146,7 +146,7 @@ GarrisonHolder.prototype.GetHealRate = function()
* Set this entity to allow or disallow garrisoning in
* Every component calling this function should do it with its own ID, and as long as one
* component doesn't allow this entity to garrison, it can't be garrisoned
* When this entity already contains garrisoned soldiers,
* When this entity already contains garrisoned soldiers,
* these will not be able to ungarrison until the flag is set to true again.
*
* This more useful for modern-day features. For example you can't garrison or ungarrison
@ -158,7 +158,7 @@ GarrisonHolder.prototype.AllowGarrisoning = function(allow, callerID)
};
/**
* Check if no component of this entity blocks garrisoning
* Check if no component of this entity blocks garrisoning
* (f.e. because the vehicle is moving too fast)
*/
GarrisonHolder.prototype.IsGarrisoningAllowed = function()
@ -389,7 +389,7 @@ GarrisonHolder.prototype.OrderWalkToRallyPoint = function(entities)
* Ejects units and orders them to move to the Rally Point.
* Returns true if successful, false if not
* If an ejection with a given obstruction radius has failed, we won't try anymore to eject
* entities with a bigger obstruction as that is compelled to also fail
* entities with a bigger obstruction as that is compelled to also fail
*/
GarrisonHolder.prototype.PerformEject = function(entities, forced)
{
@ -449,7 +449,7 @@ GarrisonHolder.prototype.Unload = function(entity, forced)
* Unload one or all units that match a template and owner from
* the garrisoning entity and order them to move to the Rally Point
* Returns true if successful, false if not
*
*
* extendedTemplate has the format "p"+ownerid+"&"+template
*/
GarrisonHolder.prototype.UnloadTemplate = function(extendedTemplate, all, forced)

View File

@ -2738,7 +2738,7 @@ UnitAI.prototype.UnitFsmSpec = {
let dropsiteTypes = cmpResourceDropsite.GetTypes();
cmpResourceGatherer.CommitResources(dropsiteTypes);
this.SetGathererAnimationOverride();
}
}
// We finished building it.
// Switch to the next order (if any)
@ -3450,7 +3450,7 @@ UnitAI.prototype.UpdateRangeQueries = function()
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
if (this.losRangeQuery)
this.SetupRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losRangeQuery));
if (this.IsHealer() && this.losHealRangeQuery)
this.SetupHealRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losHealRangeQuery));
};

View File

@ -109,7 +109,7 @@ function TestFormationExiting(mode)
unitAI.SetupRangeQuery(1);
if (mode == 1)
if (mode == 1)
{
AddMock(enemy, IID_Health, {
GetHitpoints: function() { return 10; },
@ -117,7 +117,7 @@ function TestFormationExiting(mode)
AddMock(enemy, IID_UnitAI, {
IsAnimal: function() { return false; }
});
}
}
else if (mode == 2)
AddMock(enemy, IID_Health, {
GetHitpoints: function() { return 0; },
@ -210,19 +210,19 @@ function TestMoveIntoFormationWhileAttacking()
// create units
for (var i = 0; i < unitCount; i++) {
units.push(unit + i);
var unitAI = ConstructComponent(unit + i, "UnitAI", { "FormationController": "false", "DefaultStance": "aggressive" });
AddMock(unit + i, IID_Identity, {
GetClassesList: function() { return []; },
});
AddMock(unit + i, IID_Ownership, {
GetOwner: function() { return 1; },
});
AddMock(unit + i, IID_Position, {
GetTurretParent: function() { return INVALID_ENTITY; },
GetPosition: function() { return new Vector3D(); },
@ -230,7 +230,7 @@ function TestMoveIntoFormationWhileAttacking()
GetRotation: function() { return { "y": 0 }; },
IsInWorld: function() { return true; },
});
AddMock(unit + i, IID_UnitMotion, {
GetWalkSpeed: function() { return 1; },
MoveToFormationOffset: function(target, x, z) { },
@ -239,11 +239,11 @@ function TestMoveIntoFormationWhileAttacking()
StopMoving: function() { },
GetPassabilityClassName: function() { return "default"; },
});
AddMock(unit + i, IID_Vision, {
GetRange: function() { return 10; },
});
AddMock(unit + i, IID_Attack, {
GetRange: function() { return {"max":10, "min": 0}; },
GetFullAttackRange: function() { return { "max": 40, "min": 0}; },
@ -252,14 +252,14 @@ function TestMoveIntoFormationWhileAttacking()
CanAttack: function(v) { return true; },
CompareEntitiesByPreference: function(a, b) { return 0; },
});
unitAI.OnCreate();
unitAI.SetupRangeQuery(1);
unitAIs.push(unitAI);
}
// create enemy
AddMock(enemy, IID_Health, {
GetHitpoints: function() { return 40; },
@ -292,15 +292,15 @@ function TestMoveIntoFormationWhileAttacking()
controllerAI.OnCreate();
controllerFormation.SetMembers(units);
controllerAI.Attack(enemy, []);
for each (var ent in unitAIs) {
TS_ASSERT_EQUALS(unitAI.fsmStateName, "INDIVIDUAL.COMBAT.ATTACKING");
}
controllerAI.MoveIntoFormation({"name": "Circle"});
// let all units be in position
for each (var ent in unitAIs) {
controllerFormation.SetInPosition(ent);
@ -309,7 +309,7 @@ function TestMoveIntoFormationWhileAttacking()
for each (var ent in unitAIs) {
TS_ASSERT_EQUALS(unitAI.fsmStateName, "INDIVIDUAL.COMBAT.ATTACKING");
}
controllerFormation.Disband();
}