cp_statistics.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 "../ui/ui_data.h"
00028 #include "cp_campaign.h"
00029 #include "cp_xvi.h"
00030 #include "save/save_statistics.h"
00031 
00032 #define MAX_STATS_BUFFER 2048
00033 
00036 void CL_StatsUpdate_f (void)
00037 {
00038     char *pos;
00039     static char statsBuffer[MAX_STATS_BUFFER];
00040     int hired[MAX_EMPL];
00041     int i, costs = 0, sum = 0, totalfunds = 0;
00042     employee_t *employee;
00043 
00044     /* delete buffer */
00045     memset(statsBuffer, 0, sizeof(statsBuffer));
00046     memset(hired, 0, sizeof(hired));
00047 
00048     pos = statsBuffer;
00049 
00050     /* missions */
00051     UI_RegisterText(TEXT_STATS_MISSION, pos);
00052     Com_sprintf(pos, MAX_STATS_BUFFER, _("Won:\t%i\nLost:\t%i\n\n"), ccs.campaignStats.missionsWon, ccs.campaignStats.missionsLost);
00053 
00054     /* bases */
00055     pos += (strlen(pos) + 1);
00056     UI_RegisterText(TEXT_STATS_BASES, pos);
00057     Com_sprintf(pos, (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos), _("Built:\t%i\nActive:\t%i\nAttacked:\t%i\n"),
00058             ccs.campaignStats.basesBuilt, ccs.numBases, ccs.campaignStats.basesAttacked),
00059 
00060     /* installations */
00061     pos += (strlen(pos) + 1);
00062     UI_RegisterText(TEXT_STATS_INSTALLATIONS, pos);
00063     for (i = 0; i < ccs.numInstallations; i++) {
00064         const installation_t *inst = &ccs.installations[i];
00065         Q_strcat(pos, va("%s\n", inst->name), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00066     }
00067 
00068     /* nations */
00069     pos += (strlen(pos) + 1);
00070     UI_RegisterText(TEXT_STATS_NATIONS, pos);
00071     for (i = 0; i < ccs.numNations; i++) {
00072         const nation_t *nation = &ccs.nations[i];
00073         Q_strcat(pos, va(_("%s\t%s\n"), _(nation->name), NAT_GetHappinessString(nation)), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00074         totalfunds += NAT_GetFunding(nation, 0);
00075     }
00076     Q_strcat(pos, va(_("\nFunding this month:\t%d"), totalfunds), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00077 
00078     /* costs */
00079     for (i = 0; i < MAX_EMPL; i++) {
00080         employee = NULL;
00081         while ((employee = E_GetNextHired(i, employee))) {
00082             costs += CP_GetSalaryBaseEmployee(i) + employee->chr.score.rank * CP_GetSalaryRankBonusEmployee(i);
00083             hired[employee->type]++;
00084         }
00085     }
00086 
00087     /* employees - this is between the two costs parts to count the hired employees */
00088     pos += (strlen(pos) + 1);
00089     UI_RegisterText(TEXT_STATS_EMPLOYEES, pos);
00090     for (i = 0; i < MAX_EMPL; i++) {
00091         Q_strcat(pos, va(_("%s\t%i\n"), E_GetEmployeeString(i), hired[i]), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00092     }
00093 
00094     /* costs - second part */
00095     pos += (strlen(pos) + 1);
00096     UI_RegisterText(TEXT_STATS_COSTS, pos);
00097     Q_strcat(pos, va(_("Employees:\t%i c\n"), costs), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00098     sum += costs;
00099 
00100     costs = 0;
00101     for (i = 0; i < MAX_BASES; i++) {
00102         base_t *base = B_GetFoundedBaseByIDX(i);
00103         aircraft_t *aircraft;
00104 
00105         aircraft = NULL;
00106         while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
00107             costs += aircraft->price * SALARY_AIRCRAFT_FACTOR / SALARY_AIRCRAFT_DIVISOR;
00108     }
00109     Q_strcat(pos, va(_("Aircraft:\t%i c\n"), costs), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00110     sum += costs;
00111 
00112     for (i = 0; i < MAX_BASES; i++) {
00113         const base_t const *base = B_GetFoundedBaseByIDX(i);
00114         if (!base)
00115             continue;
00116         costs = CP_GetSalaryUpKeepBase(base);
00117         Q_strcat(pos, va(_("Base (%s):\t%i c\n"), base->name, costs), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00118         sum += costs;
00119     }
00120 
00121     costs = CP_GetSalaryAdministrative();
00122     Q_strcat(pos, va(_("Administrative costs:\t%i c\n"), costs), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00123     sum += costs;
00124 
00125     if (ccs.credits < 0) {
00126         const float interest = ccs.credits * SALARY_DEBT_INTEREST;
00127 
00128         costs = (int)ceil(interest);
00129         Q_strcat(pos, va(_("Debt:\t%i c\n"), costs), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00130         sum += costs;
00131     }
00132     Q_strcat(pos, va(_("\n\t-------\nSum:\t%i c\n"), sum), (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00133 
00134     /* campaign */
00135     pos += (strlen(pos) + 1);
00136     UI_RegisterText(TEXT_GENERIC, pos);
00137     Q_strcat(pos, va(_("Max. allowed debts: %ic\n"), ccs.curCampaign->negativeCreditsUntilLost),
00138         (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00139 
00140     /* only show the xvi spread data when it's available */
00141     if (CP_IsXVIResearched()) {
00142         Q_strcat(pos, va(_("Max. allowed eXtraterrestial Viral Infection: %i%%\n"
00143             "Current eXtraterrestial Viral Infection: %i%%"),
00144             ccs.curCampaign->maxAllowedXVIRateUntilLost,
00145             CP_GetAverageXVIRate()),
00146             (ptrdiff_t)(&statsBuffer[MAX_STATS_BUFFER] - pos));
00147     }
00148 }
00149 
00154 qboolean STATS_SaveXML (mxml_node_t *parent)
00155 {
00156     mxml_node_t * stats;
00157 
00158     stats = mxml_AddNode(parent, SAVE_STATS_STATS);
00159 
00160     mxml_AddIntValue(stats, SAVE_STATS_MISSIONS, ccs.campaignStats.missions);
00161     mxml_AddIntValue(stats, SAVE_STATS_MISSIONSWON, ccs.campaignStats.missionsWon);
00162     mxml_AddIntValue(stats, SAVE_STATS_MISSIONSLOST, ccs.campaignStats.missionsLost);
00163     mxml_AddIntValue(stats, SAVE_STATS_BASESBUILT, ccs.campaignStats.basesBuilt);
00164     mxml_AddIntValue(stats, SAVE_STATS_BASESATTACKED, ccs.campaignStats.basesAttacked);
00165     mxml_AddIntValue(stats, SAVE_STATS_INTERCEPTIONS, ccs.campaignStats.interceptions);
00166     mxml_AddIntValue(stats, SAVE_STATS_SOLDIERSLOST, ccs.campaignStats.soldiersLost);
00167     mxml_AddIntValue(stats, SAVE_STATS_SOLDIERSNEW, ccs.campaignStats.soldiersNew);
00168     mxml_AddIntValue(stats, SAVE_STATS_KILLEDALIENS, ccs.campaignStats.killedAliens);
00169     mxml_AddIntValue(stats, SAVE_STATS_RESCUEDCIVILIANS, ccs.campaignStats.rescuedCivilians);
00170     mxml_AddIntValue(stats, SAVE_STATS_RESEARCHEDTECHNOLOGIES, ccs.campaignStats.researchedTechnologies);
00171     mxml_AddIntValue(stats, SAVE_STATS_MONEYINTERCEPTIONS, ccs.campaignStats.moneyInterceptions);
00172     mxml_AddIntValue(stats, SAVE_STATS_MONEYBASES, ccs.campaignStats.moneyBases);
00173     mxml_AddIntValue(stats, SAVE_STATS_MONEYRESEARCH, ccs.campaignStats.moneyResearch);
00174     mxml_AddIntValue(stats, SAVE_STATS_MONEYWEAPONS, ccs.campaignStats.moneyWeapons);
00175     mxml_AddIntValue(stats, SAVE_STATS_UFOSDETECTED, ccs.campaignStats.ufosDetected);
00176     mxml_AddIntValue(stats, SAVE_STATS_ALIENBASESBUILT, ccs.campaignStats.alienBasesBuilt);
00177     mxml_AddIntValue(stats, SAVE_STATS_UFOSSTORED, ccs.campaignStats.ufosStored);
00178     mxml_AddIntValue(stats, SAVE_STATS_AIRCRAFTHAD, ccs.campaignStats.aircraftHad);
00179     return qtrue;
00180 }
00181 
00186 qboolean STATS_LoadXML (mxml_node_t *parent)
00187 {
00188     mxml_node_t * stats;
00189 
00190     stats = mxml_GetNode(parent, SAVE_STATS_STATS);
00191     if (!stats) {
00192         Com_Printf("Did not find stats entry in xml!\n");
00193         return qfalse;
00194     }
00195     ccs.campaignStats.missions = mxml_GetInt(stats, SAVE_STATS_MISSIONS, 0);
00196     ccs.campaignStats.missionsWon = mxml_GetInt(stats, SAVE_STATS_MISSIONSWON, 0);
00197     ccs.campaignStats.missionsLost = mxml_GetInt(stats, SAVE_STATS_MISSIONSLOST, 0);
00198     ccs.campaignStats.basesBuilt = mxml_GetInt(stats, SAVE_STATS_BASESBUILT, 0);
00199     ccs.campaignStats.basesAttacked = mxml_GetInt(stats, SAVE_STATS_BASESATTACKED, 0);
00200     ccs.campaignStats.interceptions = mxml_GetInt(stats, SAVE_STATS_INTERCEPTIONS, 0);
00201     ccs.campaignStats.soldiersLost = mxml_GetInt(stats, SAVE_STATS_SOLDIERSLOST, 0);
00202     ccs.campaignStats.soldiersNew = mxml_GetInt(stats, SAVE_STATS_SOLDIERSNEW, 0);
00203     ccs.campaignStats.killedAliens = mxml_GetInt(stats, SAVE_STATS_KILLEDALIENS, 0);
00204     ccs.campaignStats.rescuedCivilians = mxml_GetInt(stats, SAVE_STATS_RESCUEDCIVILIANS, 0);
00205     ccs.campaignStats.researchedTechnologies = mxml_GetInt(stats, SAVE_STATS_RESEARCHEDTECHNOLOGIES, 0);
00206     ccs.campaignStats.moneyInterceptions = mxml_GetInt(stats, SAVE_STATS_MONEYINTERCEPTIONS, 0);
00207     ccs.campaignStats.moneyBases = mxml_GetInt(stats, SAVE_STATS_MONEYBASES, 0);
00208     ccs.campaignStats.moneyResearch = mxml_GetInt(stats, SAVE_STATS_MONEYRESEARCH, 0);
00209     ccs.campaignStats.moneyWeapons = mxml_GetInt(stats, SAVE_STATS_MONEYWEAPONS, 0);
00210     ccs.campaignStats.ufosDetected = mxml_GetInt(stats, SAVE_STATS_UFOSDETECTED, 0);
00211     ccs.campaignStats.alienBasesBuilt = mxml_GetInt(stats, SAVE_STATS_ALIENBASESBUILT, 0);
00212     /* fallback for savegame compatibility */
00213     if (ccs.campaignStats.alienBasesBuilt == 0)
00214         ccs.campaignStats.alienBasesBuilt = AB_GetAlienBaseNumber();
00215     ccs.campaignStats.ufosStored = mxml_GetInt(stats, SAVE_STATS_UFOSSTORED, 0);
00216     /* fallback for savegame compatibility */
00217     if (ccs.campaignStats.ufosStored == 0)
00218         ccs.campaignStats.ufosStored = US_StoredUFOCount();
00219     ccs.campaignStats.aircraftHad = mxml_GetInt(stats, SAVE_STATS_AIRCRAFTHAD, 0);
00220     /* fallback for savegame compatibility */
00221     if (ccs.campaignStats.aircraftHad == 0)
00222         ccs.campaignStats.aircraftHad = ccs.numAircraft;
00223     /* freeing the memory below this node */
00224     mxmlDelete(stats);
00225     return qtrue;
00226 }
00227 
00228 
00229 #ifdef DEBUG
00230 
00233 static void CP_CampaignStats_f (void)
00234 {
00235     campaign_t *campaign = ccs.curCampaign;
00236 
00237     if (!CP_IsRunning()) {
00238         Com_Printf("No campaign active\n");
00239         return;
00240     }
00241 
00242     Com_Printf("Campaign id: %s\n", campaign->id);
00243     Com_Printf("..research list: %s\n", campaign->researched);
00244     Com_Printf("..equipment: %s\n", campaign->equipment);
00245     Com_Printf("..team: %i\n", campaign->team);
00246 
00247     Com_Printf("..salaries:\n");
00248     Com_Printf("...soldier_base: %i\n", CP_GetSalaryBaseEmployee(EMPL_SOLDIER));
00249     Com_Printf("...soldier_rankbonus: %i\n", CP_GetSalaryRankBonusEmployee(EMPL_SOLDIER));
00250     Com_Printf("...worker_base: %i\n", CP_GetSalaryBaseEmployee(EMPL_WORKER));
00251     Com_Printf("...worker_rankbonus: %i\n", CP_GetSalaryRankBonusEmployee(EMPL_WORKER));
00252     Com_Printf("...scientist_base: %i\n", CP_GetSalaryBaseEmployee(EMPL_SCIENTIST));
00253     Com_Printf("...scientist_rankbonus: %i\n", CP_GetSalaryRankBonusEmployee(EMPL_SCIENTIST));
00254     Com_Printf("...pilot_base: %i\n", CP_GetSalaryBaseEmployee(EMPL_PILOT));
00255     Com_Printf("...pilot_rankbonus: %i\n", CP_GetSalaryRankBonusEmployee(EMPL_PILOT));
00256     Com_Printf("...robot_base: %i\n", CP_GetSalaryBaseEmployee(EMPL_ROBOT));
00257     Com_Printf("...robot_rankbonus: %i\n", CP_GetSalaryRankBonusEmployee(EMPL_ROBOT));
00258     Com_Printf("...aircraft_factor: %i\n", SALARY_AIRCRAFT_FACTOR);
00259     Com_Printf("...aircraft_divisor: %i\n", SALARY_AIRCRAFT_DIVISOR);
00260     Com_Printf("...base_upkeep: %i\n", SALARY_BASE_UPKEEP);
00261     Com_Printf("...admin_initial: %i\n", SALARY_ADMIN_INITIAL);
00262     Com_Printf("...admin_soldier: %i\n", CP_GetSalaryAdminEmployee(EMPL_SOLDIER));
00263     Com_Printf("...admin_worker: %i\n", CP_GetSalaryAdminEmployee(EMPL_WORKER));
00264     Com_Printf("...admin_scientist: %i\n", CP_GetSalaryAdminEmployee(EMPL_SCIENTIST));
00265     Com_Printf("...admin_pilot: %i\n", CP_GetSalaryAdminEmployee(EMPL_PILOT));
00266     Com_Printf("...admin_robot: %i\n", CP_GetSalaryAdminEmployee(EMPL_ROBOT));
00267     Com_Printf("...debt_interest: %.5f\n", SALARY_DEBT_INTEREST);
00268 }
00269 #endif /* DEBUG */
00270 
00271 void STATS_InitStartup (void)
00272 {
00273 #ifdef DEBUG
00274     Cmd_AddCommand("debug_listcampaign", CP_CampaignStats_f, "Print campaign stats to game console");
00275 #endif
00276 }

Generated by  doxygen 1.6.2