cp_team_callbacks.c

Go to the documentation of this file.
00001 
00006 /*
00007 All original material 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 "../cl_team.h"
00028 #include "../ui/ui_main.h"
00029 #include "../ui/node/ui_node_container.h"   
00031 #include "cp_campaign.h"
00032 #include "cp_team.h"
00033 #include "cp_team_callbacks.h"
00034 
00035 linkedList_t *employeeList; 
00036 int employeesInCurrentList;
00037 
00038 /***********************************************************
00039  * Bindings
00040  ***********************************************************/
00041 
00042 /* cache soldier list */
00043 static int soldierListSize = 0;
00044 static int soldierListPos = 0;
00045 
00046 static qboolean CL_UpdateEmployeeList (employeeType_t employeeType, char *nodeTag, int beginIndex, int drawableListSize)
00047 {
00048     aircraft_t *aircraft;
00049     linkedList_t *emplList;
00050     int id;
00051     base_t *base = B_GetCurrentSelectedBase();
00052     const int selected = Cvar_GetInteger("cl_selected");
00053 
00054     /* Check if we are allowed to be here.
00055      * We are only allowed to be here if we already set up a base. */
00056     if (!base) {
00057         Com_Printf("No base set up\n");
00058         return qfalse;
00059     }
00060 
00061     aircraft = base->aircraftCurrent;
00062     if (!aircraft) {
00063         return qfalse;
00064     }
00065 
00066     CL_UpdateActorAircraftVar(aircraft, employeeType);
00067 
00068     soldierListSize = drawableListSize;
00069     soldierListPos = beginIndex;
00070 
00071     /* Populate employeeList */
00072     employeesInCurrentList = E_GetHiredEmployees(aircraft->homebase, employeeType, &employeeList);
00073     emplList = employeeList;
00074     id = 0;
00075     while (emplList) {
00076         const employee_t *employee = (employee_t*)emplList->data;
00077         qboolean alreadyInOtherShip;
00078         const aircraft_t *otherShip;
00079         const int guiId = id - beginIndex;
00080         containerIndex_t container;
00081 
00082         assert(E_IsHired(employee));
00083         assert(!employee->transfer);
00084 
00085         /* id lower than viewable item */
00086         if (guiId < 0) {
00087             emplList = emplList->next;
00088             id++;
00089             continue;
00090         }
00091         /* id bigger than viewable item */
00092         if (guiId >= drawableListSize) {
00093             emplList = emplList->next;
00094             id++;
00095             continue;
00096         }
00097 
00098         /* Set name of the employee. */
00099         Cvar_Set(va("mn_ename%i", guiId), employee->chr.name);
00100 
00101         /* Search all aircraft except the current one. */
00102         otherShip = AIR_IsEmployeeInAircraft(employee, NULL);
00103         alreadyInOtherShip = (otherShip != NULL) && (otherShip != aircraft);
00104 
00105         /* Update status */
00106         if (alreadyInOtherShip)
00107             UI_ExecuteConfunc("aircraft_%s_usedelsewhere %i", nodeTag, guiId);
00108         else if (AIR_IsEmployeeInAircraft(employee, aircraft))
00109             UI_ExecuteConfunc("aircraft_%s_assigned %i", nodeTag, guiId);
00110         else
00111             UI_ExecuteConfunc("aircraft_%s_unassigned %i", nodeTag, guiId);
00112 
00113         /* Check if the employee has something equipped. */
00114         for (container = 0; container < csi.numIDs; container++) {
00115             if (!INVDEF(container)->temp && CONTAINER(&employee->chr, container))
00116                 break;
00117         }
00118         if (container < csi.numIDs)
00119             UI_ExecuteConfunc("aircraft_%s_holdsequip %i", nodeTag, guiId);
00120         else
00121             UI_ExecuteConfunc("aircraft_%s_holdsnoequip %i", nodeTag, guiId);
00122 
00123         if (selected == id)
00124             UI_ExecuteConfunc("aircraft_%s_selected %i", nodeTag, guiId);
00125 
00126         emplList = emplList->next;
00127         id++;
00128     }
00129 
00130     UI_ExecuteConfunc("aircraft_%s_list_size %i", nodeTag, id);
00131 
00132     for (; id - beginIndex < drawableListSize; id++) {
00133         const int guiId = id - beginIndex;
00134         UI_ExecuteConfunc("aircraft_%s_unusedslot %i", nodeTag, guiId);
00135         Cvar_Set(va("mn_name%i", guiId), "");
00136     }
00137 
00138     return qtrue;
00139 }
00140 
00147 static void CL_UpdateSoldierList_f (void)
00148 {
00149     qboolean result;
00150     int drawableListSize;
00151     int beginIndex;
00152 
00153     if (Cmd_Argc() != 3) {
00154         Com_Printf("Usage: %s <drawableSize> <firstIndex>\n", Cmd_Argv(0));
00155         return;
00156     }
00157     drawableListSize = atoi(Cmd_Argv(1));
00158     beginIndex = atoi(Cmd_Argv(2));
00159 
00160     result = CL_UpdateEmployeeList(EMPL_SOLDIER, "soldier", beginIndex, drawableListSize);
00161     if (!result)
00162         UI_PopWindow(qfalse);
00163 }
00164 
00171 static void CL_UpdatePilotList_f (void)
00172 {
00173     qboolean result;
00174     int drawableListSize;
00175     int beginIndex;
00176     if (Cmd_Argc() != 3) {
00177         Com_Printf("Usage: %s <drawableSize> <firstIndex>\n", Cmd_Argv(0));
00178         return;
00179     }
00180     drawableListSize = atoi(Cmd_Argv(1));
00181     beginIndex = atoi(Cmd_Argv(2));
00182 
00183     result = CL_UpdateEmployeeList(EMPL_PILOT, "pilot", beginIndex, drawableListSize);
00184     if (!result)
00185         UI_PopWindow(qfalse);
00186 }
00187 
00194 static void CL_UpdateEquipmentMenuParameters_f (void)
00195 {
00196     equipDef_t unused;
00197     int p;
00198     aircraft_t *aircraft, *aircraftInBase;
00199     base_t *base = B_GetCurrentSelectedBase();
00200     size_t size;
00201 
00202     if (!base)
00203         return;
00204 
00205     aircraft = base->aircraftCurrent;
00206     if (!aircraft)
00207         return;
00208 
00209     /* no soldiers are assigned to the current aircraft. */
00210     if (AIR_GetTeamSize(aircraft) == 0) {
00211         UI_PopWindow(qfalse);
00212         return;
00213     }
00214 
00215     Cvar_ForceSet("cl_selected", "0");
00216 
00218     p = CL_UpdateActorAircraftVar(aircraft, EMPL_SOLDIER);
00219     if (p > 0)
00220         ui_inventory = &chrDisplayList.chr[0]->i;
00221     else
00222         ui_inventory = NULL;
00223 
00224     size = MAX_ACTIVETEAM;
00225     for (; p < size; p++)
00226         UI_ExecuteConfunc("equipdisable %i", p);
00227 
00228     /* manage inventory */
00229     unused = aircraft->homebase->storage; /* copied, including arrays inside! */
00230 
00231     /* clean up aircraft crew for upcoming mission */
00232     CL_CleanTempInventory(aircraft->homebase);
00233 
00234     aircraftInBase = NULL;
00235     while ((aircraftInBase = AIR_GetNextFromBase(aircraft->homebase, aircraftInBase)) != NULL)
00236         CL_CleanupAircraftCrew(aircraftInBase, &unused);
00237 
00238     UI_ContainerNodeUpdateEquipment(&aircraft->homebase->bEquipment, &unused);
00239 }
00240 
00245 static void CL_AssignPilot_f (void)
00246 {
00247     employee_t *employee;
00248     base_t *base = B_GetCurrentSelectedBase();
00249     aircraft_t *aircraft;
00250     int relativeId = 0;
00251     int num;
00252     const employeeType_t employeeType = EMPL_PILOT;
00253 
00254     /* check syntax */
00255     if (Cmd_Argc() < 2 || Cmd_Argc() > 3) {
00256         Com_Printf("Usage: %s <num> <relative_id>\n", Cmd_Argv(0));
00257         return;
00258     }
00259 
00260     if (Cmd_Argc() == 3)
00261         relativeId = atoi(Cmd_Argv(2));
00262 
00263     num = atoi(Cmd_Argv(1)) + relativeId;
00264     if (num >= E_CountHired(base, employeeType))
00265         return;
00266 
00267     /* In case we didn't populate the list with E_GenerateHiredEmployeesList before. */
00268     if (!employeeList)
00269         return;
00270 
00271     employee = E_GetEmployeeByMenuIndex(num);
00272     if (!employee)
00273         Com_Error(ERR_DROP, "CL_AssignPilot_f: No employee at list-pos %i (base: %i)", num, base->idx);
00274 
00275     aircraft = base->aircraftCurrent;
00276     if (!aircraft)
00277         return;
00278 
00279     if (aircraft->pilot == NULL) {
00280         aircraft->pilot = employee;
00281     } else if (aircraft->pilot == employee) {
00282         aircraft->pilot = NULL;
00283     }
00284 
00285     CL_UpdateActorAircraftVar(aircraft, employeeType);
00286 
00287     UI_ExecuteConfunc("aircraft_status_change");
00288     Cmd_ExecuteString(va("pilot_select %i %i", num - relativeId, relativeId));
00289 }
00290 
00295 static void CL_AssignSoldier_f (void)
00296 {
00297     base_t *base = B_GetCurrentSelectedBase();
00298     aircraft_t *aircraft;
00299     int relativeId = 0;
00300     int num;
00301     const employeeType_t employeeType = EMPL_SOLDIER;
00302 
00303     /* check syntax */
00304     if (Cmd_Argc() < 2 || Cmd_Argc() > 3) {
00305         Com_Printf("Usage: %s <num> <relative_id>\n", Cmd_Argv(0));
00306         return;
00307     }
00308 
00309     if (Cmd_Argc() == 3)
00310         relativeId = atoi(Cmd_Argv(2));
00311 
00312     num = atoi(Cmd_Argv(1)) + relativeId;
00313     if (num >= E_CountHired(base, employeeType))
00314         return;
00315 
00316     /* In case we didn't populate the list with E_GenerateHiredEmployeesList before. */
00317     if (!employeeList)
00318         return;
00319 
00320     aircraft = base->aircraftCurrent;
00321     if (!aircraft)
00322         return;
00323 
00324     AIM_AddEmployeeFromMenu(aircraft, num);
00325     CL_UpdateActorAircraftVar(aircraft, employeeType);
00326 
00327     UI_ExecuteConfunc("aircraft_status_change");
00328     Cbuf_AddText(va("team_select %i %i\n", num - relativeId, relativeId));
00329 }
00330 
00331 static void CL_ActorPilotSelect_f (void)
00332 {
00333     employee_t *employee;
00334     character_t *chr;
00335     int num;
00336     int relativeId = 0;
00337     const employeeType_t employeeType = EMPL_PILOT;
00338     base_t *base = B_GetCurrentSelectedBase();
00339 
00340     if (!base)
00341         return;
00342 
00343     /* check syntax */
00344     if (Cmd_Argc() < 2 || Cmd_Argc() > 3) {
00345         Com_Printf("Usage: %s <num> <relative_id>\n", Cmd_Argv(0));
00346         return;
00347     }
00348 
00349     if (Cmd_Argc() == 3)
00350         relativeId = atoi(Cmd_Argv(2));
00351 
00352     num = atoi(Cmd_Argv(1)) + relativeId;
00353     if (num >= E_CountHired(base, employeeType)) {
00354         UI_ExecuteConfunc("reset_character_cvars");
00355         return;
00356     }
00357 
00358     employee = E_GetEmployeeByMenuIndex(num);
00359     if (!employee)
00360         Com_Error(ERR_DROP, "CL_ActorPilotSelect_f: No employee at list-pos %i (base: %i)", num, base->idx);
00361 
00362     chr = &employee->chr;
00363 
00364     /* now set the cl_selected cvar to the new actor id */
00365     Cvar_ForceSet("cl_selected", va("%i", num));
00366 
00367     /* set info cvars */
00368     CL_UpdateCharacterValues(chr, "mn_");
00369     UI_ExecuteConfunc("update_pilot_list %i %i", soldierListSize, soldierListPos);
00370 }
00371 
00372 static void CL_ActorTeamSelect_f (void)
00373 {
00374     employee_t *employee;
00375     character_t *chr;
00376     int num;
00377     int relativeId = 0;
00378     base_t *base = B_GetCurrentSelectedBase();
00379 
00380     if (!base)
00381         return;
00382 
00383     /* check syntax */
00384     if (Cmd_Argc() < 2 || Cmd_Argc() > 3) {
00385         Com_Printf("Usage: %s <num> <relative_id>\n", Cmd_Argv(0));
00386         return;
00387     }
00388 
00389     if (Cmd_Argc() == 3)
00390         relativeId = atoi(Cmd_Argv(2));
00391 
00392     num = atoi(Cmd_Argv(1)) + relativeId;
00393     if (num >= E_CountHired(base, EMPL_SOLDIER)) {
00394         UI_ExecuteConfunc("reset_character_cvars");
00395         return;
00396     }
00397 
00398     employee = E_GetEmployeeByMenuIndex(num);
00399     if (!employee)
00400         Com_Error(ERR_DROP, "CL_ActorTeamSelect_f: No employee at list-pos %i (base: %i)", num, base->idx);
00401 
00402     chr = &employee->chr;
00403 
00404     /* now set the cl_selected cvar to the new actor id */
00405     Cvar_ForceSet("cl_selected", va("%i", num));
00406 
00407     /* set info cvars */
00408     CL_UpdateCharacterValues(chr, "mn_");
00409     UI_ExecuteConfunc("update_soldier_list %i %i", soldierListSize, soldierListPos);
00410 }
00411 
00412 #ifdef DEBUG
00413 static void CL_TeamListDebug_f (void)
00414 {
00415     base_t *base;
00416     aircraft_t *aircraft;
00417     linkedList_t *l;
00418 
00419     aircraft = ccs.missionAircraft;
00420     base = aircraft->homebase;
00421 
00422     if (!base) {
00423         Com_Printf("Build and select a base first\n");
00424         return;
00425     }
00426 
00427     if (!aircraft) {
00428         Com_Printf("Buy/build an aircraft first.\n");
00429         return;
00430     }
00431 
00432     Com_Printf("%i members in the current team", AIR_GetTeamSize(aircraft));
00433     for (l = aircraft->acTeam; l != NULL; l = l->next) {
00434         const employee_t *employee = (const employee_t *)l->data;
00435         const character_t *chr = &employee->chr;
00436         Com_Printf("ucn %i - employee->idx: %i\n", chr->ucn, employee->idx);
00437     }
00438 }
00439 #endif
00440 
00441 void CP_TEAM_InitCallbacks (void)
00442 {
00443     Cmd_AddCommand("team_updateequip", CL_UpdateEquipmentMenuParameters_f, NULL);
00444     Cmd_AddCommand("update_soldier_list", CL_UpdateSoldierList_f, NULL);
00445     Cmd_AddCommand("update_pilot_list", CL_UpdatePilotList_f, NULL);
00446 
00447     Cmd_AddCommand("team_hire", CL_AssignSoldier_f, "Add/remove already hired actor to the aircraft");
00448     Cmd_AddCommand("pilot_hire", CL_AssignPilot_f, "Add/remove already hired pilot to an aircraft");
00449     Cmd_AddCommand("team_select", CL_ActorTeamSelect_f, "Select a soldier in the team creation menu");
00450     Cmd_AddCommand("pilot_select", CL_ActorPilotSelect_f, "Select a pilot in the team creation menu");
00451 #ifdef DEBUG
00452     Cmd_AddCommand("debug_teamlist", CL_TeamListDebug_f, "Debug function to show all hired and assigned teammembers");
00453 #endif
00454 
00455     soldierListPos = 0;
00456     soldierListSize = 0;
00457 }
00458 
00459 void CP_TEAM_ShutdownCallbacks (void)
00460 {
00461     Cmd_RemoveCommand("team_updateequip");
00462     Cmd_RemoveCommand("update_soldier_list");
00463     Cmd_RemoveCommand("update_pilot_list");
00464     Cmd_RemoveCommand("team_hire");
00465     Cmd_RemoveCommand("pilot_hire");
00466     Cmd_RemoveCommand("team_select");
00467     Cmd_RemoveCommand("pilot_select");
00468 #ifdef DEBUG
00469     Cmd_RemoveCommand("debug_teamlist");
00470 #endif
00471 }

Generated by  doxygen 1.6.2