PsyOps

Drone.Main

Jun 25th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. // Timer to control drone packet update
  7. using System.Timers;
  8. // Needed Bot core files
  9. using BattleCore;
  10. using BattleCore.Events;
  11. // Custom module for chat
  12. using BattleCore.PsyOps;
  13.  
  14. namespace Drone
  15. {
  16.     // Add the attribute and the base class
  17.     // You need to load PsyMod.IDBot for self recognition
  18.     [Behavior("Main", "true", "0.02", "PsyOps", "Main drone module.")]
  19.     public class Main:BotEventListener
  20.     {
  21.         //-------------------------------------\\
  22.         //       Command Registry               |
  23.         //-------------------------------------//
  24.         public Main()
  25.         {
  26.             // Initialization of bot - load all cfgs and needed files
  27.             RegisterTimedEvent("InitBot", 50, InitializeBot);
  28.             RegisterCommand("!dothis", dothis);
  29.             RegisterCommand("!rate", dothat);
  30.             RegisterCommand("!die", doit);
  31.         }
  32.  
  33.         // Just a shortcut to configuring and sending a message
  34.         PsyChat msg = new PsyChat();
  35.  
  36.         //-------------------------------------\\
  37.         //       Chat Commands                  |
  38.         //-------------------------------------//
  39.         public void doit(ChatEvent c)
  40.         {
  41.             if (c.ModLevel >= ModLevels.Sysop)
  42.                 Drone.Damage(c.PlayerName, 1701);
  43.         }
  44.         public void dothis(ChatEvent c)
  45.         {
  46.             if (c.ModLevel >= ModLevels.Sysop)
  47.             {
  48.                 Drone.Damage("PsyOps", 1699);
  49.                 Game(msg.pub("*prize #-13"));
  50.             }
  51.         }
  52.         public void dothat(ChatEvent c)
  53.         {
  54.             if (c.ModLevel >= ModLevels.Sysop)
  55.             {
  56.                 DroneTimer.Stop();
  57.  
  58.                 Interval = double.Parse(c.Message.Trim().Split(':')[1]);
  59.                 //setting variables
  60.                 Drone.SetVariables(Settings.Position, Interval, Settings.Range, Settings.DefaultRotation);
  61.                 Drone.SetVariables2(Settings.MaxEnergy, Settings.Recharge,Settings.BombAliveTime,Settings.BulletAliveTime,Settings.BombSpeed,Settings.BombThrust,Settings.BulletSpeed);
  62.  
  63.                 DroneTimer.Interval = Interval;
  64.                 DroneTimer.Start();
  65.  
  66.                 Game(msg.arena("Packet update rate changed to : [" + Interval + "]ms"));
  67.             }
  68.         }
  69.  
  70.         //-------------------------------------\\
  71.         //       Game Packet Update timer       |
  72.         //-------------------------------------//
  73.         // Main game timer -updates player position and anything related
  74.         Timer DroneTimer = new Timer();
  75.         // Drone timer interval in milliseconds
  76.         double Interval = 50;
  77.         bool fired = false;
  78.         public void DroneGameUpdate(object source, ElapsedEventArgs e)
  79.         {
  80.             if (Drone.Alive)
  81.             {
  82.                 while (Drone.ExpiredShots.Count > 0)
  83.                 {
  84.                     WeaponTracker.WeaponFired ExpWep = Drone.ExpiredShots.Dequeue();
  85.                     Game(msg.arena("WeaponType[" + ExpWep.Type + "] Shooter[" + ExpWep.PlayerName + "]: just expired."));
  86.                 }
  87.                 // update drone and send position packet
  88.                 Drone.UpdateDrone();
  89.  
  90.                 if (fired)
  91.                 {
  92.                     Drone.Position.MapPositionX = Drone.LastFired.MapPositionX;
  93.                     Drone.Position.MapPositionY = Drone.LastFired.MapPositionY;
  94.                 }
  95.  
  96.                 Game(Drone.Position);
  97.             }
  98.             else
  99.                 KillDrone();
  100.         }
  101.         //-------------------------------------\\
  102.         //       Initialization                 |
  103.         //-------------------------------------//
  104.         // Main Drone class
  105.         MakeDrone Drone = new MakeDrone();
  106.         // Cfg loading module
  107.         GetSettings Settings = new GetSettings();
  108.         // Initialization of bot - load all cfgs and needed files
  109.         public void InitializeBot()
  110.         {
  111.             // We need bot identifyed to enable us to dig into right directory
  112.             // for loading of cfgs.                         uses: PsyMod.BotID
  113.             if (Phase1)
  114.             {
  115.                 LoadTime = DateTime.Now;
  116.                 Phase1 = false;
  117.                 Game(msg.chan(1, "Bot Identifyed.   ...Loading CFG"));
  118.                 Settings.LoadCFG(BotName);
  119.                 SendSysRequest();
  120.                 Phase2 = true;
  121.             }
  122.             else if (Phase2)
  123.             {
  124.                 Phase2 = false;
  125.                 Game(msg.chan(1, "CFG loaded.   ...Loading Arena Settings"));
  126.                 Phase3 = true;
  127.             }
  128.             else if (Phase3)
  129.             {
  130.                 string setting = Settings.GetArenaSettings();
  131.                 if (setting.Equals("~ARENA_SETTINGS_LOADED~"))
  132.                 {
  133.                     Phase3 = false;
  134.                     Phase4 = true;
  135.                 }
  136.                 else
  137.                     Game(msg.pub(setting));
  138.             }
  139.             else if (Phase4)
  140.             {
  141.                 Phase4 = false;
  142.                 Game(msg.chan(1,"Arena settings loaded.  Applying settings to drone."));
  143.  
  144.                 //setting variables
  145.                 Ship = Settings.Ship;
  146.                 ShipNum = Settings.ShipNum;
  147.                 Drone.SetVariables(Settings.Position,Interval,Settings.Range,Settings.DefaultRotation);
  148.                 Drone.SetVariables2(Settings.MaxEnergy, Settings.Recharge, Settings.BombAliveTime,Settings.BulletAliveTime,Settings.BombSpeed,Settings.BombThrust,Settings.BulletSpeed);
  149.  
  150.                 Game(msg.chan(1,"Bot Initialized.  Bot:[" + BotName + "] Version:[" + BotVersion + "] Arena:[" + Arena + "] LoadTime:[" + ((DateTime.Now - LoadTime).Milliseconds + 50) + "ms]"));
  151.                 RemoveTimedEvent("InitBot");
  152.  
  153.                 // Configure and start main game timer
  154.                 DroneTimer.Elapsed += new ElapsedEventHandler(DroneGameUpdate);
  155.                 DroneTimer.Interval = Interval;
  156.                 DroneTimer.Start();
  157.  
  158.                 BotActivated = true;
  159.  
  160.                 Game(msg.pm(BotName,"*setship " + ShipNum));
  161.                 Game(msg.pm(BotName, "*setfreq 999"));
  162.             }
  163.         }
  164.         //-------------------------------------\\
  165.         //       BotID Info                     |     [Module] PsyMod.BotID
  166.         //-------------------------------------//
  167.         // All info received from BotID
  168.         string BotName;
  169.         string Arena;
  170.         string Ship;
  171.         byte ShipNum;
  172.         // Bot version
  173.         double BotVersion = 0.02;
  174.         DateTime LoadTime;
  175.         // Phases of initialization
  176.         bool Phase1, Phase2, Phase3, Phase4;
  177.         // Bot activated and rdy
  178.         bool BotActivated = false;
  179.         // Receive and store BotID info
  180.         public void MonitorChat(object sender, ChatEvent c)
  181.         {
  182.             if (c.Message.StartsWith("!returnID:"))
  183.             {
  184.                 string[] info = c.Message.Split(':');
  185.                 BotName = info[1];
  186.                 Arena = info[2];
  187.                 Phase1 = true;
  188.             }
  189.             else if (c.Message.Contains(":") && c.Message.Contains("=") && !c.Message.StartsWith("["))
  190.             {
  191.                 Settings.SetArenaSettings(c.Message);
  192.                 Game(msg.chan(1,"[Arena Setting] " + c.Message));
  193.             }
  194.         }
  195.         //-------------------------------------\\
  196.         //       Drone SelfSysop                |
  197.         //-------------------------------------//
  198.         // sending player info request
  199.         public void SendSysRequest()
  200.         {
  201.             PlayerInfoEvent send = new PlayerInfoEvent();
  202.             send.Players.Add(BotName);
  203.             SendGameEvent(send);
  204.         }
  205.         // Receive player list, finding itself and giving itself sysop
  206.         public void MakeDroneSys(object sender, PlayerInfoEvent p)
  207.         {
  208.             p.PlayerList.ForEach(delegate(PlayerInfo p2)
  209.             {
  210.                 if (p2.PlayerName == BotName)
  211.                 {
  212.                     p2.ModeratorLevel = ModLevels.Sysop;
  213.                     // Create the new listmod event handler
  214.                     ListModEvent makesys = new ListModEvent();
  215.                     makesys.Moderator = p2;
  216.                     SendCoreEvent(makesys);
  217.                 }
  218.             });
  219.         }
  220.         //-------------------------------------\\
  221.         //       Player Tracking                |
  222.         //-------------------------------------//
  223.         public void PlayerMovement(object sender, PlayerPositionEvent p)
  224.         {
  225.             if (BotActivated)
  226.                 Drone.NearMovement(p);
  227.  
  228.             string wep = p.Weapon.Type.ToString().ToLower();
  229.             if (wep.Contains("bullet") || wep.Contains("bomb") || wep.Contains("thor"))
  230.             {
  231.                 Game(msg.arena("Weapon[" + p.Weapon.Type + "] Tracked. Shooter[" + p.PlayerName + "]")); fired = true;
  232.             }
  233.         }
  234.         public void RemovePlayer(object sender, PlayerLeftEvent p)
  235.         { if (BotActivated) Drone.RemovePlayer(p.PlayerName); }
  236.         public void RemovePlayer(object sender, PlayerDeathEvent p)
  237.         { if (BotActivated) Drone.RemovePlayer(p.KilledName); }
  238.         public void RemovePlayer(object sender, ShipChangeEvent s)
  239.         { if (BotActivated) Drone.RemovePlayer(s.PlayerName); }
  240.  
  241.         //-------------------------------------\\
  242.         //       Bot Suicide                    |
  243.         //-------------------------------------//
  244.         // Safety in place to avoid multiple death packets sent by drone before it dies
  245.         bool sent_death_message = false;
  246.         // Drone suicide
  247.         public void KillDrone()
  248.         {
  249.             if (!sent_death_message)
  250.             {
  251.                 sent_death_message = true;
  252.                 PlayerDeathEvent DroneDie = new PlayerDeathEvent();
  253.                 DroneDie.Bounty = Drone.Position.Bounty;
  254.                 DroneDie.KillerName = Drone.Killer;
  255.                 Game(DroneDie);
  256.                 Game(msg.pm(BotName, "!kill"));
  257.             }
  258.         }
  259.         public override void Dispose()
  260.         {
  261.             throw new NotImplementedException();
  262.         }
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment