Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 48.34 KB | None | 0 0
  1. From 5e5574c77a0dbd6a6f3c0a410de7c606ed515689 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Tam=C3=A1s=20Feh=C3=A9rv=C3=A1ri?= <geever@users.sourceforge.net>
  3. Date: Sun, 7 Nov 2010 03:26:24 +0100
  4. Subject: [PATCH] * optimize the aircraft loops (with AIR_Foreach)
  5.  
  6. ---
  7. src/client/campaign/cp_aircraft.c | 247 +++++++++++++--------------
  8. src/client/campaign/cp_aircraft.h | 3 +
  9. src/client/campaign/cp_aircraft_callbacks.c | 8 +-
  10. src/client/campaign/cp_base.c | 8 +-
  11. src/client/campaign/cp_employee.c | 7 +-
  12. src/client/campaign/cp_map.c | 59 +++----
  13. src/client/campaign/cp_mapfightequip.c | 32 ++--
  14. src/client/campaign/cp_market_callbacks.c | 10 +-
  15. src/client/campaign/cp_nation.c | 12 +-
  16. src/client/campaign/cp_popup.c | 109 ++++++-------
  17. src/client/campaign/cp_radar.c | 59 +++----
  18. src/client/campaign/cp_research.c | 13 +-
  19. src/client/campaign/cp_statistics.c | 11 +-
  20. src/client/campaign/cp_team_callbacks.c | 11 +-
  21. src/client/campaign/cp_transfer_callbacks.c | 12 +-
  22. src/client/campaign/cp_ufo.c | 76 ++++-----
  23. 16 files changed, 319 insertions(+), 358 deletions(-)
  24.  
  25. diff --git a/src/client/campaign/cp_aircraft.c b/src/client/campaign/cp_aircraft.c
  26. index 9d61831..604324d 100644
  27. --- a/src/client/campaign/cp_aircraft.c
  28. +++ b/src/client/campaign/cp_aircraft.c
  29. @@ -85,11 +85,13 @@ qboolean AIR_BaseHasAircraft (const base_t *base)
  30. */
  31. int AIR_BaseCountAircraft (const base_t *base)
  32. {
  33. - aircraft_t *aircraft = NULL;
  34. + aircraft_t *aircraft;
  35. int count = 0;
  36.  
  37. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  38. - count++;
  39. + AIR_Foreach(aircraft) {
  40. + if (AIR_IsAircraftOfBase(aircraft, base))
  41. + count++;
  42. + }
  43.  
  44. return count;
  45. }
  46. @@ -162,9 +164,10 @@ void AIR_UpdateHangarCapForAll (base_t *base)
  47. base->capacities[CAP_AIRCRAFT_BIG].cur = 0;
  48. base->capacities[CAP_AIRCRAFT_SMALL].cur = 0;
  49.  
  50. - aircraft = NULL;
  51. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  52. - AIR_UpdateHangarCapForOne(aircraft->tpl, base);
  53. + AIR_Foreach(aircraft) {
  54. + if (AIR_IsAircraftOfBase(aircraft, base))
  55. + AIR_UpdateHangarCapForOne(aircraft->tpl, base);
  56. + }
  57. }
  58.  
  59. #ifdef DEBUG
  60. @@ -183,8 +186,7 @@ void AIR_ListAircraft_f (void)
  61. base = B_GetFoundedBaseByIDX(baseIdx);
  62. }
  63.  
  64. - aircraft = NULL;
  65. - while ((aircraft = AIR_GetNext(aircraft)) != NULL) {
  66. + AIR_Foreach(aircraft) {
  67. int k;
  68. linkedList_t *l;
  69.  
  70. @@ -541,11 +543,10 @@ int AIR_CountTypeInBase (const base_t *base, aircraftType_t aircraftType)
  71. int count = 0;
  72. aircraft_t *aircraft;
  73.  
  74. - aircraft = NULL;
  75. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  76. - if (aircraft->type == aircraftType)
  77. + AIR_Foreach(aircraft) {
  78. + if (AIR_IsAircraftOfBase(aircraft, base) && aircraft->type == aircraftType)
  79. count++;
  80. -
  81. + }
  82. return count;
  83. }
  84.  
  85. @@ -559,11 +560,10 @@ int AIR_CountInBaseByTemplate (const base_t *base, const aircraft_t *aircraftTem
  86. int count = 0;
  87. aircraft_t *aircraft;
  88.  
  89. - aircraft = NULL;
  90. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  91. - if (aircraft->tpl == aircraftTemplate)
  92. + AIR_Foreach(aircraft) {
  93. + if (AIR_IsAircraftOfBase(aircraft, base) && aircraft->tpl == aircraftTemplate)
  94. count++;
  95. -
  96. + }
  97. return count;
  98. }
  99.  
  100. @@ -691,6 +691,7 @@ void AIR_AircraftReturnToBase (aircraft_t *aircraft)
  101. * @brief Returns the index of the aircraft in the base->aircraft array.
  102. * @param[in] aircraft The aircraft to get the index for.
  103. * @return The array index or AIRCRAFT_INBASE_INVALID on error.
  104. + * @todo Remove this! Aircraft no longer have local index per base
  105. */
  106. int AIR_GetAircraftIDXInBase (const aircraft_t* aircraft)
  107. {
  108. @@ -719,6 +720,7 @@ int AIR_GetAircraftIDXInBase (const aircraft_t* aircraft)
  109. * @param base The base to get the aircraft from
  110. * @param index The index of the aircraft in the given base
  111. * @return @c NULL if there is no such aircraft in the given base, or the aircraft pointer that belongs to the given index.
  112. + * @todo Remove this! Aircraft no longer have local index per base
  113. */
  114. aircraft_t *AIR_GetAircraftFromBaseByIDXSafe (base_t* base, int index)
  115. {
  116. @@ -1276,49 +1278,48 @@ void CL_CampaignRunAircraft (campaign_t* campaign, int dt, qboolean updateRadarO
  117. assert(dt >= 0);
  118.  
  119. if (dt > 0) {
  120. - base_t *base = NULL;
  121. -
  122. - while ((base = B_GetNextFounded(base)) != NULL) {
  123. - aircraft_t *aircraft;
  124. -
  125. - /* Run each aircraft */
  126. - aircraft = NULL;
  127. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  128. - int k;
  129. - assert(aircraft->homebase);
  130. - if (aircraft->status == AIR_IDLE) {
  131. - /* Aircraft idle out of base */
  132. - aircraft->fuel -= dt;
  133. - } else if (AIR_IsAircraftOnGeoscape(aircraft)) {
  134. - AIR_Move(aircraft, dt);
  135. - /* radar overlay should be updated */
  136. - radarOverlayReset = qtrue;
  137. - } else if (aircraft->status == AIR_REFUEL) {
  138. - AIR_Refuel(aircraft, dt);
  139. - }
  140. + aircraft_t *aircraft;
  141.  
  142. - /* Check aircraft low fuel (only if aircraft is not already returning to base or in base) */
  143. - if (aircraft->status != AIR_RETURNING && AIR_IsAircraftOnGeoscape(aircraft) &&
  144. - !AIR_AircraftHasEnoughFuel(aircraft, aircraft->pos)) {
  145. - /** @todo check if aircraft can go to a closer base with free space */
  146. - MS_AddNewMessage(_("Notice"), va(_("Craft %s is low on fuel and must return to base."), aircraft->name), qfalse, MSG_STANDARD, NULL);
  147. - AIR_AircraftReturnToBase(aircraft);
  148. - }
  149. + /* Run each aircraft */
  150. + AIR_Foreach(aircraft) {
  151. + int k;
  152.  
  153. - /* Aircraft purchasing ufo */
  154. - if (aircraft->status == AIR_UFO) {
  155. - /* Solve the fight */
  156. - AIRFIGHT_ExecuteActions(campaign, aircraft, aircraft->aircraftTarget);
  157. - }
  158. + if (aircraft->status == AIR_CRASHED)
  159. + continue;
  160.  
  161. - for (k = 0; k < aircraft->maxWeapons; k++) {
  162. - /* Update delay to launch next projectile */
  163. - if (AIR_IsAircraftOnGeoscape(aircraft) && (aircraft->weapons[k].delayNextShot > 0))
  164. - aircraft->weapons[k].delayNextShot -= dt;
  165. - /* Reload if needed */
  166. - if (aircraft->weapons[k].ammoLeft <= 0)
  167. - AII_ReloadWeapon(&aircraft->weapons[k]);
  168. - }
  169. + assert(aircraft->homebase);
  170. + if (aircraft->status == AIR_IDLE) {
  171. + /* Aircraft idle out of base */
  172. + aircraft->fuel -= dt;
  173. + } else if (AIR_IsAircraftOnGeoscape(aircraft)) {
  174. + AIR_Move(aircraft, dt);
  175. + /* radar overlay should be updated */
  176. + radarOverlayReset = qtrue;
  177. + } else if (aircraft->status == AIR_REFUEL) {
  178. + AIR_Refuel(aircraft, dt);
  179. + }
  180. +
  181. + /* Check aircraft low fuel (only if aircraft is not already returning to base or in base) */
  182. + if (aircraft->status != AIR_RETURNING && AIR_IsAircraftOnGeoscape(aircraft) &&
  183. + !AIR_AircraftHasEnoughFuel(aircraft, aircraft->pos)) {
  184. + /** @todo check if aircraft can go to a closer base with free space */
  185. + MS_AddNewMessage(_("Notice"), va(_("Craft %s is low on fuel and must return to base."), aircraft->name), qfalse, MSG_STANDARD, NULL);
  186. + AIR_AircraftReturnToBase(aircraft);
  187. + }
  188. +
  189. + /* Aircraft purchasing ufo */
  190. + if (aircraft->status == AIR_UFO) {
  191. + /* Solve the fight */
  192. + AIRFIGHT_ExecuteActions(campaign, aircraft, aircraft->aircraftTarget);
  193. + }
  194. +
  195. + for (k = 0; k < aircraft->maxWeapons; k++) {
  196. + /* Update delay to launch next projectile */
  197. + if (AIR_IsAircraftOnGeoscape(aircraft) && (aircraft->weapons[k].delayNextShot > 0))
  198. + aircraft->weapons[k].delayNextShot -= dt;
  199. + /* Reload if needed */
  200. + if (aircraft->weapons[k].ammoLeft <= 0)
  201. + AII_ReloadWeapon(&aircraft->weapons[k]);
  202. }
  203. }
  204. }
  205. @@ -1336,8 +1337,9 @@ void CL_CampaignRunAircraft (campaign_t* campaign, int dt, qboolean updateRadarO
  206. */
  207. aircraft_t* AIR_AircraftGetFromIDX (int aircraftIdx)
  208. {
  209. - aircraft_t* aircraft = NULL;
  210. - while ((aircraft = AIR_GetNext(aircraft)) != NULL) {
  211. + aircraft_t* aircraft;
  212. +
  213. + AIR_Foreach(aircraft) {
  214. if (aircraft->idx == aircraftIdx) {
  215. Com_DPrintf(DEBUG_CLIENT, "AIR_AircraftGetFromIDX: aircraft idx: %i\n", aircraft->idx);
  216. return aircraft;
  217. @@ -1779,8 +1781,7 @@ void AIR_ListCraftIndexes_f (void)
  218. aircraft_t *aircraft;
  219.  
  220. Com_Printf("globalIDX\t(Craftname)\n");
  221. - aircraft = NULL;
  222. - while ((aircraft = AIR_GetNext(aircraft)) != NULL) {
  223. + AIR_Foreach(aircraft) {
  224. Com_Printf("%i\t(%s)\n", aircraft->idx, aircraft->name);
  225. }
  226. }
  227. @@ -1818,19 +1819,17 @@ Aircraft functions related to UFOs or missions.
  228. ===============================================*/
  229.  
  230. /**
  231. - * @brief Notify that a mission has been removed.
  232. - * @note Aircraft currently moving to the mission will be redirect to base
  233. + * @brief Notify aircraft that a mission has been removed.
  234. * @param[in] mission Pointer to the mission that has been removed.
  235. + * @note Aircraft currently moving to the mission will be redirect to base
  236. */
  237. void AIR_AircraftsNotifyMissionRemoved (const mission_t *const mission)
  238. {
  239. - base_t *base = NULL;
  240. - while ((base = B_GetNextFounded(base)) != NULL) {
  241. - aircraft_t* aircraft = NULL;
  242. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  243. - if (aircraft->status == AIR_MISSION && aircraft->mission == mission)
  244. - AIR_AircraftReturnToBase(aircraft);
  245. - }
  246. + aircraft_t* aircraft;
  247. +
  248. + AIR_Foreach(aircraft) {
  249. + if (aircraft->status == AIR_MISSION && aircraft->mission == mission)
  250. + AIR_AircraftReturnToBase(aircraft);
  251. }
  252. }
  253.  
  254. @@ -1842,12 +1841,24 @@ void AIR_AircraftsNotifyMissionRemoved (const mission_t *const mission)
  255. void AIR_AircraftsNotifyUFORemoved (const aircraft_t *const ufo, qboolean destroyed)
  256. {
  257. base_t *base;
  258. + aircraft_t* aircraft;
  259.  
  260. assert(ufo);
  261.  
  262. + /* Aircraft currently purchasing the specified ufo will be redirect to base */
  263. + AIR_Foreach(aircraft) {
  264. + if (aircraft->status == AIR_UFO) {
  265. + if (ufo == aircraft->aircraftTarget) {
  266. + AIR_AircraftReturnToBase(aircraft);
  267. + } else if (destroyed && (ufo < aircraft->aircraftTarget)) {
  268. + aircraft->aircraftTarget--;
  269. + }
  270. + }
  271. + }
  272. +
  273. + /** @todo this should be in a BDEF_NotifyUFORemoved callback */
  274. base = NULL;
  275. while ((base = B_GetNextFounded(base)) != NULL) {
  276. - aircraft_t* aircraft;
  277. int i;
  278. /* Base currently targeting the specified ufo loose their target */
  279. for (i = 0; i < base->numBatteries; i++) {
  280. @@ -1864,34 +1875,21 @@ void AIR_AircraftsNotifyUFORemoved (const aircraft_t *const ufo, qboolean destro
  281. else if (destroyed && (baseWeapon->target > ufo))
  282. baseWeapon->target--;
  283. }
  284. - /* Aircraft currently purchasing the specified ufo will be redirect to base */
  285. - aircraft = NULL;
  286. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  287. - if (aircraft->status == AIR_UFO) {
  288. - if (ufo == aircraft->aircraftTarget)
  289. - AIR_AircraftReturnToBase(aircraft);
  290. - else if (destroyed && (ufo < aircraft->aircraftTarget)) {
  291. - aircraft->aircraftTarget--;
  292. - }
  293. - }
  294. }
  295. }
  296.  
  297. /**
  298. * @brief Notify that a UFO disappear from radars.
  299. - * @note Aircraft currently pursuing the specified UFO will be redirected to base
  300. * @param[in] ufo Pointer to a UFO that has disappeared.
  301. + * @note Aircraft currently pursuing the specified UFO will be redirected to base
  302. */
  303. void AIR_AircraftsUFODisappear (const aircraft_t *const ufo)
  304. {
  305. - base_t *base = NULL;
  306. - while ((base = B_GetNextFounded(base)) != NULL) {
  307. - aircraft_t *aircraft = NULL;
  308. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  309. - if (aircraft->status == AIR_UFO)
  310. - if (ufo == aircraft->aircraftTarget)
  311. - AIR_AircraftReturnToBase(aircraft);
  312. - }
  313. + aircraft_t *aircraft;
  314. +
  315. + AIR_Foreach(aircraft) {
  316. + if (aircraft->status == AIR_UFO && ufo == aircraft->aircraftTarget)
  317. + AIR_AircraftReturnToBase(aircraft);
  318. }
  319. }
  320.  
  321. @@ -2295,9 +2293,8 @@ void AIR_AutoAddPilotToAircraft (const base_t* base, employee_t* pilot)
  322. {
  323. aircraft_t *aircraft;
  324.  
  325. - aircraft = NULL;
  326. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  327. - if (AIR_SetPilot(aircraft, pilot))
  328. + AIR_Foreach(aircraft) {
  329. + if (AIR_IsAircraftOfBase(aircraft, base) && AIR_SetPilot(aircraft, pilot))
  330. break;
  331. }
  332. }
  333. @@ -2312,9 +2309,8 @@ void AIR_RemovePilotFromAssignedAircraft (const base_t* base, const employee_t*
  334. {
  335. aircraft_t *aircraft;
  336.  
  337. - aircraft = NULL;
  338. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  339. - if (AIR_GetPilot(aircraft) == pilot) {
  340. + AIR_Foreach(aircraft) {
  341. + if (AIR_IsAircraftOfBase(aircraft, base) && AIR_GetPilot(aircraft) == pilot) {
  342. AIR_SetPilot(aircraft, NULL);
  343. break;
  344. }
  345. @@ -2549,9 +2545,9 @@ qboolean AIR_SaveXML (mxml_node_t *parent)
  346.  
  347. /* save phalanx aircraft */
  348. snode = mxml_AddNode(parent, SAVE_AIRCRAFT_PHALANX);
  349. - aircraft = NULL;
  350. - while ((aircraft = AIR_GetNext(aircraft)) != NULL)
  351. + AIR_Foreach(aircraft) {
  352. AIR_SaveAircraftXML(snode, aircraft, qfalse);
  353. + }
  354.  
  355. /* save the ufos on geoscape */
  356. snode = mxml_AddNode(parent, SAVE_AIRCRAFT_UFOS);
  357. @@ -2870,24 +2866,21 @@ qboolean AIR_LoadXML (mxml_node_t *parent)
  358. static qboolean AIR_PostLoadInitMissions (void)
  359. {
  360. qboolean success = qtrue;
  361. + aircraft_t *aircraft;
  362. aircraft_t *ufo;
  363. - base_t *base = NULL;
  364.  
  365. /* PHALANX aircraft */
  366. - while ((base = B_GetNextFounded(base)) != NULL) {
  367. - aircraft_t *aircraft = NULL;
  368. - while ((aircraft = AIR_GetNextFromBase(base, aircraft))) {
  369. - if (!aircraft->missionID || aircraft->missionID[0] == '\0')
  370. - continue;
  371. - aircraft->mission = CP_GetMissionByID(aircraft->missionID);
  372. - if (!aircraft->mission) {
  373. - Com_Printf("Aircraft %s (idx: %i) is linked to an invalid mission: %s\n", aircraft->name, aircraft->idx, aircraft->missionID);
  374. - if (aircraft->status == AIR_MISSION)
  375. - AIR_AircraftReturnToBase(aircraft);
  376. - }
  377. - Mem_Free(aircraft->missionID);
  378. - aircraft->missionID = NULL;
  379. + AIR_Foreach(aircraft) {
  380. + if (!aircraft->missionID || aircraft->missionID[0] == '\0')
  381. + continue;
  382. + aircraft->mission = CP_GetMissionByID(aircraft->missionID);
  383. + if (!aircraft->mission) {
  384. + Com_Printf("Aircraft %s (idx: %i) is linked to an invalid mission: %s\n", aircraft->name, aircraft->idx, aircraft->missionID);
  385. + if (aircraft->status == AIR_MISSION)
  386. + AIR_AircraftReturnToBase(aircraft);
  387. }
  388. + Mem_Free(aircraft->missionID);
  389. + aircraft->missionID = NULL;
  390. }
  391.  
  392. /* UFOs */
  393. @@ -3009,9 +3002,9 @@ int AIR_CalculateHangarStorage (const aircraft_t *aircraftTemplate, const base_t
  394. assert(aircraftTemplate);
  395. assert(aircraftTemplate == aircraftTemplate->tpl); /* Make sure it's an aircraft template. */
  396.  
  397. - if (!base->founded)
  398. + if (!base->founded) {
  399. return -1;
  400. - else {
  401. + } else {
  402. const int aircraftCapacity = AIR_GetCapacityByAircraftWeight(aircraftTemplate);
  403. /* If this is a small aircraft, we will check space in small hangar.
  404. * If this is a large aircraft, we will check space in big hangar. */
  405. @@ -3035,17 +3028,12 @@ qboolean AIR_RemoveEmployee (employee_t *employee, aircraft_t *aircraft)
  406. /* If no aircraft is given we search if he is in _any_ aircraft and set
  407. * the aircraft pointer to it. */
  408. if (!aircraft) {
  409. - base_t *base = NULL;
  410. - while ((base = B_GetNext(base)) != NULL) {
  411. - aircraft_t *acTemp = NULL;
  412. - while ((acTemp = AIR_GetNextFromBase(base, acTemp))) {
  413. - if (AIR_IsEmployeeInAircraft(employee, acTemp)) {
  414. - aircraft = acTemp;
  415. - break;
  416. - }
  417. - }
  418. - if (aircraft)
  419. + aircraft_t *acTemp;
  420. + AIR_Foreach(acTemp) {
  421. + if (AIR_IsEmployeeInAircraft(employee, acTemp)) {
  422. + aircraft = acTemp;
  423. break;
  424. + }
  425. }
  426. if (!aircraft)
  427. return qfalse;
  428. @@ -3075,13 +3063,10 @@ const aircraft_t *AIR_IsEmployeeInAircraft (const employee_t *employee, const ai
  429.  
  430. /* If no aircraft is given we search if he is in _any_ aircraft and return true if that's the case. */
  431. if (!aircraft) {
  432. - base_t *base = NULL;
  433. - while ((base = B_GetNext(base)) != NULL) {
  434. - aircraft_t *aircraftByIDX = NULL;
  435. - while ((aircraftByIDX = AIR_GetNextFromBase(base, aircraftByIDX))) {
  436. - if (AIR_IsEmployeeInAircraft(employee, aircraftByIDX))
  437. - return aircraftByIDX;
  438. - }
  439. + aircraft_t *anyAircraft;
  440. + AIR_Foreach(anyAircraft) {
  441. + if (AIR_IsEmployeeInAircraft(employee, anyAircraft))
  442. + return anyAircraft;
  443. }
  444. return NULL;
  445. }
  446. @@ -3281,7 +3266,7 @@ void AIR_Shutdown (void)
  447.  
  448. craft = NULL;
  449. while ((craft = AIR_GetNext(craft)))
  450. - LIST_Delete(&craft->acTeam);
  451. + AIR_ResetAircraftTeam(craft);
  452. LIST_Delete(&ccs.aircraft);
  453.  
  454. AIR_ShutdownCallbacks();
  455. diff --git a/src/client/campaign/cp_aircraft.h b/src/client/campaign/cp_aircraft.h
  456. index f928ddb..2143d81 100644
  457. --- a/src/client/campaign/cp_aircraft.h
  458. +++ b/src/client/campaign/cp_aircraft.h
  459. @@ -240,6 +240,9 @@ void AIR_ListAircraftSamples_f(void);
  460. void AIR_ListCraftIndexes_f(void);
  461. #endif
  462.  
  463. +#define AIR_IsAircraftOfBase(aircraft, base) ((aircraft)->homebase == (base) && (aircraft)->status != AIR_CRASHED)
  464. +#define AIR_Foreach(var) LIST_Foreach(ccs.aircraft, aircraft_t, var)
  465. +
  466. aircraft_t *AIR_Add(struct base_s *base, const aircraft_t *aircraftTemplate);
  467. qboolean AIR_Delete(struct base_s *base, const aircraft_t *aircraft);
  468.  
  469. diff --git a/src/client/campaign/cp_aircraft_callbacks.c b/src/client/campaign/cp_aircraft_callbacks.c
  470. index 67497c5..d901ac1 100644
  471. --- a/src/client/campaign/cp_aircraft_callbacks.c
  472. +++ b/src/client/campaign/cp_aircraft_callbacks.c
  473. @@ -223,6 +223,7 @@ void AIR_AircraftSelect (aircraft_t* aircraft)
  474.  
  475. UI_RegisterText(TEXT_AIRCRAFT_INFO, aircraftInfo);
  476.  
  477. + /** @todo This shouldn't exists. UI should use the global idx as reference */
  478. /* compute the ID and... */
  479. aircraftInBase = NULL;
  480. id = 0;
  481. @@ -248,9 +249,10 @@ static void AIR_AircraftUpdateList_f (void)
  482. base_t *base = B_GetCurrentSelectedBase();
  483. aircraft_t *aircraft;
  484.  
  485. - aircraft = NULL;
  486. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  487. - LIST_AddString(&list, aircraft->name);
  488. + AIR_Foreach(aircraft) {
  489. + if (AIR_IsAircraftOfBase(aircraft, base))
  490. + LIST_AddString(&list, aircraft->name);
  491. + }
  492.  
  493. UI_RegisterLinkedListText(TEXT_AIRCRAFT_LIST, list);
  494. }
  495. diff --git a/src/client/campaign/cp_base.c b/src/client/campaign/cp_base.c
  496. index 5dec3b9..fe217a0 100644
  497. --- a/src/client/campaign/cp_base.c
  498. +++ b/src/client/campaign/cp_base.c
  499. @@ -702,15 +702,19 @@ void B_RemoveAircraftExceedingCapacity (base_t* base, buildingType_t buildingTyp
  500. linkedList_t *awayAircraft = NULL;
  501. int numAwayAircraft;
  502. int randomNum;
  503. - aircraft_t *aircraft = NULL;
  504. + aircraft_t *aircraft;
  505.  
  506. /* destroy aircraft only if there's not enough hangar (hangar is already destroyed) */
  507. if (B_FreeCapacity(base, capacity) >= 0)
  508. return;
  509.  
  510. /* destroy one aircraft (must not be sold: may be destroyed by aliens) */
  511. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  512. + AIR_Foreach(aircraft) {
  513. const int aircraftSize = aircraft->size;
  514. +
  515. + if (!AIR_IsAircraftOfBase(aircraft, base))
  516. + continue;
  517. +
  518. switch (aircraftSize) {
  519. case AIRCRAFT_SMALL:
  520. if (buildingType != B_SMALL_HANGAR)
  521. diff --git a/src/client/campaign/cp_employee.c b/src/client/campaign/cp_employee.c
  522. index 4a3f8d8..94dac96 100644
  523. --- a/src/client/campaign/cp_employee.c
  524. +++ b/src/client/campaign/cp_employee.c
  525. @@ -87,11 +87,10 @@ qboolean E_IsAwayFromBase (const employee_t *employee)
  526.  
  527. base = employee->baseHired;
  528.  
  529. - aircraft = NULL;
  530. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  531. - if (!AIR_IsAircraftInBase(aircraft) && AIR_IsInAircraftTeam(aircraft, employee))
  532. + AIR_Foreach(aircraft) {
  533. + if (AIR_IsAircraftOfBase(aircraft, base) && !AIR_IsAircraftInBase(aircraft) && AIR_IsInAircraftTeam(aircraft, employee))
  534. return qtrue;
  535. -
  536. + }
  537. return qfalse;
  538. }
  539.  
  540. diff --git a/src/client/campaign/cp_map.c b/src/client/campaign/cp_map.c
  541. index 9ac2073..513e342 100644
  542. --- a/src/client/campaign/cp_map.c
  543. +++ b/src/client/campaign/cp_map.c
  544. @@ -270,7 +270,9 @@ static void MAP_MultiSelectExecuteAction_f (void)
  545. */
  546. void MAP_MapClick (uiNode_t* node, int x, int y)
  547. {
  548. + aircraft_t *aircraft;
  549. aircraft_t *ufo;
  550. + base_t *base;
  551. int i;
  552. vec2_t pos;
  553. const linkedList_t *list;
  554. @@ -331,22 +333,17 @@ void MAP_MapClick (uiNode_t* node, int x, int y)
  555. CP_MissionToTypeString(tempMission), _(tempMission->location));
  556. }
  557.  
  558. - /* Get selected bases */
  559. - for (i = 0; i < MAX_BASES && multiSelect.nbSelect < MULTISELECT_MAXSELECT; i++) {
  560. - const base_t *base = B_GetFoundedBaseByIDX(i);
  561. - aircraft_t *aircraft;
  562. -
  563. - if (!base)
  564. - continue;
  565. + /* Get selected aircraft which belong */
  566. + AIR_Foreach(aircraft) {
  567. + if (AIR_IsAircraftOnGeoscape(aircraft) && aircraft->fuel > 0 && MAP_IsMapPositionSelected(node, aircraft->pos, x, y))
  568. + MAP_MultiSelectListAddItem(MULTISELECT_TYPE_AIRCRAFT, aircraft->idx, _("Aircraft"), aircraft->name);
  569. + }
  570.  
  571. + /* Get selected bases */
  572. + base = NULL;
  573. + while ((base = B_GetNext(base)) != NULL && multiSelect.nbSelect < MULTISELECT_MAXSELECT) {
  574. if (MAP_IsMapPositionSelected(node, base->pos, x, y))
  575. - MAP_MultiSelectListAddItem(MULTISELECT_TYPE_BASE, i, _("Base"), base->name);
  576. -
  577. - /* Get selected aircraft which belong to the base */
  578. - aircraft = NULL;
  579. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  580. - if (AIR_IsAircraftOnGeoscape(aircraft) && aircraft->fuel > 0 && MAP_IsMapPositionSelected(node, aircraft->pos, x, y))
  581. - MAP_MultiSelectListAddItem(MULTISELECT_TYPE_AIRCRAFT, aircraft->idx, _("Aircraft"), aircraft->name);
  582. + MAP_MultiSelectListAddItem(MULTISELECT_TYPE_BASE, base->idx, _("Base"), base->name);
  583. }
  584.  
  585. /* Get selected installations */
  586. @@ -1115,6 +1112,7 @@ static void MAP_GetGeoscapeAngle (float *vector)
  587. int counter = 0;
  588. int maxEventIdx;
  589. const int numMissions = CP_CountMissionOnGeoscape();
  590. + aircraft_t *aircraft;
  591. aircraft_t *ufo;
  592. base_t *base;
  593. int numBases = B_GetCount();
  594. @@ -1193,18 +1191,14 @@ static void MAP_GetGeoscapeAngle (float *vector)
  595. counter += ccs.numInstallations;
  596.  
  597. /* Cycle through aircraft (only those present on geoscape) */
  598. - base = NULL;
  599. - while ((base = B_GetNextFounded(base)) != NULL) {
  600. - aircraft_t *aircraft = NULL;
  601. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  602. - if (AIR_IsAircraftOnGeoscape(aircraft)) {
  603. - if (centerOnEventIdx == counter) {
  604. - MAP_ConvertObjectPositionToGeoscapePosition(vector, aircraft->pos);
  605. - MAP_SelectAircraft(aircraft);
  606. - return;
  607. - }
  608. - counter++;
  609. + AIR_Foreach(aircraft) {
  610. + if (AIR_IsAircraftOnGeoscape(aircraft)) {
  611. + if (centerOnEventIdx == counter) {
  612. + MAP_ConvertObjectPositionToGeoscapePosition(vector, aircraft->pos);
  613. + MAP_SelectAircraft(aircraft);
  614. + return;
  615. }
  616. + counter++;
  617. }
  618. }
  619.  
  620. @@ -1757,6 +1751,7 @@ static void MAP_DrawMapMarkers (const uiNode_t* node)
  621. int x, y, i, installationIdx, idx;
  622. const char* font;
  623. aircraft_t *ufo;
  624. + aircraft_t *aircraft;
  625. base_t *base;
  626.  
  627. const vec4_t white = {1.f, 1.f, 1.f, 0.7f};
  628. @@ -1794,17 +1789,13 @@ static void MAP_DrawMapMarkers (const uiNode_t* node)
  629.  
  630. /* draw bases */
  631. base = NULL;
  632. - while ((base = B_GetNextFounded(base)) != NULL) {
  633. - aircraft_t *aircraft;
  634. -
  635. + while ((base = B_GetNextFounded(base)) != NULL)
  636. MAP_DrawMapOneBase(node, base, oneUFOVisible, font);
  637.  
  638. - /* draw all aircraft of base */
  639. - aircraft = NULL;
  640. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  641. - if (AIR_IsAircraftOnGeoscape(aircraft))
  642. - MAP_DrawMapOnePhalanxAircraft(node, aircraft, oneUFOVisible);
  643. - }
  644. + /* draw all aircraft */
  645. + AIR_Foreach(aircraft) {
  646. + if (AIR_IsAircraftOnGeoscape(aircraft))
  647. + MAP_DrawMapOnePhalanxAircraft(node, aircraft, oneUFOVisible);
  648. }
  649.  
  650. /* draws ufos */
  651. diff --git a/src/client/campaign/cp_mapfightequip.c b/src/client/campaign/cp_mapfightequip.c
  652. index afd4290..f660e3c 100644
  653. --- a/src/client/campaign/cp_mapfightequip.c
  654. +++ b/src/client/campaign/cp_mapfightequip.c
  655. @@ -168,9 +168,8 @@ qboolean AIM_PilotAssignedAircraft (const base_t* base, const employee_t* pilot)
  656. qboolean found = qfalse;
  657. aircraft_t *aircraft;
  658.  
  659. - aircraft = NULL;
  660. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  661. - if (AIR_GetPilot(aircraft) == pilot) {
  662. + AIR_Foreach(aircraft) {
  663. + if (AIR_IsAircraftOfBase(aircraft, base) && AIR_GetPilot(aircraft) == pilot) {
  664. found = qtrue;
  665. break;
  666. }
  667. @@ -403,6 +402,7 @@ static void AII_UpdateOneInstallationDelay (base_t* base, installation_t* instal
  668. void AII_UpdateInstallationDelay (void)
  669. {
  670. int j, k;
  671. + aircraft_t *aircraft;
  672. base_t *base;
  673.  
  674. for (j = 0; j < MAX_INSTALLATIONS; j++) {
  675. @@ -417,29 +417,25 @@ void AII_UpdateInstallationDelay (void)
  676.  
  677. base = NULL;
  678. while ((base = B_GetNextFounded(base)) != NULL) {
  679. - aircraft_t *aircraft;
  680. -
  681. /* Update base */
  682. for (k = 0; k < base->numBatteries; k++)
  683. AII_UpdateOneInstallationDelay(base, NULL, NULL, &base->batteries[k].slot);
  684. for (k = 0; k < base->numLasers; k++)
  685. AII_UpdateOneInstallationDelay(base, NULL, NULL, &base->lasers[k].slot);
  686. + }
  687.  
  688. - /* Update each aircraft */
  689. - aircraft = NULL;
  690. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  691. - if (AIR_IsAircraftInBase(aircraft)) {
  692. - /* Update electronics delay */
  693. - for (k = 0; k < aircraft->maxElectronics; k++)
  694. - AII_UpdateOneInstallationDelay(base, NULL, aircraft, aircraft->electronics + k);
  695. -
  696. + /* Update each aircraft */
  697. + AIR_Foreach(aircraft) {
  698. + if (AIR_IsAircraftInBase(aircraft)) {
  699. + /* Update electronics delay */
  700. + for (k = 0; k < aircraft->maxElectronics; k++)
  701. + AII_UpdateOneInstallationDelay(aircraft->homebase, NULL, aircraft, aircraft->electronics + k);
  702. /* Update weapons delay */
  703. - for (k = 0; k < aircraft->maxWeapons; k++)
  704. - AII_UpdateOneInstallationDelay(base, NULL, aircraft, aircraft->weapons + k);
  705. + for (k = 0; k < aircraft->maxWeapons; k++)
  706. + AII_UpdateOneInstallationDelay(aircraft->homebase, NULL, aircraft, aircraft->weapons + k);
  707.  
  708. - /* Update shield delay */
  709. - AII_UpdateOneInstallationDelay(base, NULL, aircraft, &aircraft->shield);
  710. - }
  711. + /* Update shield delay */
  712. + AII_UpdateOneInstallationDelay(aircraft->homebase, NULL, aircraft, &aircraft->shield);
  713. }
  714. }
  715. }
  716. diff --git a/src/client/campaign/cp_market_callbacks.c b/src/client/campaign/cp_market_callbacks.c
  717. index cd41ff7..ff1b2a7 100644
  718. --- a/src/client/campaign/cp_market_callbacks.c
  719. +++ b/src/client/campaign/cp_market_callbacks.c
  720. @@ -127,9 +127,8 @@ static int BS_GetStorageAmountInBase (const base_t* base, const char *aircraftID
  721. assert(base);
  722.  
  723. /* Get storage amount in the base. */
  724. - aircraft = NULL;
  725. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  726. - if (!strcmp(aircraft->id, aircraftID))
  727. + AIR_Foreach(aircraft) {
  728. + if (AIR_IsAircraftOfBase(aircraft, base) && !strcmp(aircraft->id, aircraftID))
  729. storage++;
  730. }
  731. return storage;
  732. @@ -659,8 +658,9 @@ static void BS_SellAircraft_f (void)
  733. if (!aircraftTemplate)
  734. return;
  735.  
  736. - aircraft = NULL;
  737. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  738. + AIR_Foreach(aircraft) {
  739. + if (!AIR_IsAircraftOfBase(aircraft, base))
  740. + continue;
  741. if (!strcmp(aircraft->id, aircraftTemplate->id)) {
  742. if (AIR_GetTeamSize(aircraft) > 0) {
  743. teamNote = qtrue;
  744. diff --git a/src/client/campaign/cp_nation.c b/src/client/campaign/cp_nation.c
  745. index 62c40ad..6cea693 100644
  746. --- a/src/client/campaign/cp_nation.c
  747. +++ b/src/client/campaign/cp_nation.c
  748. @@ -845,6 +845,7 @@ static void NAT_NationList_f (void)
  749. * @note Called from CL_CampaignRun
  750. * @sa CL_CampaignRun
  751. * @sa B_CreateEmployee
  752. + * @todo This is very redundant with CL_StatsUpdate_f. Ivestigate and clean up.
  753. */
  754. void NAT_HandleBudget (const campaign_t *campaign)
  755. {
  756. @@ -856,6 +857,7 @@ void NAT_HandleBudget (const campaign_t *campaign)
  757. int initialCredits = ccs.credits;
  758. base_t *base;
  759. const salary_t *salary = &campaign->salaries;
  760. + aircraft_t *aircraft;
  761.  
  762. /* Refreshes the pilot global list. Pilots who are already hired are unchanged, but all other
  763. * pilots are replaced. The new pilots is evenly distributed between the nations that are happy (happiness > 0). */
  764. @@ -913,12 +915,10 @@ void NAT_HandleBudget (const campaign_t *campaign)
  765. }
  766.  
  767. cost = 0;
  768. - base = NULL;
  769. - while ((base = B_GetNextFounded(base)) != NULL) {
  770. - const salary_t *salary = &campaign->salaries;
  771. - aircraft_t *aircraft = NULL;
  772. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  773. - cost += aircraft->price * salary->aircraftFactor / salary->aircraftDivisor;
  774. + AIR_Foreach(aircraft) {
  775. + if (aircraft->status == AIR_CRASHED)
  776. + continue;
  777. + cost += aircraft->price * salary->aircraftFactor / salary->aircraftDivisor;
  778. }
  779. totalExpenditure += cost;
  780.  
  781. diff --git a/src/client/campaign/cp_popup.c b/src/client/campaign/cp_popup.c
  782. index dc0b182..5ac1f1a 100644
  783. --- a/src/client/campaign/cp_popup.c
  784. +++ b/src/client/campaign/cp_popup.c
  785. @@ -311,7 +311,7 @@ POPUP_INTERCEPT
  786. void CL_DisplayPopupInterceptMission (mission_t* mission)
  787. {
  788. linkedList_t *aircraftList = NULL;
  789. - base_t *base;
  790. + aircraft_t *aircraft;
  791.  
  792. if (!mission)
  793. return;
  794. @@ -322,31 +322,26 @@ void CL_DisplayPopupInterceptMission (mission_t* mission)
  795. /* Create the list of aircraft, and write the text to display in popup */
  796. popupIntercept.numAircraft = 0;
  797.  
  798. - base = NULL;
  799. - while ((base = B_GetNextFounded(base)) != NULL) {
  800. - /* Check aircraft in base */
  801. - aircraft_t *aircraft = NULL;
  802. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  803. - const int teamSize = AIR_GetTeamSize(aircraft);
  804. - /* if aircraft is empty we can't send it on a ground mission */
  805. - if (teamSize > 0 && AIR_CanIntercept(aircraft)) {
  806. - char aircraftListText[256] = "";
  807. - const float distance = GetDistanceOnGlobe(aircraft->pos, mission->pos);
  808. - const char *statusName = AIR_AircraftStatusToName(aircraft);
  809. - const char *time = CL_SecondConvert((float)SECONDS_PER_HOUR * distance / aircraft->stats[AIR_STATS_SPEED]);
  810. - Com_sprintf(aircraftListText, sizeof(aircraftListText), _("%s (%i/%i)\t%s\t%s\t%s"), aircraft->name,
  811. - teamSize, aircraft->maxTeamSize, statusName, base->name, time);
  812. - LIST_AddString(&aircraftList, aircraftListText);
  813. - popupIntercept.aircraft[popupIntercept.numAircraft] = aircraft;
  814. - popupIntercept.numAircraft++;
  815. - if (popupIntercept.numAircraft >= POPUP_INTERCEPT_MAX_AIRCRAFT)
  816. - break;
  817. - }
  818. + AIR_Foreach(aircraft) {
  819. + const int teamSize = AIR_GetTeamSize(aircraft);
  820. +
  821. + if (aircraft->status == AIR_CRASHED)
  822. + continue;
  823. + /* if aircraft is empty we can't send it on a ground mission */
  824. + if (teamSize > 0 && AIR_CanIntercept(aircraft)) {
  825. + char aircraftListText[256] = "";
  826. + const float distance = GetDistanceOnGlobe(aircraft->pos, mission->pos);
  827. + const char *statusName = AIR_AircraftStatusToName(aircraft);
  828. + const char *time = CL_SecondConvert((float)SECONDS_PER_HOUR * distance / aircraft->stats[AIR_STATS_SPEED]);
  829. + Com_sprintf(aircraftListText, sizeof(aircraftListText), _("%s (%i/%i)\t%s\t%s\t%s"), aircraft->name,
  830. + teamSize, aircraft->maxTeamSize, statusName, aircraft->homebase->name, time);
  831. + LIST_AddString(&aircraftList, aircraftListText);
  832. + popupIntercept.aircraft[popupIntercept.numAircraft] = aircraft;
  833. + popupIntercept.numAircraft++;
  834. + if (popupIntercept.numAircraft >= POPUP_INTERCEPT_MAX_AIRCRAFT)
  835. + break;
  836. }
  837. - /* also leave the outer loop */
  838. - if (popupIntercept.numAircraft >= POPUP_INTERCEPT_MAX_AIRCRAFT)
  839. - break;
  840. - } /* bases */
  841. + }
  842.  
  843. if (popupIntercept.numAircraft)
  844. UI_RegisterLinkedListText(TEXT_AIRCRAFT_LIST, aircraftList);
  845. @@ -369,6 +364,7 @@ void CL_DisplayPopupInterceptUFO (aircraft_t* ufo)
  846. {
  847. linkedList_t *aircraftList = NULL;
  848. linkedList_t *baseList = NULL;
  849. + aircraft_t *aircraft;
  850. base_t *base;
  851. int installationIdx;
  852.  
  853. @@ -381,47 +377,40 @@ void CL_DisplayPopupInterceptUFO (aircraft_t* ufo)
  854. /* Create the list of aircraft, and write the text to display in popup */
  855. popupIntercept.numAircraft = 0;
  856.  
  857. + AIR_Foreach(aircraft) {
  858. + if (AIR_CanIntercept(aircraft)) {
  859. + char aircraftListText[256] = "";
  860. + /* don't show aircraft with no weapons or no ammo, or crafts that
  861. + * can't even reach the target */
  862. + const char *enoughFuelMarker = "^B";
  863. +
  864. + /* Does the aircraft has weapons and ammo ? */
  865. + if (AIRFIGHT_ChooseWeapon(aircraft->weapons, aircraft->maxWeapons, aircraft->pos, aircraft->pos) == AIRFIGHT_WEAPON_CAN_NEVER_SHOOT) {
  866. + Com_DPrintf(DEBUG_CLIENT, "CL_DisplayPopupIntercept: No useable weapon found in craft '%s' (%i)\n", aircraft->id, aircraft->maxWeapons);
  867. + continue;
  868. + }
  869. + /* now check the aircraft range */
  870. + if (!AIR_AircraftHasEnoughFuel(aircraft, ufo->pos)) {
  871. + Com_DPrintf(DEBUG_CLIENT, "CL_DisplayPopupIntercept: Target out of reach for craft '%s'\n", aircraft->id);
  872. + enoughFuelMarker = "";
  873. + }
  874. +
  875. + Com_sprintf(aircraftListText, sizeof(aircraftListText), _("%s%s (%i/%i)\t%s\t%s"), enoughFuelMarker, aircraft->name,
  876. + AIR_GetTeamSize(aircraft), aircraft->maxTeamSize, AIR_AircraftStatusToName(aircraft), aircraft->homebase->name);
  877. + LIST_AddString(&aircraftList, aircraftListText);
  878. + popupIntercept.aircraft[popupIntercept.numAircraft] = aircraft;
  879. + popupIntercept.numAircraft++;
  880. + if (popupIntercept.numAircraft >= POPUP_INTERCEPT_MAX_AIRCRAFT)
  881. + break;
  882. + }
  883. + }
  884. +
  885. base = NULL;
  886. while ((base = B_GetNextFounded(base)) != NULL) {
  887. - aircraft_t *aircraft;
  888. -
  889. /* Check if the base should be displayed in base list
  890. * don't check range because maybe UFO will get closer */
  891. if (AII_BaseCanShoot(base))
  892. LIST_AddString(&baseList, va("^B%s", base->name));
  893. -
  894. - /* Check aircraft in base */
  895. - aircraft = NULL;
  896. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  897. - if (AIR_CanIntercept(aircraft)) {
  898. - char aircraftListText[256] = "";
  899. - /* don't show aircraft with no weapons or no ammo, or crafts that
  900. - * can't even reach the target */
  901. - const char *enoughFuelMarker = "^B";
  902. -
  903. - /* Does the aircraft has weapons and ammo ? */
  904. - if (AIRFIGHT_ChooseWeapon(aircraft->weapons, aircraft->maxWeapons, aircraft->pos, aircraft->pos) == AIRFIGHT_WEAPON_CAN_NEVER_SHOOT) {
  905. - Com_DPrintf(DEBUG_CLIENT, "CL_DisplayPopupIntercept: No useable weapon found in craft '%s' (%i)\n", aircraft->id, aircraft->maxWeapons);
  906. - continue;
  907. - }
  908. - /* now check the aircraft range */
  909. - if (!AIR_AircraftHasEnoughFuel(aircraft, ufo->pos)) {
  910. - Com_DPrintf(DEBUG_CLIENT, "CL_DisplayPopupIntercept: Target out of reach for craft '%s'\n", aircraft->id);
  911. - enoughFuelMarker = "";
  912. - }
  913. -
  914. - Com_sprintf(aircraftListText, sizeof(aircraftListText), _("%s%s (%i/%i)\t%s\t%s"), enoughFuelMarker, aircraft->name,
  915. - AIR_GetTeamSize(aircraft), aircraft->maxTeamSize, AIR_AircraftStatusToName(aircraft), base->name);
  916. - LIST_AddString(&aircraftList, aircraftListText);
  917. - popupIntercept.aircraft[popupIntercept.numAircraft] = aircraft;
  918. - popupIntercept.numAircraft++;
  919. - if (popupIntercept.numAircraft >= POPUP_INTERCEPT_MAX_AIRCRAFT)
  920. - break;
  921. - }
  922. - }
  923. - /* also leave the outer loop */
  924. - if (popupIntercept.numAircraft >= POPUP_INTERCEPT_MAX_AIRCRAFT)
  925. - break;
  926. } /* bases */
  927.  
  928. if (popupIntercept.numAircraft)
  929. diff --git a/src/client/campaign/cp_radar.c b/src/client/campaign/cp_radar.c
  930. index 298c93d..0eadbdb 100644
  931. --- a/src/client/campaign/cp_radar.c
  932. +++ b/src/client/campaign/cp_radar.c
  933. @@ -97,19 +97,15 @@ static inline void RADAR_DrawCoverage (const radar_t* radar, const vec2_t pos)
  934. */
  935. void RADAR_UpdateWholeRadarOverlay (void)
  936. {
  937. - base_t *base;
  938. + aircraft_t *aircraft;
  939.  
  940. /* Copy Base and installation radar overlay*/
  941. CP_InitializeRadarOverlay(qfalse);
  942.  
  943. /* Add aircraft radar coverage */
  944. - base = NULL;
  945. - while ((base = B_GetNextFounded(base)) != NULL) {
  946. - aircraft_t *aircraft = NULL;
  947. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  948. - if (AIR_IsAircraftOnGeoscape(aircraft))
  949. - RADAR_DrawCoverage(&aircraft->radar, aircraft->pos);
  950. - }
  951. + AIR_Foreach(aircraft) {
  952. + if (AIR_IsAircraftOnGeoscape(aircraft))
  953. + RADAR_DrawCoverage(&aircraft->radar, aircraft->pos);
  954. }
  955.  
  956. CP_UploadRadarCoverage();
  957. @@ -180,22 +176,22 @@ void RADAR_DeactivateRadarOverlay (void)
  958. {
  959. int idx;
  960. base_t *base;
  961. + aircraft_t *aircraft;
  962.  
  963. /* never deactivate radar if player wants it to be always turned on */
  964. if (radarOverlayWasSet)
  965. return;
  966.  
  967. + AIR_Foreach(aircraft) {
  968. + /** @todo Is aircraft->radar cleared for crashed aircraft? */
  969. + if (aircraft->radar.numUFOs)
  970. + return;
  971. + }
  972. +
  973. base = NULL;
  974. while ((base = B_GetNextFounded(base)) != NULL) {
  975. - aircraft_t *aircraft;
  976. -
  977. if (base->radar.numUFOs)
  978. return;
  979. -
  980. - aircraft = NULL;
  981. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  982. - if (aircraft->radar.numUFOs)
  983. - return;
  984. }
  985.  
  986. for (idx = 0; idx < MAX_INSTALLATIONS; idx++) {
  987. @@ -426,27 +422,27 @@ void RADAR_AddDetectedUFOToEveryRadar (const aircraft_t const *ufo)
  988. {
  989. int idx;
  990. base_t *base = NULL;
  991. + aircraft_t *aircraft;
  992.  
  993. - while ((base = B_GetNextFounded(base)) != NULL) {
  994. - aircraft_t *aircraft;
  995. + AIR_Foreach(aircraft) {
  996. + if (!AIR_IsAircraftOnGeoscape(aircraft))
  997. + continue;
  998. +
  999. + if (!RADAR_IsUFOSensored(&aircraft->radar, ufo)) {
  1000. + /* Distance from radar to UFO */
  1001. + const float dist = GetDistanceOnGlobe(ufo->pos, aircraft->pos);
  1002. + if (dist <= aircraft->radar.trackingRange)
  1003. + RADAR_AddUFO(&aircraft->radar, ufo);
  1004. + }
  1005. + }
  1006.  
  1007. + while ((base = B_GetNextFounded(base)) != NULL) {
  1008. if (!RADAR_IsUFOSensored(&base->radar, ufo)) {
  1009. - const float dist = GetDistanceOnGlobe(ufo->pos, base->pos); /* Distance from radar to UFO */
  1010. + /* Distance from radar to UFO */
  1011. + const float dist = GetDistanceOnGlobe(ufo->pos, base->pos);
  1012. if (dist <= base->radar.trackingRange)
  1013. RADAR_AddUFO(&base->radar, ufo);
  1014. }
  1015. -
  1016. - aircraft = NULL;
  1017. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  1018. - if (!AIR_IsAircraftOnGeoscape(aircraft))
  1019. - continue;
  1020. -
  1021. - if (!RADAR_IsUFOSensored(&aircraft->radar, ufo)) {
  1022. - const float dist = GetDistanceOnGlobe(ufo->pos, aircraft->pos); /* Distance from radar to UFO */
  1023. - if (dist <= aircraft->radar.trackingRange)
  1024. - RADAR_AddUFO(&aircraft->radar, ufo);
  1025. - }
  1026. - }
  1027. }
  1028.  
  1029. for (idx = 0; idx < MAX_INSTALLATIONS; idx++) {
  1030. @@ -460,7 +456,8 @@ void RADAR_AddDetectedUFOToEveryRadar (const aircraft_t const *ufo)
  1031. continue;
  1032.  
  1033. if (!RADAR_IsUFOSensored(&installation->radar, ufo)) {
  1034. - const float dist = GetDistanceOnGlobe(ufo->pos, installation->pos); /* Distance from radar to UFO */
  1035. + /* Distance from radar to UFO */
  1036. + const float dist = GetDistanceOnGlobe(ufo->pos, installation->pos);
  1037. if (dist <= ufo->radar.trackingRange)
  1038. RADAR_AddUFO(&installation->radar, ufo);
  1039. }
  1040. diff --git a/src/client/campaign/cp_research.c b/src/client/campaign/cp_research.c
  1041. index 815c287..ccab8a6 100644
  1042. --- a/src/client/campaign/cp_research.c
  1043. +++ b/src/client/campaign/cp_research.c
  1044. @@ -625,14 +625,11 @@ void RS_InitTree (const campaign_t *campaign, qboolean load)
  1045. if (load) {
  1046. /* when you load a savegame right after starting UFO, the aircraft in bases
  1047. * and installations don't have any tech assigned */
  1048. - base_t *base = NULL;
  1049. - while ((base = B_GetNextFounded(base)) != NULL) {
  1050. - aircraft_t *aircraft = NULL;
  1051. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  1052. - /* if you already played before loading the game, tech are already defined for templates */
  1053. - if (!aircraft->tech)
  1054. - aircraft->tech = RS_GetTechByProvided(aircraft->id);
  1055. - }
  1056. + aircraft_t *aircraft;
  1057. + AIR_Foreach(aircraft) {
  1058. + /* if you already played before loading the game, tech are already defined for templates */
  1059. + if (!aircraft->tech)
  1060. + aircraft->tech = RS_GetTechByProvided(aircraft->id);
  1061. }
  1062. }
  1063.  
  1064. diff --git a/src/client/campaign/cp_statistics.c b/src/client/campaign/cp_statistics.c
  1065. index f5a94f1..3af09bc 100644
  1066. --- a/src/client/campaign/cp_statistics.c
  1067. +++ b/src/client/campaign/cp_statistics.c
  1068. @@ -32,6 +32,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  1069. #define MAX_STATS_BUFFER 2048
  1070. /**
  1071. * @brief Shows the current stats from stats_t stats
  1072. + * @todo This is very redundant with NAT_HandleBudget Ivestigate and clean up.
  1073. */
  1074. void CL_StatsUpdate_f (void)
  1075. {
  1076. @@ -42,6 +43,7 @@ void CL_StatsUpdate_f (void)
  1077. base_t *base;
  1078. const campaign_t *campaign = ccs.curCampaign;
  1079. const salary_t *salary = &campaign->salaries;
  1080. + aircraft_t *aircraft;
  1081.  
  1082. /* delete buffer */
  1083. memset(statsBuffer, 0, sizeof(statsBuffer));
  1084. @@ -102,12 +104,9 @@ void CL_StatsUpdate_f (void)
  1085. sum += costs;
  1086.  
  1087. costs = 0;
  1088. -
  1089. - base = NULL;
  1090. - while ((base = B_GetNextFounded(base)) != NULL) {
  1091. - const salary_t *salary = &campaign->salaries;
  1092. - aircraft_t *aircraft = NULL;
  1093. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
  1094. + AIR_Foreach(aircraft) {
  1095. + if (aircraft->status == AIR_CRASHED)
  1096. + continue;
  1097. costs += aircraft->price * salary->aircraftFactor / salary->aircraftDivisor;
  1098. }
  1099. Q_strcat(pos, va(_("Aircraft:\t%i c\n"), costs), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
  1100. diff --git a/src/client/campaign/cp_team_callbacks.c b/src/client/campaign/cp_team_callbacks.c
  1101. index f8b5fab..ddab095 100644
  1102. --- a/src/client/campaign/cp_team_callbacks.c
  1103. +++ b/src/client/campaign/cp_team_callbacks.c
  1104. @@ -195,7 +195,8 @@ static void CL_UpdateEquipmentMenuParameters_f (void)
  1105. {
  1106. equipDef_t unused;
  1107. int p;
  1108. - aircraft_t *aircraft, *aircraftInBase;
  1109. + aircraft_t *aircraft;
  1110. + aircraft_t *aircraftInBase;
  1111. base_t *base = B_GetCurrentSelectedBase();
  1112. size_t size;
  1113.  
  1114. @@ -231,9 +232,11 @@ static void CL_UpdateEquipmentMenuParameters_f (void)
  1115. /* clean up aircraft crew for upcoming mission */
  1116. CL_CleanTempInventory(aircraft->homebase);
  1117.  
  1118. - aircraftInBase = NULL;
  1119. - while ((aircraftInBase = AIR_GetNextFromBase(aircraft->homebase, aircraftInBase)) != NULL)
  1120. - CL_CleanupAircraftCrew(aircraftInBase, &unused);
  1121. + AIR_Foreach(aircraftInBase) {
  1122. + /** @todo should we run this for crashed aircraft too? */
  1123. + if (AIR_IsAircraftOfBase(aircraft, base))
  1124. + CL_CleanupAircraftCrew(aircraftInBase, &unused);
  1125. + }
  1126.  
  1127. UI_ContainerNodeUpdateEquipment(&aircraft->homebase->bEquipment, &unused);
  1128. }
  1129. diff --git a/src/client/campaign/cp_transfer_callbacks.c b/src/client/campaign/cp_transfer_callbacks.c
  1130. index 2dbf384..feccee6 100644
  1131. --- a/src/client/campaign/cp_transfer_callbacks.c
  1132. +++ b/src/client/campaign/cp_transfer_callbacks.c
  1133. @@ -682,10 +682,10 @@ static int TR_FillAircraft (const base_t *srcbase, const base_t *destbase, linke
  1134. {
  1135. int cnt = 0;
  1136. if (AIR_AircraftAllowed(destbase)) {
  1137. - aircraft_t *aircraft = NULL;
  1138. + aircraft_t *aircraft;
  1139.  
  1140. - while ((aircraft = AIR_GetNextFromBase(srcbase, aircraft))) {
  1141. - if (TR_AircraftListSelect(aircraft)) {
  1142. + AIR_Foreach(aircraft) {
  1143. + if (AIR_IsAircraftOfBase(aircraft, srcbase) && TR_AircraftListSelect(aircraft)) {
  1144. char str[128];
  1145. Com_sprintf(str, sizeof(str), _("Aircraft %s"), aircraft->name);
  1146. TR_AddListEntry(names, str, amounts, 1, transfers, -1);
  1147. @@ -1007,10 +1007,10 @@ static void TR_AddAircraftToTransferList (base_t *base, transferData_t *transfer
  1148. int cnt = 0;
  1149.  
  1150. if (AIR_AircraftAllowed(transferBase)) {
  1151. - aircraft_t *aircraft = NULL;
  1152. + aircraft_t *aircraft;
  1153.  
  1154. - while ((aircraft = AIR_GetNextFromBase(base, aircraft))) {
  1155. - if (TR_AircraftListSelect(aircraft)) {
  1156. + AIR_Foreach(aircraft) {
  1157. + if (AIR_IsAircraftOfBase(aircraft, base) && TR_AircraftListSelect(aircraft)) {
  1158. if (cnt == num) {
  1159. if (TR_CheckAircraft(aircraft, transferBase)) {
  1160. LIST_AddPointer(&transferData->aircraft, (void*)aircraft);
  1161. diff --git a/src/client/campaign/cp_ufo.c b/src/client/campaign/cp_ufo.c
  1162. index 5a9108f..8587569 100644
  1163. --- a/src/client/campaign/cp_ufo.c
  1164. +++ b/src/client/campaign/cp_ufo.c
  1165. @@ -390,8 +390,7 @@ void UFO_UpdateAlienInterestForAllBasesAndInstallations (void)
  1166. */
  1167. static void UFO_SearchAircraftTarget (const campaign_t* campaign, aircraft_t *ufo)
  1168. {
  1169. - base_t *base;
  1170. - aircraft_t* phalanxAircraft;
  1171. + aircraft_t *phalanxAircraft;
  1172. float distance = 999999.;
  1173.  
  1174. /* UFO never try to attack a PHALANX aircraft except if they came on earth in that aim */
  1175. @@ -413,27 +412,22 @@ static void UFO_SearchAircraftTarget (const campaign_t* campaign, aircraft_t *uf
  1176. }
  1177.  
  1178. ufo->status = AIR_TRANSIT;
  1179. - base = NULL;
  1180. - while ((base = B_GetNextFounded(base)) != NULL) {
  1181. - /* check if the ufo can attack an aircraft */
  1182. - phalanxAircraft = NULL;
  1183. - while ((phalanxAircraft = AIR_GetNextFromBase(base, phalanxAircraft)) != NULL) {
  1184. - /* check that aircraft is flying */
  1185. - if (AIR_IsAircraftOnGeoscape(phalanxAircraft)) {
  1186. - /* get the distance from ufo to aircraft */
  1187. - const float dist = GetDistanceOnGlobe(ufo->pos, phalanxAircraft->pos);
  1188. - /* check out of reach */
  1189. - if (dist > MAX_DETECTING_RANGE)
  1190. - continue;
  1191. - /* choose the nearest target */
  1192. - if (dist < distance) {
  1193. - distance = dist;
  1194. - if (UFO_SendPursuingAircraft(ufo, phalanxAircraft) && UFO_IsUFOSeenOnGeoscape(ufo)) {
  1195. - /* stop time and notify */
  1196. - MSO_CheckAddNewMessage(NT_UFO_ATTACKING, _("Notice"), va(_("A UFO is flying toward %s"), phalanxAircraft->name), qfalse,
  1197. - MSG_STANDARD, NULL);
  1198. - /** @todo present a popup with possible orders like: return to base, attack the ufo, try to flee the rockets */
  1199. - }
  1200. + AIR_Foreach(phalanxAircraft) {
  1201. + /* check that aircraft is flying */
  1202. + if (AIR_IsAircraftOnGeoscape(phalanxAircraft)) {
  1203. + /* get the distance from ufo to aircraft */
  1204. + const float dist = GetDistanceOnGlobe(ufo->pos, phalanxAircraft->pos);
  1205. + /* check out of reach */
  1206. + if (dist > MAX_DETECTING_RANGE)
  1207. + continue;
  1208. + /* choose the nearest target */
  1209. + if (dist < distance) {
  1210. + distance = dist;
  1211. + if (UFO_SendPursuingAircraft(ufo, phalanxAircraft) && UFO_IsUFOSeenOnGeoscape(ufo)) {
  1212. + /* stop time and notify */
  1213. + MSO_CheckAddNewMessage(NT_UFO_ATTACKING, _("Notice"), va(_("A UFO is flying toward %s"), phalanxAircraft->name), qfalse,
  1214. + MSG_STANDARD, NULL);
  1215. + /** @todo present a popup with possible orders like: return to base, attack the ufo, try to flee the rockets */
  1216. }
  1217. }
  1218. }
  1219. @@ -749,6 +743,7 @@ qboolean UFO_CampaignCheckEvents (void)
  1220. /* detected tells us whether or not a UFO is detected NOW, whereas ufo->detected tells
  1221. * us whether or not the UFO was detected PREVIOUSLY. */
  1222. qboolean detected = qfalse;
  1223. + aircraft_t *aircraft;
  1224. base_t *base;
  1225.  
  1226. /* don't update UFO status id UFO is landed or crashed */
  1227. @@ -758,10 +753,25 @@ qboolean UFO_CampaignCheckEvents (void)
  1228. /* note: We can't exit these loops as soon as we found the UFO detected
  1229. * RADAR_CheckUFOSensored registers the UFO in every radars' detection list
  1230. * which detect it */
  1231. +
  1232. + /* Check if UFO is detected by an aircraft */
  1233. + AIR_Foreach(aircraft) {
  1234. + if (!AIR_IsAircraftOnGeoscape(aircraft))
  1235. + continue;
  1236. + /* maybe the ufo is already detected, don't reset it */
  1237. + if (RADAR_CheckUFOSensored(&aircraft->radar, aircraft->pos, ufo, detected | ufo->detected)) {
  1238. + const int distance = GetDistanceOnGlobe(aircraft->pos, ufo->pos);
  1239. + detected = qtrue;
  1240. + if (minDistance < 0 || minDistance > distance) {
  1241. + minDistance = distance;
  1242. + Q_strncpyz(detectedBy, aircraft->name, sizeof(detectedBy));
  1243. + }
  1244. + }
  1245. + }
  1246. +
  1247. + /* Check if UFO is detected by a base */
  1248. base = NULL;
  1249. while ((base = B_GetNextFounded(base)) != NULL) {
  1250. - aircraft_t *aircraft;
  1251. -
  1252. if (!B_GetBuildingStatus(base, B_POWER))
  1253. continue;
  1254.  
  1255. @@ -775,23 +785,9 @@ qboolean UFO_CampaignCheckEvents (void)
  1256. }
  1257. }
  1258.  
  1259. - /* Check if UFO is detected by an aircraft */
  1260. - aircraft = NULL;
  1261. - while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL) {
  1262. - if (!AIR_IsAircraftOnGeoscape(aircraft))
  1263. - continue;
  1264. - /* maybe the ufo is already detected, don't reset it */
  1265. - if (RADAR_CheckUFOSensored(&aircraft->radar, aircraft->pos, ufo, detected | ufo->detected)) {
  1266. - const int distance = GetDistanceOnGlobe(aircraft->pos, ufo->pos);
  1267. - detected = qtrue;
  1268. - if (minDistance < 0 || minDistance > distance) {
  1269. - minDistance = distance;
  1270. - Q_strncpyz(detectedBy, aircraft->name, sizeof(detectedBy));
  1271. - }
  1272. - }
  1273. - }
  1274. }
  1275.  
  1276. + /* Check if UFO is detected by a radartower */
  1277. for (installationIdx = 0; installationIdx < ccs.numInstallations; installationIdx++) {
  1278. installation_t *installation = INS_GetFoundedInstallationByIDX(installationIdx);
  1279. if (!installation)
  1280. --
  1281. 1.7.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement