public bool AreConnected(Field.Field field, Point st, Point en) { var closedset = new HashSet <Point>(); var openset = new List <Point> { en }; var gScore = new Dictionary <Point, int> { [st] = 0 }; var hScore = new Dictionary <Point, int> { [st] = Dist(st, en) }; var fScore = new Dictionary <Point, int> { [st] = hScore[st] }; while (openset.Count > 0) { openset.Sort((x, y) => LowestF(x, y, fScore)); var pointX = openset.Pop(); if (Equals(pointX, en)) { return(true); } closedset.Add(pointX); foreach (var pointY in GetNeighbourNodes(field, pointX)) { if (closedset.Contains(pointY)) { continue; } var tentativeGScore = gScore[pointX] + 1; var tentativeIsBetter = false; if (!openset.Contains(pointY)) { openset.Add(pointY); tentativeIsBetter = true; } else if (tentativeGScore < gScore[pointY]) { tentativeIsBetter = true; } if (tentativeIsBetter) { gScore[pointY] = tentativeGScore; hScore[pointY] = Dist(pointY, en); fScore[pointY] = gScore[pointY] + hScore[pointY]; } } } return(false); }
public HashSet <Point> FillForm(Field.Field field, Point me, int maxi = 200) { var old = new HashSet <Point>(); var newS = new HashSet <Point> { me }; while (newS.Count > 0 && old.Count < maxi) { var t = newS.Pop(); old.Add(t); foreach (var i in GetNeighbourNodes(field, t)) { if (field.Passable(i) || old.Contains(i)) { continue; } else { newS.Add(i); } } } return(old); }
public Tuple <decimal, MoveType> AlphaBeta(Field.Field field, int depth, decimal alpha, decimal beta, string player, MoveType bestMovetype = MoveType.None) { var moves = field.Moves(player).Select(i => i.Key).ToList(); //Console.Error.WriteLine($"update Curent-depth-{depth}"); if (depth == 0 || moves.Count == 0) { alpha = Evaluate(field, field.GetByPlayerId(player)); return(Tuple.Create(alpha, MoveType.None)); } moves = OrderByClosness(field, new Point(field.Height / 2, field.Width / 2), moves); moves = OrderByClosness(field, field.EnemyPosition, moves); if (bestMovetype != MoveType.None) { moves.Remove(bestMovetype); moves.Insert(0, bestMovetype); } var bestMove = moves.Count > 0 ? moves[0] : MoveType.None; foreach (var move in moves) { CheckTime(); field.MoveForth(move, player); var val = -AlphaBeta(field, depth - 1, -beta, -alpha, field.EnId).Item1; field.MoveBack(move, player); if (val > alpha) { bestMove = move; alpha = val; if (alpha >= beta) { return(Tuple.Create(alpha, bestMove)); } } } decimal suicideVal = -11 * (ComputePlayerValue(field, player) + 1); if (suicideVal > alpha) { foreach (var direction in field.Directions) { if (player == field.MyId || player == field.EnId) { return(Tuple.Create(suicideVal, direction)); } } } return(Tuple.Create(alpha, bestMove)); }
public List <MoveType> OrderByClosness(Field.Field field, Point to, List <MoveType> moves) { var me = field.MyPosition; var dx = me.X - to.X; var dy = me.Y - to.Y; moves.Sort(((a, b) => Order(a, b, dx, dy))); return(moves); }
public int Evaluate(Field.Field field, Point player) { var meValidMoves = field.Moves(field.MyId); var enValidMobes = field.Moves(field.EnId); var playersAdjacent = field.Adjacent(field.MyPosition).Contains(field.EnemyPosition); int result = 0; if (meValidMoves.Count == 0 || enValidMobes.Count == 0) { if (meValidMoves.Count > 0) { result = playersAdjacent ? -11 : 100; } else if (enValidMobes.Count > 0) { result = playersAdjacent ? -11 : -100; } else { result = -11; } } else { if (!AreConnected(field, field.MyPosition, field.EnemyPosition)) { var mine = FillForm(field, field.MyPosition); var their = FillForm(field, field.EnemyPosition); result = 12 + Decimal.ToInt32(Convert.ToDecimal(Math.Abs(mine.Count - their.Count)) / Convert.ToDecimal(Math.Max(mine.Count, their.Count) * 86)); if (their.Count > mine.Count) { result = -result; } } else { result = 0; } } return(result * ComputePlayerValue(field, player)); }
public BotState() { Field = new Field.Field(); Players = new Dictionary <string, Player.Player>(); }
public static int CacheFieldData() { int ret = 0; foreach (NXNode mapnode in Map.BaseNode.GetChild("Map")) { if (!mapnode.Name.StartsWith("Map")) continue; foreach (NXNode mapimg in mapnode) { CField field = new CField(); field.FieldID = int.Parse(mapimg.Name.Replace(".img", "")); NXNode node = mapimg.GetChild("info"); foreach (NXNode childnode in node) switch (childnode.Name) { case "bgm": field.WzData.bgm = GetStringFromChild(node, childnode.Name); break; case "cloud": field.WzData.cloud = GetIntFromChild(node, childnode.Name); break; case "swim": field.WzData.swim = GetIntFromChild(node, childnode.Name); break; case "forcedReturn": field.WzData.forcedReturn = GetIntFromChild(node, childnode.Name); break; case "hideMinimap": field.WzData.hideMinimap = GetIntFromChild(node, childnode.Name); break; case "mapDesc": field.WzData.mapDesc = GetStringFromChild(node, childnode.Name); break; case "mapMark": field.WzData.MapMark = GetStringFromChild(node, childnode.Name); break; case "mobRate": field.WzData.mobRate = GetFloatFromChild(node, childnode.Name); break; case "moveLimit": field.WzData.moveLimit = GetIntFromChild(node, childnode.Name); break; case "returnMap": field.WzData.returnMap = GetIntFromChild(node, childnode.Name); break; case "town": field.WzData.town = GetIntFromChild(node, childnode.Name); break; case "fieldLimit": int fl = GetIntFromChild(node, childnode.Name); if (fl >= (int) Math.Pow(2, 23)) fl = fl & ((int) Math.Pow(2, 23) - 1); field.WzData.fieldLimit = (FieldLimit) fl; break; case "VRTop": case "VRBottom": case "VRLeft": case "VRRight": break; case "timeLimit": field.WzData.timeLimit = GetIntFromChild(node, childnode.Name); break; case "lvLimit": field.WzData.lvLimit = GetIntFromChild(node, childnode.Name); break; case "onFirstUserEnter": field.WzData.onFirstUserEnter = GetStringFromChild(node, childnode.Name); break; case "onUserEnter": field.WzData.onUserEnter = GetStringFromChild(node, childnode.Name); break; case "fly": field.WzData.fly = GetIntFromChild(node, childnode.Name); break; case "noMapCmd": field.WzData.noMapCmd = GetIntFromChild(node, childnode.Name); break; case "partyOnly": field.WzData.partyOnly = GetIntFromChild(node, childnode.Name); break; case "fieldType": int ft = GetIntFromChild(node, childnode.Name); if (!Enum.IsDefined(typeof (FieldType), ft)) ft = 0; field.WzData.fieldType = (FieldType) ft; break; case "miniMapOnOff": field.WzData.miniMapOnOff = GetIntFromChild(node, childnode.Name); break; case "reactorShuffle": field.WzData.reactorShuffle = GetIntFromChild(node, childnode.Name); break; case "reactorShuffleName": field.WzData.reactorShuffleName = GetStringFromChild(node, childnode.Name); break; case "personalShop": field.WzData.personalShop = GetIntFromChild(node, childnode.Name); break; case "entrustedShop": field.WzData.entrustedShop = GetIntFromChild(node, childnode.Name); break; case "effect": field.WzData.effect = GetStringFromChild(node, childnode.Name); break; case "lvForceMove": field.WzData.lvForceMove = GetIntFromChild(node, childnode.Name); break; case "timeMob": field.WzData.startHour = GetIntFromChild(node, "startHour"); field.WzData.endHour = GetIntFromChild(node, "endHour"); int? id = GetIntFromChild(node, "id"); string message = GetStringFromChild(node, "message"); if (id == null || message == null || (field.WzData.startHour == null ^ field.WzData.endHour == null)) break; else field.WzData.timeMob = new FieldWzData.TimeMob(field.WzData.startHour, field.WzData.endHour, (int) id, message); break; case "help": field.WzData.help = GetStringFromChild(node, childnode.Name); break; case "snow": field.WzData.snow = GetIntFromChild(node, childnode.Name); break; case "rain": field.WzData.rain = GetIntFromChild(node, childnode.Name); break; case "dropExpire": field.WzData.dropExpire = GetIntFromChild(node, childnode.Name); break; case "decHP": field.WzData.decHP = GetIntFromChild(node, childnode.Name); break; case "decInterval": field.WzData.decInterval = GetIntFromChild(node, childnode.Name); break; case "autoLieDetector": field.WzData.startHour = GetIntFromChild(node, "startHour"); field.WzData.endHour = GetIntFromChild(node, "endHour"); int? interval = GetIntFromChild(node, "interval"); int? propInt = GetIntFromChild(node, "prop"); if (field.WzData.startHour == null || field.WzData.endHour == null || interval == null || propInt == null) { break; } else field.WzData.autoLieDetector = new FieldWzData.AutoLieDetector((int) field.WzData.startHour, (int) field.WzData.endHour, (int) interval, (int) propInt); break; case "expeditionOnly": field.WzData.expeditionOnly = GetIntFromChild(node, childnode.Name); break; case "fs": field.WzData.fs = GetFloatFromChild(node, childnode.Name); break; case "protectItem": field.WzData.protectItem = GetIntFromChild(node, childnode.Name); break; case "createMobInterval": field.WzData.createMobInterval = GetIntFromChild(node, childnode.Name); break; case "fixedMobCapacity": field.WzData.fixedMobCapacity = GetIntFromChild(node, childnode.Name); break; case "streetName": field.WzData.streetName = GetStringFromChild(node, childnode.Name); break; case "mapName": field.WzData.mapName = GetStringFromChild(node, childnode.Name); break; case "noRegenMap": field.WzData.noRegenMap = GetIntFromChild(node, childnode.Name); break; case "recovery": field.WzData.recovery = GetFloatFromChild(node, childnode.Name); break; case "blockPBossChange": field.WzData.blockPBossChange = GetIntFromChild(node, childnode.Name); break; case "everlast": field.WzData.everlast = GetIntFromChild(node, childnode.Name); break; case "damageCheckFree": field.WzData.damageCheckFree = GetIntFromChild(node, childnode.Name); break; case "dropRate": field.WzData.dropRate = GetFloatFromChild(node, childnode.Name); break; case "scrollDisable": field.WzData.scrollDisable = GetIntFromChild(node, childnode.Name); break; case "needSkillForFly": field.WzData.needSkillForFly = GetIntFromChild(node, childnode.Name); break; case "zakum2Hack": field.WzData.zakum2Hack = GetIntFromChild(node, childnode.Name); break; case "allMoveCheck": field.WzData.allMoveCheck = GetIntFromChild(node, childnode.Name); break; case "VRLimit": field.WzData.VRLimit = GetIntFromChild(node, childnode.Name); break; case "consumeItemCoolTime": field.WzData.consumeItemCoolTime = GetIntFromChild(node, childnode.Name); break; } if (mapimg.ContainsChild("life")) { node = mapimg.GetChild("life"); foreach (NXNode childnode in node) { string type = GetStringFromChild(childnode, "type"); if (type == "m") { var monster = new Monster(); monster.mID = GetIntFromChild(childnode, "id"); monster.mX = GetIntFromChild(childnode, "x"); monster.mY = GetIntFromChild(childnode, "y"); monster.mMobTime = GetIntFromChild(childnode, "mobTime"); monster.mF = GetIntFromChild(childnode, "f"); monster.mHide = GetIntFromChild(childnode, "hide"); monster.mFh = GetIntFromChild(childnode, "fh"); monster.mCy = GetIntFromChild(childnode, "cy"); monster.mRx0 = GetIntFromChild(childnode, "rx0"); monster.mRx1 = GetIntFromChild(childnode, "rx1"); field.AddFieldObject(monster); } else if (type == "n") { var npc = new Npc(); npc.mID = GetIntFromChild(childnode, "id"); npc.mX = GetIntFromChild(childnode, "x"); npc.mY = GetIntFromChild(childnode, "y"); npc.mF = GetIntFromChild(childnode, "f"); npc.mFh = GetIntFromChild(childnode, "fh"); npc.mCy = GetIntFromChild(childnode, "cy"); npc.mRx0 = GetIntFromChild(childnode, "rx0"); npc.mRx1 = GetIntFromChild(childnode, "rx1"); field.AddFieldObject(npc); } } } /*if (mapimg.ContainsChild("reactor")) { node = mapimg.GetChild("reactor"); foreach (NXNode childnode in node) switch (childnode.Name) { } } if (mapimg.ContainsChild("foothold")) { node = mapimg.GetChild("foothold"); foreach (NXNode childnode in node) switch (childnode.Name) { } } if (mapimg.ContainsChild("ladderRope")) { node = mapimg.GetChild("ladderRope"); foreach (NXNode childnode in node) switch (childnode.Name) { } }*/ if (mapimg.ContainsChild("foothold")) { node = mapimg.GetChild("foothold"); field.mFootholdTree.mField = field; foreach (NXNode layernode in node) { foreach (NXNode groupnode in node) { foreach (NXNode footholdnode in node) { Foothold foothold = new Foothold(); foothold.mFootholdTree = field.mFootholdTree; foothold.mLayerID = int.Parse(layernode.Name); foothold.mGroupID = int.Parse(groupnode.Name); foothold.mID = int.Parse(footholdnode.Name); foothold.mStart = new Point(GetIntFromChild(footholdnode, "x1"), GetIntFromChild(footholdnode, "y1")); foothold.mEnd = new Point(GetIntFromChild(footholdnode, "x2"), GetIntFromChild(footholdnode, "y2")); foothold.mPrevious = GetIntFromChild(footholdnode, "prev"); foothold.mNext = GetIntFromChild(footholdnode, "next"); foothold.mForce = GetIntFromChild(footholdnode, "force"); foothold.mForbidFallDown = GetIntFromChild(footholdnode, "forbidFallDown"); foothold.mPiece = GetIntFromChild(footholdnode, "piece"); field.mFootholdTree.mFootholds.Add(foothold); } } } } if (mapimg.ContainsChild("seat")) { node = mapimg.GetChild("seat"); foreach (NXNode childnode in node) { Seat seat = new Seat(); int id = int.Parse(childnode.Name); seat.mID = id; seat.mPosition = GetPointFromChild(node, childnode.Name); field.AddFieldObject(seat); } } if (mapimg.ContainsChild("portal")) { node = mapimg.GetChild("portal"); foreach (NXNode childnode in node) { Portal portal = new Portal(); portal.mPortalID = int.Parse(childnode.Name); portal.mName = GetStringFromChild(childnode, "pn"); portal.mPosition = new Point(GetIntFromChild(childnode, "x"), GetIntFromChild(childnode, "y")); portal.mDestinationMapID = GetIntFromChild(childnode, "tm"); portal.mDestinationName = GetStringFromChild(childnode, "tn"); portal.mOnce = GetIntFromChild(childnode, "onlyOnce"); switch (portal.mName) { case "sp": portal.mType = PortalType.Startpoint; break; case "pi": portal.mType = PortalType.Invisible; break; case "pv": portal.mType = PortalType.Visible; break; case "pc": portal.mType = PortalType.Collision; break; case "pg": portal.mType = PortalType.Changable; break; case "pgi": portal.mType = PortalType.ChangableInvisible; break; case "tp": portal.mType = PortalType.TownportalPoint; break; case "ps": portal.mType = PortalType.Script; break; case "psi": portal.mType = PortalType.ScriptInvisible; break; case "pcs": portal.mType = PortalType.CollisionScript; break; case "ph": portal.mType = PortalType.Hidden; break; case "psh": portal.mType = PortalType.ScriptHidden; break; case "pcj": portal.mType = PortalType.CollisionVerticalJump; break; case "pci": portal.mType = PortalType.CollisionCustomImpact; break; case "pcig": portal.mType = PortalType.CollisionUnknownPcig; break; } field.AddFieldObject(portal); } } NXDataCache.Field.Add(field.FieldID, field); ret++; } } return ret; }
public IEnumerable <Tuple <Point, MoveType> > GetAvailableMoves(Point point, Field.Field field) { var validMoves = DetermineMoves(point, field.Width, field.Height); return(validMoves.Where(i => field.GetValue(i.Item1).Free())); }
public IEnumerable <Point> GetNeighbourNodes(Field.Field field, Point x) { return(GetAvailableMoves(x, field).Select(i => i.Item1)); }
private int ComputePlayerValue(Field.Field field, Point player) => (player.Equals(field.MyPosition) ? 1 : 2);
private int ComputePlayerValue(Field.Field field, string playerId) { var player = field.GetByPlayerId(playerId); return(ComputePlayerValue(field, player)); }
public GameState() { Settings = new GameSettings(); Field = new Field.Field(); }