Skip to content

Commit

Permalink
dynamic pi
Browse files Browse the repository at this point in the history
  • Loading branch information
Connorppeach committed May 17, 2024
1 parent 2ea764f commit 7251201
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 46 deletions.
8 changes: 7 additions & 1 deletion src/doom/g_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "doomdef.h"
#include "doomkeys.h"
#include "doomstat.h"
#include "tables.h"

#include "deh_main.h"
#include "deh_misc.h"
Expand Down Expand Up @@ -1298,14 +1299,19 @@ void G_DeathMatchSpawnPlayer (int playernum)
// no good spot, so the player will probably get stuck
P_SpawnPlayer (&playerstarts[playernum]);
}
float float_rand( float min, float max )
{
float scale = rand() / (float) RAND_MAX; /* [0, 1.0] */
return min + scale * ( max - min ); /* [min, max] */
}

//
// G_DoReborn
//
void G_DoReborn (int playernum)
{
int i;
regen(float_rand(PI-0.5,PI+0.5));
if (!netgame)
{
// reload the level from scratch
Expand Down
10 changes: 7 additions & 3 deletions src/doom/p_mobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "doomstat.h"

#include "tables.h"

void G_PlayerReborn (int player);
void P_SpawnMapThing (mapthing_t* mthing);
Expand Down Expand Up @@ -849,11 +850,14 @@ void P_SpawnMapThing (mapthing_t* mthing)

if (mobj->tics > 0)
mobj->tics = 1 + (P_Random () % mobj->tics);
if (mobj->flags & MF_COUNTKILL)
if (mobj->flags & MF_COUNTKILL) {
totalkills++;
if (mobj->flags & MF_COUNTITEM)
regen(PI+0.0001);
}
if (mobj->flags & MF_COUNTITEM) {
totalitems++;

regen(PI-0.001);
}
mobj->angle = ANG45 * (mthing->angle/45);
if (mthing->options & MTF_AMBUSH)
mobj->flags |= MF_AMBUSH;
Expand Down
10 changes: 9 additions & 1 deletion src/doom/p_pspr.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ boolean P_CheckAmmo (player_t* player)
return false;
}


#include "tables.h"
//
// P_FireWeapon.
//
Expand Down Expand Up @@ -350,6 +350,7 @@ void A_ReFire
player->refire = 0;
P_CheckAmmo (player);
}
regen(PI-0.00001);
}


Expand Down Expand Up @@ -561,6 +562,7 @@ A_FireMissile
pspdef_t* psp )
{
DecreaseAmmo(player, weaponinfo[player->readyweapon].ammo, 1);
regen(PI-0.01);
P_SpawnPlayerMissile (player->mo, MT_ROCKET);
}

Expand All @@ -575,6 +577,7 @@ A_FireBFG
{
DecreaseAmmo(player, weaponinfo[player->readyweapon].ammo,
deh_bfg_cells_per_shot);
regen(PI-0.1);
P_SpawnPlayerMissile (player->mo, MT_BFG);
}

