C++代码范例

前言

红警源代码中的头文件,写的非常规范,建议模仿。

命名规则

  • 如果是内部函数,那么要加下划线加以提示

头文件范例

点击显/隐内容
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
/***********************************************************************************************
*** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
***********************************************************************************************
* *
* Project Name : Command & Conquer *
* *
* File Name : AIRCRAFT.H *
* *
* Programmer : Joe L. Bostic *
* *
* Start Date : July 22, 1994 *
* *
* Last Update : November 28, 1994 [JLB] *
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

#ifndef AIRCRAFT_H
#define AIRCRAFT_H

#include "radio.h"
#include "fly.h"
#include "target.h"

/*
** This aircraft class is used for all flying sentient objects. This includes fixed wing
** aircraft as well as helicopters. It excludes bullets even though some bullets might
** be considered to be "flying" in a loose interpretatin of the word.
*/
class AircraftClass : public FootClass, public FlyClass
{
public:
/*
** This is a pointer to the class control structure for the aircraft.
*/
CCPtr<AircraftTypeClass> Class;

//-----------------------------------------------------------------------------
static void * operator new(size_t);
static void * operator new(size_t, void * ptr) {return(ptr);};
static void operator delete(void *);
operator AircraftType(void) const {return Class->Type;};
AircraftClass(AircraftType classid, HousesType house);
AircraftClass(NoInitClass const & x) : FootClass(x), FlyClass(x), Class(x), SecondaryFacing(x), SightTimer(x) {};
virtual ~AircraftClass(void);

static void Init(void);

virtual int Mission_Attack(void);
virtual int Mission_Unload(void);
virtual int Mission_Hunt(void);
virtual int Mission_Retreat(void);
virtual int Mission_Move(void);
virtual int Mission_Enter(void);
virtual int Mission_Guard(void);
virtual int Mission_Guard_Area(void);

virtual void Assign_Destination(TARGET target);
/*
** State machine support routines.
*/
bool Process_Take_Off(void);
bool Process_Landing(void);
int Process_Fly_To(bool slowdown, TARGET dest);

/*
** Query functions.
*/
virtual LayerType In_Which_Layer(void) const;
virtual DirType Turret_Facing(void) const {return(SecondaryFacing.Current());}
int Shape_Number(void) const;
virtual MoveType Can_Enter_Cell(CELL cell, FacingType facing=FACING_NONE) const;
virtual ObjectTypeClass const & Class_Of(void) const {return *Class;};
virtual ActionType What_Action(ObjectClass const * target) const;
virtual ActionType What_Action(CELL cell) const;
virtual DirType Desired_Load_Dir(ObjectClass * passenger, CELL & moveto) const;
virtual int Pip_Count(void) const;
TARGET Good_Fire_Location(TARGET target) const;
bool Cell_Seems_Ok(CELL cell, bool landing=false) const;
DirType Pose_Dir(void) const;
TARGET Good_LZ(void) const;
virtual DirType Fire_Direction(void) const;
virtual FireErrorType Can_Fire(TARGET target, int which) const;

/*
** Landing zone support functionality.
*/
virtual void Per_Cell_Process(PCPType why);
bool Is_LZ_Clear(TARGET target) const;
TARGET New_LZ(TARGET oldlz) const;

/*
** Coordinate inquiry functions. These are used for both display and
** combat purposes.
*/
virtual COORDINATE Sort_Y(void) const;

/*
** Object entry and exit from the game system.
*/
virtual bool Unlimbo(COORDINATE , DirType facing = DIR_N);

/*
** Display and rendering support functionality. Supports imagery and how
** object interacts with the map and thus indirectly controls rendering.
*/
virtual void Look(bool incremental=false);
void Draw_Rotors(int x, int y, WindowNumberType window) const;
virtual int Exit_Object(TechnoClass *);
virtual short const * Overlap_List(bool redraw=false) const;
virtual void Draw_It(int x, int y, WindowNumberType window) const;
virtual void Set_Speed(int speed);

/*
** User I/O.
*/
virtual void Active_Click_With(ActionType action, ObjectClass * object);
virtual void Active_Click_With(ActionType action, CELL cell);
virtual void Player_Assign_Mission(MissionType mission, TARGET target=TARGET_NONE, TARGET destination=TARGET_NONE);
virtual void Response_Select(void);
virtual void Response_Move(void);
virtual void Response_Attack(void);

/*
** Combat related.
*/
virtual ResultType Take_Damage(int & damage, int distance, WarheadType warhead, TechnoClass * source, bool forced=false);
virtual BulletClass * Fire_At(TARGET target, int which);

/*
** AI.
*/
bool Landing_Takeoff_AI(void);
bool Edge_Of_World_AI(void);
void Movement_AI(void);
void Rotation_AI(void);
int Paradrop_Cargo(void);
virtual void AI(void);
virtual void Enter_Idle_Mode(bool initial = false);
virtual RadioMessageType Receive_Message(RadioClass * from, RadioMessageType message, long & param);
virtual void Scatter(COORDINATE threat, bool forced=false, bool nokidding=false);

/*
** Scenario and debug support.
*/
#ifdef CHEAT_KEYS
virtual void Debug_Dump(MonoClass *mono) const;
#endif

/*
** File I/O.
*/
static void Read_INI(CCINIClass & ini);
static char * INI_Name(void) {return "AIRCRAFT";};
bool Load(Straw & file);
bool Save(Pipe & file) const;

virtual unsigned Spied_By() const;

public:

/*
** This is the facing used for the body of the aircraft. Typically, this is the same
** as the PrimaryFacing, but in the case of helicopters, it can be different.
*/
FacingClass SecondaryFacing;

/*
** If this is a passenger carrying aircraft then this flag will be set. This is
** necessary because once the passengers are unloaded, the fact that it was a
** passenger carrier must still be known.
*/
bool Passenger;

private:

/*
** Aircraft can be in either state of landing, taking off, or in steady altitude.
** These flags are used to control transition between flying and landing. It is
** necessary to handle the transition in this manner so that it occurs smoothly
** during the graphic processing section.
*/
unsigned IsLanding:1;
unsigned IsTakingOff:1;

/*
** It is very common for aircraft to be homing in on a target. When this flag is
** true, the aircraft will constantly adjust its facing toward the TarCom. When the
** target is very close (one cell away or less), then this flag is automatically cleared.
** This is because the homing algorithm is designed to get the aircraft to the destination
** but no more. Checking when this flag is cleared is a way of flagging transition into
** a new mode. Example: Transport helicopters go into a hovering into correct position
** mode when the target is reached.
*/
unsigned IsHoming:1;

/*
** Helicopters that are about to land must hover into a position exactly above the landing
** zone. When this flag is true, the aircraft will be adjusted so that it is exactly over
** the TarCom. The facing of the aircraft is not altered by this movement. The affect
** like the helicopter is hovering and shifting sideways to position over the landing
** zone. When the position is over the landing zone, then this flag is set to false.
*/
unsigned IsHovering:1;

/*
** This is the jitter tracker to be used when the aircraft is a helicopter and
** is flying. It is most noticeable when the helicopter is hovering.
*/
unsigned char Jitter;

private:

/*
** This timer controls when the aircraft will reveal the terrain around itself.
** When this timer expires and this aircraft has a sight range, then the
** look around process will occur.
*/
CDTimerClass<FrameTimerClass> SightTimer;

/*
** Most attack aircraft can make several attack runs. This value contains the
** number of attack runs the aircraft has left. When this value reaches
** zero then the aircraft is technically out of ammo.
*/
char AttacksRemaining;

/*
** Some additional padding in case we need to add data to the class and maintain backwards compatibility for save/load
*/
unsigned char SaveLoadPadding[32];
};

