Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/Mirage.js
const VIS_HIDDEN = 0; | const VIS_HIDDEN = 0; | ||||
const VIS_FOGGED = 1; | const VIS_FOGGED = 1; | ||||
const VIS_VISIBLE = 2; | const VIS_VISIBLE = 2; | ||||
function Mirage() {} | function Mirage() {} | ||||
Mirage.prototype.Schema = | Mirage.prototype.Schema = | ||||
"<a:help>Mirage entities replace real entities in the fog-of-war.</a:help>" + | "<a:help>Mirage entities replace real entities in the fog-of-war.</a:help>" + | ||||
"<empty/>"; | "<empty/>"; | ||||
Mirage.prototype.Init = function() | Mirage.prototype.Init = function() | ||||
{ | { | ||||
this.parent = INVALID_ENTITY; | |||||
this.player = null; | this.player = null; | ||||
this.parent = INVALID_ENTITY; | |||||
this.miragedIids = new Set(); | |||||
this.miragedIids = new Map(); | this.classesList = []; | ||||
this.numBuilders = 0; | |||||
this.buildTime = {}; | |||||
this.maxHitpoints = null; | |||||
this.hitpoints = null; | |||||
this.repairable = null; | |||||
this.unhealable = null; | |||||
this.injured = null; | |||||
this.capturePoints = []; | |||||
this.maxCapturePoints = 0; | |||||
this.maxAmount = null; | |||||
this.amount = null; | |||||
this.type = null; | |||||
this.isInfinite = null; | |||||
this.killBeforeGather = null; | |||||
this.maxGatherers = null; | |||||
this.numGatherers = null; | |||||
this.traders = null; | |||||
this.marketType = null; | |||||
this.internationalBonus = null; | |||||
}; | }; | ||||
Mirage.prototype.SetParent = function(ent) | Mirage.prototype.SetParent = function(ent) | ||||
{ | { | ||||
this.parent = ent; | this.parent = ent; | ||||
}; | }; | ||||
Mirage.prototype.GetParent = function() | Mirage.prototype.GetParent = function() | ||||
Show All 11 Lines | Mirage.prototype.GetPlayer = function() | ||||
return this.player; | return this.player; | ||||
}; | }; | ||||
Mirage.prototype.Mirages = function(iid) | Mirage.prototype.Mirages = function(iid) | ||||
{ | { | ||||
return this.miragedIids.has(iid); | return this.miragedIids.has(iid); | ||||
}; | }; | ||||
Mirage.prototype.Get = function(iid) | |||||
{ | |||||
return this.miragedIids.get(iid); | |||||
}; | |||||
// ============================ | // ============================ | ||||
// Parent entity data | // Parent entity data | ||||
function MiragedIdentity(cmpIdentity) | Mirage.prototype.CopyIdentity = function(cmpIdentity) | ||||
{ | { | ||||
this.miragedIids.add(IID_Identity); | |||||
// Mirages don't get identity classes via the template-filter, so that code can query | // Mirages don't get identity classes via the template-filter, so that code can query | ||||
// identity components via Engine.QueryInterface without having to explicitly check for mirages. | // identity components via Engine.QueryInterface without having to explicitly check for mirages. | ||||
// This is cloned as otherwise we get a reference to Identity's property, | // This is cloned as otherwise we get a reference to Identity's property, | ||||
// and that array is deleted when serializing (as it's not seralized), which ends in OOS. | // and that array is deleted when serializing (as it's not seralized), which ends in OOS. | ||||
this.classes = clone(cmpIdentity.GetClassesList()); | this.classesList = clone(cmpIdentity.GetClassesList()); | ||||
}; | }; | ||||
MiragedIdentity.prototype.GetClassesList = function() { return this.classes; }; | Mirage.prototype.GetClassesList = function() { return this.classesList; }; | ||||
Mirage.prototype.CopyIdentity = function(cmpIdentity) | |||||
{ | |||||
this.miragedIids.set(IID_Identity, new MiragedIdentity(cmpIdentity)); | |||||
}; | |||||
// Foundation data | // Foundation data | ||||
function MiragedFoundation(cmpFoundation) | Mirage.prototype.CopyFoundation = function(cmpFoundation) | ||||
{ | { | ||||
this.miragedIids.add(IID_Foundation); | |||||
this.numBuilders = cmpFoundation.GetNumBuilders(); | this.numBuilders = cmpFoundation.GetNumBuilders(); | ||||
this.buildTime = cmpFoundation.GetBuildTime(); | this.buildTime = cmpFoundation.GetBuildTime(); | ||||
}; | }; | ||||
MiragedFoundation.prototype.GetNumBuilders = function() { return this.numBuilders; }; | Mirage.prototype.GetNumBuilders = function() { return this.numBuilders; }; | ||||
MiragedFoundation.prototype.GetBuildTime = function() { return this.buildTime; }; | Mirage.prototype.GetBuildTime = function() { return this.buildTime; }; | ||||
Mirage.prototype.CopyFoundation = function(cmpFoundation) | |||||
{ | |||||
this.miragedIids.set(IID_Foundation, new MiragedFoundation(cmpFoundation)); | |||||
}; | |||||
// Repairable data | // Repairable data (numBuilders and buildTime shared with foundation as entities can't have both) | ||||
function MiragedRepairable(cmpRepairable) | Mirage.prototype.CopyRepairable = function(cmpRepairable) | ||||
{ | { | ||||
this.miragedIids.add(IID_Repairable); | |||||
this.numBuilders = cmpRepairable.GetNumBuilders(); | this.numBuilders = cmpRepairable.GetNumBuilders(); | ||||
this.buildTime = cmpRepairable.GetBuildTime(); | this.buildTime = cmpRepairable.GetBuildTime(); | ||||
}; | }; | ||||
MiragedRepairable.prototype.GetNumBuilders = function() { return this.numBuilders; }; | |||||
MiragedRepairable.prototype.GetBuildTime = function() { return this.buildTime; }; | |||||
Mirage.prototype.CopyRepairable = function(cmpRepairable) | |||||
{ | |||||
this.miragedIids.set(IID_Repairable, new MiragedRepairable(cmpRepairable)); | |||||
}; | |||||
// Health data | // Health data | ||||
function MiragedHealth(cmpHealth) | Mirage.prototype.CopyHealth = function(cmpHealth) | ||||
{ | { | ||||
this.miragedIids.add(IID_Health); | |||||
this.maxHitpoints = cmpHealth.GetMaxHitpoints(); | this.maxHitpoints = cmpHealth.GetMaxHitpoints(); | ||||
this.hitpoints = cmpHealth.GetHitpoints(); | this.hitpoints = cmpHealth.GetHitpoints(); | ||||
this.repairable = cmpHealth.IsRepairable(); | this.repairable = cmpHealth.IsRepairable(); | ||||
this.injured = cmpHealth.IsInjured(); | this.injured = cmpHealth.IsInjured(); | ||||
this.unhealable = cmpHealth.IsUnhealable(); | this.unhealable = cmpHealth.IsUnhealable(); | ||||
}; | }; | ||||
MiragedHealth.prototype.GetMaxHitpoints = function() { return this.maxHitpoints; }; | Mirage.prototype.GetMaxHitpoints = function() { return this.maxHitpoints; }; | ||||
MiragedHealth.prototype.GetHitpoints = function() { return this.hitpoints; }; | Mirage.prototype.GetHitpoints = function() { return this.hitpoints; }; | ||||
MiragedHealth.prototype.IsRepairable = function() { return this.repairable; }; | Mirage.prototype.IsRepairable = function() { return this.repairable; }; | ||||
MiragedHealth.prototype.IsInjured = function() { return this.injured; }; | Mirage.prototype.IsInjured = function() { return this.injured; }; | ||||
MiragedHealth.prototype.IsUnhealable = function() { return this.unhealable; }; | Mirage.prototype.IsUnhealable = function() { return this.unhealable; }; | ||||
Mirage.prototype.CopyHealth = function(cmpHealth) | |||||
{ | |||||
this.miragedIids.set(IID_Health, new MiragedHealth(cmpHealth)); | |||||
}; | |||||
// Capture data | // Capture data | ||||
function MiragedCapture(cmpCapturable) | Mirage.prototype.CopyCapturable = function(cmpCapturable) | ||||
{ | { | ||||
this.miragedIids.add(IID_Capturable); | |||||
this.capturePoints = clone(cmpCapturable.GetCapturePoints()); | this.capturePoints = clone(cmpCapturable.GetCapturePoints()); | ||||
this.maxCapturePoints = cmpCapturable.GetMaxCapturePoints(); | this.maxCapturePoints = cmpCapturable.GetMaxCapturePoints(); | ||||
this.CanCapture = cmpCapturable.CanCapture; | |||||
}; | }; | ||||
MiragedCapture.prototype.GetCapturePoints = function() { return this.capturePoints; }; | Mirage.prototype.GetMaxCapturePoints = function() { return this.maxCapturePoints; }; | ||||
MiragedCapture.prototype.GetMaxCapturePoints = function() { return this.maxCapturePoints; }; | Mirage.prototype.GetCapturePoints = function() { return this.capturePoints; }; | ||||
Mirage.prototype.CopyCapturable = function(cmpCapturable) | Mirage.prototype.CanCapture = Capturable.prototype.CanCapture; | ||||
{ | |||||
this.miragedIids.set(IID_Capturable, new MiragedCapture(cmpCapturable)); | |||||
}; | |||||
// ResourceSupply data | // ResourceSupply data | ||||
function MiragedResourceSupply(cmpResourceSupply) | |||||
Mirage.prototype.CopyResourceSupply = function(cmpResourceSupply) | |||||
{ | { | ||||
this.miragedIids.add(IID_ResourceSupply); | |||||
this.maxAmount = cmpResourceSupply.GetMaxAmount(); | this.maxAmount = cmpResourceSupply.GetMaxAmount(); | ||||
this.amount = cmpResourceSupply.GetCurrentAmount(); | this.amount = cmpResourceSupply.GetCurrentAmount(); | ||||
this.type = cmpResourceSupply.GetType(); | this.type = cmpResourceSupply.GetType(); | ||||
this.isInfinite = cmpResourceSupply.IsInfinite(); | this.isInfinite = cmpResourceSupply.IsInfinite(); | ||||
this.killBeforeGather = cmpResourceSupply.GetKillBeforeGather(); | this.killBeforeGather = cmpResourceSupply.GetKillBeforeGather(); | ||||
this.maxGatherers = cmpResourceSupply.GetMaxGatherers(); | this.maxGatherers = cmpResourceSupply.GetMaxGatherers(); | ||||
this.numGatherers = cmpResourceSupply.GetNumGatherers(); | this.numGatherers = cmpResourceSupply.GetNumGatherers(); | ||||
}; | }; | ||||
MiragedResourceSupply.prototype.GetMaxAmount = function() { return this.maxAmount; }; | Mirage.prototype.GetMaxAmount = function() { return this.maxAmount; }; | ||||
MiragedResourceSupply.prototype.GetCurrentAmount = function() { return this.amount; }; | Mirage.prototype.GetCurrentAmount = function() { return this.amount; }; | ||||
MiragedResourceSupply.prototype.GetType = function() { return this.type; }; | Mirage.prototype.GetType = function() { return this.type; }; | ||||
MiragedResourceSupply.prototype.IsInfinite = function() { return this.isInfinite; }; | Mirage.prototype.IsInfinite = function() { return this.isInfinite; }; | ||||
MiragedResourceSupply.prototype.GetKillBeforeGather = function() { return this.killBeforeGather; }; | Mirage.prototype.GetKillBeforeGather = function() { return this.killBeforeGather; }; | ||||
MiragedResourceSupply.prototype.GetMaxGatherers = function() { return this.maxGatherers; }; | Mirage.prototype.GetMaxGatherers = function() { return this.maxGatherers; }; | ||||
MiragedResourceSupply.prototype.GetNumGatherers = function() { return this.numGatherers; }; | Mirage.prototype.GetNumGatherers = function() { return this.numGatherers; }; | ||||
Mirage.prototype.CopyResourceSupply = function(cmpResourceSupply) | |||||
{ | |||||
this.miragedIids.set(IID_ResourceSupply, new MiragedResourceSupply(cmpResourceSupply)); | |||||
}; | |||||
// Market data | // Market data | ||||
function MiragedMarket(cmpMarket, entity, parent, player) | |||||
{ | |||||
this.entity = entity; | |||||
this.parent = parent; | |||||
this.player = player; | |||||
Mirage.prototype.CopyMarket = function(cmpMarket) | |||||
{ | |||||
this.miragedIids.add(IID_Market); | |||||
this.traders = new Set(); | this.traders = new Set(); | ||||
for (let trader of cmpMarket.GetTraders()) | for (let trader of cmpMarket.GetTraders()) | ||||
{ | { | ||||
let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | ||||
let cmpOwnership = Engine.QueryInterface(trader, IID_Ownership); | let cmpOwnership = Engine.QueryInterface(trader, IID_Ownership); | ||||
if (!cmpTrader || !cmpOwnership) | if (!cmpTrader || !cmpOwnership) | ||||
{ | { | ||||
cmpMarket.RemoveTrader(trader); | cmpMarket.RemoveTrader(trader); | ||||
continue; | continue; | ||||
} | } | ||||
if (this.player != cmpOwnership.GetOwner()) | if (this.player != cmpOwnership.GetOwner()) | ||||
continue; | continue; | ||||
cmpTrader.SwitchMarket(cmpMarket.entity, this.entity); | cmpTrader.SwitchMarket(cmpMarket.entity, this.entity); | ||||
cmpMarket.RemoveTrader(trader); | cmpMarket.RemoveTrader(trader); | ||||
this.AddTrader(trader); | this.AddTrader(trader); | ||||
} | } | ||||
this.marketType = cmpMarket.GetType(); | this.marketType = cmpMarket.GetType(); | ||||
this.internationalBonus = cmpMarket.GetInternationalBonus(); | this.internationalBonus = cmpMarket.GetInternationalBonus(); | ||||
}; | }; | ||||
MiragedMarket.prototype.HasType = function(type) { return this.marketType.has(type); }; | Mirage.prototype.HasType = function(type) { return this.marketType.has(type); }; | ||||
MiragedMarket.prototype.GetInternationalBonus = function() { return this.internationalBonus; }; | Mirage.prototype.GetInternationalBonus = function() { return this.internationalBonus; }; | ||||
MiragedMarket.prototype.AddTrader = function(trader) { this.traders.add(trader); }; | Mirage.prototype.AddTrader = function(trader) { this.traders.add(trader); }; | ||||
MiragedMarket.prototype.RemoveTrader = function(trader) { this.traders.delete(trader); }; | Mirage.prototype.RemoveTrader = function(trader) { this.traders.delete(trader); }; | ||||
MiragedMarket.prototype.UpdateTraders = function(msg) | Mirage.prototype.UpdateTraders = function(msg) | ||||
{ | { | ||||
let cmpMarket = Engine.QueryInterface(this.parent, IID_Market); | let cmpMarket = Engine.QueryInterface(this.parent, IID_Market); | ||||
if (!cmpMarket) // The parent market does not exist anymore | if (!cmpMarket) // The parent market does not exist anymore | ||||
{ | { | ||||
for (let trader of this.traders) | for (let trader of this.traders) | ||||
{ | { | ||||
let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | ||||
if (cmpTrader) | if (cmpTrader) | ||||
Show All 9 Lines | for (let trader of this.traders) | ||||
if (!cmpTrader) | if (!cmpTrader) | ||||
continue; | continue; | ||||
cmpTrader.SwitchMarket(this.entity, cmpMarket.entity); | cmpTrader.SwitchMarket(this.entity, cmpMarket.entity); | ||||
this.RemoveTrader(trader); | this.RemoveTrader(trader); | ||||
cmpMarket.AddTrader(trader); | cmpMarket.AddTrader(trader); | ||||
} | } | ||||
}; | }; | ||||
Mirage.prototype.CopyMarket = function(cmpMarket) | |||||
{ | |||||
this.miragedIids.set(IID_Market, new MiragedMarket(cmpMarket, this.entity, this.parent, this.player)); | |||||
}; | |||||
// ============================ | // ============================ | ||||
Mirage.prototype.OnVisibilityChanged = function(msg) | Mirage.prototype.OnVisibilityChanged = function(msg) | ||||
{ | { | ||||
// Mirages get VIS_HIDDEN when the original entity becomes VIS_VISIBLE. | // Mirages get VIS_HIDDEN when the original entity becomes VIS_VISIBLE. | ||||
if (msg.player != this.player || msg.newVisibility != VIS_HIDDEN) | if (msg.player != this.player || msg.newVisibility != VIS_HIDDEN) | ||||
return; | return; | ||||
if (this.miragedIids.has(IID_Market)) | if (this.miragedIids.has(IID_Market)) | ||||
this.miragedIids.get(IID_Market).UpdateTraders(msg); | this.UpdateTraders(msg); | ||||
if (this.parent == INVALID_ENTITY) | if (this.parent == INVALID_ENTITY) | ||||
Engine.DestroyEntity(this.entity); | Engine.DestroyEntity(this.entity); | ||||
else | else | ||||
Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": this.parent }); | Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": this.parent }); | ||||
}; | }; | ||||
Engine.RegisterComponentType(IID_Mirage, "Mirage", Mirage); | Engine.RegisterComponentType(IID_Mirage, "Mirage", Mirage); |
Wildfire Games · Phabricator