Skip to content

Commit

Permalink
improve misses mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Iluvalar committed Jun 21, 2012
1 parent e8a51a2 commit 9e413fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
13 changes: 8 additions & 5 deletions src/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ bool combFire(WEAPON *psWeap, BASE_OBJECT *psAttacker, BASE_OBJECT *psTarget, in
bool bVisibleAnyway = psTarget->player == selectedPlayer;

// see if we were lucky to hit the target
bool isHit = gameRand(100) <= resultHitChance;
int rand = gameRand(100);
bool isHit = rand <= resultHitChance;
if (isHit)
{
/* Kerrrbaaang !!!!! a hit */
Expand All @@ -329,10 +330,12 @@ bool combFire(WEAPON *psWeap, BASE_OBJECT *psAttacker, BASE_OBJECT *psTarget, in
}
else /* Deal with a missed shot */
{
const int minOffset = 5;

int missDist = 2 * (100 - resultHitChance) + minOffset;
Vector3i miss = Vector3i(iSinCosR(gameRand(DEG(360)), missDist), 0);
const ObjectShape targetShape = establishTargetShape(psTarget);
int minOffset = targetShape.radius();
Vector3i deltaPos = psAttacker->pos - predict;
int worstShot = iHypot(removeZ(deltaPos))*100/resultHitChance/3;
int missDist = minOffset + pow(worstShot*(rand-resultHitChance)/(100-resultHitChance),3)/pow(worstShot,2);

This comment has been minimized.

Copy link
@Safety0ff

Safety0ff Jun 21, 2012

You'll need to add an integer power function and make sure it doesn't overflow. As it stands, pow will use floating point.

Vector3i miss = Vector3i(iSinCosR(gameRand(DEG(270))-DEG(135)+iAtan2(removeZ(deltaPos)), missDist), 0);
predict += miss;

psTarget = NULL; // Missed the target, so don't expect to hit it.
Expand Down
14 changes: 1 addition & 13 deletions src/projectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,7 @@ BASE_OBJECT *g_pProjLastAttacker;

/***************************************************************************/

struct ObjectShape
{
ObjectShape() {}
ObjectShape(int radius) : isRectangular(false), size(radius, radius) {}
ObjectShape(int width, int breadth) : isRectangular(true), size(width, breadth) {}
ObjectShape(Vector2i widthBreadth) : isRectangular(true), size(widthBreadth) {}
int radius() const { return size.x; }

bool isRectangular; ///< True if rectangular, false if circular.
Vector2i size; ///< x == y if circular.
};

static ObjectShape establishTargetShape(BASE_OBJECT *psTarget);
static void proj_ImpactFunc( PROJECTILE *psObj );
static void proj_PostImpactFunc( PROJECTILE *psObj );
static void proj_checkBurnDamage(PROJECTILE *psProj);
Expand Down Expand Up @@ -1446,7 +1434,7 @@ SDWORD proj_GetLongRange(const WEAPON_STATS* psStats)


/***************************************************************************/
static ObjectShape establishTargetShape(BASE_OBJECT *psTarget)
ObjectShape establishTargetShape(BASE_OBJECT *psTarget)
{
CHECK_OBJECT(psTarget);

Expand Down
12 changes: 12 additions & 0 deletions src/projectile.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,17 @@ void checkProjectile(const PROJECTILE* psProjectile, const char * const location

#define syncDebugProjectile(psProj, ch) _syncDebugProjectile(__FUNCTION__, psProj, ch)
void _syncDebugProjectile(const char *function, PROJECTILE const *psProj, char ch);
struct ObjectShape
{
ObjectShape() {}
ObjectShape(int radius) : isRectangular(false), size(radius, radius) {}
ObjectShape(int width, int breadth) : isRectangular(true), size(width, breadth) {}
ObjectShape(Vector2i widthBreadth) : isRectangular(true), size(widthBreadth) {}
int radius() const { return size.x; }

bool isRectangular; ///< True if rectangular, false if circular.
Vector2i size; ///< x == y if circular.
};

ObjectShape establishTargetShape(BASE_OBJECT *psTarget);
#endif // __INCLUDED_SRC_PROJECTILE_H__

0 comments on commit 9e413fa

Please sign in to comment.