#endif

CPP文件范例

点击显/隐内容
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
/***********************************************************************************************
*** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
***********************************************************************************************
* *
* Project Name : Command & Conquer *
* *
* File Name : AIRCRAFT.CPP *
* *
* Programmer : Joe L. Bostic *
* *
* Start Date : July 22, 1994 *
* *
* Last Update : November 2, 1996 [JLB] *
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* AircraftClass::AI -- Processes the normal non-graphic AI for the aircraft. *
* AircraftClass::Active_Click_With -- Handles clicking over specified cell. *
* AircraftClass::Active_Click_With -- Handles clicking over specified object. *
* AircraftClass::AircraftClass -- The constructor for aircraft objects. *
* AircraftClass::Can_Enter_Cell -- Determines if the aircraft can land at this location. *
* AircraftClass::Can_Fire -- Checks to see if the aircraft can fire. *
* AircraftClass::Cell_Seems_Ok -- Checks to see if a cell is good to enter. *
* AircraftClass::Desired_Load_Dir -- Determines where passengers should line up. *
* AircraftClass::Draw_It -- Renders an aircraft object at the location specified. *
* AircraftClass::Draw_Rotors -- Draw rotor blades on the aircraft. *
* AircraftClass::Edge_Of_World_AI -- Detect if aircraft has exited the map. *
* AircraftClass::Enter_Idle_Mode -- Gives the aircraft an appropriate mission. *
* AircraftClass::Exit_Object -- Unloads passenger from aircraft. *
* AircraftClass::Fire_At -- Handles firing a projectile from an aircraft. *
* AircraftClass::Fire_Direction -- Determines the direction of fire. *
* AircraftClass::Good_Fire_Location -- Searches for and finds a good spot to fire from. *
* AircraftClass::Good_LZ -- Locates a good spot ot land. *
* AircraftClass::In_Which_Layer -- Calculates the display layer of the aircraft. *
* AircraftClass::Init -- Initialize the aircraft system to an empty state. *
* AircraftClass::Is_LZ_Clear -- Determines if landing zone is free for landing. *
* AircraftClass::Landing_Takeoff_AI -- Handle aircraft take off and landing processing. *
* AircraftClass::Look -- Aircraft will look if they are on the ground always. *
* AircraftClass::Mission_Attack -- Handles the attack mission for aircraft. *
* AircraftClass::Mission_Enter -- Control aircraft to fly to the helipad or repair center. *
* AircraftClass::Mission_Guard -- Handles aircraft in guard mode. *
* AircraftClass::Mission_Guard_Area -- Handles the aircraft guard area logic. *
* AircraftClass::Mission_Hunt -- Maintains hunt AI for the aircraft. *
* AircraftClass::Mission_Move -- Handles movement mission. *
* AircraftClass::Mission_Retreat -- Handles the aircraft logic for leaving the battlefield. *
* AircraftClass::Mission_Unload -- Handles unloading cargo. *
* AircraftClass::Movement_AI -- Handles aircraft physical movement logic. *
* AircraftClass::New_LZ -- Find a good landing zone. *
* AircraftClass::Overlap_List -- Returns with list of cells the aircraft overlaps. *
* AircraftClass::Paradrop_Cargo -- Drop a passenger by parachute. *
* AircraftClass::Per_Cell_Process -- Handle the aircraft per cell process. *
* AircraftClass::Pip_Count -- Returns the number of "objects" in aircraft. *
* AircraftClass::Player_Assign_Mission -- Handles player input to assign a mission. *
* AircraftClass::Pose_Dir -- Fetches the natural landing facing. *
* AircraftClass::Process_Fly_To -- Handles state machine for flying to destination. *
* AircraftClass::Process_Landing -- Landing process state machine handler. *
* AircraftClass::Process_Take_Off -- State machine support for taking off. *
* AircraftClass::Read_INI -- Reads aircraft object data from an INI file. *
* AircraftClass::Receive_Message -- Handles receipt of radio messages. *
* AircraftClass::Response_Attack -- Gives audio response to attack order. *
* AircraftClass::Response_Move -- Gives audio response to move request. *
* AircraftClass::Response_Select -- Gives audio response when selected. *
* AircraftClass::Rotation_AI -- Handle aircraft body and flight rotation. *
* AircraftClass::Scatter -- Causes the aircraft to move away a bit. *
* AircraftClass::Set_Speed -- Sets the speed for the aircraft. *
* AircraftClass::Shape_Number -- Fetch the shape number to use for the aircraft. *
* AircraftClass::Sort_Y -- Figures the sorting coordinate. *
* AircraftClass::Take_Damage -- Applies damage to the aircraft. *
* AircraftClass::Unlimbo -- Removes an aircraft from the limbo state. *
* AircraftClass::What_Action -- Determines what action to perform. *
* AircraftClass::What_Action -- Determines what action to perform. *
* AircraftClass::operator delete -- Deletes the aircraft object. *
* AircraftClass::operator new -- Allocates a new aircraft object from the pool *
* AircraftClass::~AircraftClass -- Destructor for aircraft object. *
* _Counts_As_Civ_Evac -- Is the specified object a candidate for civilian evac logic? *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

#include "function.h"


/***********************************************************************************************
* _Counts_As_Civ_Evac -- Is the specified object a candidate for civilian evac logic? *
* *
* Examines the specified object to see if it qualifies to be a civilian evacuation. This *
* can only occur if it is a civilian (or Tanya) and the special evacuation flag has been *
* set in the scenario control structure. *
* *
* INPUT: candidate -- Candidate object to examine for civilian evacuation legality. *
* *
* OUTPUT: bool; Is the specified object considered a civilian that must be auto-evacuated? *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 06/24/1996 JLB : Created. *
*=============================================================================================*/
static bool //将返回类型和函数名分行,可以更清晰,因为有的返回类型非常离谱
_Counts_As_Civ_Evac(ObjectClass const * candidate)
{
/*
** If the candidate pointer is missing, then return with failure code.
*/
if (candidate == NULL) return(false);

/*
** Only infantry objects can be considered for civilian evacuation action.
*/
if (candidate->What_Am_I() != RTTI_INFANTRY) return(false);

/*
** Working infantry object pointer.
*/
InfantryClass const * inf = (InfantryClass const *)candidate;

/*
** Certain infantry types will always be considered a civilian evacuation candidate. These
** include the special one-time infantry that appear in some missions.
*/
if (*inf == INFANTRY_EINSTEIN || *inf == INFANTRY_GENERAL || *inf == INFANTRY_DELPHI || *inf == INFANTRY_CHAN) return(true);

/*
** Consider Tanya to be part of the civilian evacuation logic if the scenario is
** specially flagged for this.
*/
if (Scen.IsTanyaEvac && *inf == INFANTRY_TANYA) return(true);

/*
** If the infantry is not a civilian, then it isn't allowed to be a civilian evacuation.
*/
if (!inf->Class->IsCivilian) return(false);

/*
** Technicians look like civilians, but are not considered a legal evacuation candidate.
*/
if (inf->IsTechnician) return(false);

/*
** All tests pass, so return the success of the infantry as a civilian evacuation candidate.
*/
return(true);
}


