File: intel.h

package info (click to toggle)
xevil 1.5.1e-2.2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,580 kB
  • ctags: 2,655
  • sloc: cpp: 17,863; makefile: 223; sh: 1
file content (379 lines) | stat: -rw-r--r-- 10,464 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// "intel.h"

/*    Copyright (C) 1994,1995,1996  Steve Hardt

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Steve Hardt 
    hardts@mit.edu (valid until Nov. 1996)
    hardts@netscape.com
    hardts@alum.mit.edu
    http://graphics.lcs.mit.edu/~hardts/xevil.html
*/

#ifndef INTEL_H
#define INTEL_H

#ifndef NO_PRAGMAS
#pragma interface
#endif


// Include Files
#include "utils.h"
#include "coord.h"
#include "id.h"
#include "locator.h"
#include "world.h"



// Defines
#define IT_STRING_LENGTH 80
#define IT_INFINITE_LIVES -1


class Physical;
typedef Physical *PhysicalP;


/* Possible commands to be given to objects.
   Order of directions is guaranteed.  (See ui.h and game.C) */
  enum {
    IT_CENTER,
    IT_R, IT_DN_R, IT_DN, IT_DN_L,
    IT_L, IT_UP_L, IT_UP, IT_UP_R,
    IT_WEAPON_CENTER,IT_WEAPON_CHANGE,IT_WEAPON_DROP,
    IT_ITEM_USE,IT_ITEM_CHANGE,IT_ITEM_DROP,
    
    IT_WEAPON_R,IT_WEAPON_DN_R,IT_WEAPON_DN,IT_WEAPON_DN_L,
    IT_WEAPON_L,IT_WEAPON_UP_L,IT_WEAPON_UP,IT_WEAPON_UP_R,

    IT_NO_COMMAND};
typedef int ITcommand;
#define IT_COMMAND_MAX IT_NO_COMMAND



// IntelOptions
typedef unsigned long ITmask;
#define ITnone 0L
#define ITharmless (1L<<0)
#define ITclassFriends (1L<<1)
#define ITpsychotic (1L<<2)
#define ITignoreItems (1L<<3)
#define ITignoreLemmings (1L<<4)

struct IntelOptions {
  Boolean classFriends; // Do not attack members of same class.
  Boolean harmless; // Will not attack even if it has the ability to do so.
  Boolean psychotic; // Will never run away.  harmless overrides psychotic.
  Boolean ignoreItems; // Don't try to pick up items.
  Boolean ignoreLemmings; // Don't attack Lemmings.
};



// IntelStatus used by ui.
// Remember Intel::die
struct IntelStatus {
  // strings and ClassIds must be kept consistent.

  char name[IT_STRING_LENGTH];
  ClassId classId;
  char className[IT_STRING_LENGTH];
  Health health; // -1 means dead.
  Mass mass;

  ClassId weaponClassId;  
  char weapon[IT_STRING_LENGTH];
  Boolean weaponReady;
  int ammo;

  ClassId itemClassId;
  char item[IT_STRING_LENGTH];
  int itemCount; // Number of the item.

  int lives; // Can be IT_INFINITE_LIVES.
  int humanKills;
  int enemyKills;
  int soups; // Human killed by something other than a human.  Only looked at
             // when game style is UIsettings::DUEL.
};

  

// Class Definitions.
class Intel {
 public:
  Intel(WorldP w,LocatorP l,char *name,int lives,
	const IntelOptions *ops,ITmask opMask); 
  /* EFFECTS: Create a new intel with the options specified in opMakse that are
     in ops.  ops will never be referenced if opMask is ITnone.  lives are
     decremented as human is created.  lives can also be set to 
     IT_INFINTE_LIVES. */

  virtual Boolean is_human();
  /* NOTE: !is_human() iff Intel is a Machine. */
  virtual Boolean is_enemy();
  virtual Boolean is_lemming_intel();

  Boolean alive() {return living;}
  WorldP get_world() {return world;}
  LocatorP get_locator() {return locator;}

