UFO: Alien Invasion
cp_mission_terror.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2022 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 
26 #include "../../../DateTime.h"
27 #include "../../../cl_shared.h"
28 #include "../cp_campaign.h"
29 #include "../cp_geoscape.h"
30 #include "../cp_ufo.h"
31 #include "../cp_missions.h"
32 #include "../cp_time.h"
33 #include "../cp_alien_interest.h"
34 #include "cp_mission_recon.h"
35 #include "cp_mission_terror.h"
36 
42 {
45 
46  CP_MissionRemove(mission);
47 }
48 
54 {
59 
60  CP_MissionRemove(mission);
61 }
62 
63 
68 {
69  /* Prevent multiple terror missions per cycle by resetting interest. */
71 }
72 
73 
79 {
80  const DateTime minMissionDelay(2, 0);
81  const DateTime maxMissionDelay(3, 0);
82 
83  mission->stage = STAGE_TERROR_MISSION;
84 
85  mission->finalDate = ccs.date + Date_Random(minMissionDelay, maxMissionDelay);
86  /* ufo becomes invisible on geoscape, but don't remove it from ufo global array (may reappear)*/
87  if (mission->ufo)
88  CP_UFORemoveFromGeoscape(mission, false);
89  /* mission appear on geoscape, player can go there */
90  CP_MissionAddToGeoscape(mission, false);
91 }
92 
97 static city_t* CP_ChooseCity (void)
98 {
99  if (ccs.numCities > 0) {
100  const int randnumber = rand() % ccs.numCities;
101 
102  return (city_t*) cgi->LIST_GetByIdx(ccs.cities, randnumber);
103  }
104 
105  return nullptr;
106 }
107 
108 static const mission_t* CP_TerrorInCity (const city_t* city)
109 {
110  if (!city)
111  return nullptr;
112 
113  MIS_Foreach(mission) {
114  if (mission->category == INTERESTCATEGORY_TERROR_ATTACK
115  && mission->stage <= STAGE_TERROR_MISSION
116  && Vector2Equal(mission->pos, city->pos))
117  return mission;
118  }
119  return nullptr;
120 }
121 
127 static void CP_TerrorMissionGo (mission_t* mission)
128 {
129  int counter;
130 
131  mission->stage = STAGE_MISSION_GOTO;
132 
133  /* Choose a map */
134  for (counter = 0; counter < MAX_POS_LOOP; counter++) {
135  city_t* city = CP_ChooseCity();
136 
137  if (!city)
138  continue;
139 
140  if (GEO_PositionCloseToBase(city->pos))
141  continue;
142 
143  if (!CP_ChooseMap(mission, city->pos))
144  continue;
145 
146  if (CP_TerrorInCity(city))
147  continue;
148 
149  Vector2Copy(city->pos, mission->pos);
150  mission->data.city = city;
151  mission->posAssigned = true;
152  break;
153  }
154  if (counter >= MAX_POS_LOOP) {
155  cgi->Com_DPrintf(DEBUG_CLIENT, "CP_TerrorMissionGo: Could not set position.\n");
156  CP_MissionRemove(mission);
157  return;
158  }
159 
160  if (mission->ufo) {
162  UFO_SendToDestination(mission->ufo, mission->pos);
163  } else {
164  /* Go to next stage on next frame */
165  mission->finalDate = ccs.date;
166  }
167 }
168 
174 {
175  switch (mission->stage) {
176  case STAGE_NOT_ACTIVE:
177  /* Create Terror attack mission */
178  CP_MissionBegin(mission);
179  break;
181  /* Go to mission */
182  CP_TerrorMissionGo(mission);
183  break;
184  case STAGE_MISSION_GOTO:
185  /* just arrived on a new Terror attack mission: start it */
186  CP_TerrorMissionStart(mission);
187  break;
189  /* Leave earth */
190  CP_ReconMissionLeave(mission);
191  break;
193  /* mission is over, remove mission */
194  CP_TerrorMissionIsSuccess(mission);
195  break;
196  default:
197  cgi->Com_Printf("CP_TerrorMissionNextStage: Unknown stage: %i, removing mission.\n", mission->stage);
198  CP_MissionRemove(mission);
199  break;
200  }
201 }
void CP_TerrorMissionNextStage(mission_t *mission)
Determine what action should be performed when a Terror attack mission stage ends.
#define Vector2Equal(a, b)
Definition: vector.h:67
missionStage_t stage
Definition: cp_missions.h:99
mission definition
Definition: cp_missions.h:86
bool CP_ChooseMap(mission_t *mission, const vec2_t pos)
Choose a map for given mission.
#define MIS_Foreach(var)
iterates through missions
Definition: cp_missions.h:119
Class describing a point of time.
Definition: DateTime.h:30
void INT_ChangeIndividualInterest(float interestFactor, interestCategory_t category)
Change individual interest value.
Campaign mission headers.
void CP_ReconMissionLeave(mission_t *mission)
Recon mission ends: UFO leave earth.
void CP_TerrorMissionStart(mission_t *mission)
Start Terror attack mission.
const int MAX_POS_LOOP
Definition: cp_missions.cpp:62
City definition.
Definition: cp_nation.h:70
union mission_t::missionData_t data
#define DEBUG_CLIENT
Definition: defines.h:59
void CP_TerrorMissionIsFailure(mission_t *mission)
Terror attack mission is over and is a failure: change interest values.
void *IMPORT * LIST_GetByIdx(linkedList_t *list, int index)
int numCities
Definition: cp_campaign.h:301
static const mission_t * CP_TerrorInCity(const city_t *city)
base_t * GEO_PositionCloseToBase(const vec2_t pos)
Check if given pos is close to an existing base.
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.
void CP_MissionAddToGeoscape(mission_t *mission, bool force)
Add a mission to geoscape: make it visible and stop time.
ccs_t ccs
Definition: cp_campaign.cpp:63
class DateTime finalDate
Definition: cp_missions.h:103
vec2_t pos
Definition: cp_missions.h:105
vec2_t pos
Definition: cp_nation.h:73
static city_t * CP_ChooseCity(void)
Choose a city for terror mission.
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
Campaign mission headers.
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 CP_TerrorMissionOnSpawn(void)
Run when the mission is spawned.
linkedList_t * cities
Definition: cp_campaign.h:300
void CP_UFORemoveFromGeoscape(mission_t *mission, bool destroyed)
Removes (temporarily or permanently) a UFO from geoscape: make it land and call notify functions...
static void CP_TerrorMissionGo(mission_t *mission)
Set Terror attack mission, and go to Terror attack mission pos.
#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
bool posAssigned
Definition: cp_missions.h:112
aircraft_t * ufo
Definition: cp_missions.h:106
void CP_MissionDisableTimeLimit(mission_t *mission)
Disable time limit for given mission.
void CP_TerrorMissionIsSuccess(mission_t *mission)
Terror attack mission is over and is a success: change interest values.