public AsyncOperation ResetNavMesh(AI_TYPE InType) { NavMeshDataInstance dataInstance; if (Map_MeshInstance.TryGetValue(InType, out dataInstance)) { NavMesh.RemoveNavMeshData(dataInstance); Map_MeshInstance.Remove(InType); } NavMeshData meshData = new NavMeshData(); dataInstance = NavMesh.AddNavMeshData(meshData); Map_MeshInstance.Add(InType, dataInstance); int agentIndex = (int)InType; List <NavMeshBuildSource> sources = new List <NavMeshBuildSource>(); NavMeshSourceTag.Collect(ref sources, GetArea(agentIndex), InType); NavMeshBuildSettings defaultBuildSettings = NavMesh.GetSettingsByIndex(agentIndex); //print(agentIndex + "," + defaultBuildSettings.agentRadius); var bounds = QuantizedBounds(); return(NavMeshBuilder.UpdateNavMeshDataAsync(meshData, defaultBuildSettings, sources, bounds)); }
private void SendBridgeMaker(AI_TYPE InType, Vector3 InBeginPosition, Vector3 InEndPosition) { Vector3 center = (InBeginPosition + InEndPosition) / 2; GameObject bridgeMaker = Instantiate(Prefab_BridgeMaker, center, Quaternion.identity); bridgeMaker.transform.parent = transform; bridgeMaker.GetComponent <BridgeMaker>().type = InType; bridgeMaker.GetComponent <BridgeMaker>().Begin = InBeginPosition; bridgeMaker.GetComponent <BridgeMaker>().End = InEndPosition; bridgeMaker.GetComponent <NavMeshSourceTag>().type = InType; bridgeMaker.GetComponent <NavMeshSourceTag>().CollectMeshs(); ResetTransform(bridgeMaker, InBeginPosition, InEndPosition); // Lazy List <GameObject> makers; if (!Map_Maker.TryGetValue(InType, out makers)) { makers = new List <GameObject>(); Map_Maker.Add(InType, makers); } makers.Add(bridgeMaker); BuildingController.Instance.BuildLittleStuff(InType); }
// Collect all the navmesh build sources for enabled objects tagged by this component public static void Collect(ref List <NavMeshBuildSource> sources, int InArea, AI_TYPE InType) { sources.Clear(); for (var i = 0; i < Map_Meshes[InType].Count; ++i) { var mf = Map_Meshes[InType][i]; if (mf == null) { continue; } var m = mf.sharedMesh; if (m == null) { continue; } var s = new NavMeshBuildSource(); s.shape = NavMeshBuildSourceShape.Mesh; s.sourceObject = m; s.transform = mf.transform.localToWorldMatrix; s.area = InArea; sources.Add(s); } }
private List <Edge> ResetGraphData(AI_TYPE InType, Vector3[] InPositions) { // Lazy Dictionary <int, Vector3> Positions; if (!Map_Positions.TryGetValue(InType, out Positions)) { Positions = new Dictionary <int, Vector3>(); Map_Positions.Add(InType, Positions); } ; Positions.Clear(); foreach (var pos in InPositions) { Positions.Add(Positions.Count, pos); } // Reset Graph, Restore edges of Prim to graph G = new Graph(Positions); List <Edge> edges = G.Prim(); G.ResetAdj(edges); return(edges); }
private void Update() { if (Input.GetMouseButtonUp(0)) { RayPointTo(); } if (Input.GetKeyDown(KeyCode.Alpha1)) { type = AI_TYPE.TRAIN; } if (Input.GetKeyDown(KeyCode.Alpha2)) { type = AI_TYPE.CAR; } if (Input.GetKeyDown(KeyCode.Alpha3)) { type = AI_TYPE.AIRPLANE; } if (Input.GetKeyDown(KeyCode.Alpha4)) { type = AI_TYPE.SHIP; } }
private List <int> FindMainPoints(AI_TYPE InType) { List <int> points = G.GetSingleAttachedPoint(); List <List <int> > resultPath = new List <List <int> >(); foreach (var p1 in points) { DepthFirstPaths dfs = new DepthFirstPaths(G, p1); foreach (var p2 in points) { if (p1 != p2) { List <int> paths = dfs.pathTo(p2); resultPath.Add(paths); } } } resultPath.Sort((Left, Right) => { return(Right.Count - Left.Count); }); return(resultPath.Count > 0 ? resultPath[0] : new List <int>()); }
private void ResetAllStations(AI_TYPE InType) { // Lazy new list of traffic station List <TrafficStation> List_TrafficStation; if (!Map_Stations.TryGetValue(InType, out List_TrafficStation)) { List_TrafficStation = new List <TrafficStation>(); Map_Stations.Add(InType, List_TrafficStation); } // Clear old stations ClearStations(List_TrafficStation); // Add new stations GameObject prefab = List_Station.Find(s => s.type == InType).Prefab; for (int i = 0; i < Map_Positions[InType].Count; i++) { GameObject instance = Instantiate(prefab, Map_Positions[InType][i] + new Vector3(0, 2, 0), prefab.transform.rotation) as GameObject; TrafficStation station = instance.GetComponent <TrafficStation>(); station.Id = i; station.ParentGraph = G; station.type = InType; station.transform.parent = transform; List_TrafficStation.Add(station); } }
private void Start() { aiCount = CurrentLevel.AiCount; CurrentAiType = AI_TYPE.AI_PAWN; StartCoroutine(StartSpawn()); }
public void BuildNewTrafficLine(AI_TYPE InType, Vector3[] InPositions, bool SmoothPath) { // lazy load Prefab_TrafficLine List <TrafficLine> List_TrafficLine; if (!Map.TryGetValue(InType, out List_TrafficLine)) { List_TrafficLine = new List <TrafficLine>(); Map.Add(InType, List_TrafficLine); } LineType line = List_LineType.Find(it => it.type == InType); GameObject t = Instantiate(line.prefab) as GameObject; t.transform.parent = this.gameObject.transform; TrafficLine trafficLine = t.GetComponent <TrafficLine>(); trafficLine.type = InType; // need to be set before addnewposition List_TrafficLine.Add(trafficLine); Vector3[] SmoothPathPostions = InPositions; Vector3[] NavmeshPostions = InPositions; if (SmoothPath) { SmoothPathPostions = GenerateSmoothPath(InPositions, SmoothAmountTimeForPath); NavmeshPostions = GenerateSmoothPath(InPositions, SmoothAmountTimeAI); } // Draw curve path trafficLine.DrawCurve(SmoothPathPostions); // Use bridge mesh as nav mesh BridgeController.Instance.SendBridgeMaker(InType, NavmeshPostions); }
public void SendBridgeMaker(AI_TYPE InType, Vector3[] InPositions) { for (int i = 0; i < InPositions.Length - 1; i++) { SendBridgeMaker(InType, InPositions[i], InPositions[i + 1]); } }
private void ClearMapAICoroutine(AI_TYPE InType) { Coroutine c = null; if (Map_AI_Coroutine.TryGetValue(InType, out c)) { StopCoroutine(c); Map_AI_Coroutine.Remove(InType); } }
public void ReCycleAI(AI_TYPE InType) { if (Map_AI.ContainsKey(InType)) { List <GameObject> List_AI = Map_AI[InType]; List_AI.ForEach(AI => Destroy(AI)); List_AI.Clear(); Map_AI.Remove(InType); } }
public void Clear(AI_TYPE InType) { NavMeshDataInstance dataInstance; if (Map_MeshInstance.TryGetValue(InType, out dataInstance)) { NavMesh.RemoveNavMeshData(dataInstance); Map_MeshInstance.Remove(InType); } }
//public void ClearMapStationsAll() //{ // foreach (var Item in Map_Stations.Values) ClearStations(Item); //} public void ClearMapStations(AI_TYPE InType) { List <TrafficStation> List_TrafficStation; if (!Map_Stations.TryGetValue(InType, out List_TrafficStation)) { return; } ClearStations(List_TrafficStation); }
private void ResetSendAICoroutine(AI_TYPE InType) { ClearMapAICoroutine(InType); Coroutine c = StartCoroutine(SendAI(InType)); if (c != null) { Map_AI_Coroutine.Add(InType, c); } }
public void BuildNewStation(AI_TYPE InType, Vector3 InPosition) { List <Edge> edges = ResetGraphData(InType, InPosition); List <int> MainPoints = FindMainPoints(InType); ResetAllStations(InType); ReDrawLines(InType, edges, MainPoints); ResetSendAICoroutine(InType); }
private GameObject SpawnAI(AI_TYPE InType, AI_LEVEL InLevel, GameObject InParent) { // for test InLevel = (AI_LEVEL)Mathf.Min((int)InLevel, AI_Level_Limit[(int)InType]); AIData AIToSpawn = List_AIData.Find(it => InType == it.type && InLevel == it.level); string prefabName = AIData.AI_Name[(int)AIToSpawn.type] + (int)AIToSpawn.level; GameObject AI = Resources.Load(Config.Folder_AITraffic + prefabName) as GameObject; AI = Instantiate(AI, InParent.transform.position - InParent.GetComponent <TrafficStation>().Offset, Quaternion.identity); return(AI); }
public List <TrafficStation> GetTrafficStations(AI_TYPE InType) { // Lazy new list of traffic station List <TrafficStation> List_TrafficStation = null; if (!Map_Stations.TryGetValue(InType, out List_TrafficStation)) { List_TrafficStation = new List <TrafficStation>(); Map_Stations.Add(InType, List_TrafficStation); } return(List_TrafficStation); }
// ---------------------------------------- private bool AllowedToBuild(AI_TYPE InType, List <GameObject> InBridges, Vector3 InPosition) { return(true); //GameObject bridge = InBridges.Find(b => (Vector3.Distance(b.transform.position, InPosition) <= BridgeDistanceLimit)); //if (bridge != null) //{ // bridge.transform.position = (bridge.transform.position + InPosition) / 2; // return false; //} //return true; }
public void BuildLittleStuff(AI_TYPE InType) { if (!NeedToBuild(InType)) { return; } List <GameObject> List_Building; if (!Map_Building.TryGetValue(InType, out List_Building)) { List_Building = new List <GameObject>(); Map_Building.Add(InType, List_Building); } else { foreach (var b in List_Building) { Destroy(b); } List_Building.Clear(); } string stuffPath = Config.Folder_BuildingStuff + GetResFloder(InType) + RandomName(InType); GameObject prefab = Resources.Load(stuffPath) as GameObject; //Dictionary<AI_TYPE, List<TrafficStation>> Map_Stations = StationContorller.Instance.Map_Stations; //List<TrafficStation> list = Map_Stations[InType]; Dictionary <AI_TYPE, List <GameObject> > map = BridgeController.Instance.Map_Maker; List <GameObject> list = map[InType]; for (int i = 0; i < list.Count; i += 5) { GameObject item = list[i]; Vector3 pos1 = item.gameObject.transform.position + RandomPositionOffset(); GameObject stuff = Instantiate(prefab, pos1, Quaternion.identity); stuff.transform.parent = transform; List_Building.Add(stuff); Vector3 pos2 = item.gameObject.transform.position - RandomPositionOffset(); stuff = Instantiate(prefab, pos2, prefab.transform.rotation); stuff.transform.parent = transform; List_Building.Add(stuff); } }
// --------------------Clear------------------------ public void ClearMakers(AI_TYPE InType) { List <GameObject> makers; if (!Map_Maker.TryGetValue(InType, out makers)) { makers = new List <GameObject>(); Map_Maker.Add(InType, makers); } foreach (var m in makers) { Destroy(m.gameObject); } makers.Clear(); }
public void ClearBridges(AI_TYPE InType) { List <GameObject> bridges; if (!Map_Bridge.TryGetValue(InType, out bridges)) { bridges = new List <GameObject>(); Map_Bridge.Add(InType, bridges); } foreach (var b in bridges) { Destroy(b.gameObject); } bridges.Clear(); }
private string RandomName(AI_TYPE InType) { if (InType == AI_TYPE.TRAIN) { return(Random.Range(0, 6).ToString()); } else if (InType == AI_TYPE.CAR) { return(Random.Range(0, 6).ToString()); } else if (InType == AI_TYPE.SHIP) { return(Random.Range(0, 5).ToString()); } return("0"); }
public void Clear(AI_TYPE InType) { NavMeshSourceTag.UnCollect(InType); List <TrafficLine> List_TrafficLine; if (!Map.TryGetValue(InType, out List_TrafficLine)) { return; } foreach (var t in List_TrafficLine) { Destroy(t.gameObject); } List_TrafficLine.Clear(); }
public void TriggerAI(AI_TYPE aiType) { AISubset next = null; foreach (AISubset s in AIList) { s.gameObject.SetActive(false); if (s.aiType == aiType) { next = s; } } if (next != null) { next.gameObject.SetActive(true); } }
private string GetResFloder(AI_TYPE InType) { if (InType == AI_TYPE.TRAIN) { return("Tree/"); } else if (InType == AI_TYPE.CAR) { return("Tree/"); } else if (InType == AI_TYPE.SHIP) { return("Mountain/"); } else { return("Tree/"); } }
public IEnumerator SendAI(AI_TYPE InType, AI_LEVEL InLevel, GameObject InParent) { List <GameObject> List_AI; if (!Map_AI.TryGetValue(InType, out List_AI)) { List_AI = new List <GameObject>(); Map_AI.Add(InType, List_AI); } for (int i = 0; i < (int)InLevel; i++) { GameObject AI = SpawnAI(InType, (AI_LEVEL)i, InParent); AI.GetComponent <TrafficAI>().type = InType; List_AI.Add(AI); yield return(new WaitForSeconds(2)); } }
private IEnumerator StartSpawn() { while (aiCount > 0) { // Whenever aiCount is a multiple of 5, doulble spawn event! // aiCount : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // aiCount % 5 : 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 int num2spawn = ((aiCount % 5) != 0) ? 1 : 2; for (int i = 0; i < num2spawn; i++) { GameObject newAI = null; switch (CurrentAiType) { case AI_TYPE.AI_PAWN: newAI = Instantiate(AiPawn); CurrentAiType = AI_TYPE.AI_TRICKSTER; break; case AI_TYPE.AI_TRICKSTER: newAI = Instantiate(AiTrickster); CurrentAiType = AI_TYPE.AI_BRUTE; break; case AI_TYPE.AI_BRUTE: newAI = Instantiate(AiBrute); CurrentAiType = AI_TYPE.AI_PAWN; break; } newAI.GetComponent <AIMovement> ().Destination = Destination; liveAI.Add(newAI); } aiCount--; yield return(new WaitForSeconds(CurrentLevel.Time)); } CurrentLevel.CheckAiCount(); }
public void BuildNewBridge(AI_TYPE InType, Vector3 InPosition, Quaternion InRotation) { // Lazy List <GameObject> bridges; if (!Map_Bridge.TryGetValue(InType, out bridges)) { bridges = new List <GameObject>(); Map_Bridge.Add(InType, bridges); } if (AllowedToBuild(InType, bridges, InPosition)) { GameObject Prefab_Bridge = List_BridgeData.Find(b => b.type == InType).Prefab; GameObject bridge = Instantiate(Prefab_Bridge, InPosition, InRotation); bridge.transform.parent = gameObject.transform; bridges.Add(bridge); } }
private IEnumerator SendAI(AI_TYPE InType) { List <TrafficStation> stations = Map_Stations[InType]; if (stations != null) { // ReCycle AI by type AIController.Instance.ReCycleAI(InType); if (stations.Count >= 2) { // Reset navgation mesh dynamicly yield return(LocalNavMeshBuilder.Instance.ResetNavMesh(InType)); GameObject ParentStation = stations[Random.Range(0, stations.Count)].gameObject; // Send new AI yield return(AIController.Instance.SendAI(InType, Map_Level[InType], ParentStation)); } } }
// Runs an entire game using the given AI type to decide on moves private static State RunAIGame(AI_TYPE AItype, bool print, int depth = 0, int timeLimit = 0, int iterationLimit = 0) { GameEngine game = new GameEngine(); State end = null; if (AItype == AI_TYPE.CLASSIC_MINIMAX) { Minimax minimax = new Minimax(game, depth); end = minimax.RunClassicMinimax(print); } else if (AItype == AI_TYPE.ALPHA_BETA) { Minimax minimax = new Minimax(game, depth); end = minimax.RunAlphaBeta(print); } else if (AItype == AI_TYPE.ITERATIVE_DEEPENING_ALPHA_BETA) { Minimax minimax = new Minimax(game, depth); end = minimax.RunIterativeDeepeningAlphaBeta(print, timeLimit); } else if (AItype == AI_TYPE.PARALLEL_ALPHA_BETA) { Minimax minimax = new Minimax(game, depth); end = minimax.RunParallelAlphaBeta(print); } else if (AItype == AI_TYPE.PARALLEL_ITERATIVE_DEEPENING_ALPHA_BETA) { Minimax minimax = new Minimax(game, depth); end = minimax.RunParallelIterativeDeepeningAlphaBeta(print, timeLimit); } else if (AItype == AI_TYPE.CLASSIC_EXPECTIMAX) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunClassicExpectimax(print, weights); } else if (AItype == AI_TYPE.EXPECTIMAX_STAR1) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunStar1Expectimax(print, weights); } else if (AItype == AI_TYPE.EXPECTIMAX_STAR1_FW_PRUNING) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunStar1WithUnlikelyPruning(print, weights); } else if (AItype == AI_TYPE.ITERATIVE_DEEPENING_EXPECTIMAX) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunIterativeDeepeningExpectimax(print, timeLimit, weights); } else if (AItype == AI_TYPE.PARALLEL_EXPECTIMAX) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunParallelClassicExpectimax(print, weights); } else if (AItype == AI_TYPE.PARALLEL_ITERATIVE_DEEPENING_EXPECTIMAX) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunParallelIterativeDeepeningExpectimax(print, timeLimit, weights); } else if (AItype == AI_TYPE.TT_ITERATIVE_DEEPENING_EXPECTIMAX) { Expectimax exptectimax = new Expectimax(game, depth); end = exptectimax.RunTTExpectimax(print, timeLimit, weights); } else if (AItype == AI_TYPE.TT_ITERATIVE_DEEPENING_STAR1) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunTTStar1(print, timeLimit, weights); } else if (AItype == AI_TYPE.EXPECTIMAX_WITH_ALL_IMPROVEMENTS) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunTTIterativeDeepeningExpectimaxWithStar1andForwardPruning(print, timeLimit, weights); } else if (AItype == AI_TYPE.EXPECTIMAX_WITH_ALL_IMPROVEMENTS_NO_FORWARDPRUNING) { Expectimax expectimax = new Expectimax(game, depth); end = expectimax.RunTTIterativeDeepeningExpectimaxWithStar1(print, timeLimit, weights); } else if (AItype == AI_TYPE.ITERATION_LIMITED_MCTS) { MonteCarlo MCTS = new MonteCarlo(game); end = MCTS.RunIterationLimitedMCTS(print, iterationLimit); } else if (AItype == AI_TYPE.TIME_LIMITED_MCTS) { MonteCarlo MCTS = new MonteCarlo(game); end = MCTS.RunTimeLimitedMCTS(print, timeLimit); } else if (AItype == AI_TYPE.ROOT_PARALLEL_ITERATION_LIMITED_MCTS) { MonteCarlo MCTS = new MonteCarlo(game); end = MCTS.RunRootParallelizationIterationLimitedMCTS(print, iterationLimit, NUM_THREADS); } else if (AItype == AI_TYPE.ROOT_PARALLEL_TIME_LIMITED_MCTS) { MonteCarlo MCTS = new MonteCarlo(game); end = MCTS.RunRootParallelizationTimeLimitedMCTS(print, timeLimit, NUM_THREADS); } else if (AItype == AI_TYPE.EXPECTIMAX_MCTS_TIME_LIMITED) { HeuristicLearning HL = new HeuristicLearning(game); end = HL.RunExpectimaxMCTStimeLimited(print, depth, timeLimit); } else if (AItype == AI_TYPE.EXPECTIMAX_MCTS_WITH_SIMULATIONS_TIME_LIMITED) { HeuristicLearning HL = new HeuristicLearning(game); end = HL.RunExpectimaxMCTSwithSimulations(print, depth, timeLimit); } else if (AItype == AI_TYPE.FINAL_COMBI) { HeuristicLearning HL = new HeuristicLearning(game); end = HL.RunParallelizationMCTSExpectimaxCombi(print, depth, timeLimit); } else { throw new Exception(); } if (print) { Console.WriteLine("GAME OVER!\nFinal score: " + game.scoreController.getScore()); } return end; }
public int SetAutoIris(AI_TYPE Value) { return ArtCam.SetAutoIris(m_hACam, Value); }
public static extern int SetAutoIris(int hACam, AI_TYPE Value);