cp_aircraft_callbacks.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_main.h"
00028 #include "../ui/ui_popup.h"
00029 #include "cp_campaign.h"
00030 #include "cp_map.h"
00031 #include "cp_aircraft_callbacks.h"
00032 #include "cp_aircraft.h"
00033 #include "cp_team.h"
00034 #include "cp_mapfightequip.h"
00035 
00043 static void AIM_AircraftReturnToBase_f (void)
00044 {
00045     base_t *base = B_GetCurrentSelectedBase();
00046 
00047     if (base && base->aircraftCurrent) {
00048         AIR_AircraftReturnToBase(base->aircraftCurrent);
00049         AIR_AircraftSelect(base->aircraftCurrent);
00050     }
00051 }
00052 
00059 static void AIM_SelectAircraft_f (void)
00060 {
00061     base_t *base = B_GetCurrentSelectedBase();
00062 
00063     if (!base)
00064         return;
00065 
00066     if (Cmd_Argc() < 2) {
00067         if (base->aircraftCurrent)
00068             AIR_AircraftSelect(base->aircraftCurrent);
00069         return;
00070     } else {
00071         const int i = atoi(Cmd_Argv(1));
00072         aircraft_t *aircraft = AIR_GetAircraftFromBaseByIDXSafe(base, i);
00073         if (aircraft != NULL)
00074             AIR_AircraftSelect(aircraft);
00075     }
00076 }
00077 
00081 static void AIM_AircraftStart_f (void)
00082 {
00083     aircraft_t *aircraft;
00084     base_t *base = B_GetCurrentSelectedBase();
00085 
00086     if (!base)
00087         return;
00088 
00089     if (!base->aircraftCurrent) {
00090         Com_DPrintf(DEBUG_CLIENT, "Error - there is no current aircraft in this base\n");
00091         return;
00092     }
00093 
00094     /* Aircraft cannot start without Command Centre operational. */
00095     if (!B_GetBuildingStatus(base, B_COMMAND)) {
00096         UI_Popup(_("Notice"), _("No operational Command Centre in this base.\n\nAircraft can not start.\n"));
00097         return;
00098     }
00099 
00100     aircraft = base->aircraftCurrent;
00101 
00102     /* Aircraft cannot start without a pilot. */
00103     if (!aircraft->pilot) {
00104         UI_Popup(_("Notice"), _("There is no pilot assigned to this aircraft.\n\nAircraft can not start.\n"));
00105         return;
00106     }
00107 
00108     if (AIR_IsAircraftInBase(aircraft)) {
00109         /* reload its ammunition */
00110         AII_ReloadAircraftWeapons(aircraft);
00111     }
00112     MS_AddNewMessage(_("Notice"), _("Aircraft started"), qfalse, MSG_STANDARD, NULL);
00113     aircraft->status = AIR_IDLE;
00114 
00115     MAP_SelectAircraft(aircraft);
00116     /* Return to geoscape. */
00117     UI_PopWindow(qfalse);
00118     UI_PopWindow(qfalse);
00119 }
00120 
00121 #define SOLDIER_EQUIP_MENU_BUTTON_NO_AIRCRAFT_IN_BASE 1
00122 #define SOLDIER_EQUIP_MENU_BUTTON_NO_SOLDIES_AVAILABLE 2
00123 #define SOLDIER_EQUIP_MENU_BUTTON_OK 3
00124 
00130 static int CL_EquipSoldierState (const aircraft_t * aircraft)
00131 {
00132     if (!AIR_IsAircraftInBase(aircraft)) {
00133         return SOLDIER_EQUIP_MENU_BUTTON_NO_AIRCRAFT_IN_BASE;
00134     } else {
00135         if (E_CountHired(aircraft->homebase, EMPL_SOLDIER) <= 0)
00136             return SOLDIER_EQUIP_MENU_BUTTON_NO_SOLDIES_AVAILABLE;
00137         else
00138             return SOLDIER_EQUIP_MENU_BUTTON_OK;
00139     }
00140 }
00141 
00148 static int AIR_GetSlotItems (aircraftItemType_t type, const aircraft_t *aircraft)
00149 {
00150     int i, max, cnt = 0;
00151     const aircraftSlot_t *slot;
00152 
00153     assert(aircraft);
00154 
00155     switch (type) {
00156     case AC_ITEM_SHIELD:
00157         if (aircraft->shield.item)
00158             return 1;
00159         else
00160             return 0;
00161         break;
00162     case AC_ITEM_WEAPON:
00163         slot = aircraft->weapons;
00164         max = MAX_AIRCRAFTSLOT;
00165         break;
00166     case AC_ITEM_ELECTRONICS:
00167         slot = aircraft->electronics;
00168         max = MAX_AIRCRAFTSLOT;
00169         break;
00170     default:
00171         Com_Printf("AIR_GetSlotItems: Unknown type of slot : %i", type);
00172         return 0;
00173     }
00174 
00175     for (i = 0; i < max; i++)
00176         if (slot[i].item)
00177             cnt++;
00178 
00179     return cnt;
00180 }
00181 
00186 void AIR_AircraftSelect (aircraft_t* aircraft)
00187 {
00188     static char aircraftInfo[256];
00189     base_t *base;
00190     aircraft_t *aircraftInBase;
00191     int id;
00192 
00193     if (aircraft != NULL)
00194         base = aircraft->homebase;
00195     else
00196         base = NULL;
00197 
00198     if (!AIR_BaseHasAircraft(base)) {
00199         UI_ResetData(TEXT_AIRCRAFT_INFO);
00200         return;
00201     }
00202 
00203     assert(aircraft);
00204     assert(aircraft->homebase == base);
00205     CL_UpdateActorAircraftVar(aircraft, EMPL_SOLDIER);
00206 
00207     Cvar_SetValue("mn_equipsoldierstate", CL_EquipSoldierState(aircraft));
00208     Cvar_Set("mn_aircraftstatus", AIR_AircraftStatusToName(aircraft));
00209     Cvar_Set("mn_aircraftinbase", AIR_IsAircraftInBase(aircraft) ? "1" : "0");
00210     Cvar_Set("mn_aircraftname", aircraft->name);
00211     if (!aircraft->tech)
00212         Com_Error(ERR_DROP, "No technology assigned to aircraft '%s'", aircraft->id);
00213     Cvar_Set("mn_aircraft_model", aircraft->tech->mdl);
00214 
00215     /* generate aircraft info text */
00216     Com_sprintf(aircraftInfo, sizeof(aircraftInfo), _("Speed:\t%i km/h\n"),
00217         CL_AircraftMenuStatsValues(aircraft->stats[AIR_STATS_SPEED], AIR_STATS_SPEED));
00218     Q_strcat(aircraftInfo, va(_("Fuel:\t%i/%i\n"), CL_AircraftMenuStatsValues(aircraft->fuel, AIR_STATS_FUELSIZE),
00219         CL_AircraftMenuStatsValues(aircraft->stats[AIR_STATS_FUELSIZE], AIR_STATS_FUELSIZE)), sizeof(aircraftInfo));
00220     Q_strcat(aircraftInfo, va(_("Operational range:\t%i km\n"), AIR_GetOperationRange(aircraft)), sizeof(aircraftInfo));
00221     Q_strcat(aircraftInfo, va(_("Weapons:\t%i on %i\n"), AIR_GetSlotItems(AC_ITEM_WEAPON, aircraft), aircraft->maxWeapons), sizeof(aircraftInfo));
00222     Q_strcat(aircraftInfo, va(_("Armour:\t%i on 1\n"), AIR_GetSlotItems(AC_ITEM_SHIELD, aircraft)), sizeof(aircraftInfo));
00223     Q_strcat(aircraftInfo, va(_("Electronics:\t%i on %i"), AIR_GetSlotItems(AC_ITEM_ELECTRONICS, aircraft), aircraft->maxElectronics), sizeof(aircraftInfo));
00224 
00225     UI_RegisterText(TEXT_AIRCRAFT_INFO, aircraftInfo);
00226 
00227     /* compute the ID and... */
00228     aircraftInBase = NULL;
00229     id = 0;
00230     while ((aircraftInBase = AIR_GetNextFromBase(base, aircraftInBase)) != NULL) {
00231         if (aircraft == aircraftInBase)
00232             break;
00233         id++;
00234     }
00235 
00236     /* the aircraft must really be in this base */
00237     assert(id != LIST_Count(base->aircraft));
00238 
00239     base->aircraftCurrent = aircraft;
00240     Cvar_SetValue("mn_aircraft_id", id);
00241 
00242     /* ...update the GUI */
00243     UI_ExecuteConfunc("aircraft_change %i", id);
00244 }
00245 
00249 static void AIR_AircraftUpdateList_f (void)
00250 {
00251     linkedList_t *list = NULL;
00252     base_t *base = B_GetCurrentSelectedBase();
00253     aircraft_t *aircraft;
00254 
00255     aircraft = NULL;
00256     while ((aircraft = AIR_GetNextFromBase(base, aircraft)) != NULL)
00257         LIST_AddString(&list, aircraft->name);
00258 
00259     UI_RegisterLinkedListText(TEXT_AIRCRAFT_LIST, list);
00260 }
00261 
00267 static void AIR_ChangeAircraftName_f (void)
00268 {
00269     const base_t *base = B_GetCurrentSelectedBase();
00270     aircraft_t *aircraft;
00271 
00272     if (!base)
00273         return;
00274     aircraft = base->aircraftCurrent;
00275     if (!aircraft)
00276         return;
00277 
00278     Q_strncpyz(aircraft->name, Cvar_GetString("mn_aircraftname"), sizeof(aircraft->name));
00279 }
00280 
00281 
00282 void AIR_InitCallbacks (void)
00283 {
00284     /* menu aircraft */
00285     Cmd_AddCommand("aircraft_start", AIM_AircraftStart_f, NULL);
00286     /* menu aircraft_equip, aircraft */
00287     Cmd_AddCommand("mn_select_aircraft", AIM_SelectAircraft_f, NULL);
00288     /* menu aircraft, popup_transferbaselist */
00289     Cmd_AddCommand("aircraft_return", AIM_AircraftReturnToBase_f, "Sends the current aircraft back to homebase");
00290     /* menu aircraft, aircraft_equip, aircraft_soldier */
00291     Cmd_AddCommand("aircraft_update_list", AIR_AircraftUpdateList_f, NULL);
00292     Cmd_AddCommand("aircraft_namechange", AIR_ChangeAircraftName_f, "Callback to change the name of the aircraft.");
00293 }
00294 
00295 void AIR_ShutdownCallbacks (void)
00296 {
00297     Cmd_RemoveCommand("aircraft_namechange");
00298     Cmd_RemoveCommand("aircraft_start");
00299     Cmd_RemoveCommand("mn_select_aircraft");
00300     Cmd_RemoveCommand("aircraft_return");
00301     Cmd_RemoveCommand("aircraft_update_list");
00302 }

Generated by  doxygen 1.6.2