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()
- {
- //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();
- //Game Tick
- DateTime ts_GameUpdate = DateTime.Now;
- //Time in Mintues to tick
- double delay_GameUpdate = 10000;
- // 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 = 5;
- //Areas
- string[] WorldMap = new string[] { "Beach", "Dark Forest", "Uncharted Desert", "Specorian Plains", "Specoria Town", " Specorian Mountains", "Burnt Forest", "Dragons Burden", "Undead Forest" };
- // ---------------------------------- 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();
- }
- }
- // 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)
- rpg_PlayerJoinGame(pe.PlayerName);
- }
- // Player needs to be updated as he leaves, and take him out of game
- public void MonitorPlayerLeave(object sender, PlayerLeftEvent pl)
- { rpg_PlayerLeave(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)
- {
- rpg_PlayerJoinGame(sc.PlayerName);
- return;
- }
- // Going out of spec
- if (sc.PreviousShipType == ShipTypes.Spectator && sc.ShipType != ShipTypes.Spectator)
- {
- rpg_PlayerLeave(sc.PlayerName);
- return;
- }
- }
- // ---------------------------------- GAME TIMER ----------------------------------------- //
- public void TimerMethod(object sender, ElapsedEventArgs e)
- {
- //Check to see if we have any commands in our command chat q
- if (msg.NeedUpdate) Game(msg.sendNextBCommand());
- // One game cycle
- rpg_GameUpdate();
- // Safety in case bot lags out
- // Joins chat every 5 min
- ReJoinChatCheck();
- }
- // ---------------------------------- MAIN CODE FOR RPG ---------------------------------- //
- // Main RPG Player List
- List<ARPlayer> rpg_PlayerList = new List<ARPlayer>();
- // 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;
- ARPlayer rpg_Player = rpg_GetRandomPlayer();
- rpg_MovePlayer(rpg_Player);
- msg.SCChanMessage(1,"Player[ "+rpg_Player.PlayerName+" ] has moved to " + WorldMap[rpg_Player.AreaName]);
- }
- }
- // Player is joining game
- public void rpg_PlayerJoinGame(string PlayerName)
- {
- // Var to hold player
- ARPlayer 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
- ARPlayer 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);
- }
- // Have the player move to new area
- public void rpg_MovePlayer(ARPlayer rpg_Player)
- {
- // Player is far left
- if (rpg_Player.AreaName == 0)
- {
- rpg_Player.AreaName++;
- return;
- }
- // Player is far right
- if (rpg_Player.AreaName == WorldMap.Length - 1)
- {
- rpg_Player.AreaName--;
- return;
- }
- // Player is not on either edge
- if (DiceRoll(0, 100) < 50)
- {
- rpg_Player.AreaName++;
- return;
- }
- rpg_Player.AreaName--;
- }
- //Get Random Player
- public ARPlayer rpg_GetRandomPlayer()
- {
- return rpg_PlayerList[DiceRoll(0, rpg_PlayerList.Count)];
- }
- // get player from player list using name
- public ARPlayer 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 ARPlayer rpg_MakeNewPlayer(string PlayerName)
- {
- // set default values
- // Maybe have options for a PREMIUM higher lvl start char?!?!?
- ARPlayer np = new ARPlayer();
- np.PlayerName = PlayerName;
- np.Experience = 0;
- np.Level = 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 ARPlayer DB_GetPlayer(string PlayerName)
- {
- // Player was not found - return empty (null)
- return null;
- }
- // Update the databse with new player stats
- public void DB_UpdatePlayerInfo(ARPlayer rpg_Player)
- {
- // Update Player Info on DB
- }
- // ---------------------------------- MISC FUNCTIONS ---------------------------------- //
- // Join chat command for bot to send
- string JoinChatCommand = "?chat=battledev";
- public void ReJoinChatCheck()
- {
- // Safety chat rejoin in case bot lags out - NOT a crash
- if ((DateTime.Now - ts_RejoinChats).TotalMinutes > delay_RejoinChats)
- {
- // Update time stamp
- ts_RejoinChats = DateTime.Now;
- msg.SCPubCommand(JoinChatCommand);
- }
- }
- //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();
- //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