actions.h

Go to the documentation of this file.
00001 /*
00002  *  actions.h - Action controllers for actors.
00003  *
00004  *  Copyright (C) 2000-2001  The Exult Team
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #ifndef ACTIONS_H
00022 #define ACTIONS_H 1
00023 
00024 #include "tiles.h"
00025 
00026 class Actor;
00027 class Game_object;
00028 class Tile_coord;
00029 class PathFinder;
00030 class Pathfinder_client;
00031 class If_else_path_actor_action;
00032 
00033 /*
00034  *  This class controls the current actions of an actor:
00035  */
00036 class Actor_action
00037   {
00038   static long seqcnt;   // Sequence # to check for deletion.
00039 protected:
00040   bool get_party;     // At each step (of the Avatar), have
00041           //   the party follow.
00042   long seq;     // 'unique' sequence #.
00043 public:
00044   Actor_action() : get_party(false)
00045     { seq = ++seqcnt; }
00046   virtual ~Actor_action() { }
00047   void set_get_party(bool tf = true)
00048     { get_party = true; }
00049   int handle_event_safely(Actor *actor, bool& deleted);
00050           // Handle time event.
00051   virtual int handle_event(Actor *actor) = 0;
00052   virtual void stop(Actor *actor) // Stop moving.
00053     {  }
00054           // Set simple path to destination.
00055   virtual Actor_action *walk_to_tile(Actor *npc, Tile_coord src, 
00056       Tile_coord dest, int dist = 0);
00057           // Set action to walk to dest, then
00058           //   exec. another action when there.
00059   static Actor_action *create_action_sequence(Actor *actor, 
00060       Tile_coord dest, Actor_action *when_there, 
00061           bool from_off_screen = false);
00062           // Get destination, or ret. 0.
00063   virtual int get_dest(Tile_coord& dest)
00064     { return 0; }
00065           // Check for Astar.
00066   virtual int following_smart_path()
00067     { return 0; }
00068   virtual If_else_path_actor_action *as_usecode_path()
00069     { return 0; }
00070   };
00071 
00072 /*
00073  *  A null action just returns 0 the first time.
00074  */
00075 class Null_action : public Actor_action
00076   {
00077 public:
00078   Null_action() {  }
00079   virtual int handle_event(Actor *actor);
00080   };
00081 
00082 /*
00083  *  Follow a path.
00084  */
00085 class Path_walking_actor_action : public Actor_action
00086   {
00087 protected:
00088   bool reached_end;   // Reached end of path.
00089   PathFinder *path;   // Allocated pathfinder.
00090 private:
00091   int original_dir;   // From src. to dest. (0-7).
00092   int speed;      // Time between frames.
00093   bool from_offscreen;    // Walking from offscreen.
00094   Actor_action *subseq;   // For opening doors.
00095   unsigned char blocked;    // Blocked-tile retries.
00096   unsigned char max_blocked;  // Try this many times.
00097   unsigned char blocked_frame;  // Frame for blocked tile.
00098   Tile_coord blocked_tile;  // Tile to retry.
00099   void set_subseq(Actor_action *sub)
00100     {
00101     delete subseq;
00102     subseq = sub;
00103     }
00104 public:
00105   Path_walking_actor_action(PathFinder *p = 0, int maxblk = 3);
00106   virtual ~Path_walking_actor_action();
00107   static Path_walking_actor_action *create_path(Tile_coord src,
00108       Tile_coord dest, Pathfinder_client& cost);
00109           // Handle time event.
00110   virtual int handle_event(Actor *actor);
00111   int open_door(Actor *actor, Game_object *door);
00112   virtual void stop(Actor *actor);// Stop moving.
00113           // Set simple path to destination.
00114   virtual Actor_action *walk_to_tile(Actor *npc, Tile_coord src, 
00115         Tile_coord dest, int dist = 0);
00116           // Get destination, or ret. 0.
00117   virtual int get_dest(Tile_coord& dest);
00118           // Check for Astar.
00119   virtual int following_smart_path();
00120   };
00121 
00122 /*
00123  *  Follow a path to approach a given object, and stop half-way if it
00124  *  moved.
00125  */
00126 class Approach_actor_action : public Path_walking_actor_action
00127   {
00128   Game_object *dest_obj;    // Destination object.
00129   Tile_coord orig_dest_pos; // Dest_obj's pos. when we start.
00130   int cur_step;     // Count steps.
00131   int check_step;     // Check at this step.
00132 public:
00133   Approach_actor_action(PathFinder *p, Game_object *d);
00134           // Handle time event.
00135   virtual int handle_event(Actor *actor);
00136   };
00137 
00138 /*
00139  *  Follow a path and execute one action if successful, another if
00140  *  failed.
00141  */
00142 class If_else_path_actor_action : public Path_walking_actor_action
00143   {
00144   bool succeeded, failed, done;
00145   Actor_action *success, *failure;
00146 public:
00147   If_else_path_actor_action(Actor *actor, Tile_coord dest, 
00148         Actor_action *s, Actor_action *f = 0);
00149   ~If_else_path_actor_action();
00150   void set_failure(Actor_action *f);
00151   bool done_and_failed()    // Happens if no path found in ctor.
00152     { return done && failed; }
00153           // Handle time event.
00154   virtual int handle_event(Actor *actor);
00155   virtual If_else_path_actor_action *as_usecode_path()
00156     { return this; }
00157   };
00158 
00159 /*
00160  *  Just move (i.e. teleport) to a desired location.
00161  */
00162 class Move_actor_action : public Actor_action
00163   {
00164   Tile_coord dest;    // Where to go.
00165 public:
00166   Move_actor_action(Tile_coord d) : dest(d)
00167     {  }
00168           // Handle time event.
00169   virtual int handle_event(Actor *actor);
00170   };
00171 
00172 #if 0 /* +++Maybe not needed. */
00173 /*
00174  *  Approach an enemy during combat.
00175  */
00176 class Combat_path_actor_action : public Actor_action
00177   {
00178   PathFinder *path;   // Allocated pathfinder.
00179   int frame_index;    // Index within frame sequence.
00180 public:
00181   Combat_path_walking_actor_action(PathFinder *p);
00182   virtual ~Combat_path_walking_actor_action();
00183           // Handle time event.
00184   virtual int handle_event(Actor *actor);
00185           // Get destination, or ret. 0.
00186   virtual int get_dest(Tile_coord& dest);
00187   };
00188 #endif
00189 
00190 /*
00191  *  Activate an object.
00192  */
00193 class Activate_actor_action : public Actor_action
00194   {
00195   Game_object *obj;
00196 public:
00197   Activate_actor_action(Game_object *o) : obj(o)
00198     {  }
00199           // Handle time event.
00200   virtual int handle_event(Actor *actor);
00201   };
00202 
00203 /*
00204  *  Go through a series of frames.
00205  */
00206 class Frames_actor_action : public Actor_action
00207   {
00208   signed char *frames;    // List to go through (a -1 means to
00209           //   leave frame alone.)
00210   int cnt;      // Size of list.
00211   int index;      // Index for next.
00212   int speed;      // Frame delay in 1/1000 secs.
00213   Game_object *obj;   // Object to animate
00214 public:
00215   Frames_actor_action(signed char *f, int c, int spd = 200, Game_object *o = 0);
00216   virtual ~Frames_actor_action()
00217     { delete [] frames; }
00218           // Handle time event.
00219   virtual int handle_event(Actor *actor);
00220   int get_index()
00221     { return index; }
00222   };
00223 
00224 /*
00225  *  Call a usecode function.
00226  */
00227 class Usecode_actor_action : public Actor_action
00228   {
00229   int fun;      // Fun. #.
00230   Game_object *item;    // Call it on this item.  
00231   int eventid;
00232 public:
00233   Usecode_actor_action(int f, Game_object *i, int ev)
00234     : fun(f), item(i), eventid(ev)
00235     {  }  
00236           // Handle time event.
00237   virtual int handle_event(Actor *actor);
00238   };
00239 
00240 /*
00241  *  Do a sequence of actions.
00242  */
00243 class Sequence_actor_action : public Actor_action
00244   {
00245   Actor_action **actions;   // List of actions, ending with null.
00246   int index;      // Index into list.
00247   int speed;      // Frame delay in 1/1000 secs. between
00248           //   actions.
00249 public:
00250           // Create with allocated list.
00251   Sequence_actor_action(Actor_action **act, int spd = 100) 
00252           : actions(act), index(0), speed(spd)
00253     {  }
00254           // Create with up to 4.
00255   Sequence_actor_action(Actor_action *a0, Actor_action *a1,
00256         Actor_action *a2 = 0, Actor_action *a3 = 0);
00257   void set_speed(int spd)
00258     { speed = spd; }
00259   virtual ~Sequence_actor_action();
00260           // Handle time event.
00261   virtual int handle_event(Actor *actor);
00262   };
00263 
00264 //
00265 //  The below could perhaps go into a highact.h file.
00266 //
00267 
00268 /*
00269  *  Rotate through an object's frames.
00270  */
00271 class Object_animate_actor_action : public Actor_action
00272   {
00273   Game_object *obj;
00274   int nframes;      // # of frames.
00275   int cycles;     // # of cycles to do.
00276   int speed;      // Time between frames.
00277 public:
00278   Object_animate_actor_action(Game_object *o, int cy, int spd);
00279   Object_animate_actor_action(Game_object *o, int nframes, int cy, int spd);
00280           // Handle time event.
00281   virtual int handle_event(Actor *actor);
00282   };
00283 
00284 /*
00285  *  Action to pick up an item or put it down.
00286  */
00287 
00288 class Pickup_actor_action : public Actor_action
00289   {
00290   Game_object *obj;   // What to pick up/put down.
00291   int pickup;     // 1 to pick up, 0 to put down.
00292   int speed;      // Time between frames.
00293   int cnt;      // 0, 1, 2.
00294   Tile_coord objpos;    // Where to put it.
00295   int dir;      // Direction to face.
00296 public:
00297           // To pick up an object:
00298   Pickup_actor_action(Game_object *o, int spd);
00299           // To put down an object:
00300   Pickup_actor_action(Game_object *o, Tile_coord opos, int spd);
00301   virtual int handle_event(Actor *actor);
00302   };
00303   
00304 
00305 /*
00306  *  Action to turn towards an object or spot.
00307  */
00308 
00309 class Face_pos_actor_action : public Actor_action
00310   {
00311   int speed;      // Time between frames.
00312   Tile_coord pos;     // Where to put it.
00313 public:
00314   Face_pos_actor_action(Tile_coord p, int spd);
00315           // To pick up an object:
00316   Face_pos_actor_action(Game_object *o, int spd);
00317   virtual int handle_event(Actor *actor);
00318   };
00319   
00320 
00321 #endif  /* INCL_ACTIONS */
00322 

Generated on Mon Jul 9 14:42:39 2007 for ExultEngine by  doxygen 1.5.1