cl_inventory_callbacks.c

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2002-2010 UFO: Alien Invasion.
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017 See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 */
00024 
00025 #include "client.h"
00026 #include "cl_inventory.h"
00027 #include "cl_inventory_callbacks.h"
00028 #include "ui/ui_main.h"
00029 #include "ui/ui_nodes.h"
00030 #include "cl_game.h"
00031 #include "ui/ui_popup.h"
00032 
00033 static const objDef_t *currentDisplayedObject;
00034 static int itemIndex;
00035 static int fireModeIndex;
00036 
00040 static const char* CL_WeaponSkillToName (int weaponSkill)
00041 {
00042     switch (weaponSkill) {
00043     case SKILL_CLOSE:
00044         return _("skill_close");
00045     case SKILL_HEAVY:
00046         return _("skill_heavy");
00047     case SKILL_ASSAULT:
00048         return _("skill_assault");
00049     case SKILL_SNIPER:
00050         return _("skill_sniper");
00051     case SKILL_EXPLOSIVE:
00052         return _("skill_explosive");
00053     default:
00054         return _("Unknown weapon skill");
00055     }
00056 }
00057 
00065 void INV_ItemDescription (const objDef_t *od)
00066 {
00067     static char itemText[UI_MAX_SMALLTEXTLEN];
00068     int i;
00069     int count;
00070 
00071     currentDisplayedObject = od;
00072 
00073     if (!od) {  /* If nothing selected return */
00074         Cvar_Set("mn_itemname", "");
00075         Cvar_Set("mn_item", "");
00076         UI_ResetData(TEXT_ITEMDESCRIPTION);
00077         itemIndex = fireModeIndex = 0;
00078         UI_ExecuteConfunc("itemdesc_view 0 0;");
00079         return;
00080     }
00081 
00082     /* select item */
00083     Cvar_Set("mn_itemname", _(od->name));
00084     Cvar_Set("mn_item", od->id);
00085 
00086     count = 0;
00087     if (INV_IsAmmo(od)) {
00088         /* We display the pre/next buttons for changing weapon only if there are at least 2 researched weapons
00089          * we are counting the number of weapons that are usable with this ammo */
00090         for (i = 0; i < od->numWeapons; i++)
00091             if (GAME_ItemIsUseable(od->weapons[i]))
00092                 count++;
00093         if (itemIndex >= od->numWeapons || itemIndex < 0)
00094             itemIndex = 0;
00095         if (count > 0) {
00096             while (!GAME_ItemIsUseable(od->weapons[itemIndex])) {
00097                 itemIndex++;
00098                 if (itemIndex >= od->numWeapons)
00099                     itemIndex = 0;
00100             }
00101             Cvar_ForceSet("mn_linkname", _(od->weapons[itemIndex]->name));
00102         }
00103     } else if (od->weapon && od->reload) {
00104         /* We display the pre/next buttons for changing ammo only if there are at least 2 researched ammo
00105          * we are counting the number of ammo that is usable with this weapon */
00106         for (i = 0; i < od->numAmmos; i++)
00107             if (GAME_ItemIsUseable(od->ammos[i]))
00108                 count++;
00109 
00110         if (itemIndex >= od->numAmmos || itemIndex < 0)
00111             itemIndex = 0;
00112 
00113         /* Only display ammos if at least one has been researched */
00114         if (count > 0) {
00115             /* We have a weapon that uses ammos */
00116             while (!GAME_ItemIsUseable(od->ammos[itemIndex])) {
00117                 itemIndex++;
00118                 if (itemIndex >= od->numAmmos)
00119                     itemIndex = 0;
00120             }
00121             Cvar_ForceSet("mn_linkname", _(od->ammos[itemIndex]->name));
00122         }
00123     }
00124 
00125     /* set description text if item has been researched or one of its ammo/weapon has been researched */
00126     if (count > 0 || GAME_ItemIsUseable(od)) {
00127         int numFiredefs = 0;
00128 
00129         *itemText = '\0';
00130         if (INV_IsArmour(od)) {
00131             Com_sprintf(itemText, sizeof(itemText), _("Size:\t%i\n"), od->size);
00132             Q_strcat(itemText, "\n", sizeof(itemText));
00133             Q_strcat(itemText, _("^BDamage type:\tProtection:\n"), sizeof(itemText));
00134             for (i = 0; i < csi.numDTs; i++) {
00135                 if (!csi.dts[i].showInMenu)
00136                     continue;
00137                 Q_strcat(itemText, va(_("%s\t%i\n"), _(csi.dts[i].id), od->ratings[i]), sizeof(itemText));
00138             }
00139         } else if ((od->weapon && od->numAmmos) || INV_IsAmmo(od)) {
00140             const objDef_t *odAmmo;
00141 
00142             if (count > 0) {
00143                 int weaponIndex;
00144                 if (od->weapon) {
00145                     Com_sprintf(itemText, sizeof(itemText), _("%s weapon\n"), (od->fireTwoHanded ? _("Two-handed") : _("One-handed")));
00146                     if (od->ammo > 0)
00147                         Q_strcat(itemText, va(_("Max ammo:\t%i\n"), od->ammo), sizeof(itemText));
00148                     odAmmo = (od->numAmmos) ? od->ammos[itemIndex] : od;
00149                     assert(odAmmo);
00150                     for (weaponIndex = 0; (weaponIndex < odAmmo->numWeapons) && (odAmmo->weapons[weaponIndex] != od); weaponIndex++);
00151                 } else {
00152                     odAmmo = od;
00153                     weaponIndex = itemIndex;
00154                 }
00155 
00157                 if (GAME_ItemIsUseable(odAmmo) && odAmmo->numFiredefs[weaponIndex] > 0) {
00158                     const fireDef_t *fd;
00159                     numFiredefs = odAmmo->numFiredefs[weaponIndex];
00160 
00161                     /* This contains everything common for weapons and ammos */
00162                     /* We check if the wanted firemode to display exists. */
00163                     if (fireModeIndex > numFiredefs - 1)
00164                         fireModeIndex = 0;
00165                     if (fireModeIndex < 0)
00166                         fireModeIndex = numFiredefs - 1;
00167 
00168                     fd = &odAmmo->fd[weaponIndex][fireModeIndex];
00169 
00170                     /* We always display the name of the firemode for an ammo */
00171                     Cvar_Set("mn_firemodename", _(fd->name));
00172 
00173                     /* We display the characteristics of this firemode */
00174                     Q_strcat(itemText, va(_("Skill:\t%s\n"), CL_WeaponSkillToName(fd->weaponSkill)), sizeof(itemText));
00175                     Q_strcat(itemText, va(_("Damage:\t%i\n"), (int) (fd->damage[0] + fd->spldmg[0]) * fd->shots), sizeof(itemText));
00176                     Q_strcat(itemText, va(_("Time units:\t%i\n"), fd->time), sizeof(itemText));
00177                     Q_strcat(itemText, va(_("Range:\t%g\n"), fd->range / UNIT_SIZE), sizeof(itemText));
00178                     Q_strcat(itemText, va(_("Spreads:\t%g\n"), (fd->spread[0] + fd->spread[1]) / 2), sizeof(itemText));
00179                 }
00180             } else {
00181                 Com_sprintf(itemText, sizeof(itemText), _("%s. No detailed info available.\n"), INV_IsAmmo(od) ? _("Ammunition") : _("Weapon"));
00182             }
00183         } else if (od->weapon) {
00184             Com_sprintf(itemText, sizeof(itemText), _("%s ammo-less weapon\n"), (od->fireTwoHanded ? _("Two-handed") : _("One-handed")));
00185         } else {
00186             /* just an item - only primary definition */
00187             Com_sprintf(itemText, sizeof(itemText), _("%s auxiliary equipment\n"), (od->fireTwoHanded ? _("Two-handed") : _("One-handed")));
00188             if (od->numWeapons > 0 && od->numFiredefs[0] > 0) {
00189                 Q_strcat(itemText, va(_("Action:\t%s\n"), _(od->fd[0][0].name)), sizeof(itemText));
00190                 Q_strcat(itemText, va(_("Time units:\t%i\n"), od->fd[0][0].time), sizeof(itemText));
00191                 Q_strcat(itemText, va(_("Range:\t%g\n"), od->fd[0][0].range / UNIT_SIZE), sizeof(itemText));
00192             }
00193         }
00194 
00195         UI_RegisterText(TEXT_ITEMDESCRIPTION, itemText);
00196         UI_ExecuteConfunc("itemdesc_view %i %i;", count, numFiredefs);
00197     } else {
00198         Com_sprintf(itemText, sizeof(itemText), _("Unknown - not useable"));
00199         UI_RegisterText(TEXT_ITEMDESCRIPTION, itemText);
00200         UI_ExecuteConfunc("itemdesc_view 0 0;");
00201     }
00202 }
00203 
00204 
00209 static void INV_IncreaseFiremode_f (void)
00210 {
00211     if (!currentDisplayedObject)
00212         return;
00213 
00214     fireModeIndex++;
00215 
00216     INV_ItemDescription(currentDisplayedObject);
00217 }
00218 
00223 static void INV_DecreaseFiremode_f (void)
00224 {
00225     if (!currentDisplayedObject)
00226         return;
00227 
00228     fireModeIndex--;
00229 
00230     INV_ItemDescription(currentDisplayedObject);
00231 }
00232 
00237 static void INV_IncreaseItem_f (void)
00238 {
00239     const objDef_t *od = currentDisplayedObject;
00240 
00241     if (!od)
00242         return;
00243 
00244     if (od->numWeapons) {
00245         const int current = itemIndex;
00246         do {
00247             itemIndex++;
00248             if (itemIndex > od->numWeapons - 1) {
00249                 itemIndex = 0;
00250             }
00251         } while (itemIndex != current && !GAME_ItemIsUseable(od->weapons[itemIndex]));
00252     } else if (od->numAmmos) {
00253         const int current = itemIndex;
00254         do {
00255             itemIndex++;
00256             if (itemIndex > od->numAmmos - 1) {
00257                 itemIndex = 0;
00258             }
00259         } while (itemIndex != current && !GAME_ItemIsUseable(od->ammos[itemIndex]));
00260     }
00261     INV_ItemDescription(od);
00262 }
00263 
00268 static void INV_DecreaseItem_f (void)
00269 {
00270     const objDef_t *od = currentDisplayedObject;
00271 
00272     if (!od)
00273         return;
00274 
00275     if (od->numWeapons) {
00276         const int current = itemIndex;
00277         do {
00278             itemIndex--;
00279             if (itemIndex < 0) {
00280                 itemIndex = od->numWeapons - 1;
00281             }
00282         } while (itemIndex != current && !GAME_ItemIsUseable(od->weapons[itemIndex]));
00283     } else if (od->numAmmos) {
00284         const int current = itemIndex;
00285         do {
00286             itemIndex--;
00287             if (itemIndex < 0) {
00288                 itemIndex = od->numAmmos - 1;
00289             }
00290         } while (itemIndex != current && !GAME_ItemIsUseable(od->ammos[itemIndex]));
00291     }
00292     INV_ItemDescription(od);
00293 }
00294 
00298 static void INV_UpdateObject_f (void)
00299 {
00300     int num;
00301     const objDef_t *obj;
00302     qboolean changeTab;
00303 
00304     /* check syntax */
00305     if (Cmd_Argc() < 2) {
00306         Com_Printf("Usage: %s <objectid> <mustwechangetab>\n", Cmd_Argv(0));
00307         return;
00308     }
00309 
00310     if (Cmd_Argc() == 3)
00311         changeTab = atoi(Cmd_Argv(2)) >= 1;
00312     else
00313         changeTab = qtrue;
00314 
00315     num = atoi(Cmd_Argv(1));
00316     if (num < 0 || num >= csi.numODs) {
00317         Com_Printf("Id %i out of range 0..%i\n", num, csi.numODs);
00318         return;
00319     }
00320     obj = INVSH_GetItemByIDX(num);
00321 
00322     /* update item description */
00323     INV_ItemDescription(obj);
00324 
00325     /* update tab */
00326     if (changeTab) {
00327         const cvar_t *var = Cvar_FindVar("mn_equiptype");
00328         const int filter = INV_GetFilterFromItem(obj);
00329         if (var && var->integer != filter) {
00330             Cvar_SetValue("mn_equiptype", filter);
00331             UI_ExecuteConfunc("update_item_list");
00332         }
00333     }
00334 }
00335 
00336 void INV_InitCallbacks (void)
00337 {
00338     Cmd_AddCommand("mn_increasefiremode", INV_IncreaseFiremode_f, "Increases the number of the firemode to display");
00339     Cmd_AddCommand("mn_decreasefiremode", INV_DecreaseFiremode_f, "Decreases the number of the firemode to display");
00340     Cmd_AddCommand("mn_increaseitem", INV_IncreaseItem_f, "Increases the number of the weapon or the ammo to display");
00341     Cmd_AddCommand("mn_decreaseitem", INV_DecreaseItem_f, "Decreases the number of the weapon or the ammo to display");
00342     Cmd_AddCommand("object_update", INV_UpdateObject_f, _("Update the GUI with the selected item"));
00343 }

Generated by  doxygen 1.6.2