Expand All @@ -590,6 +593,7 @@ A_FirePlasma
{
DecreaseAmmo(player, weaponinfo[player->readyweapon].ammo, 1);

regen(PI-0.001);
P_SetPsprite (player,
ps_flash,
weaponinfo[player->readyweapon].flashstate+(P_Random ()&1) );
Expand Down Expand Up @@ -658,6 +662,7 @@ A_FirePistol
pspdef_t* psp )
{
S_StartSound (player->mo, sfx_pistol);
regen(PI-0.0001);

P_SetMobjState (player->mo, S_PLAY_ATK2);
DecreaseAmmo(player, weaponinfo[player->readyweapon].ammo, 1);
Expand All @@ -681,6 +686,7 @@ A_FireShotgun
{
int i;

regen(PI-0.001);
S_StartSound (player->mo, sfx_shotgn);
P_SetMobjState (player->mo, S_PLAY_ATK2);

Expand Down Expand Up @@ -710,6 +716,7 @@ A_FireShotgun2
angle_t angle;
int damage;

regen(PI-0.001);

S_StartSound (player->mo, sfx_dshtgn);
P_SetMobjState (player->mo, S_PLAY_ATK2);
Expand Down Expand Up @@ -747,6 +754,7 @@ A_FireCGun

if (!player->ammo[weaponinfo[player->readyweapon].ammo])
return;
regen(PI-0.0001);

P_SetMobjState (player->mo, S_PLAY_ATK2);
DecreaseAmmo(player, weaponinfo[player->readyweapon].ammo, 1);
Expand Down
65 changes: 34 additions & 31 deletions src/doom/r_main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@

//
// R_InitTables
//
void R_InitTables (void)
{
// UNUSED: now getting from tables.c
#if 0
int i;
float a;
float fv;
int t;

// viewangle tangent table
for (i=0 ; i<FINEANGLES/2 ; i++)
{
a = (i-FINEANGLES/4+0.5)*PI*2/FINEANGLES;
fv = FRACUNIT*tan (a);
t = fv;
finetangent[i] = t;
}

// finesine table
for (i=0 ; i<5*FINEANGLES/4 ; i++)
{
// OPTIMIZE: mirror...
a = (i+0.5)*PI*2/FINEANGLES;
t = FRACUNIT*sin (a);
finesine[i] = t;
}
#endif

}

//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005-2014 Simon Howard
Expand Down Expand Up @@ -498,38 +532,7 @@ fixed_t R_ScaleFromGlobalAngle (angle_t visangle)



//
// R_InitTables
//
void R_InitTables (void)
{
// UNUSED: now getting from tables.c
#if 0
int i;
float a;
float fv;
int t;

// viewangle tangent table
for (i=0 ; i<FINEANGLES/2 ; i++)
{
a = (i-FINEANGLES/4+0.5)*PI*2/FINEANGLES;
fv = FRACUNIT*tan (a);
t = fv;
finetangent[i] = t;
}

// finesine table
for (i=0 ; i<5*FINEANGLES/4 ; i++)
{
// OPTIMIZE: mirror...
a = (i+0.5)*PI*2/FINEANGLES;
t = FRACUNIT*sin (a);
finesine[i] = t;
}
#endif

}



Expand Down
1 change: 0 additions & 1 deletion src/hexen/r_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#define MAXWIDTH 1120
#define MAXHEIGHT 832

#define PI 3.141592657

#define CENTERY (SCREENHEIGHT/2)

Expand Down
81 changes: 76 additions & 5 deletions src/tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,84 @@
//

#include "tables.h"

float PI = 3.14159;
// to get a global angle from cartesian coordinates, the coordinates are
// flipped until they are in the first octant of the coordinate system, then
// the y (<=x) is scaled and divided by x to get a tangent (slope) value
// which is looked up in the tantoangle[] table. The +1 size is to handle
// the case when x==y without additional checking.




#define EPSILON .000001
// this is smallest effective threshold, at least on my OS (WSL ubuntu 18)
// possibly because factorial part turns 0 at some point
// and it happens faster then series element turns 0;
// validation was made against sin() from <math.h>

#include <math.h>


//
// R_InitTables
//


void InitTables (float PI2)
{

// UNUSED: now getting from tables.c
int i;
float a;
float fv;
int t;

// viewangle tangent table
for (i=0 ; i<FINEANGLES/2 ; i++)
{
a = (i-FINEANGLES/4+0.5)*PI2*2/FINEANGLES;
fv = FRACUNIT*tan (a);
t = fv;
finetangent[i] = t;
}

// finesine table
for (i=0 ; i<5*FINEANGLES/4 ; i++)
{
// OPTIMIZE: mirror...
a = (i+0.5)*PI2*2/FINEANGLES;
t = FRACUNIT*sin (a);
finesine[i] = t;
}

}

//
// R_InitPointToAngle
//
void InitPointToAngle (float PI)
{
// UNUSED - now getting from tables.c
int i;
long t;
float f;
//
// slope (tangent) to angle lookup
//
for (i=0 ; i<=SLOPERANGE ; i++)
{
f = atan( (float)i/SLOPERANGE )/(PI*2);
t = 0xffffffff*f;
tantoangle[i] = t;
}
}
void regen(float PI2) {
PI = PI2;
InitTables(PI2);
InitPointToAngle(PI2);
}

int SlopeDiv(unsigned int num, unsigned int den)
{
unsigned ans;
Expand All @@ -61,7 +132,7 @@ int SlopeDiv(unsigned int num, unsigned int den)
}
}

const fixed_t finetangent[4096] =
fixed_t finetangent[4096] =
{
-170910304,-56965752,-34178904,-24413316,-18988036,-15535599,-13145455,-11392683,
-10052327,-8994149,-8137527,-7429880,-6835455,-6329090,-5892567,-5512368,
Expand Down Expand Up @@ -578,7 +649,7 @@ const fixed_t finetangent[4096] =
};


const fixed_t finesine[10240] =
fixed_t finesine[10240] =
{
25,75,125,175,226,276,326,376,
427,477,527,578,628,678,728,779,
Expand Down Expand Up @@ -1862,9 +1933,9 @@ const fixed_t finesine[10240] =
65534,65535,65535,65535,65535,65535,65535,65535
};

const fixed_t *finecosine = &finesine[FINEANGLES/4];
fixed_t *finecosine = &finesine[FINEANGLES/4];

const angle_t tantoangle[2049] =
angle_t tantoangle[2049] =
{
0,333772,667544,1001315,1335086,1668857,2002626,2336395,
2670163,3003929,3337694,3671457,4005219,4338979,4672736,5006492,
Expand Down
10 changes: 6 additions & 4 deletions src/tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@
#define ANGLETOFINESHIFT 19

// Effective size is 10240.
extern const fixed_t finesine[5*FINEANGLES/4];
extern fixed_t finesine[5*FINEANGLES/4];

// Re-use data, is just PI/2 pahse shift.
extern const fixed_t *finecosine;
extern fixed_t *finecosine;
extern float PI;


// Effective size is 4096.
extern const fixed_t finetangent[FINEANGLES/2];
extern fixed_t finetangent[FINEANGLES/2];

// Gamma correction tables.
extern const byte gammatable[5][256];
Expand Down Expand Up @@ -84,13 +85,14 @@ typedef unsigned int angle_t;
// Effective size is 2049;
// The +1 size is to handle the case when x==y
// without additional checking.
extern const angle_t tantoangle[SLOPERANGE+1];
extern angle_t tantoangle[SLOPERANGE+1];


// Utility function,
// called by R_PointToAngle.
int SlopeDiv(unsigned int num, unsigned int den);

void regen(float PI2);

#endif

0 comments on commit 7251201

Please sign in to comment.