Ticket #1807: garrison-v2.diff
File garrison-v2.diff, 16.7 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/gui/session/utility_functions.js
62 62 "teamsLocked": playerState.teamsLocked, 63 63 "state": playerState.state, 64 64 "isAlly": playerState.isAlly, 65 "isMutualAlly": playerState.isMutualAlly, 65 66 "isNeutral": playerState.isNeutral, 66 67 "isEnemy": playerState.isEnemy, 67 68 "guid": undefined, // network guid for players controlled by hosts -
binaries/data/mods/public/gui/session/input.js
217 217 var playerState = simState.players[entState.player]; 218 218 var playerOwned = (targetState.player == entState.player); 219 219 var allyOwned = playerState.isAlly[targetState.player]; 220 var mutualAllyOwned = playerState.isMutualAlly[targetState.player]; 220 221 var enemyOwned = playerState.isEnemy[targetState.player]; 221 222 var gaiaOwned = (targetState.player == 0); 222 223 … … 225 226 226 227 // default to walking there 227 228 var data = {command: "walk"}; 228 if (targetState.garrisonHolder && playerOwned)229 if (targetState.garrisonHolder && (playerOwned || mutualAllyOwned)) 229 230 { 230 231 data.command = "garrison"; 231 232 data.target = target; … … 297 298 var playerState = simState.players[entState.player]; 298 299 var playerOwned = (targetState.player == entState.player); 299 300 var allyOwned = playerState.isAlly[targetState.player]; 301 var mutualAllyOwned = playerState.isMutualAlly[targetState.player]; 300 302 var neutralOwned = playerState.isNeutral[targetState.player]; 301 303 var enemyOwned = playerState.isEnemy[targetState.player]; 302 304 var gaiaOwned = (targetState.player == 0); … … 308 310 switch (action) 309 311 { 310 312 case "garrison": 311 if (hasClass(entState, "Unit") && targetState.garrisonHolder && playerOwned)313 if (hasClass(entState, "Unit") && targetState.garrisonHolder && (playerOwned || mutualAllyOwned)) 312 314 { 313 315 var allowedClasses = targetState.garrisonHolder.allowedClasses; 314 316 for each (var unitClass in entState.identity.classes) … … 1732 1734 var unitName = getEntityName(template); 1733 1735 1734 1736 var playerID = Engine.GetPlayerID(); 1737 var simState = Engine.GuiInterfaceCall("GetSimulationState"); 1738 1735 1739 if (entState.player == playerID || g_DevSettings.controlAll) 1736 1740 { 1737 1741 switch (commandName) … … 1780 1784 break; 1781 1785 } 1782 1786 } 1787 else if (simState.players[playerID].isMutualAlly[entState.player]) 1788 { 1789 switch (commandName) 1790 { 1791 case "garrison": 1792 inputState = INPUT_PRESELECTEDACTION; 1793 preSelectedAction = ACTION_GARRISON; 1794 break; 1795 default: 1796 break; 1797 } 1798 } 1783 1799 } 1784 1800 } 1785 1801 -
binaries/data/mods/public/gui/session/unit_commands.js
689 689 affordableMask.hidden = false; 690 690 affordableMask.sprite = "colour: 0 0 0 127"; 691 691 } 692 693 if (guiName == GARRISON) 694 { 695 var ents = garrisonGroups.getEntsByName(item); 696 var entplayer = GetEntityState(ents[0]).player; 697 button.sprite = "colour: " + g_Players[entplayer].color.r + " " + g_Players[entplayer].color.g + " " + g_Players[entplayer].color.b; 698 699 var player = Engine.GetPlayerID(); 700 if(player != unitEntState.player && !g_DevSettings.controlAll) 701 { 702 if (entplayer != player) 703 { 704 button.enabled = false; 705 grayscale = "grayscale:"; 706 } 707 } 708 } 692 709 693 710 icon.sprite = "stretched:" + grayscale + "session/portraits/" + template.icon; 694 711 … … 1003 1020 1004 1021 // If the selection is friendly units, add the command panels 1005 1022 var player = Engine.GetPlayerID(); 1023 // Get player state to check some constraints 1024 // e.g. presence of a hero or build limits 1025 var simState = Engine.GuiInterfaceCall("GetSimulationState"); 1026 var playerState = simState.players[player]; 1027 1006 1028 if (entState.player == player || g_DevSettings.controlAll) 1007 1029 { 1008 // Get player state to check some constraints1009 // e.g. presence of a hero or build limits1010 var simState = Engine.GuiInterfaceCall("GetSimulationState");1011 var playerState = simState.players[player];1012 1030 1013 1031 if (selection.length > 1) 1014 1032 setupUnitPanel(SELECTION, usedPanels, entState, playerState, g_Selection.groups.getTemplateNames(), … … 1024 1042 var groups = new EntityGroups(); 1025 1043 for (var i in selection) 1026 1044 { 1027 state = GetEntityState(selection[i]);1045 var state = GetEntityState(selection[i]); 1028 1046 if (state.garrisonHolder) 1029 1047 groups.add(state.garrisonHolder.entities) 1030 1048 } … … 1191 1209 supplementalDetailsPanel.hidden = false; 1192 1210 commandsPanel.hidden = false; 1193 1211 } 1212 else if (playerState.isMutualAlly[entState.player]) // owned by allied player 1213 { 1214 1215 if (entState.garrisonHolder) 1216 { 1217 var groups = new EntityGroups(); 1218 for (var i in selection) 1219 { 1220 var state = GetEntityState(selection[i]); 1221 if (state.garrisonHolder) 1222 groups.add(state.garrisonHolder.entities) 1223 } 1224 1225 setupUnitPanel(GARRISON, usedPanels, entState, playerState, groups.getTemplateNames(), 1226 function (item) { unloadTemplate(item); } ); 1227 1228 supplementalDetailsPanel.hidden = false; 1229 } 1230 else 1231 { 1232 supplementalDetailsPanel.hidden = true; 1233 } 1234 1235 commandsPanel.hidden = true; 1236 } 1194 1237 else // owned by another player 1195 1238 { 1196 1239 supplementalDetailsPanel.hidden = true; -
binaries/data/mods/public/simulation/helpers/Player.js
209 209 if (cmpOwnership) 210 210 owner = cmpOwnership.GetOwner(); 211 211 212 // Figure out which player controls the foundation being built212 // Figure out which player controls the target entity 213 213 var targetOwner = 0; 214 214 var cmpOwnershipTarget = Engine.QueryInterface(target, IID_Ownership); 215 215 if (cmpOwnershipTarget) … … 226 226 } 227 227 228 228 /** 229 * Returns true if the entity 'target' is owned by a mutual ally of 230 * the owner of 'entity'. 231 */ 232 function IsOwnedByMutualAllyOfEntity(entity, target) 233 { 234 // Figure out which player controls us 235 var owner = 0; 236 var cmpOwnership = Engine.QueryInterface(entity, IID_Ownership); 237 if (cmpOwnership) 238 owner = cmpOwnership.GetOwner(); 239 240 // Figure out which player controls the target entity 241 var targetOwner = 0; 242 var cmpOwnershipTarget = Engine.QueryInterface(target, IID_Ownership); 243 if (cmpOwnershipTarget) 244 targetOwner = cmpOwnershipTarget.GetOwner(); 245 246 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 247 var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(owner), IID_Player); 248 249 // Check for allied diplomacy status 250 if (cmpPlayer.IsMutualAlly(targetOwner)) 251 return true; 252 253 return false; 254 } 255 256 /** 229 257 * Returns true if the entity 'target' is owned by player 230 258 */ 231 259 function IsOwnedByPlayer(player, target) … … 264 292 } 265 293 266 294 /** 295 * Returns true if the entity 'target' is owned by a mutual ally of player 296 */ 297 function IsOwnedByMutualAllyOfPlayer(player, target) 298 { 299 var targetOwner = 0; 300 var cmpOwnershipTarget = Engine.QueryInterface(target, IID_Ownership); 301 if (cmpOwnershipTarget) 302 targetOwner = cmpOwnershipTarget.GetOwner(); 303 304 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 305 var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(player), IID_Player); 306 307 // Check for allied diplomacy status 308 if (cmpPlayer.IsMutualAlly(targetOwner)) 309 return true; 310 311 return false; 312 } 313 314 /** 267 315 * Returns true if the entity 'target' is owned by someone neutral to player 268 316 */ 269 317 function IsOwnedByNeutralOfPlayer(player,target) … … 307 355 Engine.RegisterGlobal("QueryOwnerInterface", QueryOwnerInterface); 308 356 Engine.RegisterGlobal("QueryPlayerIDInterface", QueryPlayerIDInterface); 309 357 Engine.RegisterGlobal("IsOwnedByAllyOfEntity", IsOwnedByAllyOfEntity); 358 Engine.RegisterGlobal("IsOwnedByMutualAllyOfEntity", IsOwnedByMutualAllyOfEntity); 310 359 Engine.RegisterGlobal("IsOwnedByPlayer", IsOwnedByPlayer); 311 360 Engine.RegisterGlobal("IsOwnedByGaia", IsOwnedByGaia); 312 361 Engine.RegisterGlobal("IsOwnedByAllyOfPlayer", IsOwnedByAllyOfPlayer); 362 Engine.RegisterGlobal("IsOwnedByMutualAllyOfPlayer", IsOwnedByMutualAllyOfPlayer); 313 363 Engine.RegisterGlobal("IsOwnedByNeutralOfPlayer", IsOwnedByNeutralOfPlayer); 314 364 Engine.RegisterGlobal("IsOwnedByEnemyOfPlayer", IsOwnedByEnemyOfPlayer); -
binaries/data/mods/public/simulation/helpers/Commands.js
314 314 break; 315 315 316 316 case "garrison": 317 // Verify that the building can be controlled by the player 318 if (CanControlUnit (cmd.target, player, controlAllUnits))317 // Verify that the building can be controlled by the player or is mutualAlly 318 if (CanControlUnitOrIsAlly(cmd.target, player, controlAllUnits)) 319 319 { 320 320 var entities = FilterEntityList(cmd.entities, player, controlAllUnits); 321 321 GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) { … … 336 336 break; 337 337 338 338 case "unload": 339 // Verify that the building can be controlled by the player 340 if (CanControlUnit (cmd.garrisonHolder, player, controlAllUnits))339 // Verify that the building can be controlled by the player or is mutualAlly 340 if (CanControlUnitOrIsAlly(cmd.garrisonHolder, player, controlAllUnits)) 341 341 { 342 342 var cmpGarrisonHolder = Engine.QueryInterface(cmd.garrisonHolder, IID_GarrisonHolder); 343 343 var notUngarrisoned = 0; 344 for each (ent in cmd.entities) 344 if (IsOwnedByPlayer(player, cmd.garrisonHolder)) 345 var entities = cmd.entities; 346 else 347 var entities = FilterEntityList(cmd.entities, player, controlAllUnits); 348 349 for each (var ent in entities) 345 350 if (!cmpGarrisonHolder || !cmpGarrisonHolder.Unload(ent)) 346 351 notUngarrisoned++; 347 352 … … 355 360 break; 356 361 357 362 case "unload-template": 358 var selected = FilterEntityList(cmd.garrisonHolders, player, controlAllUnits); 363 var selected = FilterEntityListWithAllies(cmd.garrisonHolders, player, controlAllUnits); 364 var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); 359 365 for each (var garrisonHolder in selected) 360 366 { 361 367 var cmpGarrisonHolder = Engine.QueryInterface(garrisonHolder, IID_GarrisonHolder); 362 if (!cmpGarrisonHolder || !cmpGarrisonHolder.UnloadTemplate(cmd.template, cmd.all)) 363 notifyUnloadFailure(player, garrisonHolder) 368 if (cmpGarrisonHolder) 369 { 370 // check that we are allowed to unload entities corresponding to this template 371 if (IsOwnedByPlayer(player, garrisonHolder)) 372 var entities = cmpGarrisonHolder.entities; 373 else 374 var entities = FilterEntityList(cmpGarrisonHolder.entities, player, controlAllUnits); 375 376 for each (var ent in entities) 377 { 378 // Units with multiple ranks are grouped together. 379 var cmpIdentity = Engine.QueryInterface(ent, IID_Identity); 380 var name = cmpIdentity.GetSelectionGroupName(); 381 if (!name) 382 name = cmpTemplateManager.GetCurrentTemplateName(ent); 383 if (name == cmd.template) 384 { 385 if (!cmpGarrisonHolder.UnloadTemplate(cmd.template, cmd.all)) 386 notifyUnloadFailure(player, garrisonHolder); 387 break; 388 } 389 } 390 } 364 391 } 365 392 break; 366 393 … … 1272 1299 1273 1300 /** 1274 1301 * Check if player can control this entity 1275 * returns: true if the entity is valid and owned by the player if1276 * or control all units is activated for the player, else false1302 * returns: true if the entity is valid and owned by the player 1303 * or control all units is activated, else false 1277 1304 */ 1278 1305 function CanControlUnit(entity, player, controlAll) 1279 1306 { … … 1281 1308 } 1282 1309 1283 1310 /** 1311 * Check if player can control this entity 1312 * returns: true if the entity is valid and owned by the player 1313 * or the entity is owned by an mutualAlly 1314 * or control all units is activated, else false 1315 */ 1316 function CanControlUnitOrIsAlly(entity, player, controlAll) 1317 { 1318 return (IsOwnedByPlayer(player, entity) || IsOwnedByMutualAllyOfPlayer(player, entity) || controlAll); 1319 } 1320 1321 /** 1284 1322 * Filter entities which the player can control 1285 1323 */ 1286 1324 function FilterEntityList(entities, player, controlAll) … … 1289 1327 } 1290 1328 1291 1329 /** 1330 * Filter entities which the player can control or are mutualAlly 1331 */ 1332 function FilterEntityListWithAllies(entities, player, controlAll) 1333 { 1334 return entities.filter(function(ent) { return CanControlUnitOrIsAlly(ent, player, controlAll);} ); 1335 } 1336 1337 /** 1292 1338 * Try to transform a wall to a gate 1293 1339 */ 1294 1340 function TryTransformWallToGate(ent, cmpPlayer, template) -
binaries/data/mods/public/simulation/components/UnitAI.js
4142 4142 if (!cmpGarrisonHolder) 4143 4143 return false; 4144 4144 4145 // Verify that the target is owned by this entity's player 4145 // Verify that the target is owned by this entity's player or is mutual allied 4146 4146 var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 4147 if (!cmpOwnership || ! IsOwnedByPlayer(cmpOwnership.GetOwner(), target))4147 if (!cmpOwnership || !(IsOwnedByPlayer(cmpOwnership.GetOwner(), target) || IsOwnedByMutualAllyOfPlayer(cmpOwnership.GetOwner(), target))) 4148 4148 return false; 4149 4149 4150 4150 // Don't let animals garrison for now -
binaries/data/mods/public/simulation/components/Player.js
334 334 335 335 var sharedLos = []; 336 336 for (var i = 0; i < cmpPlayerManager.GetNumPlayers(); ++i) 337 if (this.IsAlly(i)) 338 { 339 var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(i), IID_Player); 340 if (cmpPlayer && cmpPlayer.IsAlly(this.playerID)) 341 sharedLos.push(i); 342 } 337 if (this.IsMutualAlly(i)) 338 sharedLos.push(i); 343 339 344 340 cmpRangeManager.SetSharedLos(this.playerID, sharedLos); 345 341 }; … … 407 403 return this.diplomacy[id] > 0; 408 404 }; 409 405 406 /** 407 * Check if given player is our ally, and we are its ally 408 */ 409 Player.prototype.IsMutualAlly = function(id) 410 { 411 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 412 if (!cmpPlayerManager) 413 return false; 414 var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(id), IID_Player); 415 return this.IsAlly(id) && cmpPlayer && cmpPlayer.IsAlly(this.playerID); 416 }; 417 410 418 Player.prototype.SetEnemy = function(id) 411 419 { 412 420 this.SetDiplomacyIndex(id, -1); -
binaries/data/mods/public/simulation/components/GuiInterface.js
64 64 65 65 // store player ally/neutral/enemy data as arrays 66 66 var allies = []; 67 var mutualAllies = []; 67 68 var neutrals = []; 68 69 var enemies = []; 69 70 for (var j = 0; j < n; ++j) 70 71 { 71 72 allies[j] = cmpPlayer.IsAlly(j); 73 mutualAllies[j] = cmpPlayer.IsMutualAlly(j); 72 74 neutrals[j] = cmpPlayer.IsNeutral(j); 73 75 enemies[j] = cmpPlayer.IsEnemy(j); 74 76 } … … 86 88 "teamsLocked": cmpPlayer.GetLockTeams(), 87 89 "phase": phase, 88 90 "isAlly": allies, 91 "isMutualAlly": mutualAllies, 89 92 "isNeutral": neutrals, 90 93 "isEnemy": enemies, 91 94 "entityLimits": cmpPlayerEntityLimits.GetLimits(), -
binaries/data/mods/public/simulation/components/GarrisonHolder.js
469 469 } 470 470 }; 471 471 472 473 /** 474 * Eject all foreign garrisoned entities which are no more allied 475 */ 476 GarrisonHolder.prototype.OnDiplomacyChanged = function() 477 { 478 for (var i = this.entities.length; i > 0; --i) 479 { 480 if (!IsOwnedByMutualAllyOfEntity(this.entity, this.entities[i-1])) 481 this.Eject(this.entities[i-1], true); 482 } 483 this.UpdateGarrisonFlag(); 484 }; 485 472 486 Engine.RegisterComponentType(IID_GarrisonHolder, "GarrisonHolder", GarrisonHolder); 473 487