public void Remove() { if (bot) { bots--; } ActiveKeys.Remove(KeyCode); InactiveKeys.Remove(KeyCode); HEADS.Remove(KeyCode); key.Remove(); Map.OnClear -= clear; Map.OnStartGame -= Reset; Map.OnStartRound -= Revive; OnUpdate -= e_update; if (AnimationsEnabled) { window.DrawHead -= draw_event; } OnRemove?.Invoke(); OnRemove = null; Map.SetMaxPoints(); try { IKey.WIDTH = W / (float)ActiveKeys.Count; } catch (DivideByZeroException) { IKey.WIDTH = 0; } }
public void Stage2() { text = "Right before hitting an obstacle,\ndash to avoid it\n "; Head player = HEADS[key_player]; player.act = Activities.DEFAULT; player.pos = new IPoint(W * .6d, W * .19d); player.v = speed * IVector.Left; lock (Orb.OrbLock) new Orb(true).pos = new IPoint(W * 7 / 8, W * .19d); // introduce the dash with a black wall if (!HEADS.Keys.Contains(Keys.F13)) { bot = new Head(Keys.F13); bot.DisplayKey = ""; bot.color = Color.Red; HEADS.Add(Keys.F13, bot); lock (ActiveLock) ActiveKeys.Add(Keys.F13); bot1 = new Head(Keys.F14); bot1.DisplayKey = ""; bot1.color = Color.Red; HEADS.Add(Keys.F14, bot1); lock (ActiveLock) ActiveKeys.Add(Keys.F14); } bot.act = Activities.DEFAULT; bot.pos = new IPoint(W * 3 / 4, W / 4); bot.v = IVector.Down; lock (Orb.OrbLock) for (int i = 1; i < 12; i++) { new Orb(true).pos = new IPoint(W * 3 / 4, W / 2 * i / 13); } bot1.act = Activities.DEFAULT; bot1.pos = new IPoint(W * 1 / 4, W / 4); bot1.v = IVector.Up; lock (Orb.OrbLock) for (int i = 1; i < 12; i++) { new Orb(true).pos = new IPoint(W * 1 / 4, W / 2 * i / 13); } }
public void AddKey() { //Don't touch rest of func Head head = new Head(this); Key = head.KeyCode; try { Program.HEADS.Add(Key, head); } catch (ArgumentException) { HEADS.Add(Keys.Pa1, head); Key = Keys.Pa1; } OnUpdateNNW += update; All.Add(this); }
public void Draw(Graphics g) { Brush clr; if (state == (byte)OrbStates.TRAVELLING) { clr = Brushes.White; if (Owner != Keys.None && (!HEADS.ContainsKey(Owner) || HEADS[Owner].Died)) { NewOwner(); } } else if (state == (byte)OrbStates.BULLET) { double c = Math.Pow(Math.Sin(bulletTime / 8), 2) * 2 / 3; clr = new SolidBrush(Color.FromArgb((int)(color.R + (255 - color.R) * c), (int)(color.G + (255 - color.G) * c), (int)(color.B + (255 - color.B) * c))); } else { clr = new SolidBrush(Color.FromArgb(!isWhite && KingOfTheHill && HEADS[Owner].INVINCIBLE? 128 : 255, color)); } g.FillEllipse(clr, (float)pos.X - this.r, (float)pos.Y - this.r, r * 2, r * 2); if (ContrastMode && state.GetHashCode() >= 3) { g.DrawEllipse(Pens.Black, (float)pos.X - r, (float)pos.Y - r, r * 2, r * 2); contrast_color.Set(BoolToInt(Map.InOrbit(pos) && !isBullet)); g.FillEllipse(new SolidBrush(AnimatableColor.Lurp(Color.Black, Color.White, (float)contrast_color)), (float)pos.X - 3, (float)pos.Y - 3, 6, 6); } if (!isWhite && HEADS[Owner].isBounty) { g.DrawEllipse(new Pen(Color.Red, 2f), (float)pos.X - r, (float)pos.Y - r, r * 2, r * 2); } ///Debug draw ID //g.DrawString(this.ID.ToString(), new Font(FONT, 13), Brushes.Black, (PointF) pos); }
private void window_keydown(object sender, KeyEventArgs e) { if (keydown.Contains(e.KeyCode) || e.KeyCode == Keys.NumLock || e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Menu) { return; } else { keydown.Add(e.KeyCode); } Keys ekey = e.KeyCode; if (e.KeyCode == Keys.Delete) { ekey = Keys.Decimal; } else if (e.KeyCode == Keys.Insert) { ekey = Keys.NumPad0; } else if (e.KeyCode == Keys.Clear) { ekey = Keys.NumPad5; } else if (e.KeyCode == Keys.F2) { ContrastMode = !ContrastMode; return; } else if (e.KeyCode == Keys.F3) { if (state == States.NEWGAME && Neat.All.Count < 24) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Bot files (*.bot)|*.bot|All files (*.*)|*.*"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { List <byte> bytes = new List <byte>(File.ReadAllBytes(dialog.FileName)); Neat.remove(bytes, 8); Neat n = Neat.decompile(bytes); n.SetupGenZero(); n.AddKey(); IKey.UpdateAll(); Map.SetMaxPoints(); // Set color Program.HEADS[n.Key].color = n.color.GetValueOrDefault(Entities.Head.GenerateColor()); } } return; } else if (e.KeyCode == Keys.F1) { if (state != States.NEWGAME) { return; } if (ActiveKeys.Count > 0) { new AI(ActiveKeys[ActiveKeys.Count - 1]); MVP.Flash($"AI added to {HEADS[ActiveKeys[ActiveKeys.Count - 1]].DisplayKey}"); HEADS[ActiveKeys[ActiveKeys.Count - 1]].DisplayKey = "AI"; } return; } else if (e.KeyCode == Keys.F4 && !e.Alt) { if (state == States.NEWGAME) { if (Neat.All.Count > 0) { Neat toRemove = Neat.All.Last(); toRemove.Remove(); IKey.UpdateAll(); } Map.SetMaxPoints(); } return; } else if (e.KeyCode == Keys.F9 && e.Alt) { if (ActiveKeys.Count == 0 || InactiveKeys.Count == 0) { return; } HEADS[InactiveKeys[0]].Reward(0, ActiveKeys[0]); HEADS[ActiveKeys[0]].Die(); return; } else if (e.KeyCode == Keys.F10) { if (SyncUpdate) { SyncUpdate = false; UpdateThread.Stop(); NeuralThread.Stop(); StartAsyncUpdate(); } else { SyncUpdate = true; UpdateThread.Start(); NeuralThread.Start(); } return; } else if (e.KeyCode == Keys.F7) { SlowMo = true; return; } else if (e.KeyCode == Keys.F8) { SpeedMo = true; return; } else if (e.KeyCode == Keys.F11) { FullScreen = !FullScreen; if (FullScreen) { Width = Screen.PrimaryScreen.Bounds.Width; FormBorderStyle = FormBorderStyle.None; WindowState = FormWindowState.Maximized; Cursor.Hide(); } else { Width = 1024; Height = 600; FormBorderStyle = FormBorderStyle.Sizable; WindowState = FormWindowState.Normal; Cursor.Show(); } return; } else if (e.KeyCode == Keys.F12) { if (!e.Alt) { if (map is BotArena) { List <byte> bytes = new List <byte>(); bytes.AddRange(((BotArena)map).GetGeneration()); foreach (Neat bot in ((BotArena)map).bots) { bytes.AddRange(Neat.compile(bot)); } SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Bot files (*.bot)|*.bot|All files (*.*)|*.*"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { File.WriteAllBytes(dialog.FileName, bytes.ToArray()); } } else if (ActiveKeys.Count == 0) { Cursor.Show(); OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Bot files (*.bot)|*.bot|All files (*.*)|*.*"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { List <byte> bytes = new List <byte>(File.ReadAllBytes(dialog.FileName)); BotArena arena = new BotArena(BotArena.Type.MAX_POINTS); arena.SetGeneration(bytes); map = arena; while (bytes.Count > 0) { arena.bots.Add(Neat.decompile(bytes)); } arena.AddBots(false); Map.StartGame(); Ingame = true; state = States.INGAME; } if (FullScreen) { Cursor.Hide(); } } } else if (state == States.NEWGAME && !TutorialActive) { if (ActiveKeys.Count != 1) { return; } TUTO = new Tutorial(Map); TutorialActive = true; Map.StartGame(); if (SyncUpdate) { NeuralThread.Start(); } } return; // trigger tutorial } else if (e.KeyCode == Keys.F6) { if (ActiveKeys.Count != 0) { return; } BotArena arena = new BotArena(7, BotArena.Type.MAX_POINTS); /// < - - - - - - - - - - - - - - - - map = arena; arena.AddBots(false); Map.StartGame(); Ingame = true; state = States.INGAME; return; // trigger bot arena } else if (e.KeyCode == Keys.F5) { if (state != States.NEWGAME) { return; } switch (Gamemode) { case Gamemodes.CLASSIC: MVP.SetText("Red chaos"); Gamemode = Gamemodes.CHAOS_RED; foreach (Head head in HEADS.Values) { head.NewColor(true); } break; case Gamemodes.CHAOS_RED: MVP.SetText("Rainbow chaos"); Gamemode = Gamemodes.CHAOS_RAINBOW; foreach (Head head in HEADS.Values) { head.NewColor(); } break; case Gamemodes.CHAOS_RAINBOW: MVP.SetText("King of the hill"); foreach (Head head in HEADS.Values) { head.NewColor(false); } Map.MaxOrbs = 7; Gamemode = Gamemodes.KING_OF_THE_HILL; break; case Gamemodes.KING_OF_THE_HILL: MVP.SetText("Hidden"); Map.MaxOrbs = 8; Gamemode = Gamemodes.HIDDEN; break; case Gamemodes.HIDDEN: MVP.SetText("Yeet Mode"); Gamemode = Gamemodes.YEET_MODE; Map.blastSpawn = BlastSpawn.RARE; break; case Gamemodes.YEET_MODE: MVP.SetText("Classic"); Map.MaxOrbs = 255; Gamemode = Gamemodes.CLASSIC; break; } UpdateColors?.Invoke(); return; } switch (state) { case States.NEWGAME: //add new player if (e.KeyCode == Keys.Enter && ActiveKeys.Count > 0) // (re)start game { if (e.Shift) { Map.ResumeGame(); } else { Map.StartGame(); } if (SyncUpdate) { NeuralThread.Start(); } Ingame = true; state = States.INGAME; } else if (e.KeyCode == Keys.Escape) // clear all keys { HashSet <Neat> NeatCopy = new HashSet <Neat>(Neat.All); foreach (Neat neat in NeatCopy) { neat.Remove(); } HashSet <Head> HeadCopy = new HashSet <Head>(HEADS.Values); foreach (Head head in HeadCopy) { head.Remove(); } Map.MaxPoints = 0; Leader = Keys.None; } else if (0 <= e.KeyValue && e.KeyValue < 256) //add key { lock (ActiveLock) { if (ActiveKeys.Contains(ekey)) { HEADS[ekey].Remove(); } else { if (HEADSOnPause.ContainsKey(ekey)) { HEADS.Add(ekey, new Head(HEADSOnPause[ekey])); } else { HEADS.Add(ekey, new Head(ekey)); } ActiveKeys.Add(ekey); Map.SetMaxPoints(); } } IKey.UpdateAll(); } break; case States.INGAME: if (e.KeyCode == Keys.Escape) //pause game { ApplicationPause(); } else if (ActiveKeys.Contains(ekey)) //default action { HEADS[ekey].Action(); } break; case States.PAUSED: if (e.KeyCode == Keys.Escape) // unpause { state = States.INGAME; Ingame = true; if (SyncUpdate) { UpdateThread.Start(); NeuralThread.Start(); } else { StartAsyncUpdate(); } VisualThread.Start(); //ForcePaused = false; } else if (e.KeyCode == Keys.Enter) // let players join { HEADSOnPause.Clear(); foreach (KeyValuePair <Keys, Head> player in HEADS) { HEADSOnPause.Add(player.Key, player.Value); } Map.EndGame(); Map.Clear(); Map.phase = Phases.NONE; state = States.NEWGAME; MVP.Show("Prepare!"); lock (ActiveLock) foreach (Keys key in ActiveKeys) { HEADS[key].v = IVector.Up; } Ingame = false; VisualThread.Start(); if (SyncUpdate) { UpdateThread.Start(); } else { StartAsyncUpdate(); } } break; } }
private void fetch_input() { //inorbit, rotation, x, y, orbs in tail //twelve rays //each has distance to first { orbit, blast, orb, orb, player } //type: player | blast | orb(w) | orb(p) | wall | orbits //65 input nodes total if (!HEADS.ContainsKey(Key) || Map.phase != Phases.NONE) { return; } Head head = HEADS[Key]; if (head.Died) { return; } ray.Set(head.pos, head.v); Input[0].add(BoolToInt(Map.InOrbit(head.pos))); Input[1].add(head.v.A / PI); Input[2].add(head.pos.X / W * 2 - 1); Input[3].add(head.pos.Y / W); lock (Orb.OrbLock) Input[4].add(head.tail.length == 0? -1 : 1 / head.tail.length); for (int i = 0; i < 12; i++) { foreach (Circle orbit in Map.Orbits) { if (ray.Hit(orbit)) { distances.Add(ray.Distance(orbit)); } } if (distances.Count == 0) { distances.Add(diagonal); } Input[5 + 5 * i].add(distances.Min() / diagonal); distances.Clear(); lock (Blast.BlastLock) foreach (Circle blast in Blast.All) { if (ray.Hit(blast)) { distances.Add(ray.Distance(blast)); } } if (distances.Count == 0) { distances.Add(diagonal); } Input[6 + 5 * i].add(distances.Min() / diagonal); distances.Clear(); lock (Orb.OrbLock) { int color = 760; double d = diagonal; double d_temp; foreach (Orb orb in Orb.All) { if (orb.state == (byte)OrbStates.TRAVELLING) { d_temp = ray.AutoDistance(orb); } else { d_temp = diagonal; } if (d_temp < d) { d = d_temp; color = (orb.color.R + orb.color.G + orb.color.B); } } Input[7 + 5 * i].add(d / diagonal); Input[8 + 5 * i].add(color / 760d); } lock (ActiveLock) foreach (Keys k in ActiveKeys) { if (k != Key && ray.Hit(HEADS[k])) { distances.Add(ray.Distance(HEADS[k])); } } if (distances.Count == 0) { distances.Add(diagonal); } Input[9 + 5 * i].add(distances.Min() / diagonal); distances.Clear(); ray.laser.A += deltaA; } // 66th input neuron Input[65].add(1); // Always on }