Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using BattleCore;
- using BattleCore.Events;
- using BattleCorePsyOps;
- using System.Timers;
- namespace AutoRPG
- {
- [Behavior("Main", "true", "0.01", "KenNPsy", "Rpg game that plays itself.")]
- public class Main: BotEventListener
- {
- public Main()
- {
- RegisterCommand("!togglechat", doToggleChatNotifications);
- RegisterCommand("!debug", doToggleDebugMode);
- RegisterCommand("!map", doPrintMap);
- //Makes sure spam control is on
- msg.SpamControl = true;
- //Log bot into channel
- msg.SCPubCommand(JoinChatCommand);
- }
- // ---------------------------------- Variables and needed Classes -------------------------- //
- //Psy's Module to help with chat events
- ShortChat msg = new ShortChat();
- //Stores botname and arena
- string BotName, Arena = "";
- //Game Timer
- Timer GameTimer = new Timer();
- //Random for dice
- Random random = new Random();
- // Join chat command for bot to send
- string JoinChatCommand = "?chat=battledev";
- // All Map Operations
- TTMapOps World = new TTMapOps();
- // List of players on spec
- List<string> ArenaSpecList = new List<string>();
- // Main RPG Player List
- List<TTPlayer> rpg_PlayerList = new List<TTPlayer>();
- // Game status
- bool GameOn = false;
- // Game chat - on or off
- bool ChatNotificationOn = true;
- // ---------------------------------- Subspace Game Events ---------------------------------- //
- //Monitor all incoming chat events
- public void MonitorChatEvents(object sender, ChatEvent c)
- {
- //Core sends each bot a pm with its own info 500ms after startup
- //Use it to Initialize Bot
- if (c.ChatType == ChatTypes.Private && c.Message.StartsWith("@BotInfo@"))
- {
- // Catch and store bot info that core sends
- string[] data = c.Message.Split(':');
- BotName = data[1];
- Arena = data[2];
- // Initialize everything needed to get bot started
- InitializeBot();
- }
- if (c.ChatType == ChatTypes.Arena && c.Message.StartsWith("WARNING: Disconnected from server"))
- {
- if (GameOn) GameOn = false;
- BotCrashed = true;
- }
- else if (c.ChatType == ChatTypes.Arena && c.Message.StartsWith("This arena is Continuum-only. Please get Continuum client"))
- {
- if (!GameOn)
- Game(msg.arena("Normal login detected!"));
- else
- Game(msg.arena("Bot crash detected!"));
- if (!BotCrashed) return;
- BotCrashed = false;
- GameOn = true;
- Game(msg.pub(JoinChatCommand));
- }
- }
- bool BotCrashed = false;
- // All players coming in should be in spec because of arena settings
- // Have the game check their status here - registered/unregistered
- public void MonitorPlayerEnter(object sender, PlayerEnteredEvent pe)
- {
- if (pe.ShipType == ShipTypes.Spectator)
- WentToSpec(pe.PlayerName);
- }
- // Player needs to be updated as he leaves, and take him out of game
- public void MonitorPlayerLeave(object sender, PlayerLeftEvent pl)
- { RemoveFromSpecList(pl.PlayerName); }
- // Check to see if players are going into/out of spectator mode
- public void MonitorPlayerShipChanges(object sender, ShipChangeEvent sc)
- {
- // Coming into spec
- if (sc.PreviousShipType != ShipTypes.Spectator && sc.ShipType == ShipTypes.Spectator)
- WentToSpec(sc.PlayerName);
- // Going out of spec
- if (sc.PreviousShipType == ShipTypes.Spectator && sc.ShipType != ShipTypes.Spectator)
- RemoveFromSpecList(sc.PlayerName);
- }
- // Print out map
- public void doPrintMap(ChatEvent c)
- {
- Queue<string> m = World.MapPrintOut(BotName);
- while (m.Count > 0) Game(msg.pm(c.PlayerName, m.Dequeue()));
- }
- // Toggle debug mode on and off
- public void doToggleDebugMode(ChatEvent c)
- {
- if (c.ModLevel < ModLevels.Sysop) return;
- msg.DebugMode = !msg.DebugMode;
- Game(msg.arena("Debug Mode now set to [ "+msg.DebugMode+" ]"));
- }
- // Toggle Chat notification
- public void doToggleChatNotifications(ChatEvent c)
- {
- if (c.ModLevel < ModLevels.Sysop) return;
- ChatNotificationOn = !ChatNotificationOn;
- Game(msg.arena("Game Notifications - now set to [ " + ChatNotificationOn + " ]"));
- }
- // Add player to spec list
- public void WentToSpec( string PlayerName)
- {
- // add player to our spec list
- ArenaSpecList.Add(PlayerName);
- // stop if game isnt on
- if (!GameOn) return;
- // add player to game
- rpg_PlayerJoinGame(PlayerName);
- }
- // Remove player from pub spec list
- public void RemoveFromSpecList(string PlayerName)
- {
- // Find player in list- and remove
- for (int i = 0; i < ArenaSpecList.Count; i += 1)
- if (ArenaSpecList[i] == PlayerName)
- ArenaSpecList.RemoveAt(i);
- // stop if game isnt on
- if (!GameOn) return;
- // remove player from game
- rpg_PlayerLeave(PlayerName);
- return;
- }
- // ---------------------------------- GAME TIMER ----------------------------------------- //
- // In case of a Bot disconnect BUT not a crash we need bot to rejoin chats
- DateTime ts_RejoinChats = DateTime.Now;
- // Time in minutes that you want bot to rejoin chats
- double delay_RejoinChats = 1;
- //Time stamp for game cycle
- DateTime ts_GameUpdate = DateTime.Now;
- //Each game update interval - ms
- double delay_GameUpdate = 10000;
- public void TimerMethod(object sender, ElapsedEventArgs e)
- {
- //Check to see if we have any commands in our command chat q
- if (msg.NeedUpdate)
- {
- if (ChatNotificationOn) Game(msg.sendNextBCommand());
- else msg.sendNextBCommand();
- }
- // Only proceed if game is on
- if (!GameOn) return;
- // One game cycle
- rpg_GameUpdate();
- }
- // ---------------------------------- MAIN CODE FOR RPG ---------------------------------- //
- // Start the game
- public void rpg_StartGame()
- {
- // Clear list - in case we somehow have ppl in it on restart
- rpg_PlayerList = new List<TTPlayer>();
- // Add spec players to list
- for (int i = 0; i < ArenaSpecList.Count; i += 1)
- rpg_PlayerJoinGame(ArenaSpecList[i]);
- // Change bool to start events
- GameOn = true;
- }
- // Update game - Basically one game cycle
- public void rpg_GameUpdate()
- {
- if ((DateTime.Now - ts_GameUpdate).TotalMilliseconds > delay_GameUpdate)
- {
- // Update time stamp
- ts_GameUpdate = DateTime.Now;
- // Grab random player
- TTPlayer rpg_Player = rpg_GetRandomPlayer();
- int disSize = 14;
- if (rpg_Player.PlayerName.Length > disSize) disSize = rpg_Player.PlayerName.Length + 1;
- int pre = (int)Math.Ceiling((decimal)((disSize - rpg_Player.PlayerName.Length) / 2)) + 1;
- int post = (disSize - rpg_Player.PlayerName.Length - pre) + rpg_Player.PlayerName.Length;
- // u can tweak 10 up or down depending on how you wanna weigh event ignore
- switch (0)
- //switch (DiceRoll(0, 10))
- {
- case 0://Move Event
- World.Map(rpg_Player.MapIndex).MovePlayer(rpg_Player);
- if (!msg.DebugMode) msg.SCChanMessage(1, "[".PadRight(pre) + rpg_Player.PlayerName.PadRight(post) + "] has moved to " + World.Map(rpg_Player.MapIndex).MapName + ".");
- 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) + "]");
- break;
- case 1://Duel Event
- msg.DebugMessage("Event dice rolled a [1]");
- break;
- case 2://Mob Event
- msg.DebugMessage("Event dice rolled a [2]");
- break;
- case 3://Item Event
- msg.DebugMessage("Event dice rolled a [3]");
- break;
- case 4://Luck Event
- msg.DebugMessage("Event dice rolled a [4]");
- break;
- default:
- msg.DebugMessage("Ignore events.");
- break;
- }
- }
- }
- // Player is joining game
- public void rpg_PlayerJoinGame(string PlayerName)
- {
- // Var to hold player
- TTPlayer rpg_Player;
- // Check DB for updating player info
- rpg_Player = DB_GetPlayer(PlayerName);
- // A safety for player NOT found in DB and player IS in current game somehow
- if (rpg_GetPlayer(PlayerName) != null)
- {
- msg.SCChanMessage(1, "*** Check Game Logic *** rpg_PlayerJoinGame found player INGame. Exiting method.");
- return;
- }
- // Player Not found - register player here
- if (rpg_Player == null)
- {
- rpg_Player = rpg_MakeNewPlayer(PlayerName);
- msg.SCChanMessage(1,"A baby in BattleRPG has been born. Welcome baby " + PlayerName + "!");
- }
- // Add player to ongoing game here - (PlayerList)
- rpg_PlayerList.Add(rpg_Player);
- }
- public void rpg_PlayerLeave(string PlayerName)
- {
- // Grab player info from list
- TTPlayer rpg_Player = rpg_GetPlayer(PlayerName);
- // If the player was not playing we stop code here
- // example being player left from a non spec ship
- if (rpg_Player == null) return;
- // Update player on the DB if needed
- DB_UpdatePlayerInfo(rpg_Player);
- // Take Player out of the ongoing game
- // not sure if this is going to work
- // may have to loop through list and take out that way
- rpg_PlayerList.Remove(rpg_Player);
- }
- //Get Random Player
- public TTPlayer rpg_GetRandomPlayer()
- {
- return rpg_PlayerList[DiceRoll(0, rpg_PlayerList.Count)];
- }
- // get player from player list using name
- public TTPlayer rpg_GetPlayer(string PlayerName)
- {
- // Loop through list
- for (int i = 0; i < rpg_PlayerList.Count; i+=1)
- {
- // if there is a name match, send back player
- if (PlayerName == rpg_PlayerList[i].PlayerName)
- return rpg_PlayerList[i];
- }
- // No matches were found, send back null
- return null;
- }
- // Create a player and set all default start variables here
- public TTPlayer rpg_MakeNewPlayer(string PlayerName)
- {
- // set default values
- // Maybe have options for a PREMIUM higher lvl start char?!?!?
- TTPlayer np = new TTPlayer();
- np.PlayerName = PlayerName;
- np.Experience = new int []{0,10};
- np.Level = 1;
- np.MapIndex = 0;
- // Possibly update database here to include new player
- // --------------------- //
- // return configured player
- return np;
- }
- // Grab all the needed player info from db here
- // If the player is not found we will return null
- public TTPlayer DB_GetPlayer(string PlayerName)
- {
- // Player was not found - return empty (null)
- return null;
- }
- // Update the databse with new player stats
- public void DB_UpdatePlayerInfo(TTPlayer rpg_Player)
- {
- // Update Player Info on DB
- }
- // ---------------------------------- MISC FUNCTIONS ---------------------------------- //
- //Dices
- // Our custom random method
- public int DiceRoll(int MinValue, int MaxValue)
- {
- int[] intbag = new int[30];
- for (int i = 0; i < 30; i++)
- intbag[i] = random.Next(MinValue,MaxValue);
- return intbag[random.Next(0,30)];
- }
- public void InitializeBot()
- {
- //Configure and start main game timer
- GameTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
- GameTimer.Interval = 10;
- GameTimer.Start();
- // Load all cfg maps
- msg.SCChanMessage(1,World.LoadMaps(BotName));
- // StartGame
- rpg_StartGame();
- rpg_PlayerJoinGame("Mikey Mouse");
- rpg_PlayerJoinGame("Minnie Mouse");
- rpg_PlayerJoinGame("Goofey");
- rpg_PlayerJoinGame("Donald Duck");
- rpg_PlayerJoinGame("Daisy Duck");
- rpg_PlayerJoinGame("Gizmo Duck");
- rpg_PlayerJoinGame("Scrooge McDuck");
- rpg_PlayerJoinGame("Darkwing Duck");
- rpg_PlayerJoinGame("Chip");
- rpg_PlayerJoinGame("Dale");
- rpg_PlayerJoinGame("Pluto");
- rpg_PlayerJoinGame("Huey");
- rpg_PlayerJoinGame("Dewey");
- rpg_PlayerJoinGame("Louie");
- rpg_PlayerJoinGame("Doc");
- rpg_PlayerJoinGame("Grumpy");
- rpg_PlayerJoinGame("Bashful");
- rpg_PlayerJoinGame("Sleepy");
- rpg_PlayerJoinGame("Happy");
- rpg_PlayerJoinGame("Sneezy");
- rpg_PlayerJoinGame("Dopey");
- //update chat with bot initialized msg
- msg.SCChanMessage(1, BotName + " initialized.");
- }
- public override void Dispose()
- {
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment