MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / SwitchPlayerWeapon

Function SwitchPlayerWeapon

Descent3/weapon.cpp:1192–1261  ·  view source on GitHub ↗

automatically switches weapon up to next level in autoselect order to this value. and type.

Source from the content-addressed store, hash-verified

1190
1191// automatically switches weapon up to next level in autoselect order to this value. and type.
1192int SwitchPlayerWeapon(int weapon_type) {
1193 player *plr;
1194 ship *ship;
1195 int new_index;
1196 void (*setwpnfunc)(int, int); // Call either primary or secondary set weapon function
1197 uint16_t *sel_list;
1198 int plr_wpn_index;
1199
1200 plr = &Players[Player_num];
1201 ship = &Ships[plr->ship_index];
1202 plr_wpn_index = plr->weapon[weapon_type].index;
1203
1204 setwpnfunc = (weapon_type == PW_SECONDARY) ? SetSecondaryWeapon : SetPrimaryWeapon;
1205 sel_list = (weapon_type == PW_SECONDARY) ? SecondaryWpnSelectList : PrimaryWpnSelectList;
1206
1207 new_index = 0;
1208 while (WPNINDEX(new_index) != plr_wpn_index && WPNINDEX(new_index) != SELLIST_END)
1209 new_index++;
1210
1211 if (WPNINDEX(new_index) == SELLIST_END) {
1212 Int3(); // every weapon should be in list that's in the game. bad
1213 new_index = 1; // 1st item after SELLIST_START
1214 }
1215
1216 // select weapon if we can. find a weapon we can select, if we go back to start, return.
1217 // THIS CODE IS SIMILAR BUT NOT THE SAME AS THE AUTO SELECT CODE.
1218 int old_index = new_index;
1219 new_index++;
1220
1221 while (old_index != new_index) {
1222 if (WPNINDEX(new_index) == SELLIST_END) {
1223 new_index = 0;
1224 } else if (WPNINDEX(new_index) == SELLIST_START) {
1225 new_index++;
1226 } else {
1227 uint16_t wpn_index = WPNINDEX(new_index);
1228 otype_wb_info *wb = &ship->static_wb[wpn_index];
1229 int slot = (weapon_type == PW_SECONDARY)
1230 ? (((wpn_index - SECONDARY_INDEX) % NUM_SECONDARY_SLOTS) + NUM_PRIMARY_SLOTS)
1231 : (wpn_index % NUM_PRIMARY_SLOTS);
1232
1233 // mprintf(0, "wpn_index = %d\n", wpn_index);
1234
1235 if ((Players[Player_num].weapon_flags & HAS_FLAG(wpn_index)) && !(sel_list[new_index] & WPNSEL_SKIP)) {
1236
1237 if (wpn_index >= SECONDARY_INDEX && wb->ammo_usage && (wb->ammo_usage <= plr->weapon_ammo[wpn_index])) {
1238 // we've found a weapon to select to that uses ammo!
1239 (*setwpnfunc)(wpn_index, slot);
1240 LOGFILE((_logfp, "ammo wpn: switch to new index %d\n", wpn_index));
1241 break;
1242 } else if (wpn_index < SECONDARY_INDEX && wb->ammo_usage && plr->weapon_ammo[wpn_index]) {
1243 // we've found a weapon to select to that uses ammo!
1244 (*setwpnfunc)(wpn_index, slot);
1245 LOGFILE((_logfp, "ammo wpn: switch to new index %d\n", wpn_index));
1246 break;
1247 } else if (!wb->ammo_usage && (plr->energy >= wb->energy_usage)) {
1248 // we've found an energy weapon to select to!
1249 (*setwpnfunc)(wpn_index, slot);

Callers 2

DoKeyboardWeaponsFunction · 0.85
DoControllerWeaponsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected