Advertisement

How can my robots "see" what is next to them?

Started by December 06, 2005 01:53 PM
35 comments, last by GameDev.net 18 years, 11 months ago
Quote: Original post by HyperHacker
Rather than going through each cell and seeing if there's a robot there, keep a list of all X/Y coordinates at which there is a robot. The robots needn't know the entire world map; they can just call some function to tell them what's in front of them, behind them, etc.




I am headed in this direction, I hope.
Quote: Original post by dawidjoubert
What you are doing is extremely complex, firstly you dont have a completed simulation but want to move on to AI&Pathfinding (although i agree a basic A* Algorithm is the best place to start)

Now you want a human player to use the keyboard and be able to play the game first before moving on to the AI..

Now the AI that you want to do is damn complex, it involves LEARNING and mutally exlusive LEARNING in this case since robots dont share there knowledge.

You also want ppl to program their own ai and plug it into the code, and thats where the proble comes in with the CHEATING thing.

A solution is to have a script on-top of your code for example a state machine which the programmer can then set.

Event-->Condition-->Action

Example:
UnderFire-->Can move to safety & doesnt contradict other logic-->Move to safety

And then further the AI will handle exactly where this SAFETY is and how to get there and so on.




Yes...some sort of script would be fantastic (eventually).
Advertisement
I started doing programming like this 30 years ago.


Diections 0-7 0=North 1=NE 2=E etc... (4th quadrant coord system)


INT delta_x[8] = {0,1,1,1,0,-1,-1,-1};
INT delta_y[8] = {-1,-1,0,1,1,1,0,-1};


now you can compare and refer to directions as a positional relation and apply it to movement or a facing, etc...

object.move(INT dir) // move an object along a direction
{
object.x = object.x + delta_x[dir];
object.y = object.y + delta_y[dir];
}



to check if something is in the grid position adjacent to the object ::::

for (dir = 0; dirMAXGRID_X-1) goto OFF_GRID;
if (testxMAXGRID_Y-1) goto OFF_GRID;
if (testy<

More stuff:


for things that are not just adjacent you do a 'box check' where you check the list of all other target objects against a box around your object.

//object is the center object searching around
for (tx=object.x-maxrange; tx 0) dx = 1;
else if (dx 0) dy = 1;
else if (dy <
$%^&^%$#%^&^%$ stupid forum mechanisms that dont kill special chars....


please look at entire text of prev 2 postings using 'quote' to see their full text.....
Quote: Original post by Anonymous Poster
I started doing programming like this 30 years ago.


Diections 0-7 0=North 1=NE 2=E etc... (4th quadrant coord system)


INT delta_x[8] = {0,1,1,1,0,-1,-1,-1};
INT delta_y[8] = {-1,-1,0,1,1,1,0,-1};


now you can compare and refer to directions as a positional relation and apply it to movement or a facing, etc...

object.move(INT dir) // move an object along a direction
{
object.x = object.x + delta_x[dir];
object.y = object.y + delta_y[dir];
}



to check if something is in the grid position adjacent to the object ::::

for (dir = 0; dirMAXGRID_X-1) goto OFF_GRID;
if (testxMAXGRID_Y-1) goto OFF_GRID;
if (testy<





Here is what I have so far.


I am not sure I understand the "OFF_GRID" stuff:


using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace TestAI{	public enum Dir	{		N = 0,NE,E,SE,S,SW,W,NW	}	public class Robot : Object	{		public Square _current_square;		public Square _min_square;		public Square _max_square;		int[] delta_row = new int[8] {-1,-1,0,1,1,1,0,-1};		int[] delta_col = new int[8] {0,1,1,1,0,-1,-1,-1};				public Robot(int initial_row, int initial_col, int min_row, int max_row, int min_col, int max_col)		{			_current_square = new Square(initial_row,initial_col);			_min_square = new Square(min_row,min_col);			_max_square = new Square(max_row,max_col);			_status_icon = Image.FromFile(_ROBOT_IMAGE_PATH);						_kind = "robot";		}				private ArrayList _squares_visited = new ArrayList();		//CONSTANTS		private const string _ROBOT_IMAGE_PATH = "images\\robot.bmp";		//CONSTANTS		public string _name;				public bool _has_moved = false;		public int Row		{			get{return this._current_square._row;}			set			{				if((value >= this._min_square._row) && (value <= this._max_square._row))				{					this._current_square._row = value;				}			}		}		public int Col		{			get{return this._current_square._col;}			set			{				if((value >= this._min_square._col) && (value <= this._max_square._col))				{					this._current_square._col = value;				}			}		}		public void Move(int Dir)		{								this._current_square._row += delta_row[Dir];			this._current_square._col += delta_col[Dir];			_has_moved = true;							}					}}

Advertisement
The OFF_GRID stuff was just a generalization because depending on where you detect that condition you might have different ways of handling it (of its a move its invalid if its a search its skipped....)



Your code col and row functionality might not work the way you intend if they are not combined/integrated -- one may set and the other not set a new value giving you a result not intended (ie- a diagonal move NE that hits the East map edge would still set the Y row new value but the X col value would not set -- you would move North even if you didnt intend it)

Thats why the code sample I gave aborted (goto OFF_GRID) if either X/Y case failed.

You usually want to test if the proposed location is valid BEFORE you do anything further....

------------

Also you appear to be setting (in your code) min/max col/row values within the Robot data itself (which can work) but its more properly part of the map/world/grid data set/object (to be shared/accesses by all the robots that might be on the map...)

This topic is closed to new replies.

Advertisement