private void _syncInventory(String info) { // No need to do on server as server already perform it if (!GetTree().IsNetworkServer()) { String[] splitInfo = info.Split(","); int infoIndex = 0; TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(splitInfo[infoIndex])]; infoIndex++; Agent agent = teamMapAI.GetUnit(splitInfo[infoIndex]); infoIndex++; if (agent != null && IsInstanceValid(agent)) { Inventory inventory = agent.GetInventory(); if (inventory != null && IsInstanceValid(inventory)) { inventory.SyncInventoryState(splitInfo[infoIndex], _inventoryDatabase); } } } }
protected void InitializeTeamMapAI() { Godot.Collections.Array <TeamMapAISetting> teamMapAISettings = GameStates.GetTeamMapAISettings(); TeamMapAIs = new Godot.Collections.Array <TeamMapAI>(); // Start with neutral and above for (int index = 0; index < (int)(Team.TeamCode.NEUTRAL); index++) { TeamMapAI ai = (TeamMapAI)((PackedScene)GD.Load("res://ai/TeamMapAI.tscn")).Instance(); ai.Name = nameof(TeamMapAI) + "_" + (Team.TeamCode)index; AddChild(ai); ai.Initialize(this, _inventoryManager, CapaturableBaseManager.GetBases(), (Team.TeamCode)index, _pathFinding); if (teamMapAISettings != null) { ai.SetMaxUnitUsageAmount(teamMapAISettings[index].Budget); ai.SetAutoSpawnMember(teamMapAISettings[index].AutoSpawnMember); } TeamMapAIs.Add(ai); foreach (CapturableBase capturable in CapaturableBaseManager.GetBases()) { capturable.Connect(nameof(CapturableBase.BaseTeamChangeSignal), ai, nameof(TeamMapAI.HandleCapturableBaseCaptured)); } } }
private void _serverEquipItem(String info) { int inventoryItemIndex = int.Parse(info.Split(";")[0]); TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(info.Split(";")[1])]; Agent agent = teamMapAI.GetUnit(info.Split(";")[2]); Weapon.WeaponOrder weaponOrder = (Weapon.WeaponOrder) int.Parse(info.Split(";")[3]); int weaponIndex = int.Parse(info.Split(";")[4]); if (agent != null && IsInstanceValid(agent)) { Inventory inventory = agent.GetInventory(); // Inventory is not null if (inventory != null && IsInstanceValid(inventory) && inventory.GetItem(inventoryItemIndex) != null) { // It is not in used and is equiptable if (!inventory.IsItemIndexInUsed(inventoryItemIndex) && inventory.GetItem(inventoryItemIndex).CurrentItemType == ItemResource.ItemType.EQUIPABLE) { // Server local directly process inventory.EquipItem(inventoryItemIndex, weaponOrder, weaponIndex); } // Sync the client no matter if is passing (if not passing, it may indicate cliet is out of sync with server inventory so need to sync) if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { SyncInventory(-1, agent); } } } }
private void _serverAddItem(String info) { String[] splitInfo = info.Split(";"); int infoIndex = 0; ItemResource itemResource = _inventoryDatabase.GetItemByID(splitInfo[infoIndex]); infoIndex++; TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(splitInfo[infoIndex])]; infoIndex++; Agent agent = teamMapAI.GetUnit(splitInfo[infoIndex]); infoIndex++; if (agent != null && IsInstanceValid(agent) && itemResource != null && IsInstanceValid(itemResource)) { Inventory inventory = agent.GetInventory(); if (inventory != null && IsInstanceValid(inventory)) { inventory.AddItem(itemResource); // Sync the client no matter if is passing (if not passing, it may indicate cliet is out of sync with server inventory so need to sync) if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { SyncInventory(-1, agent); } } } }
private void _syncBots() { if (GetTree().IsNetworkServer()) { // Calculate the target amount of spawned bots int bot_count = network.serverinfo.max_players - network.networkPlayers.Count; if (SpawnBots.Count > bot_count) { while (SpawnBots.Count > bot_count) { foreach (Agent spawnBot in SpawnBots.Values) { _removeUnitOnNetwork(spawnBot.Name); break; } } } else if (SpawnBots.Count < bot_count) { // If bot_count while (SpawnBots.Count < bot_count) { TeamMapAI targetAI = null; // Set the initial to max bot count int smallestUnitCount = bot_count; foreach (TeamMapAI currentAI in TeamMapAIs) { if (currentAI.GetAutoSpawnMember() && currentAI.isNewUnitAllow()) { if (currentAI.GetUnitsContainer().GetChildren().Count <= smallestUnitCount) { smallestUnitCount = currentAI.GetUnitsContainer().GetChildren().Count; targetAI = currentAI; } } } if (targetAI != null) { String botId = (int)targetAI.GetCurrentTeam() + ";" + AgentPrefix + AgentBotCounter; AgentBotCounter++; Rpc(nameof(_addBotOnNetwork), botId); _addBotOnNetwork(botId); } else { // No longer allowed to add more bot now, so exit loop break; } } } _checkGameWinningCondition(); } }
private void _serverRemoveItem(String info) { String[] splitInfo = info.Split(";"); int infoIndex = 0; int inventoryIndex = int.Parse(splitInfo[infoIndex]); infoIndex++; TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(splitInfo[infoIndex])]; infoIndex++; Agent agent = teamMapAI.GetUnit(splitInfo[infoIndex]); infoIndex++; int dropItem = int.Parse(splitInfo[infoIndex]); infoIndex++; if (agent != null && IsInstanceValid(agent)) { Inventory inventory = agent.GetInventory(); if (inventory != null && IsInstanceValid(inventory)) { if (inventory.GetItem(inventoryIndex) != null && !inventory.IsItemIndexInUsed(inventoryIndex)) { ItemResource itemResource = inventory.GetItem(inventoryIndex); inventory.RemoveItem(inventoryIndex); // Will drop item even if local client not able to remove it, as to sync up state with other servers if (dropItem == 1) { Vector2 itemPosition = agent.GlobalPosition + new Vector2(200f, 200f); _createPickUp(itemResource, itemPosition); if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { String dropItemInfo = itemResource.ItemID + ";" + itemPosition.x + ";" + itemPosition.y + ";"; Rpc(nameof(_clientCreatePickUp), dropItemInfo); } } } // Sync the client no matter if is passing (if not passing, it may indicate cliet is out of sync with server inventory so need to sync) if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { SyncInventory(-1, agent); } } } }
public virtual void Initialize(GameWorld gameWorld, String unitName, String displayName, TeamMapAI teamMapAI, PathFinding pathFinding) { _team = (Team)GetNode("Team"); _teamMapAI = teamMapAI; _gameWorld = gameWorld; SetCurrentTeam(_teamMapAI.GetCurrentTeam()); SetUnitName(unitName); SetDisplayName(displayName); _health = MaxHealth; _energy = MaxEnergy; CurrentInventory.Initialize(this); _initializeWeapon(LeftWeapons); _initializeWeapon(RightWeapons); }
private void _serverUseItem(String info) { String[] splitInfo = info.Split(";"); int infoIndex = 0; int inventoryIndex = int.Parse(splitInfo[infoIndex]); infoIndex++; TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(splitInfo[infoIndex])]; infoIndex++; Agent agent = teamMapAI.GetUnit(splitInfo[infoIndex]); infoIndex++; if (agent != null && IsInstanceValid(agent)) { Inventory inventory = agent.GetInventory(); if (inventory != null && IsInstanceValid(inventory)) { if (inventory.GetItem(inventoryIndex) != null && !inventory.IsItemIndexInUsed(inventoryIndex)) { ItemResource itemResource = inventory.GetItem(inventoryIndex); inventory.RemoveItem(inventoryIndex); if (itemResource.ItemID == "SYC-010") { agent.Heal(agent.MaxHealth); } } // Sync the client no matter if is passing (if not passing, it may indicate cliet is out of sync with server inventory so need to sync) if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { SyncInventory(-1, agent); } } } }
private void _serverPurchaseItem(String info) { String[] splitInfo = info.Split(";"); int infoIndex = 0; ItemResource itemResource = _inventoryDatabase.GetItemByID(splitInfo[infoIndex]); infoIndex++; TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(splitInfo[infoIndex])]; infoIndex++; Agent agent = teamMapAI.GetUnit(splitInfo[infoIndex]); infoIndex++; if (agent != null && IsInstanceValid(agent)) { Inventory inventory = agent.GetInventory(); // No need to sync cost as cost will sync by team AI system if (inventory != null && IsInstanceValid(inventory) && itemResource != null && IsInstanceValid(itemResource)) { if (inventory.GetAvailableCapacity() > 0 && teamMapAI.ChargeAmount(itemResource.Price)) { inventory.AddItem(itemResource); } // Sync the client no matter if is passing the lastcheck // If not passing, as long as the agent still exit it may indicate cliet is out of sync with server inventory so need to sync if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { SyncInventory(-1, agent); } } } }
private void _serverUnequipItem(String info) { String[] splitInfo = info.Split(";"); int infoIndex = 0; TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(splitInfo[infoIndex])]; infoIndex++; Agent agent = teamMapAI.GetUnit(splitInfo[infoIndex]); infoIndex++; Weapon.WeaponOrder weaponOrder = (Weapon.WeaponOrder) int.Parse(splitInfo[infoIndex]); infoIndex++; int weaponIndex = int.Parse(splitInfo[infoIndex]); infoIndex++; if (agent != null && IsInstanceValid(agent)) { Inventory inventory = agent.GetInventory(); if (inventory != null && IsInstanceValid(inventory)) { if (agent.GetWeapons(weaponOrder)[weaponIndex] != null) { inventory.UnequipItem(weaponOrder, weaponIndex); } // Sync the client no matter if is passing (if not passing, it may indicate cliet is out of sync with server inventory so need to sync) if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { SyncInventory(-1, agent); } } } }
public override void Initialize(GameWorld gameWorld, String unitName, String displayName, TeamMapAI teamMapAI, PathFinding pathFinding) { base.Initialize(gameWorld, unitName, displayName, teamMapAI, pathFinding); if (GetTree().IsNetworkServer()) { _agentAI.Initialize(_gameWorld, this, pathFinding, DetectRadius); } }