/* * Date: 12/16/2005 * Time: 9:31 PM * * Copyright 2005, Static Boy Productions */ using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using Jessie.AI.Tasks; using Jessie.GameState; using Jessie.Utils; namespace Jessie.AI { /// /// Description of jtbvGameAI. /// public class jtbvGameAI { private const string CRLF = "\r\n"; private ArrayList votes; private Point target; private jtbvGameState state; public jtbvGameAI(jtbvGameState State) { votes = new ArrayList(); target = new Point(); state = State; //currentAction; } public IEnumerator GetEnumerator() { // Vote for the next task jtbvTask electedTask = this.ElectTask(state); if (electedTask == null) { // ERROR!!! Console.WriteLine("Election came up blank!"); yield return " "; } else { foreach (string cmd in electedTask) { if ((state.MoreRequested) || (state.Player.Dead)) { state.MoreRequested = false; yield return " "; } else if (state.FullRedrawRequested) { state.FullRedrawRequested = false; yield return ((char) 0x12).ToString(); } yield return cmd; } } } string lastModeString = ""; private jtbvTask ElectTask(jtbvGameState state) { jtbvTask retVal = null; votes.Clear(); switch (state.Mode) { case jtbvMode.GAME: if (state.CurrDlvl.Curiosities.Count > 0) // If we have questions about a dungeon feature { //Console.WriteLine("There are " + gameState.CurrDlvl.Curiosities.Count.ToString() + " requests in the curiosities list."); Vote("INQUIRE_ABOUT", 80, (Point) state.CurrDlvl.Curiosities[0]); } if (state.Player.Hunger >= jtbvHungerStatus.WEAK) { Vote("FIX_HUNGER", 20 * (int) state.Player.Hunger); } if (state.CurrDlvl.DownStairsKnown())//) && (gameState.Player.Stats.Lvl > gameState.CurrentDepth)) { Vote("DESCEND", 5); } if (state.CurrDlvl.GetDanger(state.Player.Loc) > 0) { Vote("ATTACK", 40); } if (state.Player.Stats.HP <= (state.Player.Stats.MaxHP / 3)) { Vote("HEAL", 50); } if ((!state.ExcalCreated) && (state.Player.Stats.Lvl >= 5)) { Vote("GET_EXCAL", 15); } if (state.CurrDlvl[state.Player.Loc].LowestAdjacentSearchValue() == 0) { Vote("SEARCH", 10); } Vote("EXPLORE", 8); Vote("EXPLORE_SEARCH", 2); Vote("WANDER", 1); break; case jtbvMode.TEXT_PROMPT: Vote("ANSWER_QUESTION", 100); break; case jtbvMode.PROMPT: case jtbvMode.YN_PROMPT: Vote("ANSWER_PROMPT", 100); break; case jtbvMode.WINDOW_LIST: Vote("ANSWER_QUESTION", 100); break; case jtbvMode.DGAMELAUNCH: Vote("DGAMELAUNCH_LOGIN", 100); break; case jtbvMode.UNKNOWN: default: break; } votes.Sort(); votes.Reverse(); if (votes.Count > 0) { int voteCnt = 0; while (voteCnt < votes.Count) { jtbvActionVote choice = votes[voteCnt] as jtbvActionVote; retVal = GetTask(choice); retVal.State = state; //Console.WriteLine("Voting trying to get '" + choice.ActionName + "'"); // TODO: Implement somthing like this. if (retVal.CanExecute) { if (lastModeString != choice.ActionName) Console.WriteLine(choice.ActionName); else Console.Write("."); lastModeString = choice.ActionName; break; // If that vote can execute, break out of the loop so that we keep this action. } voteCnt++; } } else { Console.WriteLine("ERROR! No votes cast!"); retVal = null; } return retVal; } private void Vote(string cmd, int weight) { votes.Add(new jtbvActionVote(cmd, target, weight)); } private void Vote(string cmd, int weight, Point loc) { votes.Add(new jtbvActionVote(cmd, loc, weight)); } private jtbvTask GetTask(jtbvActionVote choice) { jtbvTask retVal = null; switch (choice.ActionName.ToUpper()) { case "FIX_HUNGER": retVal = new jtbvTaskFixHunger(); break; case "EXPLORE": retVal = new jtbvTaskExplore(false); break; case "EXPLORE_SEARCH": retVal = new jtbvTaskExplore(true); break; case "SEARCH": retVal = new jtbvTaskSearch(); break; case "WANDER": retVal = new jtbvTaskWander(); break; case "GET_EXCAL": retVal = new jtbvTaskGetExcal(); break; case "DGAMELAUNCH_LOGIN": retVal = new jtbvTaskDGameLaunch(); break; case "INQUIRE_ABOUT": retVal = new jtbvTaskInquireAbout(choice.Location); break; case "DESCEND": retVal = new jtbvTaskDescend(); break; case "ATTACK": retVal = new jtbvTaskAttack(); break; case "HEAL": retVal = new jtbvTaskFixHealth(); break; case "ANSWER_QUESTION": retVal = new jtbvTaskAnswerQuestion(); break; case "ANSWER_PROMPT": retVal = new jtbvTaskAnswerPrompt(); break; } return retVal; } } }