Factor out common torpedo fire code into shp_torp()

This takes care of a number of bugs / inconsistencies:

* Submarines with zero mobility could interdict.  Change to require
  positive mobility.

* Submarines with zero firing range could not interdict.  Fix by
  dropping the test from perform_mission().  No ships in the stock
  game are affected.

* Submarines without capability torp could fire return torpedoes and
  interdict.  Stock sbc, nm and msb were affected by the return fire
  bug.  Closes bug#950936.

* Shell resupply bugs: quiet_bigdef(), fire_torp() and
  perform_mission() resupplied before checking all other requirements
  and could thus get more shells than actually needed.

torp() no longer resupplies shells.  It's hardly worth the bother, and
fire doesn't do it either.
This commit is contained in:
Markus Armbruster 2008-03-02 15:45:41 +01:00
parent 21733d1473
commit 6d83090aad
5 changed files with 60 additions and 105 deletions

View file

@ -33,6 +33,7 @@
#include <config.h>
#include "damage.h"
#include "file.h"
#include "nat.h"
#include "optlist.h"
@ -151,6 +152,33 @@ shp_dchrg(struct shpstr *sp)
return (int)seagun(sp->shp_effic, 3);
}
/*
* Fire torpedo from ship SP.
* Use ammo and mobility, resupply if necessary.
* Return damage if the ship fires, else -1.
*/
int
shp_torp(struct shpstr *sp, int usemob)
{
int shells;
if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_TORP) == 0)
return -1;
if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
return -1;
if (usemob && sp->shp_mobil <= 0)
return -1;
shells = sp->shp_item[I_SHELL];
shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
I_SHELL, SHP_TORP_SHELLS - shells);
if (shells < SHP_TORP_SHELLS)
return -1;
sp->shp_item[I_SHELL] = shells - SHP_TORP_SHELLS;
if (usemob)
sp->shp_mobil -= (int)shp_mobcost(sp) / 2.0;
return TORP_DAMAGE();
}
/*
* Return effective firing range for range factor RNG at tech TLEV.
*/