Petra: improve performance of initialization in naval maps

This was SVN commit r16817.
This commit is contained in:
mimo 2015-06-25 18:19:12 +00:00
parent e1a34eb4ea
commit 0b8597fd2d

View File

@ -107,6 +107,19 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
} }
} }
// load units and buildings from the config files
let civ = gameState.civ();
if (civ in this.Config.buildings.naval)
this.bNaval = this.Config.buildings.naval[civ];
else
this.bNaval = this.Config.buildings.naval['default'];
for (let i in this.bNaval)
this.bNaval[i] = gameState.applyCiv(this.bNaval[i]);
if (deserializing)
return;
// determination of the possible landing zones // determination of the possible landing zones
var width = gameState.getMap().width; var width = gameState.getMap().width;
var length = width * gameState.getMap().height; var length = width * gameState.getMap().height;
@ -129,54 +142,34 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
{ {
for (let sea in this.landingZones[land]) for (let sea in this.landingZones[land])
{ {
let nbmax = 0; let landing = this.landingZones[land][sea];
for (let i = 0; i < this.landingZones[land][sea].length; i++) let nbaround = {};
let nbcut = 0;
for (let i = 0; i < landing.length; i++)
{ {
let j = this.landingZones[land][sea][i]; let j = landing[i];
let nb = 0; let nb = 0;
if (this.landingZones[land][sea].indexOf(j-1) !== -1) if (i > 0 && landing[i-1] == j-1)
nb++; nb++;
if (this.landingZones[land][sea].indexOf(j+1) !== -1) if (i < landing.length-1 && landing[i+1] == j+1)
nb++; nb++;
if (this.landingZones[land][sea].indexOf(j+width) !== -1) if (landing.indexOf(j+width) != -1)
nb++; nb++;
if (this.landingZones[land][sea].indexOf(j-width) !== -1) if (landing.indexOf(j-width) != -1)
nb++; nb++;
if (nb > nbmax) nbaround[j] = nb;
nbmax = nb; nbcut = Math.max(nb, nbcut);
} }
let nbcut = Math.min(2, nbmax); nbcut = Math.min(2, nbcut);
for (let i = 0; i < this.landingZones[land][sea].length; i++) for (let i = 0; i < landing.length; i++)
{ {
let j = this.landingZones[land][sea][i]; let j = landing[i];
let nb = 0; if (nbaround[j] < nbcut)
if (this.landingZones[land][sea].indexOf(j-1) !== -1) landing.splice(i--, 1);
nb++;
if (this.landingZones[land][sea].indexOf(j+1) !== -1)
nb++;
if (this.landingZones[land][sea].indexOf(j+width) !== -1)
nb++;
if (this.landingZones[land][sea].indexOf(j-width) !== -1)
nb++;
if (nb < nbcut)
this.landingZones[land][sea].splice(i--, 1);
} }
} }
} }
// load units and buildings from the config files
let civ = gameState.civ();
if (civ in this.Config.buildings.naval)
this.bNaval = this.Config.buildings.naval[civ];
else
this.bNaval = this.Config.buildings.naval['default'];
for (let i in this.bNaval)
this.bNaval[i] = gameState.applyCiv(this.bNaval[i]);
if (deserializing)
return;
// Assign our initial docks and ships // Assign our initial docks and ships
for (let ship of this.ships.values()) for (let ship of this.ships.values())
this.setShipIndex(gameState, ship); this.setShipIndex(gameState, ship);