PsyOps

ToonRPG

May 7th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.98 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 AutoRPG
  12. {
  13.     [Behavior("Main", "true", "0.01", "KenNPsy", "Rpg game that plays itself.")]
  14.     public class Main: BotEventListener
  15.     {
  16.         public Main()
  17.         {
  18.             RegisterCommand("!togglechat", doToggleChatNotifications);
  19.             RegisterCommand("!debug", doToggleDebugMode);
  20.             RegisterCommand("!map", doPrintMap);
  21.  
  22.             //Makes sure spam control is on
  23.             msg.SpamControl = true;
  24.             //Log bot into channel
  25.             msg.SCPubCommand(JoinChatCommand);
  26.         }
  27.  
  28.         // ---------------------------------- Variables and needed Classes -------------------------- //
  29.         //Psy's Module to help with chat events
  30.         ShortChat msg = new ShortChat();
  31.  
  32.         //Stores botname and arena
  33.         string BotName, Arena = "";
  34.  
  35.         //Game Timer
  36.         Timer GameTimer = new Timer();
  37.  
  38.         //Random for dice
  39.         Random random = new Random();
  40.  
  41.         // Join chat command for bot to send
  42.         string JoinChatCommand = "?chat=battledev";
  43.  
  44.         // All Map Operations
  45.         TTMapOps World = new TTMapOps();
  46.  
  47.         // List of players on spec
  48.         List<string> ArenaSpecList = new List<string>();
  49.  
  50.         // Main RPG Player List
  51.         List<TTPlayer> rpg_PlayerList = new List<TTPlayer>();
  52.  
  53.         // Game status
  54.         bool GameOn = false;
  55.  
  56.         // Game chat - on or off
  57.         bool ChatNotificationOn = true;
  58.  
  59.         // ---------------------------------- Subspace Game Events ---------------------------------- //
  60.         //Monitor all incoming chat events
  61.         public void MonitorChatEvents(object sender, ChatEvent c)
  62.         {
  63.             //Core sends each bot a pm with its own info 500ms after startup
  64.             //Use it to Initialize Bot
  65.             if (c.ChatType == ChatTypes.Private && c.Message.StartsWith("@BotInfo@"))
  66.             {
  67.                 // Catch and store bot info that core sends
  68.                 string[] data = c.Message.Split(':');
  69.                 BotName = data[1];
  70.                 Arena = data[2];
  71.  
  72.                 // Initialize everything needed to get bot started
  73.                 InitializeBot();
  74.             }
  75.  
  76.             if (c.ChatType == ChatTypes.Arena && c.Message.StartsWith("WARNING: Disconnected from server"))
  77.             {
  78.                 if (GameOn) GameOn = false;
  79.                 BotCrashed = true;
  80.             }
  81.             else if (c.ChatType == ChatTypes.Arena && c.Message.StartsWith("This arena is Continuum-only. Please get Continuum client"))
  82.             {
  83.                 if (!GameOn)
  84.                     Game(msg.arena("Normal login detected!"));
  85.                 else
  86.                     Game(msg.arena("Bot crash detected!"));
  87.  
  88.                 if (!BotCrashed) return;
  89.  
  90.                 BotCrashed = false;
  91.                 GameOn = true;
  92.  
  93.                 Game(msg.pub(JoinChatCommand));
  94.             }
  95.         }
  96.         bool BotCrashed = false;
  97.         // All players coming in should be in spec because of arena settings
  98.         // Have the game check their status here - registered/unregistered
  99.         public void MonitorPlayerEnter(object sender, PlayerEnteredEvent pe)
  100.         {
  101.             if (pe.ShipType == ShipTypes.Spectator)
  102.                  WentToSpec(pe.PlayerName);
  103.         }
  104.  
  105.         // Player needs to be updated as he leaves, and take him out of game
  106.         public void MonitorPlayerLeave(object sender, PlayerLeftEvent pl)
  107.         {   RemoveFromSpecList(pl.PlayerName);  }
  108.  
  109.         // Check to see if players are going into/out of spectator mode
  110.         public void MonitorPlayerShipChanges(object sender, ShipChangeEvent sc)
  111.         {
  112.             // Coming into spec
  113.             if (sc.PreviousShipType != ShipTypes.Spectator && sc.ShipType == ShipTypes.Spectator)
  114.                 WentToSpec(sc.PlayerName);
  115.             // Going out of spec
  116.             if (sc.PreviousShipType == ShipTypes.Spectator && sc.ShipType != ShipTypes.Spectator)
  117.                 RemoveFromSpecList(sc.PlayerName);
  118.         }
  119.  
  120.         // Print out map
  121.         public void doPrintMap(ChatEvent c)
  122.         {
  123.             Queue<string> m = World.MapPrintOut(BotName);
  124.             while (m.Count > 0) Game(msg.pm(c.PlayerName, m.Dequeue()));
  125.         }
  126.  
  127.         // Toggle debug mode on and off
  128.         public void doToggleDebugMode(ChatEvent c)
  129.         {
  130.             if (c.ModLevel < ModLevels.Sysop) return;
  131.  
  132.             msg.DebugMode = !msg.DebugMode;
  133.             Game(msg.arena("Debug Mode now set to [ "+msg.DebugMode+" ]"));
  134.         }
  135.  
  136.         // Toggle Chat notification
  137.         public void doToggleChatNotifications(ChatEvent c)
  138.         {
  139.             if (c.ModLevel < ModLevels.Sysop) return;
  140.  
  141.             ChatNotificationOn = !ChatNotificationOn;
  142.             Game(msg.arena("Game Notifications - now set to [ " + ChatNotificationOn + " ]"));
  143.         }
  144.  
  145.         // Add player to spec list
  146.         public void WentToSpec( string PlayerName)
  147.         {
  148.             // add player to our spec list
  149.             ArenaSpecList.Add(PlayerName);
  150.            
  151.             // stop if game isnt on
  152.             if (!GameOn) return;
  153.  
  154.             // add player to game
  155.             rpg_PlayerJoinGame(PlayerName);
  156.         }
  157.  
  158.         // Remove player from pub spec list
  159.         public void RemoveFromSpecList(string PlayerName)
  160.         {
  161.             // Find player in list- and remove
  162.             for (int i = 0; i < ArenaSpecList.Count; i += 1)
  163.                 if (ArenaSpecList[i] == PlayerName)
  164.                     ArenaSpecList.RemoveAt(i);
  165.  
  166.             // stop if game isnt on
  167.             if (!GameOn) return;
  168.  
  169.             // remove player from game
  170.             rpg_PlayerLeave(PlayerName);
  171.             return;
  172.         }
  173.  
  174.         // ---------------------------------- GAME TIMER ----------------------------------------- //
  175.  
  176.         // In case of a Bot disconnect BUT not a crash we need bot to rejoin chats
  177.         DateTime ts_RejoinChats = DateTime.Now;
  178.         // Time in minutes that you want bot to rejoin chats
  179.         double delay_RejoinChats = 1;
  180.  
  181.         //Time stamp for game cycle
  182.         DateTime ts_GameUpdate = DateTime.Now;
  183.         //Each game update interval - ms
  184.         double delay_GameUpdate = 10000;
  185.  
  186.         public void TimerMethod(object sender, ElapsedEventArgs e)
  187.         {
  188.             //Check to see if we have any commands in our command chat q
  189.             if (msg.NeedUpdate)
  190.             {
  191.                 if (ChatNotificationOn) Game(msg.sendNextBCommand());
  192.                 else msg.sendNextBCommand();
  193.             }
  194.  
  195.             // Only proceed if game is on
  196.             if (!GameOn) return;
  197.  
  198.             // One game cycle
  199.             rpg_GameUpdate();
  200.         }
  201.  
  202.         // ---------------------------------- MAIN CODE FOR RPG ---------------------------------- //
  203.  
  204.         // Start the game
  205.         public void rpg_StartGame()
  206.         {
  207.             // Clear list - in case we somehow have ppl in it on restart
  208.             rpg_PlayerList = new List<TTPlayer>();
  209.  
  210.             // Add spec players to list
  211.             for (int i = 0; i < ArenaSpecList.Count; i += 1)
  212.                 rpg_PlayerJoinGame(ArenaSpecList[i]);
  213.  
  214.             // Change bool to start events
  215.             GameOn = true;
  216.         }
  217.  
  218.         // Update game - Basically one game cycle
  219.         public void rpg_GameUpdate()
  220.         {
  221.             if ((DateTime.Now - ts_GameUpdate).TotalMilliseconds > delay_GameUpdate)
  222.             {
  223.                 // Update time stamp
  224.                 ts_GameUpdate = DateTime.Now;
  225.  
  226.                 // Grab random player
  227.                 TTPlayer rpg_Player = rpg_GetRandomPlayer();
  228.                 int disSize = 14;
  229.  
  230.                 if (rpg_Player.PlayerName.Length > disSize) disSize = rpg_Player.PlayerName.Length + 1;
  231.  
  232.                 int pre = (int)Math.Ceiling((decimal)((disSize - rpg_Player.PlayerName.Length) / 2)) + 1;
  233.                 int post = (disSize - rpg_Player.PlayerName.Length - pre) + rpg_Player.PlayerName.Length;
  234.  
  235.                 // u can tweak 10 up or down depending on how you wanna weigh event ignore
  236.                 switch (0)
  237.                 //switch (DiceRoll(0, 10))
  238.                 {
  239.                     case 0://Move Event
  240.                         World.Map(rpg_Player.MapIndex).MovePlayer(rpg_Player);
  241.                         if (!msg.DebugMode) msg.SCChanMessage(1, "[".PadRight(pre) + rpg_Player.PlayerName.PadRight(post) + "] has moved to " + World.Map(rpg_Player.MapIndex).MapName + ".");
  242.                         else msg.DebugMessage("[Event: Move][Player: " + rpg_Player.PlayerName.PadRight(13) + "][MapID:" + rpg_Player.MapIndex.ToString().PadLeft(2) + " ][ MapType: " + World.Map(rpg_Player.MapIndex).MapType.ToString().PadRight(11) + "]");
  243.                         break;
  244.                     case 1://Duel Event
  245.                         msg.DebugMessage("Event dice rolled a [1]");
  246.                         break;
  247.                     case 2://Mob Event
  248.                         msg.DebugMessage("Event dice rolled a [2]");
  249.                         break;
  250.                     case 3://Item Event
  251.                         msg.DebugMessage("Event dice rolled a [3]");
  252.                         break;
  253.                     case 4://Luck Event
  254.                         msg.DebugMessage("Event dice rolled a [4]");
  255.                         break;
  256.                     default:
  257.                         msg.DebugMessage("Ignore events.");
  258.                         break;
  259.                 }
  260.             }
  261.         }
  262.  
  263.         // Player is joining game
  264.         public void rpg_PlayerJoinGame(string PlayerName)
  265.         {
  266.             // Var to hold player
  267.             TTPlayer rpg_Player;
  268.  
  269.             // Check DB for updating player info
  270.             rpg_Player = DB_GetPlayer(PlayerName);
  271.  
  272.             // A safety for player NOT found in DB and player IS in current game somehow
  273.             if (rpg_GetPlayer(PlayerName) != null)
  274.             {
  275.                 msg.SCChanMessage(1, "*** Check Game Logic *** rpg_PlayerJoinGame found player INGame. Exiting method.");
  276.                 return;
  277.             }
  278.  
  279.             // Player Not found - register player here
  280.             if (rpg_Player == null)
  281.             {
  282.                 rpg_Player = rpg_MakeNewPlayer(PlayerName);
  283.                 msg.SCChanMessage(1,"A baby in BattleRPG has been born. Welcome baby " + PlayerName + "!");
  284.             }
  285.  
  286.             // Add player to ongoing game here - (PlayerList)
  287.             rpg_PlayerList.Add(rpg_Player);
  288.         }
  289.  
  290.         public void rpg_PlayerLeave(string PlayerName)
  291.         {
  292.             // Grab player info from list
  293.             TTPlayer rpg_Player = rpg_GetPlayer(PlayerName);
  294.  
  295.             // If the player was not playing we stop code here
  296.             // example being player left from a non spec ship
  297.             if (rpg_Player == null) return;
  298.  
  299.             // Update player on the DB if needed
  300.             DB_UpdatePlayerInfo(rpg_Player);
  301.  
  302.             // Take Player out of the ongoing game
  303.             // not sure if this is going to work
  304.             // may have to loop through list and take out that way
  305.             rpg_PlayerList.Remove(rpg_Player);
  306.         }
  307.  
  308.         //Get Random Player
  309.         public TTPlayer rpg_GetRandomPlayer()
  310.         {
  311.             return rpg_PlayerList[DiceRoll(0, rpg_PlayerList.Count)];
  312.         }
  313.  
  314.         // get player from player list using name
  315.         public TTPlayer rpg_GetPlayer(string PlayerName)
  316.         {
  317.             // Loop through list
  318.             for (int i = 0; i < rpg_PlayerList.Count; i+=1)
  319.             {
  320.                 // if there is a name match, send back player
  321.                 if (PlayerName == rpg_PlayerList[i].PlayerName)
  322.                     return rpg_PlayerList[i];
  323.             }
  324.  
  325.             // No matches were found, send back null
  326.             return null;
  327.         }
  328.  
  329.         // Create a player and set all default start variables here
  330.         public TTPlayer rpg_MakeNewPlayer(string PlayerName)
  331.         {
  332.             // set default values
  333.             // Maybe have options for a PREMIUM higher lvl start char?!?!?
  334.             TTPlayer np = new TTPlayer();
  335.             np.PlayerName = PlayerName;
  336.             np.Experience = new int []{0,10};
  337.             np.Level = 1;
  338.             np.MapIndex = 0;
  339.  
  340.             // Possibly update database here to include new player
  341.             // --------------------- //
  342.  
  343.             // return configured player
  344.             return np;
  345.         }
  346.  
  347.         // Grab all the needed player info from db here
  348.         // If the player is not found we will return null
  349.         public TTPlayer DB_GetPlayer(string PlayerName)
  350.         {
  351.             // Player was not found - return empty (null)
  352.             return null;
  353.         }
  354.  
  355.         // Update the databse with new player stats
  356.         public void DB_UpdatePlayerInfo(TTPlayer rpg_Player)
  357.         {
  358.             // Update Player Info on DB
  359.         }
  360.  
  361.         // ---------------------------------- MISC FUNCTIONS ---------------------------------- //
  362.  
  363.         //Dices
  364.         // Our custom random method
  365.         public int DiceRoll(int MinValue, int MaxValue)
  366.         {
  367.             int[] intbag = new int[30];
  368.  
  369.             for (int i = 0; i < 30; i++)
  370.                 intbag[i] = random.Next(MinValue,MaxValue);
  371.            
  372.             return intbag[random.Next(0,30)];
  373.         }
  374.  
  375.         public void InitializeBot()
  376.         {
  377.             //Configure and start main game timer
  378.             GameTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
  379.             GameTimer.Interval = 10;
  380.             GameTimer.Start();
  381.  
  382.             // Load all cfg maps
  383.             msg.SCChanMessage(1,World.LoadMaps(BotName));
  384.  
  385.             // StartGame
  386.             rpg_StartGame();
  387.  
  388.             rpg_PlayerJoinGame("Mikey Mouse");
  389.             rpg_PlayerJoinGame("Minnie Mouse");
  390.             rpg_PlayerJoinGame("Goofey");
  391.             rpg_PlayerJoinGame("Donald Duck");
  392.             rpg_PlayerJoinGame("Daisy Duck");
  393.             rpg_PlayerJoinGame("Gizmo Duck");
  394.             rpg_PlayerJoinGame("Scrooge McDuck");
  395.             rpg_PlayerJoinGame("Darkwing Duck");
  396.             rpg_PlayerJoinGame("Chip");
  397.             rpg_PlayerJoinGame("Dale");
  398.             rpg_PlayerJoinGame("Pluto");
  399.             rpg_PlayerJoinGame("Huey");
  400.             rpg_PlayerJoinGame("Dewey");
  401.             rpg_PlayerJoinGame("Louie");
  402.             rpg_PlayerJoinGame("Doc");
  403.             rpg_PlayerJoinGame("Grumpy");
  404.             rpg_PlayerJoinGame("Bashful");
  405.             rpg_PlayerJoinGame("Sleepy");
  406.             rpg_PlayerJoinGame("Happy");
  407.             rpg_PlayerJoinGame("Sneezy");
  408.             rpg_PlayerJoinGame("Dopey");
  409.  
  410.             //update chat with bot initialized msg
  411.             msg.SCChanMessage(1, BotName + " initialized.");
  412.         }
  413.  
  414.         public override void Dispose()
  415.         {
  416.             throw new NotImplementedException();
  417.         }
  418.     }
  419. }
Advertisement
Add Comment
Please, Sign In to add comment