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

Function AutoSelectWeapon

Descent3/weapon.cpp:1266–1374  ·  view source on GitHub ↗

Auto selects a weapon, usually the next best weapon. step_up = true if we're going to next higher in order. step_up = false if we're going to next lower in order.

Source from the content-addressed store, hash-verified

1264// step_up = true if we're going to next higher in order.
1265// step_up = false if we're going to next lower in order.
1266bool AutoSelectWeapon(int weapon_type, int new_wpn) {
1267 player *plr;
1268 ship *ship;
1269 uint16_t *sel_list; // a weapon selection list
1270 int weapon_index; // the current weapon index
1271 int list_index; // index into a selection list
1272 int list_initial; // initial index in list.
1273 int slot;
1274 bool sel_new_wpn = false;
1275
1276 void (*setwpnfunc)(int, int); // Call either primary or secondary set weapon function
1277
1278 LOGFILE((_logfp, "Entering AutoSelect\n"));
1279
1280 ASSERT((weapon_type == PW_PRIMARY) || (weapon_type == PW_SECONDARY));
1281 plr = &Players[Player_num];
1282 weapon_index = plr->weapon[weapon_type].index;
1283 ship = &Ships[plr->ship_index];
1284
1285 // choose primary or secondary list and select function
1286 sel_list = (weapon_type == PW_SECONDARY) ? SecondaryWpnSelectList : PrimaryWpnSelectList;
1287 setwpnfunc = (weapon_type == PW_SECONDARY) ? SetSecondaryWeapon : SetPrimaryWeapon;
1288
1289 list_index = 0;
1290 list_initial = 0;
1291 while (WPNINDEX(list_index) != SELLIST_END) {
1292 if (WPNINDEX(list_index) == weapon_index)
1293 list_initial = list_index;
1294 list_index++;
1295 }
1296
1297 list_index--;
1298
1299 // this code takes care of selecting a GIVEN new weapon over the existing.
1300 if (new_wpn > -1) {
1301 while (WPNINDEX(list_index) != SELLIST_START && WPNINDEX(list_index) != new_wpn)
1302 list_index--;
1303 if (!(sel_list[list_index] & WPNSEL_SKIP)) {
1304 if (list_initial >= list_index) {
1305 uint16_t index = WPNINDEX(list_initial);
1306 otype_wb_info *wb = &ship->static_wb[index];
1307 if (index >= SECONDARY_INDEX && wb->ammo_usage && (wb->ammo_usage <= plr->weapon_ammo[index])) {
1308 LOGFILE((_logfp, "keep current ammo weapon...(ind=%d)\n", list_index));
1309 return sel_new_wpn; // the current weapon supercedes the new weapon, (or is the same) so return
1310 } else if (index < SECONDARY_INDEX && wb->ammo_usage && plr->weapon_ammo[index]) {
1311 LOGFILE((_logfp, "keep current ammo weapon...(ind=%d)\n", list_index));
1312 return sel_new_wpn; // the current weapon supercedes the new weapon, (or is the same) so return
1313 } else if (!wb->ammo_usage && (plr->energy >= wb->energy_usage)) {
1314 LOGFILE((_logfp, "keep current energy weapon...(ind=%d)\n", list_index));
1315 return sel_new_wpn; // the current weapon supercedes the new weapon, (or is the same) so return
1316 }
1317 LOGFILE((_logfp, "tried to keep current weapon, but no ammo!...(ind=%d)\n", list_index));
1318 }
1319 } else {
1320 LOGFILE((_logfp, "tried to autoselect skipped weapon, will not do...(ind=%d)\n", list_index));
1321 return sel_new_wpn;
1322 }
1323 }

Callers 4

msafe_CallFunctionFunction · 0.85
CheckForWeaponSelectFunction · 0.85
AddWeaponToPlayerFunction · 0.85
FireWeaponFromPlayerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected