ai cleanup, mainly var -> let

This was SVN commit r18258.
This commit is contained in:
mimo 2016-05-29 12:26:41 +00:00
parent f1610ce4fa
commit 3d6c48e357
4 changed files with 69 additions and 64 deletions

View File

@ -11,15 +11,15 @@ m.warn = function(output)
m.VectorDistance = function(a, b)
{
var dx = a[0] - b[0];
var dz = a[1] - b[1];
let dx = a[0] - b[0];
let dz = a[1] - b[1];
return Math.sqrt(dx*dx + dz*dz);
};
m.SquareVectorDistance = function(a, b)
{
var dx = a[0] - b[0];
var dz = a[1] - b[1];
let dx = a[0] - b[0];
let dz = a[1] - b[1];
return dx*dx + dz*dz;
};
@ -31,22 +31,22 @@ m.inRange = function(a, b, range)// checks for X distance
if (a === undefined || b === undefined || range === undefined)
return undefined;
var dx = a[0] - b[0];
var dz = a[1] - b[1];
let dx = a[0] - b[0];
let dz = a[1] - b[1];
return dx*dx + dz*dz < range;
};
// slower than SquareVectorDistance, faster than VectorDistance but not exactly accurate.
m.ManhattanDistance = function(a, b)
{
var dx = a[0] - b[0];
var dz = a[1] - b[1];
let dx = a[0] - b[0];
let dz = a[1] - b[1];
return Math.abs(dx) + Math.abs(dz);
};
m.AssocArraytoArray = function(assocArray) {
var endArray = [];
for (var i in assocArray)
let endArray = [];
for (let i in assocArray)
endArray.push(assocArray[i]);
return endArray;
};
@ -59,46 +59,52 @@ m.PickRandom = function(list)
// Utility functions for conversions of maps of different sizes
// It expects that cell size of map 1 is a multiple of cell size of map 2
// return the index of map2 with max content from indices contained inside the cell i of map1
/**
*return the index of map2 with max content from indices contained inside the cell i of map1
* map1.cellSize must be a multiple of map2.cellSize
*/
m.getMaxMapIndex = function(i, map1, map2)
{
var ratio = map1.cellSize / map2.cellSize;
var ix = (i % map1.width) * ratio;
var iy = Math.floor(i / map1.width) * ratio;
var index;
for (var kx = 0; kx < ratio; ++kx)
for (var ky = 0; ky < ratio; ++ky)
let ratio = map1.cellSize / map2.cellSize;
let ix = (i % map1.width) * ratio;
let iy = Math.floor(i / map1.width) * ratio;
let index;
for (let kx = 0; kx < ratio; ++kx)
for (let ky = 0; ky < ratio; ++ky)
if (!index || map2.map[ix+kx+(iy+ky)*map2.width] > map2.map[index])
index = ix+kx+(iy+ky)*map2.width;
return index;
};
// return the list of indices of map2 contained inside the cell i of map1
// map1.cellSize must be a multiple of map2.cellSize
/**
* return the list of indices of map2 contained inside the cell i of map1
* map1.cellSize must be a multiple of map2.cellSize
*/
m.getMapIndices = function(i, map1, map2)
{
var ratio = map1.cellSize / map2.cellSize; // TODO check that this is integer >= 1 ?
var ix = (i % map1.width) * ratio;
var iy = Math.floor(i / map1.width) * ratio;
var ret = [];
for (var kx = 0; kx < ratio; ++kx)
for (var ky = 0; ky < ratio; ++ky)
let ratio = map1.cellSize / map2.cellSize; // TODO check that this is integer >= 1 ?
let ix = (i % map1.width) * ratio;
let iy = Math.floor(i / map1.width) * ratio;
let ret = [];
for (let kx = 0; kx < ratio; ++kx)
for (let ky = 0; ky < ratio; ++ky)
ret.push(ix+kx+(iy+ky)*map2.width);
return ret;
};
// return the list of points of map2 contained inside the cell i of map1
// map1.cellSize must be a multiple of map2.cellSize
/**
* return the list of points of map2 contained inside the cell i of map1
* map1.cellSize must be a multiple of map2.cellSize
*/
m.getMapPoints = function(i, map1, map2)
{
var ratio = map1.cellSize / map2.cellSize; // TODO check that this is integer >= 1 ?
var ix = (i % map1.width) * ratio;
var iy = Math.floor(i / map1.width) * ratio;
var ret = [];
for (var kx = 0; kx < ratio; ++kx)
for (var ky = 0; ky < ratio; ++ky)
let ratio = map1.cellSize / map2.cellSize; // TODO check that this is integer >= 1 ?
let ix = (i % map1.width) * ratio;
let iy = Math.floor(i / map1.width) * ratio;
let ret = [];
for (let kx = 0; kx < ratio; ++kx)
for (let ky = 0; ky < ratio; ++ky)
ret.push([ix+kx, iy+ky]);
return ret;
};

