Index: ps/trunk/binaries/data/mods/public/simulation/components/Barter.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/Barter.js +++ ps/trunk/binaries/data/mods/public/simulation/components/Barter.js @@ -47,20 +47,6 @@ return prices; }; -Barter.prototype.PlayerHasMarket = function(playerID) -{ - var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); - var entities = cmpRangeManager.GetEntitiesByPlayer(playerID); - for (var entity of entities) - { - var cmpFoundation = Engine.QueryInterface(entity, IID_Foundation); - var cmpIdentity = Engine.QueryInterface(entity, IID_Identity); - if (!cmpFoundation && cmpIdentity && cmpIdentity.HasClass("Barter")) - return true; - } - return false; -}; - Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resourceToBuy, amount) { if (amount <= 0) @@ -82,15 +68,11 @@ return; } - // This can occur when the player issues the order just before the market is destroyed or captured - if (!this.PlayerHasMarket(playerID)) - return; - if (amount != 100 && amount != 500) return; let cmpPlayer = QueryPlayerIDInterface(playerID); - if (!cmpPlayer) + if (!cmpPlayer || !cmpPlayer.CanBarter()) return; let prices = this.GetPrices(cmpPlayer); Index: ps/trunk/binaries/data/mods/public/simulation/components/GuiInterface.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/GuiInterface.js +++ ps/trunk/binaries/data/mods/public/simulation/components/GuiInterface.js @@ -124,7 +124,7 @@ "researchedTechs": cmpTechnologyManager ? cmpTechnologyManager.GetResearchedTechs() : null, "classCounts": cmpTechnologyManager ? cmpTechnologyManager.GetClassCounts() : null, "typeCountsByClass": cmpTechnologyManager ? cmpTechnologyManager.GetTypeCountsByClass() : null, - "canBarter": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).PlayerHasMarket(i), + "canBarter": cmpPlayer.CanBarter(), "barterPrices": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).GetPrices(cmpPlayer) }); } Index: ps/trunk/binaries/data/mods/public/simulation/components/Player.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/Player.js +++ ps/trunk/binaries/data/mods/public/simulation/components/Player.js @@ -71,6 +71,7 @@ this.disabledTechnologies = {}; this.startingTechnologies = []; this.spyCostMultiplier = +this.template.SpyCostMultiplier; + this.barterEntities = []; this.barterMultiplier = { "buy": clone(this.template.BarterMultiplier.Buy), "sell": clone(this.template.BarterMultiplier.Sell) @@ -215,6 +216,11 @@ return Math.round(ApplyValueModificationsToEntity("Player/MaxPopulation", this.maxPop, this.entity)); }; +Player.prototype.CanBarter = function() +{ + return this.barterEntities.length > 0; +}; + Player.prototype.GetBarterMultiplier = function() { return this.barterMultiplier; @@ -748,7 +754,6 @@ if (msg.from != this.playerID && msg.to != this.playerID) return; - let cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); let cmpCost = Engine.QueryInterface(msg.entity, IID_Cost); if (msg.from == this.playerID) @@ -756,20 +761,28 @@ if (cmpCost) this.popUsed -= cmpCost.GetPopCost(); - if (cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) - { - let index = this.panelEntities.indexOf(msg.entity); - if (index >= 0) - this.panelEntities.splice(index, 1); - } + let panelIndex = this.panelEntities.indexOf(msg.entity); + if (panelIndex >= 0) + this.panelEntities.splice(panelIndex, 1); + + let barterIndex = this.barterEntities.indexOf(msg.entity); + if (barterIndex >= 0) + this.barterEntities.splice(barterIndex, 1); } if (msg.to == this.playerID) { if (cmpCost) this.popUsed += cmpCost.GetPopCost(); - if (cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) + let cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); + if (!cmpIdentity) + return; + + if (MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) this.panelEntities.push(msg.entity); + + if (cmpIdentity.HasClass("Barter") && !Engine.QueryInterface(msg.entity, IID_Foundation)) + this.barterEntities.push(msg.entity); } }; Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Barter.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Barter.js +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Barter.js @@ -61,19 +61,10 @@ bought = amount; return true; }, + "CanBarter": () => true, "GetBarterMultiplier": () => multiplier }); -AddMock(SYSTEM_ENTITY, IID_RangeManager, { - "GetEntitiesByPlayer": (id) => id == playerID ? [62, 60, 61, 63] : [] -}); - -AddMock(60, IID_Identity, { - "HasClass": (cl) => true -}); - -AddMock(60, IID_Foundation, {}); - // GetPrices cmpBarter.priceDifferences = { "wood": 8, "stone": 0, "metal": 0 }; TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).buy, { @@ -100,15 +91,6 @@ }); multiplier.buy.stone = 1.0; -// PlayerHasMarket -TS_ASSERT(!cmpBarter.PlayerHasMarket(playerID)); - -AddMock(61, IID_Identity, { - "HasClass": (cl) => true -}); - -TS_ASSERT(cmpBarter.PlayerHasMarket(playerID)); - // ExchangeResources // Price differences magnitude are caped by 99 - CONSTANT_DIFFERENCE. // If we have a bigger DIFFERENCE_PER_DEAL than 99 - CONSTANT_DIFFERENCE at each deal we reach the cap. @@ -157,6 +139,7 @@ AddMock(playerEnt, IID_Player, { "TrySubtractResources": () => false, "AddResource": () => {}, + "CanBarter": () => true, "GetBarterMultiplier": () => multiplier }); cmpBarter.ExchangeResources(playerID, "wood", "stone", 100); Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js @@ -62,8 +62,7 @@ "buy": { "food": 150 }, "sell": { "food": 25 } }; - }, - "PlayerHasMarket": function() { return false; } + } }); AddMock(SYSTEM_ENTITY, IID_EndGameManager, { @@ -114,6 +113,7 @@ "IsEnemy": function() { return true; }, "GetDisabledTemplates": function() { return {}; }, "GetDisabledTechnologies": function() { return {}; }, + "CanBarter": function() { return false; }, "GetSpyCostMultiplier": function() { return 1; }, "HasSharedDropsites": function() { return false; }, "HasSharedLos": function() { return false; } @@ -199,6 +199,7 @@ "IsEnemy": function() { return false; }, "GetDisabledTemplates": function() { return {}; }, "GetDisabledTechnologies": function() { return {}; }, + "CanBarter": function() { return false; }, "GetSpyCostMultiplier": function() { return 1; }, "HasSharedDropsites": function() { return false; }, "HasSharedLos": function() { return false; } Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Player.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Player.js +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Player.js @@ -18,6 +18,8 @@ Engine.LoadHelperScript("ValueModification.js"); Engine.LoadComponentScript("interfaces/Player.js"); +Engine.LoadComponentScript("interfaces/Cost.js"); +Engine.LoadComponentScript("interfaces/Foundation.js"); Engine.LoadComponentScript("interfaces/ModifiersManager.js"); Engine.LoadComponentScript("Player.js"); @@ -37,6 +39,10 @@ }, }); +var playerID = 1; +cmpPlayer.SetPlayerID(playerID); +TS_ASSERT_EQUALS(cmpPlayer.GetPlayerID(), playerID); + TS_ASSERT_EQUALS(cmpPlayer.GetPopulationCount(), 0); TS_ASSERT_EQUALS(cmpPlayer.GetPopulationLimit(), 0); @@ -66,3 +72,29 @@ "metal": 1.0 } }); + +AddMock(60, IID_Identity, { + "GetClassesList": () => {}, + "HasClass": (cl) => true +}); +AddMock(60, IID_Ownership); +AddMock(60, IID_Foundation, {}); +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 60, "from": INVALID_PLAYER, "to": playerID }); +TS_ASSERT(!cmpPlayer.CanBarter()); + +AddMock(61, IID_Identity, { + "GetClassesList": () => {}, + "HasClass": (cl) => false +}); +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 61, "from": INVALID_PLAYER, "to": playerID }); +TS_ASSERT(!cmpPlayer.CanBarter()); + +AddMock(62, IID_Identity, { + "GetClassesList": () => {}, + "HasClass": (cl) => true +}); +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 62, "from": INVALID_PLAYER, "to": playerID }); +TS_ASSERT(cmpPlayer.CanBarter()); + +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 62, "from": playerID, "to": INVALID_PLAYER }); +TS_ASSERT(!cmpPlayer.CanBarter());