UFO: Alien Invasion
Loading...
Searching...
No Matches
cp_mission_intercept.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, 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"
32#include "../cp_xvi.h"
34
49
63
64#define UFO_EPSILON 0.00001f
65
72void CP_InterceptMissionLeave (mission_t* mission, bool destroyed)
73{
74 installation_t* installation;
75
76 assert(mission->ufo);
77
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
197static 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;
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}
DateTime class definition.
Share stuff between the different cgame implementations.
Class describing a point of time.
Definition DateTime.h:31
@ AIR_UFO
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.
#define AIRFIGHT_WEAPON_CAN_NEVER_SHOOT
Definition cp_airfight.h:38
void INT_ChangeIndividualInterest(float interestFactor, interestCategory_t category)
Change individual interest value.
Alien interest header.
@ INTERESTCATEGORY_BASE_ATTACK
@ INTERESTCATEGORY_BUILDING
@ INTERESTCATEGORY_RECON
@ INTERESTCATEGORY_XVI
@ INTERESTCATEGORY_TERROR_ATTACK
@ INTERESTCATEGORY_INTERCEPT
@ INTERESTCATEGORY_HARVEST
ccs_t ccs
Header file for single player campaign control.
const cgame_import_t * cgi
void INS_DestroyInstallation(installation_t *installation)
Destroys an installation.
bool INS_HasAny(installationStatus_t status)
Checks whether any installation is available.
#define INS_Foreach(var)
void CP_InterceptMissionIsFailure(mission_t *mission)
Intercept mission is over and is a failure: change interest values.
void CP_InterceptNextStage(mission_t *mission)
Determine what action should be performed when a Intercept mission stage ends.
void CP_InterceptMissionIsSuccess(mission_t *mission)
Intercept mission is over and is a success: change interest values.
void CP_InterceptGoToInstallation(mission_t *mission)
Set Intercept mission: UFO chooses an installation an flies to it.
static void CP_InterceptAttackInstallation(mission_t *mission)
UFO starts to attack the installation.
void CP_InterceptAircraftMissionSet(mission_t *mission)
Set Intercept mission: UFO looks for new aircraft target.
static installation_t * CP_InterceptChooseInstallation(const mission_t *mission)
Choose Base that will be attacked, and add it to mission description.
static void CP_InterceptMissionSet(mission_t *mission)
Set Intercept mission: choose between attacking aircraft or installations.
void CP_InterceptMissionLeave(mission_t *mission, bool destroyed)
Intercept mission ends: UFO leave earth.
#define UFO_EPSILON
Campaign mission headers.
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.
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.
Campaign missions headers.
@ STAGE_RETURN_TO_ORBIT
Definition cp_missions.h:51
@ STAGE_MISSION_GOTO
Definition cp_missions.h:39
@ STAGE_NOT_ACTIVE
Definition cp_missions.h:35
@ STAGE_COME_FROM_ORBIT
Definition cp_missions.h:36
@ STAGE_INTERCEPT
Definition cp_missions.h:47
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
Campaign geoscape time header.
void UFO_SendToDestination(aircraft_t *ufo, const vec2_t dest)
Make the specified UFO go to destination.
Definition cp_ufo.cpp:562
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
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 UFO_SetRandomDest(aircraft_t *ufocraft)
Give a random destination to the given UFO, and make him to move there.
Definition cp_ufo.cpp:259
Campaign XVI header.
#define CP_IsXVIStarted()
Definition cp_xvi.h:37
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
float frand(void)
Return random values between 0 and 1.
Definition mathlib.cpp:506
QGL_EXTERN GLint i
Definition r_gl.h:113
aircraftSlot_t weapons[MAX_AIRCRAFTSLOT]
aircraftStatus_t status
ufoType_t getUfoType() const
A installation with all it's data.
mission definition
Definition cp_missions.h:86
aircraft_t * ufo
missionStage_t stage
Definition cp_missions.h:99
vec2_t pos
union mission_t::missionData_t data
bool posAssigned
class DateTime finalDate
vec_t vec3_t[3]
Definition ufotypes.h:39
installation_t * installation
Definition cp_missions.h:94
#define Vector2Copy(src, dest)
Definition vector.h:52