public Caravan(int numTraders, CaravanTypeEnum caravanType, List <TileManager.Tile> spawnTiles, TileManager.Tile targetTile) { this.numTraders = numTraders; this.caravanType = caravanType; this.targetTile = targetTile; location = GameManager.caravanM.CreateLocation(); for (int i = 0; i < numTraders && spawnTiles.Count > 0; i++) { TileManager.Tile spawnTile = spawnTiles[UnityEngine.Random.Range(0, spawnTiles.Count)]; SpawnTrader(spawnTile); spawnTiles.Remove(spawnTile); } inventory = new ResourceManager.Inventory(this, int.MaxValue, int.MaxValue); resourceGroup = GameManager.resourceM.GetRandomResourceGroup(); foreach (ResourceManager.Resource resource in resourceGroup.resources.OrderBy(r => UnityEngine.Random.Range(0f, 1f))) // Randomize resource group list { int resourceGroupResourceCount = Mathf.Clamp(resourceGroup.resources.Count, minDistinctResources + 1, int.MaxValue); // Ensure minimum count of (minimumDistinctResources + 1) if (UnityEngine.Random.Range(0f, 1f) < Mathf.Clamp(((resourceGroupResourceCount - inventory.resources.Count) - minDistinctResources) / (float)(resourceGroupResourceCount - minDistinctResources), minDistinctResourceChance, 1f)) // Decrease chance of additional distinct resources on caravan as distinct resources on caravan increase { int resourceAvailableAmount = resource.GetAvailableAmount(); int caravanAmount = Mathf.RoundToInt(Mathf.Clamp(UnityEngine.Random.Range(resourceAvailableAmount * minAvailableAmountModifier, resourceAvailableAmount * maxAvailableAmountModifier), UnityEngine.Random.Range(minMinimumCaravanAmount, maxMinimumCaravanAmount), int.MaxValue)); // Ensure a minimum number of the resource on the caravan inventory.ChangeResourceAmount(resource, caravanAmount, false); } } }
public void SpawnCaravan(CaravanTypeEnum caravanType, int maxNumTraders) { List <TileManager.Tile> validSpawnTiles = null; if (caravanType == CaravanTypeEnum.Foot || caravanType == CaravanTypeEnum.Wagon) { validSpawnTiles = GameManager.colonyM.colony.map.edgeTiles.Where(tile => tile.walkable && !tile.tileType.classes[TileManager.TileType.ClassEnum.LiquidWater]).ToList(); } else if (caravanType == CaravanTypeEnum.Boat) { validSpawnTiles = new List <TileManager.Tile>(); // TODO Implement boat caravans } if (validSpawnTiles.Count > 0) { TileManager.Tile targetSpawnTile = validSpawnTiles[UnityEngine.Random.Range(0, validSpawnTiles.Count)]; List <TileManager.Tile> edgeTilesConnectedToTargetSpawnTile = new List <TileManager.Tile>(); List <TileManager.Tile> spawnTilesFrontier = new List <TileManager.Tile>() { targetSpawnTile }; List <TileManager.Tile> spawnTilesChecked = new List <TileManager.Tile>(); while (spawnTilesFrontier.Count > 0) { TileManager.Tile currentTile = spawnTilesFrontier[0]; spawnTilesFrontier.RemoveAt(0); spawnTilesChecked.Add(currentTile); edgeTilesConnectedToTargetSpawnTile.Add(currentTile); foreach (TileManager.Tile nTile in currentTile.horizontalSurroundingTiles) { if (!spawnTilesChecked.Contains(nTile) && validSpawnTiles.Contains(nTile)) { spawnTilesFrontier.Add(nTile); } } } if (edgeTilesConnectedToTargetSpawnTile.Count > 0) { int numTraders = UnityEngine.Random.Range(1, maxNumTraders + 1); List <TileManager.Tile> edgeTilesCloseToTargetSpawnTile = edgeTilesConnectedToTargetSpawnTile.Where(tile => Vector2.Distance(tile.obj.transform.position, targetSpawnTile.obj.transform.position) <= numTraders * 2).ToList(); if (edgeTilesCloseToTargetSpawnTile.Count > 0) { if (edgeTilesCloseToTargetSpawnTile.Count < numTraders) { numTraders = edgeTilesCloseToTargetSpawnTile.Count; } List <ResourceManager.ObjectInstance> tradingPosts = new List <ResourceManager.ObjectInstance>(); foreach (ResourceManager.ObjectPrefab prefab in GameManager.resourceM.objectInstances.Keys.Where(op => op.subGroupType == ResourceManager.ObjectSubGroupEnum.TradingPosts)) { foreach (ResourceManager.ObjectInstance tradingPost in GameManager.resourceM.objectInstances[prefab]) { if (tradingPost.zeroPointTile.region == targetSpawnTile.region) { tradingPosts.Add(tradingPost); } } } ResourceManager.ObjectInstance selectedTradingPost = null; if (tradingPosts.Count > 0) { selectedTradingPost = tradingPosts[UnityEngine.Random.Range(0, tradingPosts.Count)]; } TileManager.Tile targetTile = null; if (selectedTradingPost != null) { targetTile = selectedTradingPost.zeroPointTile; } else { List <TileManager.Tile> validTargetTiles = targetSpawnTile.region.tiles.Where(tile => tile.walkable && !tile.tileType.classes[TileManager.TileType.ClassEnum.LiquidWater]).ToList(); targetTile = validTargetTiles[UnityEngine.Random.Range(0, validTargetTiles.Count)]; } AddCaravan(new Caravan(numTraders, CaravanTypeEnum.Foot, edgeTilesCloseToTargetSpawnTile, targetTile)); } } } }