UFO: Alien Invasion
cp_mission_harvest.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_xvi.h"
34 #include "../cp_alien_interest.h"
35 #include "cp_mission_harvest.h"
36 #include "cp_mission_recon.h"
37 
42 static void CP_HarvestMissionIsSuccess (mission_t* mission)
43 {
47  if (CP_IsXVIStarted())
49 
50  CP_MissionRemove(mission);
51 }
52 
58 {
62 
63  CP_MissionRemove(mission);
64 }
65 
70 static void CP_HarvestMissionStart (mission_t* mission)
71 {
72  const DateTime minMissionDelay(2, 0);
73  const DateTime maxMissionDelay(3, 0);
74 
75  mission->stage = STAGE_HARVEST;
76 
77  if (mission->ufo) {
78  mission->finalDate = ccs.date + Date_Random(minMissionDelay, maxMissionDelay);
79  /* ufo becomes invisible on geoscape, but don't remove it from ufo global array (may reappear)*/
80  CP_UFORemoveFromGeoscape(mission, false);
81  } else {
82  /* Go to next stage on next frame */
83  mission->finalDate = ccs.date;
84  }
85 
86  /* mission appear on geoscape, player can go there */
87  CP_MissionAddToGeoscape(mission, false);
88 }
89 
97 static bool CP_ChooseNation (const mission_t* mission, linkedList_t** nationList)
98 {
99  int randomNumber, max = 0;
100  /* Increase this factor to make probability to select non-infected nation higher
101  * Used to make sure that non-infected nation can still be attacked */
102  const int OFFSET = 1;
103 
104  if (mission->ufo)
105  return false;
106 
107  /* favour mission with higher XVI level */
108  NAT_Foreach(nation) {
109  const nationInfo_t* stats = NAT_GetCurrentMonthInfo(nation);
110  max += OFFSET + stats->xviInfection;
111  }
112 
113  randomNumber = (int) (frand() * (float) max);
114 
115  /* Select the corresponding nation */
116  NAT_Foreach(nation) {
117  const nationInfo_t* stats = NAT_GetCurrentMonthInfo(nation);
118  randomNumber -= OFFSET + stats->xviInfection;
119  if (randomNumber < 0) {
120  cgi->LIST_AddString(nationList, nation->id);
121  return true;
122  }
123  }
124 
125  return false;
126 }
127 
136 {
137  mission->stage = STAGE_MISSION_GOTO;
138 
139  /* Choose a map */
140  if (CP_ChooseMap(mission, nullptr)) {
141  int counter;
142  linkedList_t* nationList = nullptr;
143  const bool nationTest = CP_ChooseNation(mission, &nationList);
144  for (counter = 0; counter < MAX_POS_LOOP; counter++) {
145  if (!CP_GetRandomPosOnGeoscapeWithParameters(mission->pos, mission->mapDef->terrains, mission->mapDef->cultures, mission->mapDef->populations, nationTest ? nationList : nullptr))
146  continue;
147  if (GEO_PositionCloseToBase(mission->pos))
148  continue;
149  mission->posAssigned = true;
150  break;
151  }
152  if (counter >= MAX_POS_LOOP) {
153  cgi->Com_Printf("CP_HarvestMissionGo: Error, could not set position.\n");
154  CP_MissionRemove(mission);
155  return;
156  }
157  cgi->LIST_Delete(&nationList);
158  } else {
159  cgi->Com_Printf("CP_HarvestMissionGo: No map found, remove mission.\n");
160  CP_MissionRemove(mission);
161  return;
162  }
163 
164  if (mission->ufo) {
166  UFO_SendToDestination(mission->ufo, mission->pos);
167  } else {
168  /* Go to next stage on next frame */
169  mission->finalDate = ccs.date;
170  }
171 }
172 
178 {
179  switch (mission->stage) {
180  case STAGE_NOT_ACTIVE:
181  /* Create Harvesting mission */
182  CP_MissionBegin(mission);
183  break;
185  /* Go to mission */
186  CP_HarvestMissionGo(mission);
187  break;
188  case STAGE_MISSION_GOTO:
189  /* just arrived on a new Harvesting mission: start it */
190  CP_HarvestMissionStart(mission);
191  break;
192  case STAGE_HARVEST:
193  /* Leave earth */
194  CP_ReconMissionLeave(mission);
195  break;
197  /* mission is over, remove mission */
199  break;
200  default:
201  cgi->Com_Printf("CP_HarvestMissionNextStage: Unknown stage: %i, removing mission.\n", mission->stage);
202  CP_MissionRemove(mission);
203  break;
204  }
205 }
static void CP_HarvestMissionStart(mission_t *mission)
Start Harvesting mission.
const nationInfo_t * NAT_GetCurrentMonthInfo(const nation_t *const nation)
Get the current month nation stats.
Definition: cp_nation.cpp:133
static void CP_HarvestMissionIsSuccess(mission_t *mission)
Harvesting mission is over and is a success: change interest values.
missionStage_t stage
Definition: cp_missions.h:99
mission definition
Definition: cp_missions.h:86
Campaign mission headers.
bool CP_ChooseMap(mission_t *mission, const vec2_t pos)
Choose a map for given mission.
void CP_HarvestMissionNextStage(mission_t *mission)
Determine what action should be performed when a Harvesting mission stage ends.
Detailed information about the nation relationship (currently per month, but could be used elsewhere)...
Definition: cp_nation.h:35
Class describing a point of time.
Definition: DateTime.h:30
void INT_ChangeIndividualInterest(float interestFactor, interestCategory_t category)
Change individual interest value.
void CP_HarvestMissionIsFailure(mission_t *mission)
Harvesting mission is over and is a failure: change interest values.
void CP_ReconMissionLeave(mission_t *mission)
Recon mission ends: UFO leave earth.
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
linkedList_t * populations
Definition: q_shared.h:487
const int MAX_POS_LOOP
Definition: cp_missions.cpp:62
int xviInfection
Definition: cp_nation.h:40
#define NAT_Foreach(var)
iterates trough nations
Definition: cp_nation.h:80
base_t * GEO_PositionCloseToBase(const vec2_t pos)
Check if given pos is close to an existing base.
void CP_HarvestMissionGo(mission_t *mission)
Set Harvest mission, and go to mission pos.
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
bool CP_GetRandomPosOnGeoscapeWithParameters(vec2_t pos, const linkedList_t *terrainTypes, const linkedList_t *cultureTypes, const linkedList_t *populationTypes, const linkedList_t *nations)
Determines a random position on geoscape that fulfills certain criteria given via parameters...
static bool CP_ChooseNation(const mission_t *mission, linkedList_t **nationList)
Choose nation if needed for given mission.
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
Campaign mission headers.
float frand(void)
Return random values between 0 and 1.
Definition: mathlib.cpp:506
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_UFORemoveFromGeoscape(mission_t *mission, bool destroyed)
Removes (temporarily or permanently) a UFO from geoscape: make it land and call notify functions...
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
linkedList_t * terrains
Definition: q_shared.h:486
linkedList_t * cultures
Definition: q_shared.h:488
void CP_MissionDisableTimeLimit(mission_t *mission)
Disable time limit for given mission.
#define CP_IsXVIStarted()
Definition: cp_xvi.h:37
mapDef_t * mapDef
Definition: cp_missions.h:89