cp_employee_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 #include "../cl_shared.h"
00026 #include "../cl_team.h" /* CL_UpdateCharacterValues */
00027 #include "../ui/ui_main.h"
00028 #include "../ui/ui_data.h"
00029 #include "../ui/ui_draw.h"
00030 #include "cp_campaign.h"
00031 #include "cp_employee_callbacks.h"
00032 #include "cp_employee.h"
00033 
00034 
00036 static employee_t *selectedEmployee = NULL;
00037 /* Holds the current active employee category */
00038 static int employeeCategory = 0;
00039 
00040 static const int maxEmployeesPerPage = 15;
00041 
00042 static int employeeScrollPos = 0;
00043 
00044 /* List of (hired) employees in the current category (employeeType). */
00045 linkedList_t *employeeList; 
00047 /* How many employees in current list (changes on every catergory change, too) */
00048 int employeesInCurrentList;
00049 
00053 static void E_UpdateGUICount_f (void)
00054 {
00055     int max;
00056     base_t *base = B_GetCurrentSelectedBase();
00057 
00058     if (!base)
00059         return;
00060 
00061     max = base->capacities[CAP_EMPLOYEES].max;
00062     Cvar_SetValue("mn_hiresoldiers", E_CountHired(base, EMPL_SOLDIER));
00063     Cvar_SetValue("mn_hireworkers", E_CountHired(base, EMPL_WORKER));
00064     Cvar_SetValue("mn_hirescientists", E_CountHired(base, EMPL_SCIENTIST));
00065     Cvar_SetValue("mn_hirepilots", E_CountHired(base, EMPL_PILOT));
00066     Cvar_Set("mn_hirepeople", va("%d/%d", E_CountAllHired(base), max));
00067 }
00068 
00069 static void E_EmployeeSelect (employee_t *employee)
00070 {
00071     const base_t *base = B_GetCurrentSelectedBase();
00072     if (!base)
00073         return;
00074 
00075     selectedEmployee = employee;
00076     if (selectedEmployee) {
00077         const character_t *chr = &selectedEmployee->chr;
00078         /* mn_employee_hired is needed to allow renaming */
00079         Cvar_SetValue("mn_employee_hired", E_IsHired(selectedEmployee) ? 1 : 0);
00080         Cvar_SetValue("mn_ucn", chr->ucn);
00081 
00082         /* set info cvars */
00083         CL_UpdateCharacterValues(chr, "mn_");
00084     }
00085 }
00086 
00091 static void E_EmployeeListScroll_f (void)
00092 {
00093     int j, cnt = 0;
00094     employee_t* employee;
00095     base_t *base = B_GetCurrentSelectedBase();
00096 
00097     if (!base)
00098         return;
00099 
00100     if (Cmd_Argc() < 2)
00101         return;
00102 
00103     employeeScrollPos = atoi(Cmd_Argv(1));
00104     j = employeeScrollPos;
00105 
00106     employee = NULL;
00107     while ((employee = E_GetNext(employeeCategory, employee))) {
00108         /* don't show employees of other bases */
00109         if (E_IsHired(employee) && !E_IsInBase(employee, base))
00110             continue;
00111 
00112         /* drop the first j entries */
00113         if (j) {
00114             j--;
00115             continue;
00116         }
00117         /* change the buttons */
00118         if (E_IsHired(employee))
00119             UI_ExecuteConfunc("employeeadd %i", cnt);
00120         else
00121             UI_ExecuteConfunc("employeedel %i", cnt);
00122 
00123         cnt++;
00124 
00125         /* only 19 buttons */
00126         if (cnt >= maxEmployeesPerPage)
00127             break;
00128     }
00129 
00130     for (;cnt < maxEmployeesPerPage; cnt++) {
00131         Cvar_Set(va("mn_name%i", cnt), "");
00132         UI_ExecuteConfunc("employeedisable %i", cnt);
00133     }
00134 
00135     UI_ExecuteConfunc("hire_fix_scroll %i", employeeScrollPos);
00136 }
00137 
00142 static void E_EmployeeList_f (void)
00143 {
00144     employee_t* employee;
00145     int hiredEmployeeIdx;
00146     linkedList_t *employeeListName;
00147     base_t *base = B_GetCurrentSelectedBase();
00148 
00149     if (!base)
00150         return;
00151 
00152     if (Cmd_Argc() < 2) {
00153         Com_Printf("Usage: %s <category> <employeeid>\n", Cmd_Argv(0));
00154         return;
00155     }
00156 
00157     employeeCategory = atoi(Cmd_Argv(1));
00158     if (employeeCategory >= MAX_EMPL || employeeCategory < 0)
00159         employeeCategory = EMPL_SOLDIER;
00160 
00161     if (Cmd_Argc() == 3)
00162         hiredEmployeeIdx = atoi(Cmd_Argv(2));
00163     else
00164         hiredEmployeeIdx = -1;
00165 
00166     /* reset the employee count */
00167     employeesInCurrentList = 0;
00168 
00169     LIST_Delete(&employeeList);
00170     /* make sure, that we are using the linked list */
00171     UI_ResetData(TEXT_LIST);
00172     employeeListName = NULL;
00173 
00174     employee = NULL;
00175     while ((employee = E_GetNext(employeeCategory, employee))) {
00176         /* don't show employees of other bases */
00177         if (E_IsHired(employee) && !E_IsInBase(employee, base))
00178             continue;
00179         LIST_AddPointer(&employeeListName, employee->chr.name);
00180         LIST_AddPointer(&employeeList, employee);
00181         employeesInCurrentList++;
00182     }
00183     UI_RegisterLinkedListText(TEXT_LIST, employeeListName);
00184 
00185     /* If the list is empty OR we are in pilots/scientists/workers-mode: don't show the model&stats. */
00192     if (employeesInCurrentList == 0) {
00193         Cvar_Set("mn_show_employee", "0");
00194     } else {
00195         if (employeeCategory == EMPL_PILOT || employeeCategory == EMPL_SCIENTIST || employeeCategory == EMPL_WORKER)
00196             Cvar_Set("mn_show_employee", "2");
00197         else
00198             Cvar_Set("mn_show_employee", "1");
00199     }
00200     /* Select the current employee if name was changed or first one. Use the direct string
00201      * execution here - otherwise the employeeCategory might be out of sync */
00202     if (hiredEmployeeIdx < 0 || selectedEmployee == NULL)
00203         employee = E_GetEmployeeByMenuIndex(0);
00204     else
00205         employee = selectedEmployee;
00206 
00207     E_EmployeeSelect(employee);
00208 
00209     /* update scroll */
00210     UI_ExecuteConfunc("hire_update_number %i", employeesInCurrentList);
00211     UI_ExecuteConfunc("employee_scroll 0");
00212 }
00213 
00214 
00219 static void E_ChangeName_f (void)
00220 {
00221     employee_t *employee = E_GetEmployeeFromChrUCN(Cvar_GetInteger("mn_ucn"));
00222     if (employee)
00223         Q_strncpyz(employee->chr.name, Cvar_GetString("mn_name"), sizeof(employee->chr.name));
00224 }
00225 
00231 int E_GenerateHiredEmployeesList (const base_t *base)
00232 {
00233     assert(base);
00234     employeesInCurrentList = E_GetHiredEmployees(base, EMPL_SOLDIER, &employeeList);
00235     return employeesInCurrentList;
00236 }
00237 
00238 
00243 employee_t* E_GetEmployeeByMenuIndex (int num)
00244 {
00245     return (employee_t*)LIST_GetByIdx(employeeList, num);
00246 }
00247 
00248 
00253 static void E_EmployeeDelete_f (void)
00254 {
00255     /* num - menu index (line in text) */
00256     int num;
00257     employee_t* employee;
00258 
00259     /* Check syntax. */
00260     if (Cmd_Argc() < 2) {
00261         Com_Printf("Usage: %s <num>\n", Cmd_Argv(0));
00262         return;
00263     }
00264 
00265     num = employeeScrollPos + atoi(Cmd_Argv(1));
00266 
00267     employee = E_GetEmployeeByMenuIndex(num);
00268     /* empty slot selected */
00269     if (!employee)
00270         return;
00271 
00272     if (E_IsHired(employee)) {
00273         if (!E_UnhireEmployee(employee)) {
00274             UI_DisplayNotice(_("Could not fire employee"), 2000, "employees");
00275             Com_DPrintf(DEBUG_CLIENT, "Couldn't fire employee\n");
00276             return;
00277         }
00278     }
00279     E_DeleteEmployee(employee, employee->type);
00280     Cbuf_AddText(va("employee_init %i\n", employeeCategory));
00281 }
00282 
00288 static void E_EmployeeHire_f (void)
00289 {
00290     /* num - menu index (line in text), button - number of button */
00291     int num, button;
00292     const char *arg;
00293     employee_t* employee;
00294     base_t *base = B_GetCurrentSelectedBase();
00295 
00296     if (!base)
00297         return;
00298 
00299     /* Check syntax. */
00300     if (Cmd_Argc() < 2) {
00301         Com_Printf("Usage: %s <+num>\n", Cmd_Argv(0));
00302         return;
00303     }
00304 
00305     arg = Cmd_Argv(1);
00306 
00307     /* check whether this is called with the text node click function
00308      * with values from 0 - #available employees (bigger values than
00309      * maxEmployeesPerPage) possible ... */
00310     if (arg[0] == '+') {
00311         num = atoi(arg + 1);
00312         button = num - employeeScrollPos;
00313     /* ... or with the hire pictures that are using only values from
00314      * 0 - maxEmployeesPerPage */
00315     } else {
00316         button = atoi(Cmd_Argv(1));
00317         num = button + employeeScrollPos;
00318     }
00319 
00320     employee = E_GetEmployeeByMenuIndex(num);
00321     /* empty slot selected */
00322     if (!employee)
00323         return;
00324 
00325     if (E_IsHired(employee)) {
00326         if (!E_UnhireEmployee(employee)) {
00327             Com_DPrintf(DEBUG_CLIENT, "Couldn't fire employee\n");
00328             UI_DisplayNotice(_("Could not fire employee"), 2000, "employees");
00329         } else
00330             UI_ExecuteConfunc("employeedel %i", button);
00331     } else {
00332         if (!E_HireEmployee(base, employee)) {
00333             Com_DPrintf(DEBUG_CLIENT, "Couldn't hire employee\n");
00334             UI_DisplayNotice(_("Could not hire employee"), 2000, "employees");
00335             UI_ExecuteConfunc("employeedel %i", button);
00336         } else
00337             UI_ExecuteConfunc("employeeadd %i", button);
00338     }
00339     E_EmployeeSelect(employee);
00340 
00341     E_UpdateGUICount_f();
00342 }
00343 
00347 static void E_EmployeeSelect_f (void)
00348 {
00349     int num;
00350 
00351     /* Check syntax. */
00352     if (Cmd_Argc() < 2) {
00353         Com_Printf("Usage: %s <num>\n", Cmd_Argv(0));
00354         return;
00355     }
00356 
00357     num = atoi(Cmd_Argv(1));
00358     if (num < 0 || num >= employeesInCurrentList)
00359         return;
00360 
00361     E_EmployeeSelect(E_GetEmployeeByMenuIndex(num));
00362 }
00363 
00364 void E_InitCallbacks (void)
00365 {
00366     Cmd_AddCommand("employee_update_count", E_UpdateGUICount_f, "Callback to update the employee count of the current GUI");
00367 
00368     /* add commands */
00369     Cmd_AddCommand("employee_init", E_EmployeeList_f, "Init function for employee hire menu");
00370     Cmd_AddCommand("employee_delete", E_EmployeeDelete_f, "Removed an employee from the global employee list");
00371     Cmd_AddCommand("employee_hire", E_EmployeeHire_f, NULL);
00372     Cmd_AddCommand("employee_select", E_EmployeeSelect_f, NULL);
00373     Cmd_AddCommand("employee_changename", E_ChangeName_f, "Change the name of an employee");
00374     Cmd_AddCommand("employee_scroll", E_EmployeeListScroll_f, "Scroll callback for employee list");
00375 }
00376 
00377 void E_ShutdownCallbacks (void)
00378 {
00379     Cmd_RemoveCommand("employee_update_count");
00380     Cmd_RemoveCommand("employee_init");
00381     Cmd_RemoveCommand("employee_delete");
00382     Cmd_RemoveCommand("employee_hire");
00383     Cmd_RemoveCommand("employee_select");
00384     Cmd_RemoveCommand("employee_changename");
00385     Cmd_RemoveCommand("employee_scroll");
00386 }

Generated by  doxygen 1.6.2