UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cp_mission_harvest.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 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 "../../../cl_shared.h"
27 #include "../cp_campaign.h"
28 #include "../cp_geoscape.h"
29 #include "../cp_ufo.h"
30 #include "../cp_missions.h"
31 #include "../cp_time.h"
32 #include "../cp_xvi.h"
33 #include "../cp_alien_interest.h"
34 #include "cp_mission_harvest.h"
35 #include "cp_mission_recon.h"
36 
41 static void CP_HarvestMissionIsSuccess (mission_t* mission)
42 {
46  if (CP_IsXVIStarted())
48 
49  CP_MissionRemove(mission);
50 }
51 
57 {
61 
62  CP_MissionRemove(mission);
63 }
64 
69 static void CP_HarvestMissionStart (mission_t* mission)
70 {
71  const date_t minMissionDelay = {2, 0};
72  const date_t missionDelay = {3, 0};
73 
74  mission->stage = STAGE_HARVEST;
75 
76  if (mission->ufo) {
77  mission->finalDate = Date_Add(ccs.date, Date_Random(minMissionDelay, missionDelay));
78  /* ufo becomes invisible on geoscape, but don't remove it from ufo global array (may reappear)*/
79  CP_UFORemoveFromGeoscape(mission, false);
80  } else {
81  /* Go to next stage on next frame */
82  mission->finalDate = ccs.date;
83  }
84 
85  /* mission appear on geoscape, player can go there */
86  CP_MissionAddToGeoscape(mission, false);
87 }
88 
96 static bool CP_ChooseNation (const mission_t* mission, linkedList_t** nationList)
97 {
98  int randomNumber, max = 0;
99  /* Increase this factor to make probability to select non-infected nation higher
100  * Used to make sure that non-infected nation can still be attacked */
101  const int OFFSET = 1;
102 
103  if (mission->ufo)
104  return false;
105 
106  /* favour mission with higher XVI level */
107  NAT_Foreach(nation) {
108  const nationInfo_t* stats = NAT_GetCurrentMonthInfo(nation);
109  max += OFFSET + stats->xviInfection;
110  }
111 
112  randomNumber = (int) (frand() * (float) max);
113 
114  /* Select the corresponding nation */
115  NAT_Foreach(nation) {
116  const nationInfo_t* stats = NAT_GetCurrentMonthInfo(nation);
117  randomNumber -= OFFSET + stats->xviInfection;
118  if (randomNumber < 0) {
119  cgi->LIST_AddString(nationList, nation->id);
120  return true;
121  }
122  }
123 
124  return false;
125 }
126 
135 {
136  mission->stage = STAGE_MISSION_GOTO;
137 
138  /* Choose a map */
139  if (CP_ChooseMap(mission, nullptr)) {
140  int counter;
141  linkedList_t* nationList = nullptr;
142  const bool nationTest = CP_ChooseNation(mission, &nationList);
143  for (counter = 0; counter < MAX_POS_LOOP; counter++) {
144  if (!CP_GetRandomPosOnGeoscapeWithParameters(mission->pos, mission->mapDef->terrains, mission->mapDef->cultures, mission->mapDef->populations, nationTest ? nationList : nullptr))
145  continue;
146  if (GEO_PositionCloseToBase(mission->pos))
147  continue;
148  mission->posAssigned = true;
149  break;
150  }
151  if (counter >= MAX_POS_LOOP) {
152  cgi->Com_Printf("CP_HarvestMissionGo: Error, could not set position.\n");
153  CP_MissionRemove(mission);
154  return;
155  }
156  cgi->LIST_Delete(&nationList);
157  } else {
158  cgi->Com_Printf("CP_HarvestMissionGo: No map found, remove mission.\n");
159  CP_MissionRemove(mission);
160  return;
161  }
162 
163  if (mission->ufo) {
165  UFO_SendToDestination(mission->ufo, mission->pos);
166  } else {
167  /* Go to next stage on next frame */
168  mission->finalDate = ccs.date;
169  }
170 }
171 
177 {
178  switch (mission->stage) {
179  case STAGE_NOT_ACTIVE:
180  /* Create Harvesting mission */
181  CP_MissionBegin(mission);
182  break;
184  /* Go to mission */
185  CP_HarvestMissionGo(mission);
186  break;
187  case STAGE_MISSION_GOTO:
188  /* just arrived on a new Harvesting mission: start it */
189  CP_HarvestMissionStart(mission);
190  break;
191  case STAGE_HARVEST:
192  /* Leave earth */
193  CP_ReconMissionLeave(mission);
194  break;
196  /* mission is over, remove mission */
198  break;
199  default:
200  cgi->Com_Printf("CP_HarvestMissionNextStage: Unknown stage: %i, removing mission.\n", mission->stage);
201  CP_MissionRemove(mission);
202  break;
203  }
204 }
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:132
linkedList_t * terrains
Definition: q_shared.h:486
static void CP_HarvestMissionIsSuccess(mission_t *mission)
Harvesting mission is over and is a success: change interest values.
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.
linkedList_t * populations
Definition: q_shared.h:487
bool posAssigned
Definition: cp_missions.h:111
int xviInfection
Definition: cp_nation.h:38
date_t date
Definition: cp_campaign.h:245
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
mission definition
Definition: cp_missions.h:85
const int MAX_POS_LOOP
Definition: cp_missions.cpp:61
date_t finalDate
Definition: cp_missions.h:102
#define NAT_Foreach(var)
iterates trough nations
Definition: cp_nation.h:80
aircraft_t * ufo
Definition: cp_missions.h:105
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:62
Engine-side time information in the game.
Definition: common.h:290
date_t Date_Add(date_t a, const date_t &b)
Add two dates and return the result.
Definition: cp_time.cpp:272
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
void CP_UFORemoveFromGeoscape(mission_t *mission, bool destroyed)
Removes (temporarily or permanently) a UFO from geoscape: make it land and call notify functions...
linkedList_t * cultures
Definition: q_shared.h:488
void UFO_SendToDestination(aircraft_t *ufo, const vec2_t dest)
Make the specified UFO go to destination.
Definition: cp_ufo.cpp:562
date_t Date_Random(date_t minFrame, date_t maxFrame)
Return a random relative date which lies between a lower and upper limit.
Definition: cp_time.cpp:302
mapDef_t * mapDef
Definition: cp_missions.h:88
void CP_MissionDisableTimeLimit(mission_t *mission)
Disable time limit for given mission.
#define CP_IsXVIStarted()
Definition: cp_xvi.h:37
vec2_t pos
Definition: cp_missions.h:104
missionStage_t stage
Definition: cp_missions.h:98
Detailed information about the nation relationship (currently per month, but could be used elsewhere)...
Definition: cp_nation.h:33