UFO: Alien Invasion
Doxygen documentation generating
cp_mission_intercept.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2023 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24 
25 #include "../../../DateTime.h"
26 #include "../../../cl_shared.h"
27 #include "../cp_campaign.h"
28 #include "../cp_ufo.h"
29 #include "../cp_missions.h"
30 #include "../cp_time.h"
31 #include "../cp_alien_interest.h"
32 #include "../cp_xvi.h"
33 #include "cp_mission_intercept.h"
34 
40 {
44  if (CP_IsXVIStarted())
46 
47  CP_MissionRemove(mission);
48 }
49 
55 {
60 
61  CP_MissionRemove(mission);
62 }
63 
64 #define UFO_EPSILON 0.00001f
65 
72 void CP_InterceptMissionLeave (mission_t* mission, bool destroyed)
73 {
74  installation_t* installation;
75 
76  assert(mission->ufo);
77 
78  mission->stage = STAGE_RETURN_TO_ORBIT;
79 
80  /* if the mission was an attack of an installation, destroy it */
81  installation = mission->data.installation;
82  if (installation) {
83  vec3_t missionPos;
84 
85  Vector2Copy(mission->pos, missionPos);
86  missionPos[2] = installation->pos[2];
87  if (destroyed && VectorCompareEps(missionPos, installation->pos, UFO_EPSILON))
88  INS_DestroyInstallation(installation);
89  }
90 
92  UFO_SetRandomDest(mission->ufo);
94  /* Display UFO on geoscape if it is detected */
95  mission->ufo->landed = false;
96 }
97 
103 {
104  const DateTime minAttackDelay(0, 3600);
105  const DateTime maxAttackDelay(0, 21600); /* How long the UFO should stay on earth */
106  installation_t* installation;
107  vec3_t missionPos;
108 
109  mission->stage = STAGE_INTERCEPT;
110 
111  installation = mission->data.installation;
112  Vector2Copy(mission->pos, missionPos);
113  if (!VectorCompareEps(missionPos, installation->pos, UFO_EPSILON)) {
114  mission->finalDate = ccs.date;
115  return;
116  }
117 
118  /* Make round around the position of the mission */
119  UFO_SetRandomDestAround(mission->ufo, mission->pos);
120  mission->finalDate = ccs.date + Date_Random(minAttackDelay, maxAttackDelay);
121 }
122 
128 {
129  const DateTime minReconDelay(3, 0);
130  const DateTime maxReconDelay(6, 0); /* How long the UFO should stay on earth */
131 
132  mission->stage = STAGE_INTERCEPT;
133  mission->finalDate = ccs.date + Date_Random(minReconDelay, maxReconDelay);
134 }
135 
142 {
143  float randomNumber, sum = 0.0f;
144  installation_t* installation = nullptr;
145 
146  assert(mission);
147 
148  /* Choose randomly a base depending on alienInterest values for those bases */
149  INS_Foreach(i) {
150  sum += i->alienInterest;
151  }
152  randomNumber = frand() * sum;
153  INS_Foreach(i) {
154  randomNumber -= i->alienInterest;
155  if (randomNumber < 0) {
156  installation = i;
157  break;
158  }
159  }
160 
161  /* Make sure we have a base */
162  assert(installation && (randomNumber < 0));
163 
164  return installation;
165 }
166 
172 {
173  installation_t* installation;
174  assert(mission->ufo);
175 
176  mission->stage = STAGE_MISSION_GOTO;
177 
178  installation = CP_InterceptChooseInstallation(mission);
179  if (!installation) {
180  cgi->Com_Printf("CP_InterceptGoToInstallation: no installation found\n");
181  CP_MissionRemove(mission);
182  return;
183  }
184  mission->data.installation = installation;
185 
186  Vector2Copy(installation->pos, mission->pos);
187  mission->posAssigned = true;
188 
190  UFO_SendToDestination(mission->ufo, mission->pos);
191 }
192 
197 static void CP_InterceptMissionSet (mission_t* mission)
198 {
199  assert(mission->ufo);
200 
201  /* Only large UFOs can attack installations -- if there are installations to attack */
202  if (UFO_CanDoMission(mission->ufo->getUfoType(), "interceptbombing"))
203  if (INS_HasAny()) {
204  /* Probability to get a UFO that targets installations. */
205  const float TARGET_INS_PROBABILITY = 0.25;
206  /* don't make attack on installation happen too often */
207  if (frand() < TARGET_INS_PROBABILITY)
209  }
210 
211  /* if not attacking an installation */
212  if (mission->stage != STAGE_MISSION_GOTO)
214 }
215 
221 {
222  switch (mission->stage) {
223  case STAGE_NOT_ACTIVE:
224  /* Create Intercept mission */
225  CP_MissionBegin(mission);
226  break;
228  /* UFO start looking for target */
229  CP_InterceptMissionSet(mission);
230  break;
231  case STAGE_MISSION_GOTO:
233  break;
234  case STAGE_INTERCEPT:
235  assert(mission->ufo);
236  /* Leave earth */
237  if (AIRFIGHT_ChooseWeapon(mission->ufo->weapons, mission->ufo->maxWeapons, mission->ufo->pos, mission->ufo->pos) !=
238  AIRFIGHT_WEAPON_CAN_NEVER_SHOOT && mission->ufo->status == AIR_UFO && !mission->data.installation) {
239  /* UFO is fighting and has still ammo, wait a little bit before leaving (UFO is not attacking an installation) */
240  const DateTime additionalDelay(0, 3600); /* check every hour if there is still ammos */
241  mission->finalDate = ccs.date + additionalDelay;
242  } else
243  CP_InterceptMissionLeave(mission, true);
244  break;
246  /* mission is over, remove mission */
248  break;
249  default:
250  cgi->Com_Printf("CP_InterceptNextStage: Unknown stage: %i, removing mission.\n", mission->stage);
251  CP_MissionRemove(mission);
252  break;
253  }
254 }
Class describing a point of time.
Definition: DateTime.h:30
aircraftStatus_t status
Definition: cp_aircraft.h:126
#define AIRFIGHT_WEAPON_CAN_NEVER_SHOOT
Definition: cp_airfight.h:38
void CP_InterceptMissionIsFailure(mission_t *mission)
Intercept mission is over and is a failure: change interest values.
void UFO_SetRandomDest(aircraft_t *ufocraft)
Give a random destination to the given UFO, and make him to move there.
Definition: cp_ufo.cpp:259
missionStage_t stage
Definition: cp_missions.h:99
mission definition
Definition: cp_missions.h:86
void CP_InterceptNextStage(mission_t *mission)
Determine what action should be performed when a Intercept mission stage ends.
int maxWeapons
Definition: cp_aircraft.h:145
void INT_ChangeIndividualInterest(float interestFactor, interestCategory_t category)
Change individual interest value.
Campaign mission headers.
bool INS_HasAny(installationStatus_t status)
Checks whether any installation is available.
A installation with all it&#39;s data.
#define UFO_EPSILON
static installation_t * CP_InterceptChooseInstallation(const mission_t *mission)
Choose Base that will be attacked, and add it to mission description.
#define INS_Foreach(var)
static void CP_InterceptAttackInstallation(mission_t *mission)
UFO starts to attack the installation.
void CP_InterceptMissionLeave(mission_t *mission, bool destroyed)
Intercept mission ends: UFO leave earth.
bool UFO_CanDoMission(const ufoType_t uType, const char *mType)
Check if the UFO type is available for the given mission type.
Definition: cp_ufo.cpp:137
void CP_InterceptAircraftMissionSet(mission_t *mission)
Set Intercept mission: UFO looks for new aircraft target.
union mission_t::missionData_t data
int AIRFIGHT_ChooseWeapon(const aircraftSlot_t *slot, int maxSlot, const vec2_t pos, const vec2_t targetPos)
Choose the weapon an attacking aircraft will use to fire on a target.
aircraftSlot_t weapons[MAX_AIRCRAFTSLOT]
Definition: cp_aircraft.h:144
void CP_InterceptGoToInstallation(mission_t *mission)
Set Intercept mission: UFO chooses an installation an flies to it.
const cgame_import_t * cgi
bool CP_MissionBegin(mission_t *mission)
mission begins: UFO arrive on earth.
void CP_MissionRemove(mission_t *mission)
Removes a mission from mission global array.
ccs_t ccs
Definition: cp_campaign.cpp:63
vec3_t pos
Definition: cp_aircraft.h:132
class DateTime finalDate
Definition: cp_missions.h:103
vec2_t pos
Definition: cp_missions.h:105
ufoType_t getUfoType() const
Definition: cp_aircraft.h:180
void CP_InterceptMissionIsSuccess(mission_t *mission)
Intercept mission is over and is a success: change interest values.
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
static void CP_InterceptMissionSet(mission_t *mission)
Set Intercept mission: choose between attacking aircraft or installations.
installation_t * installation
Definition: cp_missions.h:94
float frand(void)
Return random values between 0 and 1.
Definition: mathlib.cpp:506
QGL_EXTERN GLint i
Definition: r_gl.h:113
DateTime Date_Random(const DateTime &minFrame, const DateTime &maxFrame)
Return a random relative date which lies between a lower and upper limit.
Definition: cp_time.cpp:239
class DateTime date
Definition: cp_campaign.h:246
void UFO_SetRandomDestAround(aircraft_t *ufocraft, const vec2_t pos)
Give a random destination to the given UFO close to a position, and make him to move there...
Definition: cp_ufo.cpp:274
vec_t vec3_t[3]
Definition: ufotypes.h:39
#define Vector2Copy(src, dest)
Definition: vector.h:52
void UFO_SendToDestination(aircraft_t *ufo, const vec2_t dest)
Make the specified UFO go to destination.
Definition: cp_ufo.cpp:562
int VectorCompareEps(const vec3_t v1, const vec3_t v2, float epsilon)
Compare two vectors that may have an epsilon difference but still be the same vectors.
Definition: mathlib.cpp:413
bool posAssigned
Definition: cp_missions.h:112
aircraft_t * ufo
Definition: cp_missions.h:106
void CP_MissionRemoveFromGeoscape(mission_t *mission)
Removes a mission from geoscape: make it non visible and call notify functions.
void CP_MissionDisableTimeLimit(mission_t *mission)
Disable time limit for given mission.
#define CP_IsXVIStarted()
Definition: cp_xvi.h:37
void INS_DestroyInstallation(installation_t *installation)
Destroys an installation.