public static bbStructure GenerateStructure(bbStructureType _type, bbPos _pos, ClickType _clickType) { if (_type == bbStructureType.STRUCTURE_HQ) { return(new bbStructureHQ(_type, _pos)); } else if (_type == bbStructureType.STRUCTURE_MINE) { return(new bbStructureMine(_type, _pos)); } else if (_type == bbStructureType.STRUCTURE_FARM) { return(new bbStructureFarm(_type, _pos)); } else if (_type == bbStructureType.STRUCTURE_HOUSE) { return(new bbStructureHouse(_type, _pos)); } else if (_type == bbStructureType.STRUCTURE_CONSTRUCTION) { return(new bbStructureConstruction(_type, _pos, _clickType)); } else { return(new bbStructureEx(_type, _pos)); } }
List <bbPos> SetNeighborsSquare(bbPos p, Dictionary <string, bbPos> map, int _dim, bool _wrapEastWest = false, bool _wrapNorthSouth = false) { float x = p.gridLoc.x(); float y = p.gridLoc.y(); List <bbPos> neighbors = new List <bbPos>(); for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i != 0 || j != 0) { float X = _wrapEastWest ? (_dim + x + i) % _dim : x + i; float Y = _wrapNorthSouth ? (_dim + y + j) % _dim : y + j; bbLoc l2 = new bbLoc(X, Y); if (map.ContainsKey(l2.key())) { neighbors.Add(map[l2.key()]); } else { Debug.Log("Map doesn't contain " + l2.x() + "," + l2.y()); } } } } return(neighbors); }
public static float DistanceMinkowski(bbPos p1, bbPos p2, float d = 2) { float pSum = 0f; for (int i = 0; i < p1.mapLoc.coordinates.Length; i++) { pSum += Mathf.Pow(Mathf.Abs(p1.mapLoc.coordinates[i] - p2.mapLoc.coordinates[i]), d); } return(Mathf.Pow(pSum, (1 / d))); }
bool CheckBuildable(bbPos pos) { bool buildable = false; if (currentIsland.lands[pos].terrainType == bbTerrainType.LAND) { buildable = true; } return(buildable); }
public static float DistancePath(bbPos p1, bbPos p2) { List <bbPos> path = bbPathfinder.findAStarPath(p1, p2); float d = 0f; for (int i = 1; i < path.Count; i++) { d += path[i].getMoveToCost(path[i - 1]); } return(d); }
public bool HandleClick(bbPos pos, bool leftClick, bool rightClick) { bool clickDidSomething = false; if (leftClick) { if (!currentIsland.structures.ContainsKey(pos)) { if (CheckBuildable(pos)) { if (currentClickType == ClickType.SETTLE_HQ || currentClickType == ClickType.SETTLE_HOUSE) { if (currentClickType == ClickType.SETTLE_HQ) { currentIsland.AddStructure(bbStructureType.STRUCTURE_HQ, pos.gridLoc.x(), pos.gridLoc.y(), currentClickType); currentClickType = ClickType.SETTLE_HOUSE; } else if (currentClickType == ClickType.SETTLE_HOUSE) { currentIsland.AddStructure(bbStructureType.STRUCTURE_HOUSE, pos.gridLoc.x(), pos.gridLoc.y(), currentClickType); currentClickType = ClickType.NONE; paused = false; } clickDidSomething = true; } else if (playerResources[ItemType.ITEM_GOLD] >= STRUCTURE_COST) { playerResources[ItemType.ITEM_GOLD] -= STRUCTURE_COST; ClickType buildingType = ClickType.BUILD_HOUSE; buildingType = currentIsland.lands[pos].terrainFeature == bbTerrainFeature.ARABLE ? ClickType.BUILD_FARM : buildingType; buildingType = currentIsland.lands[pos].terrainFeature == bbTerrainFeature.MINEABLE ? ClickType.BUILD_MINE : buildingType; currentIsland.AddStructure(bbStructureType.STRUCTURE_CONSTRUCTION, pos.gridLoc.x(), pos.gridLoc.y(), buildingType); List <bbJob> jobs = new List <bbJob>(); bbStructure site = currentIsland.structures[pos]; bbJobMoveTo moveToMine = new bbJobMoveTo(site.getPos()); jobs.Add(moveToMine); bbJobUseStructure useMine = new bbJobUseStructure(site); jobs.Add(useMine); playerJobQueue.Add(jobs); clickDidSomething = true; } } } } if (rightClick) { gameManager.drawer.HighlightTile(pos); } return(clickDidSomething); }
bool GetAgentAtPos(bbPos pos, out bbAgent myAgent) { foreach (bbAgent agent in playerAgents) { if (agent.pos == pos) { myAgent = agent; return(true); } } myAgent = new bbAgent(); return(false); }
List <bbPos> SetNeighborsHex(bbPos p, Dictionary <string, bbPos> map, int _dim, bool _wrapEastWest = false, bool _wrapNorthSouth = false) { List <bbPos> neighbors = new List <bbPos>(); List <int[]> hexNeighbors = new List <int[]>(); if (p.gridLoc.y() % 2 == 0) { hexNeighbors.Add(new int[] { 1, 0 }); hexNeighbors.Add(new int[] { 1, -1 }); hexNeighbors.Add(new int[] { 0, -1 }); hexNeighbors.Add(new int[] { -1, 0 }); hexNeighbors.Add(new int[] { 0, 1 }); hexNeighbors.Add(new int[] { 1, 1 }); } else { hexNeighbors.Add(new int[] { 1, 0 }); hexNeighbors.Add(new int[] { -1, -1 }); hexNeighbors.Add(new int[] { 0, -1 }); hexNeighbors.Add(new int[] { -1, 0 }); hexNeighbors.Add(new int[] { 0, 1 }); hexNeighbors.Add(new int[] { -1, 1 }); } float x = p.gridLoc.x(); float y = p.gridLoc.y(); for (int k = 0; k < hexNeighbors.Count; k++) { int i = hexNeighbors[k][0]; int j = hexNeighbors[k][1]; float X = _wrapEastWest ? (_dim + x + i) % _dim : x + i; float Y = _wrapNorthSouth ? (_dim + y + j) % _dim : y + j; bbLoc l2 = new bbLoc(X, Y); if (map.ContainsKey(l2.key())) { neighbors.Add(map[l2.key()]); } else { Debug.Log("Map doesn't contain " + l2.x() + "," + l2.y()); } } return(neighbors); }
public float getMoveToCost(bbPos moveFrom) { if (game.currentIsland.lands[this].terrainType == bbTerrainType.MOUNTAIN || game.currentIsland.lands[this].terrainType == bbTerrainType.OCEAN) { return(float.PositiveInfinity); } else if (neighbors.Contains(moveFrom)) { float distance = DistanceMinkowski(this, moveFrom); return(distance); } else { return(float.PositiveInfinity); } }
bool SelectAgent(bbPos pos, out bbAgent selectedAgent) { bool clickDidSomething = false; bbAgent agentAtPos; if (GetAgentAtPos(pos, out agentAtPos)) { selectedAgent = agentAtPos; clickDidSomething = true; } else { selectedAgent = null; } return(clickDidSomething); }
private Dictionary <string, bbPos> Generate2DGrid(int _dim) { Dictionary <string, bbPos> map = new Dictionary <string, bbPos>(); for (int x = 0; x < _dim; x++) { for (int y = 0; y < _dim; y++) { bbLoc l = new bbLoc(x, y); bbPos p = new bbPos(l, game); map[p.gridLoc.key()] = p; } } return(map); }
private Dictionary <string, bbPos> SetNeighborsFor2DGrid(Dictionary <string, bbPos> map, int _dim, TileShape tileShape, bool _wrapEastWest = false, bool _wrapNorthSouth = false) { foreach (string k in map.Keys) { bbPos p = map[k]; if (tileShape == TileShape.SQUARE) { p.neighbors = SetNeighborsSquare(p, map, _dim, _wrapEastWest, _wrapNorthSouth); } else if (tileShape == TileShape.HEX) { p.neighbors = SetNeighborsHex(p, map, _dim, _wrapEastWest, _wrapNorthSouth); } } return(map); }
public bbAgent(string _name, bbPos _pos, BaseBuilderMVP _game, bbStructure _myHouse) { alive = true; animating = false; name = _name; //Debug.Log("Created Agent named " + name); pos = _pos; game = _game; myHouse = _myHouse; jobQueue = new List <bbJob>(); needTimer = new Dictionary <ItemType, int> { { ItemType.ITEM_FOOD, 50 } }; needQuantity = new Dictionary <ItemType, int> { { ItemType.ITEM_FOOD, 1 } }; inventory = new Dictionary <ItemType, int> { { ItemType.ITEM_FOOD, 0 }, { ItemType.ITEM_GOLD, 0 } }; needsAddressing = new List <ItemType>(); }
public bbLand(bbPos _pos) { pos = _pos; terrainFeature = bbTerrainFeature.NONE; }
public bbStructureConstruction(bbStructureType _type, bbPos _pos, ClickType _clickType) { pos = _pos; structureType = _type; clickType = _clickType; }
public bbStructureEx(bbStructureType _structureType, bbPos _pos) { structureType = _structureType; pos = _pos; }
public bbStructureHouse(bbStructureType _type, bbPos _pos) { pos = _pos; structureType = _type; spawned = false; }
public bbStructureMine(bbStructureType _type, bbPos _pos) { pos = _pos; structureType = _type; hasResource = false; }
public bbStructureHQ(bbStructureType _type, bbPos _pos) { pos = _pos; structureType = _type; }
public List <bbPos> findPath(bbPos pathTarget) { List <bbPos> path = bbPathfinder.findAStarPath(this, pathTarget); return(path); }
public static List <bbPos> findAStarPath(bbPos start, bbPos end, int maxIter = 100000) { Dictionary <bbPos, float> DistanceFromStart = new Dictionary <bbPos, float>(); Dictionary <bbPos, float> DistanceToEnd = new Dictionary <bbPos, float>(); Dictionary <bbPos, bbPos> FastestPath = new Dictionary <bbPos, bbPos>(); List <bbPos> Searched = new List <bbPos>(); List <bbPos> path = new List <bbPos>(); if (start != end) { // Create the queue of pos to check List <bbPos> nextStep = new List <bbPos>(); // Add start pos' neighbors to queue foreach (bbPos p in start.neighbors) { DistanceFromStart[p] = p.getMoveToCost(start); DistanceToEnd[p] = bbPos.DistanceMinkowski(p, end); FastestPath[p] = start; nextStep.Add(p); } bool pathFound = false; int iter = 0; while (!pathFound && iter < maxIter) { // Order queue by distance nextStep = nextStep.OrderBy(p => DistanceFromStart[p] + DistanceToEnd[p]).ToList(); // Pull next pos to search bbPos thisStep = nextStep[0]; //Debug.Log("thisStep is at " + thisStep.loc.x + " , " + thisStep.loc.y); // Mark pos as searched Searched.Add(thisStep); if (thisStep.neighbors.Contains(end)) { pathFound = true; bbPos p = end; float newPathCost = p.getMoveToCost(thisStep) + DistanceFromStart[thisStep]; if (!DistanceFromStart.ContainsKey(p) || newPathCost < DistanceFromStart[p]) { DistanceFromStart[p] = newPathCost; FastestPath[p] = thisStep; } if (DistanceToEnd.ContainsKey(p)) { DistanceToEnd[p] = bbPos.DistanceMinkowski(p, end); } } else { foreach (bbPos p in thisStep.neighbors) { float newPathCost = p.getMoveToCost(thisStep) + DistanceFromStart[thisStep]; if (!DistanceFromStart.ContainsKey(p) || newPathCost < DistanceFromStart[p]) { DistanceFromStart[p] = newPathCost; FastestPath[p] = thisStep; } if (!DistanceToEnd.ContainsKey(p)) { DistanceToEnd[p] = bbPos.DistanceMinkowski(p, end); } if (!nextStep.Contains(p) && !Searched.Contains(p)) { nextStep.Add(p); //Debug.Log("Added to nextStep Pos at " + p.loc.x + " , " + p.loc.y); } } nextStep.Remove(thisStep); //Debug.Log("Removed from nextStep Pos at " + thisStep.loc.x + " , " + thisStep.loc.y); } iter++; } //Debug.Log("Completed with " + iter + " / " + maxIter + " iterations."); bbPos pathStep = end; while (pathStep != start) { path.Add(pathStep); if (FastestPath.ContainsKey(pathStep)) { pathStep = FastestPath[pathStep]; } else { return(null); } } path.Add(start); path.Reverse(); } return(path); }
public bbJobMoveTo(bbPos _target) { target = _target; }