[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Wesnoth-wiki-changes] WritingYourOwnAI
From: |
wiki |
Subject: |
[Wesnoth-wiki-changes] WritingYourOwnAI |
Date: |
Sun, 24 Oct 2004 22:47 +0200 |
UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040809
Firefox/0.9.3
IP: 168.209.98.35
URI: http://wesnoth.slack.it/?WritingYourOwnAI
- - - - -
Index: WritingYourOwnAI
===================================================================
RCS file: /home/wesnoth/cvsroot/wikiroot/WritingYourOwnAI,v
retrieving revision 1.8
diff -u -r1.8 WritingYourOwnAI
--- WritingYourOwnAI 24 Oct 2004 19:02:36 -0000 1.8
+++ WritingYourOwnAI 24 Oct 2004 20:47:37 -0000
@@ -73,18 +73,23 @@
Let us conclude with a small sample AI, called 'sample_ai'. How should this AI
behave?
-- first it should detect if there are any enemies in range, and if there are
it should attack them by moving onto the
-best defensive terrain next to them. Attacks should be made with the weapon
for which damage*strikes*chance to hit is
-the highest
-- if there are no enemies in range, it should move units onto villages that
don't already belong to it
-- if there are no enemies or villages in range, it should move toward the
enemy leader along the shortest possible
-route.
-- at the end of its turn, it should recruit random units until it runs out of
money or doesn't have any space
-
-In the following example, I will place all functions in-line rather than in
the cpp file. To do this properly, of
-course you should put them in the cpp file. The entire definition of this AI
can be found in ai.cpp/ai.hpp in the
-source
-distribution.
+- First it should detect if there are any enemies in range,
+and if there are it should attack them by moving onto the
+best defensive terrain next to them.
+Attacks should be made with the weapon for which damage*strikes*chance to hit
is
+the highest.
+- If there are no enemies in range, it should move units onto
+villages that don't already belong to it.
+- If there are no enemies or villages in range,
+it should move toward the enemy leader along the shortest possible route.
+- At the end of its turn, it should recruit random units
+until it runs out of money or doesn't have any space.
+
+In the following example, I will place all functions in-line
+rather than in the cpp file. To do this properly, of
+course you should put them in the cpp file.
+The entire definition of this AI can be found in ai.cpp/ai.hpp in the
+source distribution.
We start the definition,
@@ -92,8 +97,10 @@
public:
sample_ai(info& i) : ai_interface(i) {}
-We have defined the constructor which takes an 'info' object and passes it
straight onto ai_interface. We don't need to
-store anything ourselves in this simple AI. (Although it'd be fine to have
data members if we wanted them).
+We have defined the constructor which takes an 'info' object
+and passes it straight onto ai_interface. We don't need to
+store anything ourselves in this simple AI.
+(Although it would be fine to have data members if we wanted them.)
Next we define the main function, play_turn():
@@ -104,7 +111,8 @@
do_recruitment();
}
-Just a series of calls to functions we are about to write which do the actual
work. Firstly, do_attacks(). We start by
+Just a series of calls to functions we are about to write which do the actual
work.
+Firstly, do_attacks(). We start by
calculating all the moves our units can make:
private:
@@ -113,9 +121,12 @@
move_map srcdst, dstsrc;
calculate_possible_moves(possible_moves,srcdst,dstsrc,false);
-Note that the 'possible_moves' thing is of little direct interest. It contains
details of exactly which tiles the unit
-moves along to get from one tile to another. This is useful for the display to
know about when it draws the unit
-moving, but as an AI programmer, it's not likely you'll ever care about what
it contains. Just pass it along to the
+Note that the 'possible_moves' thing is of little direct interest.
+It contains details of exactly which tiles the unit
+moves along to get from one tile to another.
+This is useful for the display to know about when it draws the unit
+moving, but as an AI programmer, it's not likely you'll ever care about
+what it contains. Just pass it along to the
move_unit() function so it can draw the unit moving along the correct path.
The things we're interested in are srcdst and dstsrc. Especially dstsrc. It
will tell us all the hexes our units can
@@ -132,15 +143,20 @@
location adjacent_tiles[6];
get_adjacent_tiles(i->first,adjacent_tiles);
-This kind of call is very common in the game's code -- make an array of 6
locations, and fill them up with the
-locations adjacent to a certain location. We actually want to find the
position to attack from which gives our unit the
-bestpossible defense. So, we initialize some variables to find the best
possible defense:
+This kind of call is very common in the game's code --
+make an array of 6 locations, and fill them up with the
+locations adjacent to a certain location.
+We actually want to find the position to attack from which gives our unit the
+best possible defense. So, we initialize some variables
+to find the best possible defense:
int best_defense = -1;
std::pair<location,location> best_movement;
-'best_defense' will of course be between 1 and 100, but we give it a value of
-1 to mean 'not initialized', since we
-haven't found any possible attacks at all yet. 'best_movement' will contain
the destination/source pair that gives the
+The value of 'best_defense' will of course be between 1 and 100,
+but we give it a value of -1 to mean 'not initialized', since we
+haven't found any possible attacks at all yet.
+Variable 'best_movement' will contain the destination/source pair that gives
the
best possible defense for our attacking unit.
for(size_t n = 0; n != 6; ++n) {