namespace Otaku.Virtual.Rooms.Bots { using Otaku; using Otaku.Virtual.Rooms; using Otaku.Virtual.Rooms.Pathfinding; using Otaku.Virtual.Users; using System; using System.Collections; 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) { this.roomUID = roomUID; this.Room = Room; string[] strArray = DB.runReadRow("SELECT name,mission,figure,x,y,z,freeroam,message_noshouting FROM roombots WHERE id = '" + botID + "'"); this.Name = strArray[0]; this.Mission = strArray[1]; this.Figure = strArray[2]; this.X = int.Parse(strArray[3]); this.Y = int.Parse(strArray[4]); this.Z1 = byte.Parse(strArray[5]); this.Z2 = this.Z1; this.goalX = -1; this.freeRoam = strArray[6] == "1"; this.noShoutingMessage = strArray[7]; this.Sayings = DB.runReadColumn("SELECT text FROM roombots_texts WHERE id = '" + botID + "' AND type = 'say'", 0); this.Shouts = DB.runReadColumn("SELECT text FROM roombots_texts WHERE id = '" + botID + "' AND type = 'shout'", 0); string[] strArray2 = DB.runReadColumn("SELECT words FROM roombots_texts_triggers WHERE id = '" + botID + "' ORDER BY id ASC", 0); if (strArray2.Length > 0) { string[] strArray3 = DB.runReadColumn("SELECT replies FROM roombots_texts_triggers WHERE id = '" + botID + "' ORDER BY id ASC", 0); string[] strArray4 = DB.runReadColumn("SELECT serve_replies FROM roombots_texts_triggers WHERE id = '" + botID + "' ORDER BY id ASC", 0); string[] strArray5 = DB.runReadColumn("SELECT serve_item FROM roombots_texts_triggers WHERE id = '" + botID + "' ORDER BY id ASC", 0); this.chatTriggers = new chatTrigger[strArray2.Length]; for (int i = 0; i < strArray2.Length; i++) { this.chatTriggers[i] = new chatTrigger(strArray2[i].Split(new char[] { '}' }), strArray3[i].Split(new char[] { '}' }), strArray4[i].Split(new char[] { '}' }), strArray5[i]); } } int[] numArray = DB.runReadColumn("SELECT x FROM roombots_coords WHERE id = '" + botID + "' ORDER BY id ASC", 0, null); this.Coords = new Coord[numArray.Length + 1]; this.Coords[numArray.Length] = new Coord(this.X, this.Y); if (numArray.Length > 1) { int[] numArray2 = DB.runReadColumn("SELECT y FROM roombots_coords WHERE id = '" + botID + "' ORDER BY id ASC", 0, null); for (int j = 0; j < numArray.Length; j++) { this.Coords[j] = new Coord(numArray[j], numArray2[j]); } } 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 num3; int num = -1; Random random = new Random(this.roomUID * DateTime.Now.Millisecond); Label_0022: while (this.Customer != null) { } switch (random.Next(0, 15)) { case 1: { Coord coord = new Coord(); if (this.freeRoam) { int[] numArray = this.Room.getMapBorders(); coord = new Coord(random.Next(0, numArray[0]), random.Next(0, numArray[1])); } else { coord = this.Coords[random.Next(0, this.Coords.Length)]; } 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; } goto Label_02C1; } case 2: { byte r = (byte) random.Next(0, 10); while (r == this.Z2) { r = (byte) random.Next(0, 10); } this.Rotate(r); goto Label_02C1; } case 3: if (this.Shouts.Length <= 0) { goto Label_02C1; } num3 = random.Next(0, this.Shouts.Length); if (this.Shouts.Length > 1) { while (num3 == num) { num3 = random.Next(0, this.Shouts.Length); } num = num3; } 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_02C1; case 5: if (random.Next(0, 2) == 0) { this.Statuses.Remove("dance"); this.handleStatus("wave", "", Config.Statuses_Wave_waveDuration); } else { this.addStatus("dance", "3"); this.Refresh(); } goto Label_02C1; default: goto Label_02C1; } this.Room.sendShout(this, this.Shouts[num3]); Label_02C1: Thread.Sleep(0xbb8); Out.WriteTrace("Bot AI loop"); goto Label_0022; } 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(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.removeStatus("dance"); this.addStatus("carryd", trigger.serveItem); this.goalX = coord.X; this.goalY = coord.Y; this.Customer = roomUser; this.customerTrigger = trigger; } } } 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 { string str = string.Concat(new object[] { "i:", this.roomUID, Convert.ToChar(13), "a:-1", Convert.ToChar(13), "n:", this.Name, Convert.ToChar(13), "f:", this.Figure, Convert.ToChar(13), "l:", this.X, " ", this.Y, " ", this.H, Convert.ToChar(13) }); if (this.Mission != "") { object obj2 = str; str = string.Concat(new object[] { obj2, "c:", this.Mission, Convert.ToChar(13) }); } return (str + "[bot]" + Convert.ToChar(13)); } } internal string statusString { get { string str = string.Concat(new object[] { this.roomUID, " ", this.X, ",", this.Y, ",", this.H.ToString().Replace(",", "."), ",", this.Z1, ",", 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); } }