PsyOps

Body.cs

Apr 26th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using BattleCore;
  7. using BattleCore.Events;
  8. using BattleCorePsyOps;
  9. using System.Timers;
  10.  
  11. namespace SpecRPG
  12. {
  13.  
  14.     [Behavior("Body", "true", "0.01", "KenNPsy", "SpecRPG")]
  15.  
  16.     public class Body : BotEventListener
  17.     {
  18.  
  19.         public Body()
  20.         {
  21.  
  22.             //Makes sure spam control is on
  23.             msg.SpamControl = true;
  24.  
  25.             //Log bot into channel
  26.             msg.SCPubCommand(JoinChatCommand);
  27.  
  28.             RegisterCommand("!map", doPrintMap);
  29.  
  30.         }
  31.  
  32.         // -----------------------------Variables and Needed Classes---+
  33.  
  34.         //Psy's Module to help with chat events
  35.         ShortChat msg = new ShortChat();
  36.  
  37.         //Stores botname and arena
  38.         string BotName, Arena = "";
  39.  
  40.         //Game Timer
  41.         Timer GameTimer = new Timer();
  42.  
  43.         //Random for dice
  44.         Random random = new Random();
  45.  
  46.         //Game Tick
  47.         DateTime ts_GameUpdate = DateTime.Now;
  48.  
  49.         //MapOps
  50.         SPMapOps gm = new SPMapOps();
  51.        
  52.         //Time in Minutes to tick
  53.         double delay_GameUpdate = 5000;
  54.  
  55.         //In case of a Bot disconnect BUT not a crash bot rejoins chat
  56.         DateTime ts_RejoinChats = DateTime.Now;
  57.  
  58.         //Time in minutes that you want bot to rejoin chats
  59.         double delay_RejoinChats = 5;
  60.  
  61.  
  62.         //lists
  63.         List<Spec> SpecList = new List<Spec>();
  64.         List<SPlayer> PlayerList = new List<SPlayer>();
  65.         List<SPMap> MapList = new List<SPMap>();
  66.  
  67.         // -----------------------------Game Timer---+
  68.         public void TimerMethod(object sender, ElapsedEventArgs e)
  69.         {
  70.  
  71.             //Check to see if we have any commands in our command chat q
  72.             if (msg.NeedUpdate) Game(msg.sendNextBCommand());
  73.  
  74.             // One game cycle
  75.             GameUpdate();
  76.  
  77.             // Safety in case bot lags out
  78.             // Joins chat every 5 min
  79.             ReJoinChatCheck();
  80.  
  81.         }
  82.  
  83.         // -----------------------------SubSpace Events ---+
  84.  
  85.         //Monitor all incoming chat events
  86.         public void MonitorChatEvents(object sender, ChatEvent c)
  87.         {
  88.  
  89.             //Core sends each bot a pm with its own info 500ms after startup
  90.             //Use it to Initialize Bot
  91.             if (c.ChatType == ChatTypes.Private && c.Message.StartsWith("@BotInfo@"))
  92.             {
  93.  
  94.                 // Catch and store bot info that core sends
  95.                 string[] data = c.Message.Split(':');
  96.                 BotName = data[1];
  97.                 Arena = data[2];
  98.  
  99.                 // Initialize everything needed to get bot started
  100.                 InitializeBot();
  101.  
  102.             }
  103.         }
  104.  
  105.         public void MonitorPlayerEnter(object sender, PlayerEnteredEvent pe)
  106.         {
  107.  
  108.             if (pe.ShipType == ShipTypes.Spectator)
  109.             {
  110.  
  111.                 msg.SCChanMessage(1, pe.PlayerName + " has joined spec! [Entered Zone/Arena]");
  112.                 Spec inspec = new Spec();
  113.                 inspec.PlayerName = pe.PlayerName;
  114.  
  115.                 PlayerJoinGame(pe.PlayerName);
  116.  
  117.             }
  118.  
  119.             if (pe.ShipType != ShipTypes.Spectator)
  120.             {
  121.  
  122.                 msg.SCChanMessage(1, pe.PlayerName + " has left spec! [Not in Spec]");
  123.                 SpecList.RemoveAll(a => a.PlayerName == pe.PlayerName);
  124.  
  125.             }
  126.         }
  127.  
  128.         public void MonitorPlayerLeave(object sender, PlayerLeftEvent pl)
  129.         {
  130.  
  131.             msg.SCChanMessage(1, pl.PlayerName + " has left spec! [Left Zone/Arena]");
  132.             SpecList.RemoveAll(a => a.PlayerName == pl.PlayerName);
  133.  
  134.         }
  135.  
  136.         public void MonitorShipEvents(object sender, ShipChangeEvent sc)
  137.         {
  138.  
  139.             //Checks if player changed into spec
  140.             if (sc.ShipType == ShipTypes.Spectator)
  141.             {
  142.  
  143.                 msg.SCChanMessage(1, sc.PlayerName + " has joined spec! [Ship Change]");
  144.                 Spec inspec = new Spec();
  145.                 inspec.PlayerName = sc.PlayerName;
  146.  
  147.                 PlayerJoinGame(sc.PlayerName);
  148.  
  149.             }
  150.  
  151.             //Checks if player changed out of spec
  152.             if (sc.ShipType != ShipTypes.Spectator)
  153.             {
  154.  
  155.                 msg.SCChanMessage(1, sc.PlayerName + " has left spec! [Ship Change]");
  156.                 SpecList.RemoveAll(a => a.PlayerName == sc.PlayerName);
  157.  
  158.             }
  159.         }
  160.  
  161.         public void doPrintMap(ChatEvent c)
  162.         {
  163.             Queue<string> m = gm.MapPrintOut(BotName);
  164.             while (m.Count > 0) Game(msg.pm(c.PlayerName, m.Dequeue()));
  165.         }
  166.  
  167.         // -----------------------------Main RPG Code---+
  168.  
  169.         public void GameUpdate()
  170.         {
  171.             if ((DateTime.Now - ts_GameUpdate).TotalMilliseconds > delay_GameUpdate)
  172.             {
  173.                 // Update time stamp
  174.                 ts_GameUpdate = DateTime.Now;
  175.  
  176.                 // Grab random player
  177.                 SPlayer RPlayer = GetRandomPlayer();
  178.  
  179.                 // u can tweak 10 up or down depending on how you wanna weigh event ignore
  180.                 switch (0)
  181.                 {
  182.                     case 0://Move Event
  183.                         gm.MovePlayer(RPlayer);
  184.                         msg.SCChanMessage(1, "|MOVE| - "+RPlayer.PlayerName+ " has moved to " + gm.MapList[1].MapName+"!");
  185.                         break;
  186.                     case 1://Duel Event
  187.                         msg.SCChanMessage(1, "Event dice rolled a [1]");
  188.                         break;
  189.                     case 2://Mob Event
  190.                         msg.SCChanMessage(1, "Event dice rolled a [2]");
  191.                         break;
  192.                     case 3://Item Event
  193.                         msg.SCChanMessage(1, "Event dice rolled a [3]");
  194.                         break;
  195.                     case 4://Luck Event
  196.                         msg.SCChanMessage(1, "Event dice rolled a [4]");
  197.                         break;
  198.                     default:
  199.                         msg.SCChanMessage(1, "Ignore events.");
  200.                         break;
  201.                 }
  202.             }
  203.         }
  204.  
  205.         // -----------------------------RPG Functions---+
  206.  
  207.         //Dice Roll
  208.         //Creates 30 random numbers from Min,Max and selects 1 from that 30 randomly
  209.         public int DiceRoll(int MinValue, int MaxValue)
  210.         {
  211.             int[] intbag = new int[30];
  212.  
  213.             for (int i = 0; i < 30; i++)
  214.             {
  215.  
  216.                 intbag[i] = random.Next(MinValue, MaxValue);
  217.  
  218.             }
  219.  
  220.             return intbag[random.Next(0, 30)];
  221.  
  222.         }
  223.  
  224.         //Create Player with default attributes
  225.         public SPlayer CreatePlayer(string PlayerName)
  226.         {
  227.  
  228.             //Default Values
  229.             SPlayer cp = new SPlayer();
  230.             cp.PlayerName = PlayerName;
  231.             cp.MapIndex = 4;
  232.             cp.Experience = 0;
  233.             cp.Level = 1;
  234.             cp.WepCon = 0;
  235.             cp.WepMat = 0;
  236.             cp.Weapon = 0;
  237.  
  238.             return cp;
  239.  
  240.         }
  241.  
  242.         //Let Player Join Game
  243.         public void PlayerJoinGame(string PlayerName)
  244.         {
  245.  
  246.             //Var to hold player
  247.             SPlayer Player;
  248.  
  249.                 //If Player not found - Register Player
  250.  
  251.             if (PlayerList.Exists(a => a.PlayerName == PlayerName))
  252.             {
  253.                 msg.SCChanMessage(1, "|LOGIN| " + GetPlayer(PlayerName).PlayerName + " has Logged on!!");
  254.             }
  255.             else
  256.             {
  257.                 Player = CreatePlayer(PlayerName);
  258.                 msg.SCChanMessage(1, "|REGISTER| " + Player.PlayerName + " was born!");
  259.                 PlayerList.Add(Player);
  260.             }
  261.  
  262.         }
  263.  
  264.         //Get player from list
  265.         public SPlayer GetPlayer(string PlayerName)
  266.         {
  267.  
  268.             //Loop through list
  269.             for (int i = 0; i < PlayerList.Count; i += 1)
  270.             {
  271.  
  272.                 //if there is a name match, send back player
  273.                 if (PlayerName == PlayerList[i].PlayerName)
  274.                     return PlayerList[i];
  275.  
  276.             }
  277.  
  278.             return null;
  279.  
  280.         }
  281.  
  282.         public Spec GetSpecPlayer(string PlayerName)
  283.         {
  284.  
  285.             //Loop through list
  286.             for (int i = 0; i < SpecList.Count; i += 1)
  287.             {
  288.  
  289.                 //if there is a name match, send back player
  290.                 if (PlayerName == SpecList[i].PlayerName)
  291.                     return SpecList[i];
  292.  
  293.             }
  294.  
  295.             return null;
  296.  
  297.         }
  298.  
  299.         public SPlayer GetRandomPlayer()
  300.         {
  301.  
  302.             return PlayerList[DiceRoll(0, PlayerList.Count)];
  303.  
  304.         }
  305.  
  306.  
  307.  
  308.         // -----------------------------Misc Functions---+
  309.  
  310.         //Join chat command for bot to send
  311.         string JoinChatCommand = "?chat=specrpg";
  312.  
  313.         public void ReJoinChatCheck()
  314.         {
  315.  
  316.             //Saftey chat rejoin in case bot lags out - NOT a crash
  317.             if ((DateTime.Now - ts_RejoinChats).TotalMinutes > delay_RejoinChats)
  318.             {
  319.  
  320.                 //Updates time stamp
  321.                 ts_RejoinChats = DateTime.Now;
  322.                 msg.SCPubCommand(JoinChatCommand);
  323.  
  324.             }
  325.  
  326.         }
  327.  
  328.         public void InitializeBot()
  329.         {
  330.             //Configure and start main game timer
  331.             GameTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
  332.             GameTimer.Interval = 10;
  333.             GameTimer.Start();
  334.  
  335.             //Creates map in MapList if need be
  336.             gm.MapCreate(9);
  337.  
  338.             //update chat with bot initialized msg
  339.             msg.SCChanMessage(1, BotName + " initialized.");
  340.         }
  341.  
  342.         public override void Dispose()
  343.         {
  344.  
  345.             throw new NotImplementedException();
  346.  
  347.         }
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment