public static AstarResult DistanceToMove(LocationDescription description, LocationState state, int LocalTarget) { LocationResult <MainNodeResult> res = description.GenLocalForm(state); Graph <Node> graph = new Graph <Node>(res.Nodes, res.Edges, Node.HeuristicDistance); return(graph.Astar(res.CurrentLocation, LocalTarget)); }
public LocationState MoveTo(int globalNewNode, LocationState initialState) { LocationState state = new LocationState() { CurrentLocation = globalNewNode, IsDiscovered = initialState.IsDiscovered, IsVisited = initialState.IsVisited, }; var mainnode = this.MainNodes.FirstOrDefault(e => e.NodeID == globalNewNode); if (mainnode == null) { throw new Exception("Move to notmain node!"); } if (!initialState.IsDiscovered[globalNewNode]) { throw new Exception("Move to undiscovered node!"); } if (!initialState.IsVisited[globalNewNode]) { state.IsVisited[globalNewNode] = true; var paths = this.Paths.Where(e => (e.NodeFrom == globalNewNode) || (e.NodeTo == globalNewNode)); foreach (ExtensionPath path in paths) { state.IsDiscovered[path.NodeFrom] = true; state.IsDiscovered[path.NodeTo] = true; foreach (int node in path.Path) { state.IsDiscovered[node] = true; } } } return(state); }
public LocationResult <MainNodeResult> GenLocalForm(LocationState state) { //check loaded data if (state.IsDiscovered.Length != this.Nodes.Length) { throw new ArgumentException("Bad state desc"); } int counter = 0; Dictionary <int, int> dict = new Dictionary <int, int>(); List <Node> seenNodes = new List <Node>(); List <MainNodeResult> seenMains = new List <MainNodeResult>(); for (int i = 0; i < this.Nodes.Length; i++) { if (state.IsDiscovered[i]) { seenNodes.Add(this.Nodes[i]); dict.Add(i, counter++); } } foreach (MainNode main in this.MainNodes) { if (state.IsDiscovered[main.NodeID]) { MainNodeResult nowa = new MainNodeResult() { LocationType = (state.IsVisited[main.NodeID]) ? main.LocationType : LOCATIONS.UNKNOWN, Name = main.Name, NodeID = dict[main.NodeID], }; seenMains.Add(nowa); } } List <Edge> seenEdges = new List <Edge>(); foreach (Edge e in this.Edges) { if (state.IsDiscovered[e.From] && state.IsDiscovered[e.To]) { seenEdges.Add(new Edge(dict[e.From], dict[e.To], e.Value)); } } return(new LocationResult <MainNodeResult>() { LocationGlobalType = this.LocationGlobalType, CurrentLocation = dict[state.CurrentLocation], Edges = seenEdges.ToArray(), MainNodes = seenMains.ToArray(), Nodes = seenNodes.ToArray(), LocationName = this.LocationName, TravelScale = this.TravelScale, LocationID = this.LocationID, }); }
public LocationState InitializeLocation() { LocationState state = new LocationState { CurrentLocation = this.InitialNode, IsDiscovered = new bool[this.Nodes.Length], IsVisited = new bool[this.Nodes.Length] }; state.IsDiscovered[state.CurrentLocation] = true; return(MoveTo(state.CurrentLocation, state)); }
private List <int> GenerateDictionary(LocationState initialstate) { List <int> dict = new List <int>(); for (int i = 0; i < this.Nodes.Length; i++) { if (initialstate.IsDiscovered[i]) { dict.Add(i); } } return(dict); }
public int GlobalMainNodeID(int localID, LocationState localstate) { List <int> mapper = GenerateDictionary(localstate); int globalNewNode = mapper[localID]; var mainnode = this.MainNodes.FirstOrDefault(e => e.NodeID == globalNewNode); if (mainnode == null) { throw new Exception("Move to notmain node!"); } if (!localstate.IsDiscovered[globalNewNode]) { throw new Exception("Move to undiscovered node!"); } return(globalNewNode); }
public static GeneralStatus GetHeroGeneralStatus(my_appContext _context, Heros hero, DateTime now) { object statusData = null; var location = _context.HerosLocations.FirstOrDefault(e => (e.HeroId == hero.HeroId) && (e.LocationIdentifier == hero.CurrentLocation)); if (location == null) { throw new Exception("Location is not available."); } var descr = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == location.LocationIdentifier); if (descr == null) { throw new Exception("LocationData is not available."); } // TODO Location Type object locationResult = new LocationResult <object>(); int LocationType = descr.LocationGlobalType; if (LocationType != 2) { LocationDescription description = JsonConvert.DeserializeObject <LocationDescription>(descr.Sketch); LocationState state = JsonConvert.DeserializeObject <LocationState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; if (hero.Status == 1) { Traveling travel = _context.Traveling.FirstOrDefault(e => e.HeroId == hero.HeroId); if (travel == null) { throw new Exception("Traveling hero without travel in DB."); } if (travel.HasEnded(now)) { state = description.MoveTo(travel.UpdatedLocationID(), state); hero.Status = 0; location.Description = JsonConvert.SerializeObject(state); _context.Traveling.Remove(travel); try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove travel."); } } else { statusData = travel.GenTravelResult(now); } } locationResult = description.GenLocalForm(state); } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(descr.Sketch); InstanceState state = JsonConvert.DeserializeObject <InstanceState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; if (hero.Status == 1) { Traveling travel = _context.Traveling.FirstOrDefault(e => e.HeroId == hero.HeroId); if (travel == null) { throw new Exception("Traveling hero without travel in DB."); } if (travel.HasEnded(now)) { state = description.MoveTo(travel.UpdatedLocationID(), state); hero.Status = 0; location.Description = JsonConvert.SerializeObject(state); _context.Traveling.Remove(travel); try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove travel."); } } else { statusData = travel.GenTravelResult(now); } } locationResult = description.GenLocalForm(state); } if (hero.Status == 2) { Healing heal = _context.Healing.FirstOrDefault(e => e.HeroId == hero.HeroId); if (heal == null) { throw new Exception("Healing hero without heal in DB."); } if (heal.HasEnded(now)) { int newHP = heal.FinalHealth(now); hero.Hp = newHP; HeroCalculator.CheckHeroHP(hero, _context); _context.Healing.Remove(heal); hero.Status = 0; try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove healing."); } } else { statusData = heal.GenHealingResult(now); } } if (hero.Status == 3) { Fighting fight = _context.Fighting.FirstOrDefault(e => e.HeroId == hero.HeroId); if (fight == null) { throw new Exception("Healing hero without heal in DB."); } statusData = fight.GenResult(_context, hero); } return(new GeneralStatus() { HeroStatus = hero.Status, Location = locationResult, StatusData = statusData, }); }