namespace Holo.Virtual.Rooms.Bots { using Holo; using Holo.Virtual.Rooms; using Holo.Virtual.Rooms.Pathfinding; using Holo.Virtual.Users; using Ion.Storage; using System; using System.Collections; using System.Data; using System.Runtime.CompilerServices; using System.Threading; internal class virtualBot { private Thread aiHandler; private chatTrigger[] chatTriggers; private Coord[] Coords; private virtualRoomUser Customer; private chatTrigger customerTrigger; internal string Figure; internal bool freeRoam; internal int goalX; internal int goalY; internal double H; internal string Mission; internal string Name; internal string noShoutingMessage; internal virtualRoom Room; internal int roomUID; private string[] Sayings; private string[] Shouts; private Hashtable Statuses; internal int X; internal int Y; internal byte Z1; internal byte Z2; internal virtualBot(int botID, int roomUID, virtualRoom Room) { DataRow row; DataColumn column; DataTable table; this.roomUID = roomUID; this.Room = Room; using (DatabaseClient client = Eucalypt.dbManager.GetClient()) { row = client.getRow("SELECT name,mission,figure,x,y,z,freeroam,message_noshouting FROM roombots WHERE id = '" + botID + "'"); } this.Name = Convert.ToString(row[0]); this.Mission = Convert.ToString(row[1]); this.Figure = Convert.ToString(row[2]); this.X = Convert.ToInt32(row[3]); this.Y = Convert.ToInt32(row[4]); this.Z1 = Convert.ToByte(row[5]); this.Z2 = this.Z1; this.goalX = -1; this.freeRoam = Convert.ToString(row[6]) == "1"; this.noShoutingMessage = Convert.ToString(row[7]); using (DatabaseClient client2 = Eucalypt.dbManager.GetClient()) { column = client2.getColumn("SELECT text FROM roombots_texts WHERE id = '" + botID + "' AND type = 'say'"); } this.Sayings = dataHandling.dColToArray(column); using (DatabaseClient client3 = Eucalypt.dbManager.GetClient()) { column = client3.getColumn("SELECT text FROM roombots_texts WHERE id = '" + botID + "' AND type = 'shout'"); } this.Shouts = dataHandling.dColToArray(column); using (DatabaseClient client4 = Eucalypt.dbManager.GetClient()) { table = client4.getTable("SELECT words, replies, serve_replies, serve_item FROM roombots_texts_triggers WHERE id = '" + botID + "'"); } this.chatTriggers = new chatTrigger[table.Rows.Count]; int index = 0; foreach (DataRow row2 in table.Rows) { this.chatTriggers[index] = new chatTrigger(Convert.ToString(row2["words"]).ToLower().Split(new char[] { '}' }), Convert.ToString(row2["replies"]).Split(new char[] { '}' }), Convert.ToString(row2["serve_replies"]).Split(new char[] { '}' }), Convert.ToString(row2["serve_item"])); index++; } using (DatabaseClient client5 = Eucalypt.dbManager.GetClient()) { table = client5.getTable("SELECT x,y FROM roombots_coords WHERE id = '" + botID + "'"); } this.Coords = new Coord[table.Rows.Count]; index = 0; foreach (DataRow row3 in table.Rows) { this.Coords[index] = new Coord(Convert.ToInt32(row3["x"]), Convert.ToInt32(row3["y"])); index++; } this.Statuses = new Hashtable(); this.aiHandler = new Thread(new ThreadStart(this.AI)); this.aiHandler.Priority = ThreadPriority.BelowNormal; this.aiHandler.Start(); } internal void addStatus(string Key, string Value) { if (this.Statuses.ContainsKey(Key)) { this.Statuses.Remove(Key); } this.Statuses.Add(Key, Value); } private void AI() { int num4; int num = -1; Random random = new Random(this.roomUID * DateTime.Now.Millisecond); Label_001D: while (this.Customer != null) { } switch (random.Next(0, 15)) { case 1: { Coord coord = new Coord(); if (!this.freeRoam) { if ((coord.X == this.X) && (coord.Y == this.Y)) { this.Z1 = (byte) random.Next(0, 10); this.Z2 = this.Z1; this.Refresh(); } else { this.goalX = coord.X; this.goalY = coord.Y; } } else { int[] numArray = this.Room.getMapBorders(); coord = new Coord(random.Next(0, numArray[0]), random.Next(0, numArray[1])); } goto Label_01D7; } case 2: { byte r = (byte) random.Next(0, 10); while (r == this.Z2) { r = (byte) random.Next(0, 10); } this.Rotate(r); goto Label_01D7; } case 3: if (this.Shouts.Length <= 0) { goto Label_01D7; } num4 = random.Next(0, this.Shouts.Length); if (this.Shouts.Length > 1) { while (num4 == num) { num4 = random.Next(0, this.Shouts.Length); } num = num4; } break; case 4: if (this.Sayings.Length > 0) { int index = random.Next(0, this.Sayings.Length); if (this.Sayings.Length > 1) { while (index == num) { index = random.Next(0, this.Sayings.Length); } num = index; } this.Room.sendSaying(this, this.Sayings[index]); } goto Label_01D7; default: goto Label_01D7; } this.Room.sendShout(this, this.Shouts[num4]); Label_01D7: Thread.Sleep(0xbb8); Out.WriteTrace("Bot AI loop"); goto Label_001D; } internal void checkOrders() { if (this.Customer != null) { this.goalX = -1; this.Rotate(this.Customer.X, this.Customer.Y); this.removeStatus("carryd"); this.Room.sendSaying(this, this.customerTrigger.serveReply); if (!this.Customer.statusManager.containsStatus("sit")) { this.Customer.Z1 = Rotation.Calculate(this.Customer.X, this.Customer.Y, this.X, this.Y); this.Customer.Z2 = this.Customer.Z1; } this.Customer.statusManager.carryItem(int.Parse(this.customerTrigger.serveItem)); this.Customer = null; this.customerTrigger = null; } } internal bool containsStatus(string Key) { return this.Statuses.ContainsKey(Key); } internal Coord getClosestWalkCoordTo(int X, int Y) { int num = 6; Coord coord = new Coord(-1, 0); foreach (Coord coord2 in this.Coords) { int num2 = Math.Abs((int) (X - coord2.X)) + Math.Abs((int) (Y - coord2.Y)); if (num2 < num) { num = num2; coord = coord2; } } return coord; } internal void handleStatus(string Key, string Value, int Length) { if (this.Statuses.ContainsKey(Key)) { this.Statuses.Remove(Key); } new statusVoid(this.HANDLESTATUS).BeginInvoke(Key, Value, Length, null, null); } private void HANDLESTATUS(string Key, string Value, int Length) { try { this.Statuses.Add(Key, Value); this.Refresh(); Thread.Sleep(Length); this.Statuses.Remove(Key); this.Refresh(); } catch { } } internal void Interact(virtualRoomUser roomUser, string Message) { Message = Message.ToLower(); string[] strArray = Message.Split(new char[] { ' ' }); if (this.chatTriggers != null) { foreach (chatTrigger trigger in this.chatTriggers) { for (int i = 0; i < strArray.Length; i++) { if (trigger.containsWord(strArray[i])) { if (trigger.serveItem != "") { if (this.Customer == null) { Coord coord = this.getClosestWalkCoordTo(roomUser.X, roomUser.Y); if (coord.X != -1) { this.Room.sendSaying(this, trigger.Reply); this.goalX = coord.X; this.goalY = coord.Y; this.Customer = roomUser; this.customerTrigger = trigger; roomUser.User.statusID = "Gb"; roomUser.User.statusCount = int.Parse(trigger.serveItem); this.Room.sendData(roomUser.User.statusID + Encoding.encodeVL64(roomUser.roomUID) + Encoding.encodeVL64(roomUser.User.statusCount)); } } } else { this.Z1 = Rotation.Calculate(this.X, this.Y, roomUser.X, roomUser.Y); this.Z2 = this.Z1; this.Room.sendSaying(this, trigger.Reply); } return; } } } } } internal void Kill() { try { this.aiHandler.Abort(); } catch { } this.aiHandler = null; this.Room = null; this.Statuses = null; this.Coords = null; this.Sayings = null; this.Shouts = null; this.chatTriggers = null; this.Customer = null; this.customerTrigger = null; } internal void Refresh() { this.Room.Refresh(this); } internal void removeStatus(string Key) { try { if (this.Statuses.ContainsKey(Key)) { this.Statuses.Remove(Key); } } catch { } } internal void Rotate(byte R) { if ((R != this.Z1) && !this.Statuses.ContainsKey("sit")) { this.Z1 = R; this.Z2 = R; this.Refresh(); } } internal void Rotate(int toX, int toY) { this.Rotate(Rotation.Calculate(this.X, this.Y, toX, toY)); } internal string detailsString { get { return string.Concat(new object[] { Encoding.encodeVL64(this.roomUID), this.Name, Convert.ToChar(2), this.Mission, " ", Convert.ToChar(2), this.Figure, Convert.ToChar(2), Encoding.encodeVL64(this.roomUID), Encoding.encodeVL64(this.X), Encoding.encodeVL64(this.Y), this.H, Convert.ToChar(2), "IK" }); } } internal string statusString { get { string str = string.Concat(new object[] { Encoding.encodeVL64(this.roomUID), Encoding.encodeVL64(this.X), Encoding.encodeVL64(this.Y), this.H.ToString().Replace(",", "."), Convert.ToChar(2), Encoding.encodeVL64(this.Z1), Encoding.encodeVL64(this.Z2), "/" }); foreach (string str2 in this.Statuses.Keys) { str = str + str2; string str3 = (string) this.Statuses[str2]; if (str3 != "") { str = str + " " + str3; } str = str + "/"; } return str; } } private class chatTrigger { private string[] Replies; internal string serveItem; private string[] serveReplies; private string[] Words; internal chatTrigger(string[] Words, string[] Replies, string[] serveReplies, string serveItem) { this.Words = Words; this.Replies = Replies; this.serveReplies = serveReplies; this.serveItem = serveItem; } internal bool containsWord(string Word) { if (Word.Substring(Word.Length - 1, 1) == "?") { Word = Word.Substring(0, Word.Length - 1); } for (int i = 0; i < this.Words.Length; i++) { if (this.Words[i] == Word) { return true; } } return false; } internal string Reply { get { return this.Replies[new Random(DateTime.Now.Millisecond).Next(0, this.Replies.Length)]; } } internal string serveReply { get { return this.serveReplies[new Random(DateTime.Now.Millisecond).Next(0, this.serveReplies.Length)]; } } } private delegate void statusVoid(string Key, string Value, int Length); } }