public static Packet Read(Connection c) { byte[] abHead = new byte[6]; int len = 0; while (len < 6) len += c.RetreiveBytes(ref abHead, len); //if (c.RetreiveBytes(ref abHead) != 6) // throw new Exception("ouch: malformed head"); int id = readShortFrom(ref abHead, 0); len = readIntFrom(ref abHead, 2); if (len < 0) { Console.WriteLine("Something went wrong: got negative Packet length"); return null; } byte[] abData = new byte[len]; len = c.RetreiveBytes(ref abData); Packet oRet = null; if (types.ContainsKey(id)) { oRet = (Packet)Assembly.GetExecutingAssembly().CreateInstance(types[id].FullName); oRet.createFromData(ref abData); } else { oRet = new PackUnknown(); } return oRet; }
public bool DoHandshake(Connection c) { byte[] buffer = this.Build(); c.SendBytes(ref buffer); Packet p = Packet.Read(c); if (p == null || !p.GetType().Equals(typeof(PackHandshake))) return false; this.mlPlayerId = ((PackHandshake)p).PlayerId; return true; }
public static void RunCommand(string vsName, Connection voConn) { string[] asParts = vsName.Split(' '); string sCommand = asParts[0].Trim(); List<string> lsParams = new List<string>(); for (int i=1; i<asParts.Length; i++) if (asParts[i].Trim().Length > 0) lsParams.Add(asParts[i].Trim()); if (!mcCommands.ContainsKey(sCommand)) Console.WriteLine("command " + sCommand + " not found"); else mcCommands[sCommand].Run(lsParams, voConn); }
public static void Main(string[] args) { string sName = "borg"; string sURL = "10.1.1.19"; //string sURL = "test.scubywars.de"; int lPort = 1337; MainClass mc = new MainClass(); mc.moWorld = new World(sName); mc.moWorld.PlayerEntered += mc.PlayerEntered; InpScript oScript = InpScript.TryParse("/home/uditaren/td/src/ScubyNet/testbot.inp"); mc.c = new Connection(sName, sURL, lPort); mc.moWorld.RegisterBot(oScript, mc.c); mc.mcConnections.Add(mc.c.ID, mc.c); for (int i=0;i< 2; i++) { Connection oC = new Connection(sName, sURL, lPort); mc.moWorld.RegisterBot(oScript, oC); mc.mcConnections.Add(oC.ID, oC); new Thread(new ThreadStart(new DummyReader(oC).Read)).Start(); } //new Thread(new ThreadStart(mc.ProcessPackages)).Start(); Vector2D a = new Vector2D(100.0, 100.0); Vector2D b = new Vector2D(800.0, 100.0); Console.WriteLine(a.DistanceTo(b)); Console.WriteLine(b.DistanceTo(a)); Console.WriteLine(a.VirtualDistanceTo(b)); Console.WriteLine(b.VirtualDistanceTo(a)); Console.WriteLine(a.AngleTo(b)); Console.WriteLine(b.AngleTo(a)); //Console.WriteLine (Math.Atan2( 0.0, 1.0 )); //Console.WriteLine (Math.Atan2( 1.0, 1.0 )); //Console.WriteLine (Math.Atan2( 1.0, 0.0 )); //Console.WriteLine (Math.Atan2( 1.0, -1.0 )); //Console.WriteLine (Math.Atan2( 0.0, -1.0 )); //Console.WriteLine (Math.Atan2( -1.0, -1.0 )); //Console.WriteLine (Math.Atan2( -1.0, 0.0 )); //Console.WriteLine (Math.Atan2( -1.0, 1.0 )); }
public void Trigger(Connection voConn) { bool nextif = false; foreach (string sLine in msCommands) { string sFullLine = sLine.Replace("_", "{" + voConn.ID + "}" ); sFullLine = sFullLine.Replace("*", GetStarValue(voConn) ); sFullLine = Eval(sFullLine, false).Trim(); if (sFullLine.Contains("{ERR")) { Console.WriteLine("error in line: " + sLine); Console.WriteLine(" >> " + sFullLine); continue; } if (nextif) { if (sFullLine.Equals(".")) { nextif = false; continue; } else if (sFullLine.StartsWith("?")) { nextif = false; } else continue; } if (sFullLine.Equals(".")) continue; if (sFullLine.StartsWith("?")) { string sCompare = sFullLine.Substring(1).Trim(); if (!MatchCondition(sCompare)) { nextif = true; } else { Console.WriteLine(sCompare + " is true!"); } } else if (InpCommand.HasCommand(sFullLine.Split(' ')[0].Trim())) { Console.WriteLine("process line: " + sFullLine); InpCommand.RunCommand(sFullLine, voConn); } else { // unknown Console.WriteLine("Dunno what to do with: " + sFullLine); } } }
public override void Run(List<string> vlsParams, Connection voConn) { lock (voConn) { foreach (string param in vlsParams) { if (param.Equals("move")) { voConn.NextAction.Thrust = false; } if (param.Equals("left")) { voConn.NextAction.Left = false; } if (param.Equals("right")) { voConn.NextAction.Right = false; } if (param.Equals("fire")) { voConn.NextAction.Fire = false; } } } }
private string GetStarValue(Connection voConn) { Player me = World.TheWorld.GetPlayer(voConn.ID); if (EntityPool == EPool.HITPOINT) { if (EntityMatch != EMatch.OTHER) { throw new NotImplementedException(); } List<Vector2D> lP = new List<Vector2D>(); foreach (Player p in World.TheWorld.Players.Values) { if (!p.IsFriend) { Vector2D pnt = me.GetHitpoint(p, true); lP.Add(pnt); } } if (lP.Count == 0) return "(500.0|500.0)"; if (lP.Count == 1) return "(" + lP[0].X.ToString() + "|" + lP[0].Y.ToString() + ")"; Vector2D pRet = lP[0]; double dist = me.Position.VirtualDistanceTo(pRet); for (int i=1; i<lP.Count; i++) { double dist2 = me.Position.VirtualDistanceTo(lP[i]); if ((EMin && dist2 < dist) || (!EMin && dist2 > dist)) { dist = dist2; pRet = lP[i]; } } return "(" + pRet.X.ToString() + "|" + pRet.Y.ToString() + ")"; } else { List<Entity> lEnt = new List<Entity>(); if (EntityPool == EPool.ENTITY || EntityPool == EPool.PLAYER) { foreach (Player e in World.TheWorld.Players.Values) { switch (EntityMatch) { case EMatch.ALL: lEnt.Add(e); break; case EMatch.MY: if (e.ID == voConn.ID) lEnt.Add(e); break; case EMatch.OUR: if (e.IsFriend) lEnt.Add(e); break; case EMatch.OTHER: if (!e.IsFriend) lEnt.Add(e); break; } } } else { foreach (Shot e in World.TheWorld.Shots.Values) { switch (EntityMatch) { case EMatch.ALL: lEnt.Add(e); break; case EMatch.MY: if (e.ParentId == voConn.ID) lEnt.Add(e); break; case EMatch.OUR: if (e.Parent.IsFriend) lEnt.Add(e); break; case EMatch.OTHER: if (!e.Parent.IsFriend) lEnt.Add(e); break; } } } if (lEnt.Count == 0) return "(500.0|500.0)"; if (lEnt.Count == 1) return "{" + lEnt[0].ID + "}"; if (EntityOrder == EOrder.NONE) return "{" + lEnt[0].ID + "}"; Entity eRet = lEnt[0]; double diff = GetDiffToEntity(me, eRet); for (int i = 1; i < lEnt.Count; i++) { double diff2 = GetDiffToEntity(me, lEnt[i]); if ((EMin && diff2 < diff) || !EMin && diff2 > diff) { diff = diff2; eRet = lEnt[i]; } } return "{" + eRet.ID + "}"; } }
public DummyReader(Connection voConn) { moConn = voConn; }
public override void Run(List<string> vlsParams, Connection voConn) { lock (voConn) { voConn.NextAction.Thrust = true; } }
public abstract void Run(List<string> vlsParams, Connection voConn);
public InpScriptEventRunner(InpScriptEvent voSEvent, Connection voConn) { moEvent = voSEvent; moConn = voConn; }
public void RegisterBot(InpScript voScript, Connection voConn) { foreach (InpScriptEvent oSE in voScript.Events.Values) { InpEvent e = InpEvent.GetEvent(oSE.Name); if (e != null) { InpScriptEventRunner oRunner = new InpScriptEventRunner(oSE, voConn); e.FireEvent += oRunner.ConsumeEvent; mcoRunners.Add(oRunner); Console.WriteLine("Event " + oSE.Name + " registered"); } else { Console.WriteLine("Event " + oSE.Name + " not found"); } } }