Ticket #1807: garrison-v3.diff
File garrison-v3.diff, 18.6 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/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": 363 var selected = FilterEntityListWithAllies(cmd.garrisonHolders, player, controlAllUnits); 364 var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); 365 for each (var garrisonHolder in selected) 366 { 367 var cmpGarrisonHolder = Engine.QueryInterface(garrisonHolder, IID_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 } 391 } 392 break; 393 394 case "unload-all-own": 358 395 var selected = FilterEntityList(cmd.garrisonHolders, player, controlAllUnits); 359 396 for each (var garrisonHolder in selected) 360 397 { 361 398 var cmpGarrisonHolder = Engine.QueryInterface(garrisonHolder, IID_GarrisonHolder); 362 if (!cmpGarrisonHolder || !cmpGarrisonHolder.Unload Template(cmd.template, cmd.all))399 if (!cmpGarrisonHolder || !cmpGarrisonHolder.UnloadAllOwn()) 363 400 notifyUnloadFailure(player, garrisonHolder) 364 401 } 365 402 break; … … 1272 1309 1273 1310 /** 1274 1311 * 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 false1312 * returns: true if the entity is valid and owned by the player 1313 * or control all units is activated, else false 1277 1314 */ 1278 1315 function CanControlUnit(entity, player, controlAll) 1279 1316 { … … 1281 1318 } 1282 1319 1283 1320 /** 1321 * Check if player can control this entity 1322 * returns: true if the entity is valid and owned by the player 1323 * or the entity is owned by an mutualAlly 1324 * or control all units is activated, else false 1325 */ 1326 function CanControlUnitOrIsAlly(entity, player, controlAll) 1327 { 1328 return (IsOwnedByPlayer(player, entity) || IsOwnedByMutualAllyOfPlayer(player, entity) || controlAll); 1329 } 1330 1331 /** 1284 1332 * Filter entities which the player can control 1285 1333 */ 1286 1334 function FilterEntityList(entities, player, controlAll) … … 1289 1337 } 1290 1338 1291 1339 /** 1340 * Filter entities which the player can control or are mutualAlly 1341 */ 1342 function FilterEntityListWithAllies(entities, player, controlAll) 1343 { 1344 return entities.filter(function(ent) { return CanControlUnitOrIsAlly(ent, player, controlAll);} ); 1345 } 1346 1347 /** 1292 1348 * Try to transform a wall to a gate 1293 1349 */ 1294 1350 function TryTransformWallToGate(ent, cmpPlayer, template) -
binaries/data/mods/public/simulation/helpers/Player.js
214 214 if (cmpOwnership) 215 215 owner = cmpOwnership.GetOwner(); 216 216 217 // Figure out which player controls the foundation being built217 // Figure out which player controls the target entity 218 218 var targetOwner = 0; 219 219 var cmpOwnershipTarget = Engine.QueryInterface(target, IID_Ownership); 220 220 if (cmpOwnershipTarget) … … 231 231 } 232 232 233 233 /** 234 * Returns true if the entity 'target' is owned by a mutual ally of 235 * the owner of 'entity'. 236 */ 237 function IsOwnedByMutualAllyOfEntity(entity, target) 238 { 239 // Figure out which player controls us 240 var owner = 0; 241 var cmpOwnership = Engine.QueryInterface(entity, IID_Ownership); 242 if (cmpOwnership) 243 owner = cmpOwnership.GetOwner(); 244 245 // Figure out which player controls the target entity 246 var targetOwner = 0; 247 var cmpOwnershipTarget = Engine.QueryInterface(target, IID_Ownership); 248 if (cmpOwnershipTarget) 249 targetOwner = cmpOwnershipTarget.GetOwner(); 250 251 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 252 var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(owner), IID_Player); 253 254 // Check for allied diplomacy status 255 if (cmpPlayer.IsMutualAlly(targetOwner)) 256 return true; 257 258 return false; 259 } 260 261 /** 234 262 * Returns true if the entity 'target' is owned by player 235 263 */ 236 264 function IsOwnedByPlayer(player, target) … … 269 297 } 270 298 271 299 /** 300 * Returns true if the entity 'target' is owned by a mutual ally of player 301 */ 302 function IsOwnedByMutualAllyOfPlayer(player, target) 303 { 304 var targetOwner = 0; 305 var cmpOwnershipTarget = Engine.QueryInterface(target, IID_Ownership); 306 if (cmpOwnershipTarget) 307 targetOwner = cmpOwnershipTarget.GetOwner(); 308 309 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 310 var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(player), IID_Player); 311 312 // Check for allied diplomacy status 313 if (cmpPlayer.IsMutualAlly(targetOwner)) 314 return true; 315 316 return false; 317 } 318 319 /** 272 320 * Returns true if the entity 'target' is owned by someone neutral to player 273 321 */ 274 322 function IsOwnedByNeutralOfPlayer(player,target) … … 312 360 Engine.RegisterGlobal("QueryOwnerInterface", QueryOwnerInterface); 313 361 Engine.RegisterGlobal("QueryPlayerIDInterface", QueryPlayerIDInterface); 314 362 Engine.RegisterGlobal("IsOwnedByAllyOfEntity", IsOwnedByAllyOfEntity); 363 Engine.RegisterGlobal("IsOwnedByMutualAllyOfEntity", IsOwnedByMutualAllyOfEntity); 315 364 Engine.RegisterGlobal("IsOwnedByPlayer", IsOwnedByPlayer); 316 365 Engine.RegisterGlobal("IsOwnedByGaia", IsOwnedByGaia); 317 366 Engine.RegisterGlobal("IsOwnedByAllyOfPlayer", IsOwnedByAllyOfPlayer); 367 Engine.RegisterGlobal("IsOwnedByMutualAllyOfPlayer", IsOwnedByMutualAllyOfPlayer); 318 368 Engine.RegisterGlobal("IsOwnedByNeutralOfPlayer", IsOwnedByNeutralOfPlayer); 319 369 Engine.RegisterGlobal("IsOwnedByEnemyOfPlayer", IsOwnedByEnemyOfPlayer); -
binaries/data/mods/public/simulation/components/UnitAI.js
4247 4247 if (!cmpGarrisonHolder) 4248 4248 return false; 4249 4249 4250 // Verify that the target is owned by this entity's player 4250 // Verify that the target is owned by this entity's player or is mutual allied 4251 4251 var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 4252 if (!cmpOwnership || ! IsOwnedByPlayer(cmpOwnership.GetOwner(), target))4252 if (!cmpOwnership || !(IsOwnedByPlayer(cmpOwnership.GetOwner(), target) || IsOwnedByMutualAllyOfPlayer(cmpOwnership.GetOwner(), target))) 4253 4253 return false; 4254 4254 4255 4255 // Don't let animals garrison for now -
binaries/data/mods/public/simulation/components/Player.js
351 351 352 352 var sharedLos = []; 353 353 for (var i = 0; i < cmpPlayerManager.GetNumPlayers(); ++i) 354 if (this.IsAlly(i)) 355 { 356 var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(i), IID_Player); 357 if (cmpPlayer && cmpPlayer.IsAlly(this.playerID)) 358 sharedLos.push(i); 359 } 354 if (this.IsMutualAlly(i)) 355 sharedLos.push(i); 360 356 361 357 cmpRangeManager.SetSharedLos(this.playerID, sharedLos); 362 358 }; … … 424 420 return this.diplomacy[id] > 0; 425 421 }; 426 422 423 /** 424 * Check if given player is our ally, and we are its ally 425 */ 426 Player.prototype.IsMutualAlly = function(id) 427 { 428 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 429 if (!cmpPlayerManager) 430 return false; 431 var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(id), IID_Player); 432 return this.IsAlly(id) && cmpPlayer && cmpPlayer.IsAlly(this.playerID); 433 }; 434 427 435 Player.prototype.SetEnemy = function(id) 428 436 { 429 437 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
281 281 }; 282 282 283 283 /** 284 * Unload all units with same owner than the entity 285 * Returns true if all successful, false if not 286 */ 287 GarrisonHolder.prototype.UnloadAllOwn = function(forced) 288 { 289 var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 290 if (!cmpOwnership) 291 return false; 292 var owner = cmpOwnership.GetOwner(); 293 294 // Make copy of entity list 295 var entities = []; 296 for each (var entity in this.entities) 297 { 298 var cmpOwnership = Engine.QueryInterface(entity, IID_Ownership); 299 if (cmpOwnership && cmpOwnership.GetOwner() == owner) 300 entities.push(entity); 301 } 302 303 var ejectedEntities = []; 304 var success = true; 305 for each (var entity in entities) 306 { 307 if (this.Eject(entity, forced)) 308 { 309 ejectedEntities.push(entity); 310 } 311 else 312 { 313 success = false; 314 } 315 } 316 317 this.OrderWalkToRallyPoint(ejectedEntities); 318 this.UpdateGarrisonFlag(); 319 320 return success; 321 }; 322 323 /** 284 324 * Unload all units from the entity 285 325 * Returns true if all successful, false if not 286 326 */ … … 469 509 } 470 510 }; 471 511 512 513 /** 514 * Eject all foreign garrisoned entities which are no more allied 515 */ 516 GarrisonHolder.prototype.OnDiplomacyChanged = function() 517 { 518 for (var i = this.entities.length; i > 0; --i) 519 { 520 if (!IsOwnedByMutualAllyOfEntity(this.entity, this.entities[i-1])) 521 this.Eject(this.entities[i-1], true); 522 } 523 this.UpdateGarrisonFlag(); 524 }; 525 472 526 Engine.RegisterComponentType(IID_GarrisonHolder, "GarrisonHolder", GarrisonHolder); 473 527 -
binaries/data/mods/public/simulation/ai/common-api-v3/entity.js
580 580 unloadAll: function() { 581 581 if (!this._template.GarrisonHolder) 582 582 return undefined; 583 Engine.PostCommand({"type": "unload-all ", "garrisonHolders": [this.id()]});583 Engine.PostCommand({"type": "unload-all-own", "garrisonHolders": [this.id()]}); 584 584 return this; 585 585 }, 586 586