View File

@ -964,7 +964,7 @@ m.AttackPlan.prototype.checkTargetObstruction = function(gameState, target, posi
{
if (!struct.position() || !struct.get("Obstruction") || struct.hasClass("Field"))
continue;
// we consider that we can reach the target, but nenetheless check that we did not cross any enemy gate
// we consider that we can reach the target, but nonetheless check that we did not cross any enemy gate
if (dist < radius + 10 && !struct.hasClass("Gates"))
continue;
// Check that we are really blocked by this structure, i.e. advancing by 1+0.8(clearance)m

View File

@ -41,7 +41,7 @@ AIInterface.prototype.Init = function()
AIInterface.prototype.Serialize = function()
{
var state = {};
let state = {};
for (var key in this)
{
if (!this.hasOwnProperty(key))
@ -57,7 +57,7 @@ AIInterface.prototype.Serialize = function()
AIInterface.prototype.Deserialize = function(data)
{
for (var key in data)
for (let key in data)
{
if (!data.hasOwnProperty(key))
continue;
@ -74,7 +74,7 @@ AIInterface.prototype.Deserialize = function(data)
AIInterface.prototype.Disable = function()
{
this.enabled = false;
var nop = function(){};
let nop = function(){};
this.ChangedEntity = nop;
this.PushEvent = nop;
this.OnGlobalPlayerDefeated = nop;
@ -86,10 +86,10 @@ AIInterface.prototype.Disable = function()
AIInterface.prototype.GetNonEntityRepresentation = function()
{
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
let cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
// Return the same game state as the GUI uses
var state = cmpGuiInterface.GetSimulationState(-1);
let state = cmpGuiInterface.GetSimulationState(-1);
// Add some extra AI-specific data
// add custom events and reset them for the next turn
@ -105,16 +105,16 @@ AIInterface.prototype.GetNonEntityRepresentation = function()
AIInterface.prototype.GetRepresentation = function()
{
var state = this.GetNonEntityRepresentation();
let state = this.GetNonEntityRepresentation();
// Add entity representations
Engine.ProfileStart("proxy representations");
state.entities = {};
for (var id in this.changedEntities)
for (let id in this.changedEntities)
{
var aiProxy = Engine.QueryInterface(+id, IID_AIProxy);
if (aiProxy)
state.entities[id] = aiProxy.GetRepresentation();
let cmpAIProxy = Engine.QueryInterface(+id, IID_AIProxy);
if (cmpAIProxy)
state.entities[id] = cmpAIProxy.GetRepresentation();
}
this.changedEntities = {};
Engine.ProfileStop();
@ -132,7 +132,7 @@ AIInterface.prototype.GetRepresentation = function()
*/
AIInterface.prototype.GetFullRepresentation = function(flushEvents)
{
var state = this.GetNonEntityRepresentation();
let state = this.GetNonEntityRepresentation();
if (flushEvents)
for (let name of this.EventNames)
@ -183,8 +183,7 @@ AIInterface.prototype.OnGlobalPlayerDefeated = function(msg)
AIInterface.prototype.OnGlobalEntityRenamed = function(msg)
{
var cmpMirage = Engine.QueryInterface(msg.entity, IID_Mirage);
if (!cmpMirage)
if (!Engine.QueryInterface(msg.entity, IID_Mirage))
this.events.EntityRenamed.push(msg);
};

View File

@ -47,7 +47,7 @@ AIProxy.prototype.Deserialize = function ()
AIProxy.prototype.GetRepresentation = function()
{
// Return the full representation the first time we're called
var ret;
let ret;
if (this.needsFullGet)
ret = this.GetFullRepresentation();
else
@ -138,7 +138,7 @@ AIProxy.prototype.OnProductionQueueChanged = function(msg)
{
if (!this.NotifyChange())
return;
var cmpProductionQueue = Engine.QueryInterface(this.entity, IID_ProductionQueue);
let cmpProductionQueue = Engine.QueryInterface(this.entity, IID_ProductionQueue);
this.changes.trainingQueue = cmpProductionQueue.GetQueue();
};
@ -147,7 +147,7 @@ AIProxy.prototype.OnGarrisonedUnitsChanged = function(msg)
if (!this.NotifyChange())
return;
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
this.changes.garrisoned = cmpGarrisonHolder.GetEntities();
// Send a message telling a unit garrisoned or ungarrisoned.
@ -206,15 +206,15 @@ AIProxy.prototype.OnTerritoryDecayChanged = function(msg)
AIProxy.prototype.GetFullRepresentation = function()
{
this.needsFullGet = false;
var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
var ret = {
let ret = {
// These properties are constant and won't need to be updated
"id": this.entity,
"template": cmpTemplateManager.GetCurrentTemplateName(this.entity)
};
var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
if (cmpPosition)
{
// Updated by OnPositionChanged
@ -232,21 +232,21 @@ AIProxy.prototype.GetFullRepresentation = function()
}
}
var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
let cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
if (cmpHealth)
{
// Updated by OnHealthChanged
ret.hitpoints = cmpHealth.GetHitpoints();
}
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
if (cmpOwnership)
{
// Updated by OnOwnershipChanged
ret.owner = cmpOwnership.GetOwner();
}
var cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI);
let cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI);
if (cmpUnitAI)
{
// Updated by OnUnitIdleChanged
@ -257,21 +257,21 @@ AIProxy.prototype.GetFullRepresentation = function()
ret.unitAIOrderData = cmpUnitAI.GetOrderData();
}
var cmpProductionQueue = Engine.QueryInterface(this.entity, IID_ProductionQueue);
let cmpProductionQueue = Engine.QueryInterface(this.entity, IID_ProductionQueue);
if (cmpProductionQueue)
{
// Updated by OnProductionQueueChanged
ret.trainingQueue = cmpProductionQueue.GetQueue();
}
var cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation);
let cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation);
if (cmpFoundation)
{
// Updated by OnFoundationProgressChanged
ret.foundationProgress = cmpFoundation.GetBuildPercentage();
}
var cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply);
let cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply);
if (cmpResourceSupply)
{
// Updated by OnResourceSupplyChanged
@ -279,25 +279,25 @@ AIProxy.prototype.GetFullRepresentation = function()
ret.resourceSupplyNumGatherers = cmpResourceSupply.GetNumGatherers();
}
var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
if (cmpResourceGatherer)
{
// Updated by OnResourceCarryingChanged
ret.resourceCarrying = cmpResourceGatherer.GetCarryingStatus();
}
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
if (cmpGarrisonHolder)
{
// Updated by OnGarrisonedUnitsChanged
ret.garrisoned = cmpGarrisonHolder.GetEntities();
}
var cmpTerritoryDecay = Engine.QueryInterface(this.entity, IID_TerritoryDecay);
let cmpTerritoryDecay = Engine.QueryInterface(this.entity, IID_TerritoryDecay);
if (cmpTerritoryDecay)
ret.decaying = cmpTerritoryDecay.IsDecaying();
var cmpCapturable = Engine.QueryInterface(this.entity, IID_Capturable);
let cmpCapturable = Engine.QueryInterface(this.entity, IID_Capturable);
if (cmpCapturable)
ret.capturePoints = cmpCapturable.GetCapturePoints();