objs.h

Go to the documentation of this file.
00001 /*
00002  *  objs.h - Game objects.
00003  *
00004  *  Copyright (C) 1998-1999  Jeffrey S. Freedman
00005  *  Copyright (C) 2000-2001  The Exult Team
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #ifndef OBJS_H
00023 #define OBJS_H
00024 
00025 #include <string> // STL string
00026 #include "exult_constants.h"
00027 #include "exult_types.h"
00028 #include "flags.h"
00029 #include "rect.h"
00030 #include "shapeid.h"
00031 #include "tqueue.h"
00032 #include "tiles.h"
00033 #include "vec.h"
00034 
00035 #include "objlist.h"
00036 
00037 class Actor;
00038 class Map_chunk;
00039 class Container_game_object;
00040 class Terrain_game_object;
00041 class Egg_object;
00042 class Barge_object;
00043 class Game_window;
00044 class Npc_actor;
00045 class PathFinder;
00046 class Rectangle;
00047 class Schedule;
00048 class Usecode_machine;
00049 class Vga_file;
00050 class DataSource;
00051 
00052 template<class T>
00053 class T_Object_list;
00054 
00055 /*
00056  *  A game object is a shape from shapes.vga along with info. about its
00057  *  position within its chunk.
00058  */
00059 class Game_object : public ShapeID
00060   {
00061 protected:
00062   static Game_object *editing;  // Obj. being edited by ExultStudio.
00063   unsigned char shape_pos;  // (X,Y) of shape within chunk.
00064   unsigned char lift;   // Raise by 4* this number.
00065   short quality;      // Some sort of game attribute.
00066 private:
00067   Game_object *next, *prev; // ->next in chunk list or container.
00068   Game_object_vector dependencies;// Objects which must be painted before
00069           //   this can be rendered.
00070   Game_object_vector dependors; // Objects which must be painted after.
00071   static unsigned char rotate[8]; // For getting rotated frame #.
00072 public:
00073   uint32 render_seq;    // Render sequence #.
00074 protected:
00075   unsigned char cx, cy;   // (Absolute) chunk coords., or if this
00076           //   is in a container, coords. within
00077           //   gump's rectangle.
00078           // Handle attack on an object.
00079   int attack_object(Actor *attacker, int weapon_shape, int ammo_shape);
00080           // Create from ifix record.
00081   Game_object(unsigned char *ifix)
00082       : ShapeID(ifix[2], ifix[3]), shape_pos(ifix[0]),
00083         lift(ifix[1] & 0xf), quality(0), cx(255), cy(255)
00084     {  }
00085 public:
00086   friend class T_Object_list<Game_object *>;
00087   friend class T_Object_iterator<Game_object *>;
00088   friend class T_Flat_object_iterator<Game_object *,Map_chunk *>;
00089   friend class T_Object_iterator_backwards<Game_object *, 
00090               Map_chunk *>;
00091   friend class Map_chunk;
00092   Game_object(int shapenum, int framenum, unsigned int tilex, 
00093         unsigned int tiley, unsigned int lft = 0)
00094     : ShapeID(shapenum, framenum),
00095         shape_pos((tilex << 4) + tiley),
00096       lift(lft), quality(0), cx(255), cy(255)
00097     {  }
00098           // Copy constructor.
00099   Game_object(const Game_object& obj2)
00100     : ShapeID(obj2), shape_pos(obj2.shape_pos), lift(obj2.lift),
00101       quality(obj2.quality), cx(obj2.cx), cy(obj2.cy)
00102     {  }
00103   Game_object() : ShapeID() // Create fake entry.
00104     {  }
00105   virtual ~Game_object()
00106     {  }
00107   int get_tx() const    // Get tile (0-15) within chunk.
00108     { return (shape_pos >> 4) & 0xf; }
00109   int get_ty() const
00110     { return shape_pos & 0xf; }
00111   int get_lift() const
00112     { return lift; }
00113   Tile_coord get_tile() const // Get location in abs. tiles.
00114     {
00115     return Tile_coord(cx*c_tiles_per_chunk + get_tx(),
00116         cy*c_tiles_per_chunk + get_ty(), lift);
00117     }
00118           // Get distance to another object.
00119   int distance(Game_object *o2) const
00120     { return get_tile().distance(
00121           o2->get_tile()); }
00122           // Get direction to another object.
00123   int get_direction(Game_object *o2) const;
00124   int get_direction(Tile_coord t2) const;
00125   Map_chunk *get_chunk();   // Get chunk this is in.
00126   int get_quality() const
00127     { return quality; }
00128   void set_quality(int q)
00129     { quality = q; }
00130   int get_quantity() const; // Like # of coins.
00131   virtual int get_obj_hp() const;     // hitpoints for non-NPCs
00132   virtual void set_obj_hp(int hp);
00133   int get_volume() const;   // Get space taken.
00134           // Add/remove to/from quantity.
00135   int modify_quantity(int delta, bool *del = 0);
00136           // Set shape coord. within chunk.
00137   void set_shape_pos(unsigned int shapex, unsigned int shapey)
00138     { shape_pos = (shapex << 4) + shapey; }
00139   void set_lift(int l)
00140     { lift = l; }
00141   Game_object *get_next()
00142     { return next; }
00143   Game_object *get_prev()
00144     { return prev; }
00145           // Compare for render order.
00146   static int compare(class Ordering_info& inf1, Game_object *obj2);
00147   int lt(Game_object& obj2);  // Is this less than another in pos.?
00148           // Return chunk coords.
00149   int get_cx() const
00150     { return cx; }
00151   int get_cy() const
00152     { return cy; }
00153   void set_invalid()    // Set to invalid position.
00154     { cx = cy = 255; }
00155   bool is_pos_invalid() const
00156     { return cx >= c_num_chunks || cy >= c_num_chunks; }
00157   void set_chunk(int newcx, int newcy)
00158     { cx = newcx; cy = newcy; }
00159           // Get frame for desired direction.
00160   int get_dir_framenum(int dir, int frnum) const
00161     { return (frnum&0xf) + rotate[dir]; }
00162           // Get it using current dir.
00163   int get_dir_framenum(int frnum) const
00164     { return (frnum&0xf) + (get_framenum()&(16 | 32)); }
00165           // Get direction (NPC) is facing.
00166   int get_dir_facing() const;
00167           // Move to new abs. location.
00168   virtual void move(int newtx, int newty, int newlift);
00169   void move(Tile_coord t)
00170     { move(t.tx, t.ty, t.tz); }
00171   void change_frame(int frnum); // Change frame & set to repaint.
00172           // Swap positions.
00173   int swap_positions(Game_object *obj2);
00174   int get_dependency_count()  // Get objs. to paint first.
00175     { return dependencies.size(); }
00176   Game_object *get_dependency(int i)
00177     { return dependencies[i]; }
00178   void clear_dependencies();  // Remove all dependencies.
00179 
00180           // Find nearby objects.
00181 #define HDR_DECLARE_FIND_NEARBY(decl_type) \
00182   static int find_nearby(decl_type vec, Tile_coord pos, \
00183       int shapenum, int delta, int mask,  \
00184       int qual = c_any_qual, int framenum = c_any_framenum)
00185 
00186   HDR_DECLARE_FIND_NEARBY(Egg_vector&);
00187   HDR_DECLARE_FIND_NEARBY(Actor_vector&);
00188   HDR_DECLARE_FIND_NEARBY(Game_object_vector&);
00189 
00190 #undef HDR_DECLARE_FIND_NEARBY
00191 
00192   int find_nearby_actors(Actor_vector& vec, int shapenum, int delta) 
00193                   const;
00194   int find_nearby_eggs(Egg_vector& vec, int shapenum, int delta,
00195     int qual = c_any_qual, int frnum = c_any_framenum) const;
00196   int find_nearby(Game_object_vector& vec, int shapenum, int delta, 
00197     int mask, int qual = c_any_qual, 
00198           int framenum = c_any_framenum) const;
00199 
00200   Game_object *find_closest(Game_object_vector& vec,
00201         int *shapenums, int num_shapes, int dist = 24);
00202   Game_object *find_closest(int *shapenums, int num_shapes, 
00203               int dist = 24);
00204   Game_object *find_closest(int shapenum, int dist = 24)
00205     { return find_closest(&shapenum, 1, dist); }
00206   Rectangle get_footprint();  // Get tile footprint.
00207   bool blocks(Tile_coord tile); // Do we block a given tile?
00208           // Find object blocking given tile.
00209   static Game_object *find_blocking(Tile_coord tile);
00210   static Game_object *find_door(Tile_coord tile);
00211   int is_closed_door() const; // Checking for a closed door.
00212   Game_object *get_outermost(); // Get top 'owner' of this object.
00213   void say(const char *text);   // Put text up by item.
00214   void say(int from, int to); // Show random msg. from 'text.flx'.
00215           // Render.
00216   virtual void paint();
00217   void paint_outline(Pixel_colors pix);
00218           // Make this class abstract.
00219   virtual void paint_terrain() = 0;
00220           // Can this be clicked on?
00221   virtual int is_findable()
00222     { return 1; }
00223           // Run usecode function.
00224   virtual void activate(int event = 1);
00225   virtual bool edit();    // Edit in ExultStudio.
00226           // Saved from ExultStudio.
00227   static void update_from_studio(unsigned char *data, int datalen);
00228   virtual std::string get_name() const;
00229           // Remove/delete this object.
00230   virtual void remove_this(int nodel = 0);
00231   virtual Container_game_object *get_owner()
00232     { return 0; }
00233   virtual void set_owner(Container_game_object *o)
00234     {  }
00235   static int get_weight(int shnum, int quant = 1);
00236   virtual int get_weight();
00237   virtual int get_max_weight(); // Get max. weight allowed.
00238   virtual int is_dragable() const;// Can this be dragged?
00239           // Drop another onto this.
00240   virtual int drop(Game_object *obj);
00241           // Set/clear/get actor flag.
00242   virtual void set_flag(int flag) {  }
00243   virtual void set_siflag(int flag) { }
00244   virtual void clear_siflag(int flag) { }
00245   virtual void clear_flag(int flag) { }
00246   virtual int get_flag(int flag) const  { return 0; }
00247   virtual void set_flag_recursively(int flag) { }
00248   virtual int get_siflag(int flag) const { return 0; }
00249   virtual int get_type_flag(int flag) const { return 0; }
00250 
00251   virtual Actor *as_actor() { return 0; }
00252   virtual Npc_actor *as_npc() { return 0; }
00253   virtual Barge_object *as_barge() { return 0; }
00254   virtual Terrain_game_object *as_terrain() { return 0; }
00255   virtual Container_game_object *as_container() { return 0; }
00256   virtual Egg_object *as_egg() { return 0; }
00257   virtual int is_egg() const  // An egg?
00258     { return 0; }
00259           // Count contained objs.
00260   virtual int count_objects(int shapenum, int qual = c_any_qual,
00261               int framenum = c_any_framenum)
00262     { return 0; }
00263           // Get contained objs.
00264   virtual int get_objects(Game_object_vector& vec, int shapenum, int qual,
00265             int framenum)
00266     { return 0; }
00267           // Add an object.
00268   virtual bool add(Game_object *obj, bool dont_check = false,
00269               bool combine = false);
00270           // Add to NPC 'ready' spot.
00271   virtual int add_readied(Game_object *obj, int index, int dont_check = 0, int force_pos = 0)
00272     { return add(obj, dont_check!=0); }
00273   virtual int add_quantity(int delta, int shapenum, int qual = c_any_qual,
00274         int framenum = c_any_framenum, int dontcreate = 0)
00275     { return delta; }
00276   virtual int create_quantity(int delta, int shapenum, int qual,
00277             int framenum, bool temporary = false)
00278     { return delta; }
00279   virtual int remove_quantity(int delta, int shapenum, int qual,
00280                 int framenum)
00281     { return delta; }
00282   virtual Game_object *find_item(int shapenum, int qual, int framenum)
00283     { return 0; }
00284           // Get coord. where this was placed.
00285   virtual Tile_coord get_original_tile_coord() const
00286     { return get_tile(); }
00287           // Move out of the way.
00288   virtual int move_aside(Actor *for_actor, int dir)
00289     { return 0; }   // For now.
00290           // Get frame if rotated clockwise.
00291   virtual int get_rotated_frame(int quads);
00292           // Step onto an (adjacent) tile.
00293   virtual int step(Tile_coord t, int frame)
00294     { return 0; }
00295   virtual int is_monster()
00296     { return 0; }
00297           // Under attack.
00298   virtual Game_object *attacked(Actor *attacker, int weapon_shape = 0,
00299           int ammo_shape = 0);
00300           // Write out to IREG file.
00301   virtual void write_ireg(DataSource* out)
00302     {  }
00303         // Get size of IREG. Returns -1 if can't write to buffer
00304   virtual int get_ireg_size()
00305     { return 0; }
00306           // Write out IFIX, CHUNKS.
00307   virtual void write_ifix(DataSource* ifix)
00308     {  }
00309   virtual void elements_read()  // Called when all member items read.
00310     {  }
00311           // Write common IREG data.
00312   void write_common_ireg(unsigned char *buf);
00313   virtual int get_live_npc_num()
00314     { return -1; }
00315 
00316   virtual void delete_contents() { }
00317 
00318   };
00319 
00320 /*
00321  *  Object from U7chunks.
00322  */
00323 class Terrain_game_object : public Game_object
00324   {
00325 public:
00326   Terrain_game_object(int shapenum, int framenum, unsigned int tilex, 
00327         unsigned int tiley, unsigned int lft = 0)
00328     : Game_object(shapenum, framenum, tilex, tiley, lft)
00329     {  }
00330   virtual Terrain_game_object *as_terrain() { return this; }
00331   virtual void paint_terrain();
00332   };
00333 
00334 /*
00335  *  Object from an IFIXxx file.
00336  */
00337 class Ifix_game_object : public Game_object
00338   {
00339 public:
00340           // Create from ifix record.
00341   Ifix_game_object(unsigned char *ifix) : Game_object(ifix)
00342     {  }
00343   Ifix_game_object(int shapenum, int framenum, unsigned int tilex, 
00344         unsigned int tiley, unsigned int lft = 0)
00345     : Game_object(shapenum, framenum, tilex, tiley, lft)
00346     {  }
00347           // Move to new abs. location.
00348   virtual void move(int newtx, int newty, int newlift);
00349           // Remove/delete this object.
00350   virtual void remove_this(int nodel = 0);
00351   virtual void paint_terrain() {  }
00352   virtual void write_ifix(DataSource* ifix);
00353   };
00354 
00355 #endif

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