/***********************************************************************************************
* AircraftClass::operator new -- Allocates a new aircraft object from the pool *
* *
* This routine will allocate an aircraft object from the free aircraft object pool. If *
* there are no free object available, then this routine will fail (return NULL). *
* *
* INPUT: none *
* *
* OUTPUT: Returns with a pointer to the allocate aircraft object or NULL if none were *
* available. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 07/26/1994 JLB : Created. *
*=============================================================================================*/
void * AircraftClass::operator new(size_t)
{
void * ptr = Aircraft.Allocate();
if (ptr) {
((AircraftClass *)ptr)->Set_Active();
}
return(ptr);
}


/***********************************************************************************************
* AircraftClass::operator delete -- Deletes the aircraft object. *
* *
* This routine will return the aircraft object back to the free aircraft object pool. *
* *
* INPUT: ptr -- Pointer to the aircraft object to delete. *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 07/26/1994 JLB : Created. *
*=============================================================================================*/
void AircraftClass::operator delete(void * ptr)
{
if (ptr) {
((AircraftClass *)ptr)->IsActive = false;
}
Aircraft.Free((AircraftClass *)ptr);
}


/***********************************************************************************************
* AircraftClass::AircraftClass -- The constructor for aircraft objects. *
* *
* This routine is the constructor for aircraft objects. An aircraft object can be *
* created and possibly placed into the game system by this routine. *
* *
* INPUT: classid -- The type of aircraft to create. *
* *
* house -- The owner of this aircraft. *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 07/26/1994 JLB : Created. *
*=============================================================================================*/
AircraftClass::AircraftClass(AircraftType classid, HousesType house) :
FootClass(RTTI_AIRCRAFT, Aircraft.ID(this), house),
Class(AircraftTypes.Ptr((int)classid)),
SecondaryFacing(PrimaryFacing),
Passenger(false),
IsLanding(false),
IsTakingOff(false),
IsHovering(false),
Jitter(0),
SightTimer(0),
AttacksRemaining(1)
{
/*
** For two shooters, clear out the second shot flag -- it will be set the first time
** the object fires. For non two shooters, set the flag since it will never be cleared
** and the second shot flag tells the system that normal rearm times apply -- this is
** what is desired for non two shooters.
*/
IsSecondShot = !Class->Is_Two_Shooter();
House->Tracking_Add(this);
Ammo = Class->MaxAmmo;
Height = FLIGHT_LEVEL;
Strength = Class->MaxStrength;
NavCom = TARGET_NONE;

/*
** Keep count of the number of units created. Dont track cargo planes as they are created
** automatically, not bought.
*/
// if (/*classid != AIRCRAFT_CARGO && */ Session.Type == GAME_INTERNET) {
// House->AircraftTotals->Increment_Unit_Total((int)classid);
// }

}
0%