/*
* PatrolAI.cs
* Authors: Karl Orosz
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System.Collections.Generic;
namespace StarJack
{
///
/// This AI will cause a object to patrol across a series of
/// tiles;
///
public class PatrolAI:AI,DefferedTurn
{
GameObject mGame;
int mCurEvent;
int[] mMod;
bool start;
int[] pos;
///
/// This list of events
///
List> mEventList;
public PatrolAI()
{
//empty on purpose
}
public PatrolAI(GameObject go):base()
{
mGame = go;
mEventList = new List>();
mCurEvent = 0;
curPriority = Priority.High;
mMod = new int[] { 0, 0 };
start = true;
pos = new int[] { 0, 0 };
}
//This will process the events
public override void TakeTurn(int pTurn, bool deffered)
{
if (!deffered)
{
//bool next = false;
if (start)
{
start = false;
mMod[0] = mEventList[mCurEvent].Key[0] - mGame.Position[0];
if (mMod[0] < 0)
{
mMod[0] = -1;
}
else if (mMod[0] > 0)
{
mMod[0] = 1;
}
mMod[1] = mEventList[mCurEvent].Key[1] - mGame.Position[1];
if (mMod[1] < 0)
{
mMod[1] = -1;
}
else if (mMod[1] > 0)
{
mMod[1] = 1;
}
}
KeyValuePair turnEvent = mEventList[mCurEvent];
pos = mGame.Position.Clone() as int[];
mGame.Facing = turnEvent.Value;
pos[0] += mMod[0];
pos[1] += mMod[1];
mGame.Move(pos);
//mGame.Position = pos;
}
else
{
KeyValuePair turnEvent = mEventList[mCurEvent];
bool next = false;
if (mGame.Position[0] == turnEvent.Key[0] && mGame.Position[1] == turnEvent.Key[1])
{
next = true;
}
if (next)
{
mCurEvent += 1;
if (mCurEvent >= mEventList.Count)
{
mCurEvent = 0;
}
mMod[0] = mEventList[mCurEvent].Key[0] - turnEvent.Key[0];
if (mMod[0] < 0)
{
mMod[0] = -1;
}
else if (mMod[0] > 0)
{
mMod[0] = 1;
}
mMod[1] = mEventList[mCurEvent].Key[1] - turnEvent.Key[1];
if (mMod[1] < 0)
{
mMod[1] = -1;
}
else if (mMod[1] > 0)
{
mMod[1] = 1;
}
}
}
}
///
/// This will add an node to the patrol list;
///
///
///
public void AddNode(int[] action, GameObject.Direction param)
{
mEventList.Add(new KeyValuePair(action, param));
}
public override void Reset()
{
mCurEvent = 0;
mGame.Move(mEventList[mCurEvent].Key.Clone() as int[]);
}
}
}