  const Id &get_id() {return id;}
  /* NOTE: If dead, return an invalid id. */

  Boolean reincarnate_me();
  /* EFFECTS: Should *this be reincarnated. */

  Boolean intel_status_changed() {return intelStatusChanged;}

  const IntelStatus *get_intel_status() 
    {intelStatusChanged = False; return &intelStatus;}

  const char *get_name() {return intelStatus.name;}
  int get_lives() {return intelStatus.lives;} // Can be IT_INFINITE_LIVES.
  int get_human_kills() {return intelStatus.humanKills;}
  int get_enemy_kills() {return intelStatus.enemyKills;}
  int get_soups() {return intelStatus.soups;}
  /* NOTE: Could achieve the same thing with get_intel_status.  Convenience 
   functions. */

  IntelId get_killer_intel_id() {return killerIntelId;}

  IntelId get_intel_id() {return intelId;}

  void set_id(const Id &i) {id = i;}
  /* REQUIRES: i is valid. */
  /* NOTE: Only needed for Physical::set_intel.  id is updated on every 
     clock. */

  void set_lives(int lives) 
    {intelStatus.lives = lives; intelStatusChanged = True;}
  /* NOTE: Can be IT_INFINITE_LIVES. */

  virtual void add_human_kill();

  virtual void add_enemy_kill();

  virtual void add_soup();

  void set_killer_intel_id(const IntelId &k) {killerIntelId = k;}

  void set_intel_id(const IntelId &i) {intelId = i;}
  /* NOTE: Should only be used by the Locator. */

  static ITcommand dir_to_command(Dir dir);
  /* REQUIRES: dir is a "pure" direction or CO_air. */
  /* NOTE: CO_air -> IT_CENTER. */

  static ITcommand dir_to_command_weapon(Dir dir);

  static Dir command_weapon_to_dir_4(ITcommand);

  static Dir command_weapon_to_dir_8(ITcommand);
  /* EFFECTS: If command is a weapon command return the corresponding Dir. */
  /* NOTE: Like Gun::compute_weapon_dir. */

  static Boolean is_command_weapon(ITcommand command)
    {return (command >= IT_WEAPON_R && command <= IT_WEAPON_UP_R) || 
       command == IT_WEAPON_CENTER;}

  static ITcommand center_pos_to_command(const Pos &pos);
  /* EFFECTS: Returns the command corresponding to the direction from (0,0) to
     pos. */
  /* NOTE: pos can have negative components. */
     

  void die(); 
  /* EFFECTS: Tell *this that it no longer has anything clocking it. */
  /* NOTE: This MUST be called at death.  Game requires it to refill enemy
     players. */

  void reincarnate();
  /* EFFECTS: Tell *this that it will be clocked again. */

  virtual void clock(PhysicalP p);
  /* NOTE: Should be called by Intel's children. */


#ifndef PROTECTED_IS_PUBLIC
 protected:
#endif
  const IntelOptions &get_intel_options() {return intelOptions;}


 private:
  IntelStatus intelStatus;
  Boolean intelStatusChanged; // Since last get_intel_status.
  Boolean living;
  WorldP world;
  LocatorP locator;
  Id id;
  IntelId killerIntelId;  // Intel that most recently attacked the Intel.
  IntelId intelId;
  IntelOptions intelOptions;

  const static IntelOptions intelOptionsDefault;
};
typedef Intel *IntelP;



// Simply buffers input from a user interface.
class Human: public Intel {
 public: 
  Human(WorldP w,LocatorP l,char *name,int lives,ColorNum colorNum);
  /* NOTE: Uses all default options.  intelOptions should be meaningless for 
     a human. */

  ColorNum get_color_num() {return colorNum;}

  virtual Boolean is_human();
    
  void set_command(ITcommand c) {command = c;}
  /* EFFECTS: Asynchronously set the command for the current turn to be c.
     Overrides previous settings. */

