source: golgotha/src/golg/team_api.hh

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 7.7 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#ifndef TEAM_API_HH
10#define TEAM_API_HH
11
12#include "global_id.hh"
13#include "objs/map_piece.hh"
14
15class i4_file_class;                       // file/file.hh
16class g1_player_piece_class;               // objs/stank.hh
17class g1_player_info_class;                // player.hh
18class g1_team_api_definition_class;        // later in file
19
20class g1_team_api_class
21{
22private:
23  friend class g1_player_info_class;
24  friend class g1_team_api_definition_class;
25
26  const g1_team_api_definition_class *def;
27
28protected:
29  i4_file_class *record, *playback;
30public:
31  g1_player_info_class *player;
32
33  // required by human to set mode of stank
34  friend class g1_human_class;
35  g1_player_piece_class *commander() const;       // get supertank pointer (NULL = dead)
36
37  const g1_team_api_definition_class *definer() const { return def; }
38  g1_team_api_class();
39  virtual ~g1_team_api_class();
40
41  // super tank info
42  sw16 health() const;
43  w16 ammo0() const;            // amount of ammo
44  w16 ammo1() const;
45  w16 ammo2() const;
46  i4_bool full0() const;        // ammo fully loaded?
47  i4_bool full1() const;
48  i4_bool full2() const;
49  i4_bool in_range0() const;    // attack target in range?
50  i4_bool in_range1() const;
51  i4_bool in_range2() const;
52
53  // super tank input (mainly useful to human player)
54  void turn(i4_float angle);                      // turn supertank clockwise
55  void accelerate(i4_float ratio);                // accelerate ratio of maximum acceleration
56  void strafe(i4_float ratio);                    // slide right ratio of maximum acceleration
57
58  void look(i4_float dax, i4_float day);          // set turret direction relative to heading
59  i4_bool fire0();                                // fire first weapon
60  i4_bool fire1();                                // fire second weapon
61  i4_bool fire2();                                // fire third weapon
62
63  i4_bool continue_game();                        // when dead/game over etc, this indicates game continues
64
65  // interface to units
66  class unit_class
67  //{{{
68  {
69  protected:
70    friend class g1_team_api_class;
71
72    w32 global_id;
73    unit_class(w32 id) : global_id(0)
74    {
75      if (g1_global_id.check_id(id) && g1_map_piece_class::cast(g1_global_id.get(id)))
76        // living map pieces only
77        global_id = id;
78    }
79    g1_map_piece_class *cast() const { return (g1_map_piece_class *)g1_global_id.get(global_id); }
80  public:
81    unit_class() : global_id(0) {}
82    unit_class(const unit_class &a) : global_id(a.global_id) {}
83
84    i4_bool moving() const
85    {
86      g1_map_piece_class *p=cast();
87      return (p->x==p->lx && p->y==p->ly);
88    }
89
90    i4_float x() const        { return cast()->x; }
91    i4_float y() const        { return cast()->y; }
92    i4_float height() const   { return cast()->h; }
93    i4_float dir() const      { return cast()->theta; }
94    sw16 health() const       { return cast()->health; }
95    w8 team() const           { return cast()->player_num; }
96
97    w32 id() const            { return global_id; }
98    w32 type() const          { return cast()->id; }
99
100    i4_bool alive() const     { return g1_global_id.check_id(global_id) && health()>0; }
101    i4_bool built() const     { return g1_global_id.check_id(global_id) && cast()->alive(); }
102  };
103  //}}}
104  unit_class unit(w32 id) { return unit_class(id); }
105
106  // interface to objects
107  class object_class
108  //{{{
109  {
110  protected:
111    friend class g1_team_api_class;
112
113    w32 global_id;
114    object_class(w32 id) : global_id(id)
115    {
116      if (!alive())
117        global_id = 0;
118    }
119    g1_object_class *cast() const { return g1_global_id.get(global_id); }
120  public:
121    object_class() : global_id(0) {}
122    object_class(const object_class &a) : global_id(a.global_id) {}
123
124    i4_float x() const        { return cast()->x; }
125    i4_float y() const        { return cast()->y; }
126    i4_float height() const   { return cast()->h; }
127    i4_float dir() const      { return cast()->theta; }
128    sw16 health() const       { return cast()->health; }
129    w8 team() const           { return cast()->player_num; }
130
131    w32 id() const            { return global_id; }
132    w32 type() const          { return cast()->id; }
133
134    i4_bool alive() const     { return g1_global_id.check_id(global_id) && health()>0; }
135  };
136  //}}}
137  object_class object(w32 id) { return object_class(id); }
138
139  // strategic info
140
141  g1_player_type team() const;
142  sw32 money() const;
143
144  g1_map_class *map() const;                      // get strategic map
145  w32 map_width() const;
146  w32 map_height() const;
147  i4_float terrain_height(i4_float x, i4_float y,     // get map height below location
148                          i4_float z=0) const;
149
150  i4_bool is_building() const;                    // still building objects?
151
152  w16 object_type(const char *name) const;
153
154  // ******************** COMMANDS (sent to server) *******************
155  i4_bool deploy_unit(w32 global_id, i4_float x, i4_float y);  // send object to location
156                   
157  int build_unit(g1_object_type type);
158  // try to build unit
159
160  i4_bool set_current_target(w32 global_id); // should be a path_object!
161
162  // ******************** RESPONSES (come back from server) ************
163  virtual void object_built(w32 id)
164  {
165  }
166 
167  virtual void object_lost(w32 id)     {}
168  virtual void object_added(w32 id)    {}
169
170  virtual void init()                       {}
171  // should handle reinitialization as well.  assume that everything about the level disappeared.
172
173  virtual void save(g1_saver_class *fp) {}
174  virtual void load(g1_loader_class *fp) {}
175
176  // playback handling
177  i4_bool record_start(char *name);
178  void record_end();
179  i4_bool playback_start(i4_file_class *fp);     // this file pointer will be closed when teamapi is done with it
180  void playback_end();
181  i4_bool playback_think();                       // think function to implement playbacks
182  void post_think();                              // stuff to do after thinking (recording)
183
184  // team think
185  virtual void think() = 0;                       // AI's think function
186};
187
188// AI definition class to add new AI's to game
189class g1_team_api_definition_class
190{
191protected:
192  static g1_team_api_definition_class *first;
193  g1_team_api_definition_class *next;
194
195  virtual g1_team_api_class *create(g1_loader_class *f)=0;
196  const char *_name;
197  g1_team_api_class* own_team_api(g1_team_api_class *ai) { ai->def = this; return ai; }
198public:
199  g1_team_api_definition_class(const char *name);
200  ~g1_team_api_definition_class();
201
202  const char *name() const { return _name; }
203  static g1_team_api_class *create(const char *name, g1_loader_class *f=0);
204};
205
206// give a simpler alias to the create mechanism
207inline g1_team_api_class *g1_create_ai(const char *name, g1_loader_class *f=0)
208{
209  return g1_team_api_definition_class::create(name,f);
210}
211
212// template magic to ease adding new AI's into game
213template <class T>
214class g1_team_api_definer : public g1_team_api_definition_class
215{
216public:
217  g1_team_api_definer<T>(const char *name)
218    : g1_team_api_definition_class(name) {}
219
220  virtual g1_team_api_class *create(g1_loader_class *f) { return own_team_api(new T(f)); }
221};
222
223#endif
224
225//{{{ Emacs Locals
226// Local Variables:
227// folded-file: t
228// End:
229//}}}
Note: See TracBrowser for help on using the repository browser.