cp_alien_interest.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2002-2010 UFO: Alien Invasion.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "../cl_shared.h"
00027 #include "cp_campaign.h"
00028 #include "cp_alien_interest.h"
00029 #include "save/save_interest.h"
00030 
00036 void CL_ResetAlienInterest (void)
00037 {
00038     int i;
00039 
00040     ccs.lastInterestIncreaseDelay = 0;
00041     ccs.lastMissionSpawnedDelay = 0;
00042     ccs.overallInterest = INITIAL_OVERALL_INTEREST;
00043 
00044     for (i = 0; i < INTERESTCATEGORY_MAX; i++)
00045         ccs.interest[i] = 0;
00046     ccs.interest[INTERESTCATEGORY_RECON] = INITIAL_OVERALL_INTEREST;
00047 }
00048 
00055 void CL_ChangeIndividualInterest (float interestFactor, interestCategory_t category)
00056 {
00057     if (category == INTERESTCATEGORY_MAX) {
00058         Com_Printf("CL_ChangeIndividualInterest: Unsupported value of category\n");
00059         return;
00060     }
00061 
00062     if (interestFactor > 0.0f) {
00063         const int gain = (int) (interestFactor * ccs.overallInterest);
00064         const int diff = ccs.overallInterest - ccs.interest[category];
00065         /* Fraction of individual interest that will be won if
00066          * individal interest becomes higher than overall interest. 0 means no increase */
00067         const float slowerIncreaseFraction = 0.5f;
00068         /* Value increases: interestFactor is taken from the overall interest value
00069          * But it increase slower if the individual interest becomes higher than the overall interest value */
00070         if (diff > gain)
00071             /* Final value of individual interest is below overall interest */
00072             ccs.interest[category] += gain;
00073         else if (diff > 0)
00074             /* Initial value of individual interest is below overall interest */
00075             ccs.interest[category] = ccs.overallInterest + (int) (slowerIncreaseFraction * (gain - diff));
00076         else
00077             /* Final value of individual interest is above overall interest */
00078             ccs.interest[category] += (int) (slowerIncreaseFraction * gain);
00079     } else {
00080         /* Value decreases: interestFactor is taken from the individual interest value */
00081         ccs.interest[category] += (int) (interestFactor * ccs.interest[category]);
00082         if (ccs.interest[category] < 0) {
00083             /* this may be reached if interestFactor is below -1 */
00084             ccs.interest[category] = 0;
00085         }
00086     }
00087 }
00088 
00094 void CP_IncreaseAlienInterest (void)
00095 {
00096     /* Adjust interest increase rate by difficulty. */
00097     const int delayBetweenIncrease = HOURS_PER_ONE_INTEREST - ccs.curCampaign->difficulty;
00098 
00099     ccs.lastInterestIncreaseDelay++;
00100 
00101     if (ccs.lastInterestIncreaseDelay > delayBetweenIncrease) {
00102         ccs.overallInterest++;
00103         ccs.lastInterestIncreaseDelay %= delayBetweenIncrease;
00104     }
00105 }
00106 
00111 qboolean CP_SaveInterestsXML (mxml_node_t *parent)
00112 {
00113     mxml_node_t *interestsNode = mxml_AddNode(parent, SAVE_INTERESTS);
00114     int i;
00115 
00116     mxml_AddShortValue(interestsNode, SAVE_INTERESTS_LASTINCREASEDELAY, ccs.lastInterestIncreaseDelay);
00117     mxml_AddShortValue(interestsNode, SAVE_INTERESTS_LASTMISSIONSPAWNEDDELAY, ccs.lastMissionSpawnedDelay);
00118     mxml_AddShortValue(interestsNode, SAVE_INTERESTS_OVERALL, ccs.overallInterest);
00119     Com_RegisterConstList(saveInterestConstants);
00120     for (i = 0; i < INTERESTCATEGORY_MAX; i++) {
00121         mxml_node_t * interestNode = mxml_AddNode(interestsNode, SAVE_INTERESTS_INTEREST);
00122         mxml_AddString(interestNode, SAVE_INTERESTS_ID, Com_GetConstVariable(SAVE_INTERESTCAT_NAMESPACE, i));
00123         mxml_AddShort(interestNode, SAVE_INTERESTS_VAL, ccs.interest[i]);
00124     }
00125     Com_UnregisterConstList(saveInterestConstants);
00126     return qtrue;
00127 }
00128 
00133 qboolean CP_LoadInterestsXML (mxml_node_t *parent)
00134 {
00135     mxml_node_t *node;
00136     mxml_node_t *interestsNode = mxml_GetNode(parent, SAVE_INTERESTS);
00137     qboolean success = qtrue;
00138 
00139     ccs.lastInterestIncreaseDelay = mxml_GetInt(interestsNode, SAVE_INTERESTS_LASTINCREASEDELAY, 0);
00140     ccs.lastMissionSpawnedDelay = mxml_GetInt(interestsNode, SAVE_INTERESTS_LASTMISSIONSPAWNEDDELAY, 0);
00141     ccs.overallInterest = mxml_GetInt(interestsNode, SAVE_INTERESTS_OVERALL, 0);
00142     Com_RegisterConstList(saveInterestConstants);
00143     for (node = mxml_GetNode(interestsNode, SAVE_INTERESTS_INTEREST); node;
00144             node = mxml_GetNextNode(node, interestsNode, SAVE_INTERESTS_INTEREST)) {
00145         const char *categoryId = mxml_GetString(node, SAVE_INTERESTS_ID);
00146         int cat;
00147 
00148         if (!Com_GetConstInt(categoryId, (int*) &cat)) {
00149             Com_Printf("Invaild interest category '%s'\n", categoryId);
00150             success = qfalse;
00151             break;
00152         }
00153         ccs.interest[cat]= mxml_GetInt(node, SAVE_INTERESTS_VAL, 0);
00154     }
00155     Com_UnregisterConstList(saveInterestConstants);
00156     return success;
00157 }
00158 

Generated by  doxygen 1.6.2