  virtual void clock(PhysicalP p);
  /* EFFECTS: Sets the command for p. */


 private:
  ITcommand command;
  ColorNum colorNum;
};
typedef Human *HumanP;



class Machine: public Intel {
  enum Strategy {doNothing, toPos, toTarget, awayTarget, toMaster};
  
public:
  Machine(WorldP w,LocatorP l,char *name,
	  const IntelOptions *ops,ITmask opMask,
	  IntelP master = NULL);
  
  Boolean is_slave() {return !masterSet;}

  IntelId get_master_intel_id();
  /* EFFECTS: If a slave, return the IntelId of the master.  Otherwise, 
     return an invalid IntelId. */

  virtual void add_human_kill();

  virtual void add_enemy_kill();

  virtual void add_soup();

  virtual void clock(PhysicalP);
  /* NOTE: Not called by Machine's children. */


#ifndef PROTECTED_IS_PUBLIC
protected:
#endif
  static Boolean attack_target(PhysicalP p,PhysicalP target);
  static Boolean move_target(PhysicalP p,PhysicalP target,Timer &ladderJump);
  static Boolean away_target(PhysicalP p,PhysicalP target,Timer &ladderJump);
  static Boolean move_pos(PhysicalP p,const Pos &targetPos,Timer &ladderJump);
  static Boolean move_dir(PhysicalP p,Dir d,Timer &ladderJump);
  /* REQUIRES: p and target must be non-NULL. */
  /* REQUIRES: d is an element of {CO_R..CO_UP_R,CO_air} */
  /* EFFECTS: The above are all utility functions that attempt to set the
     command for p to do something.  Returns whether the command was set or
     not. */
  /* IMPLEMENTATION NOTE: Declared static to ensure that no data members of 
     Machine are used in these utility functions.  We */


  static Boolean has_gun(PhysicalP);
  static Boolean has_cutter(PhysicalP);
  static Boolean has_shield(PhysicalP);
  static Boolean has_item(PhysicalP,ClassId);
  static Boolean weapon_current_is_gun(PhysicalP);
  static Boolean weapon_current_is_cutter(PhysicalP);
  /* EFFECTS: Return info about a Physical. */


private:
  PhysicalP new_target_id(Boolean &isEnemy,PhysicalP p,PhysicalP masterP);
  /* MODIFIES: isItem */
  /* EFFECTS: Get a new target for p.  Sets targetId, sets isEnemy to True iff
     returned pointer is an enemy (as opposed to an item).
     Returns NULL if no suitable target found.  masterP is the master's
     Physical or NULL. */


  IntelId masterIntelId; // Meaningful iff masterSet.
  Boolean masterSet; // Is the Machine a slave to another Intel.
  Strategy strategy;
  Pos targetPos;
  Id targetId;
  Timer strategyChange;
  Timer reflexes;
  Timer ladderJump; // don't get back on ladder immediately after jumping off.
};
typedef Machine *MachineP;



class Enemy: public Machine {
public:
  Enemy(WorldP w,LocatorP l,char *name,
	const IntelOptions *ops,ITmask opMask,
	IntelP master = NULL)
    :Machine(w,l,name,ops,opMask,master) {}
  
  virtual Boolean is_enemy();
};
typedef Enemy *EnemyP;




class Neutral: public Machine {
public:
  Neutral(WorldP w,LocatorP l,char *name,
	const IntelOptions *ops,ITmask opMask,
	IntelP master = NULL)
    :Machine(w,l,name,ops,opMask,master) {}
  
  virtual Boolean is_enemy();
};
typedef Neutral *NeutralP;




class LemmingIntel: public Neutral {
public:
  LemmingIntel(WorldP w,LocatorP l,char *name,const Id &homeId);
  
  virtual Boolean is_lemming_intel();

  virtual void clock(PhysicalP);
  /* NOTE: Does not call Neutral::clock. */
  

 private:
  Timer ladderJump; // don't get back on ladder immediately after jumping off.
  Id homeId;
};
#endif