[Tutorial] Weapon accuracy stat on Level screen
Select messages from
# through # FAQ
[/[Print]\]

DieHard Wolfers -> SDL Code Crackers

#1: [Tutorial] Weapon accuracy stat on Level screen Author: insurrectionmanLocation: Wisconsin PostPosted: Thu Aug 24, 2017 7:00 pm
    —
Hi all,

I've come up with some interesting features that might be fun to have in a mod. This one being Weapon accuracy at the end. This doesn't accumulate at the end of the episode, it just is visible for that one level, and then it's gone. But there is always the option for a little extra to make that show on the Victory screen. Also give the player a bonus for 100% accuracy, etc etc. I'll just say this is a stepping stone for more ideas.

Let's get started, open WL_DEF.H and find the "gamestate structure", and add this at the end:
::: CODE :::

    int32_t     TimeCount;
    int32_t     killx,killy;
    boolean     victoryflag;            // set during victory animations
    short       weaponShotsHit, weaponShotsFired;
} gametype;


These will get the values stored on how many shots you've fired, and how many shots hit enemies.

Next, open WL_AGENT.CPP, and find the GunAttack function, adding this:

::: CODE :::


    madenoise = true;
    gamestate.weaponShotsFired++;

    //
    // find potential targets
    //
    viewdist = 0x7fffffffl;
    closest = NULL;


and a little farther down:

::: CODE :::

        damage = US_RndT() / 6;
    }

    gamestate.weaponShotsHit++;
    DamageActor (closest,damage);
}


And if you choose to also count knife hits/total, you can add these 2 lines to the KnifeAttack function:

::: CODE :::

void    KnifeAttack (objtype *ob)
{
    objtype *check,*closest;
    int32_t  dist;

    SD_PlaySound (ATKKNIFESND);
    gamestate.weaponShotsFired++;
    // actually fire
    dist = 0x7fffffff;
    closest = NULL;
    for (check=ob->next; check; check=check->next)
    {
        if ( (check->flags & FL_SHOOTABLE) && (check->flags & FL_VISABLE)
            && abs(check->viewx-centerx) < shootdelta)
        {
            if (check->transx < dist)
            {
                dist = check->transx;
                closest = check;
            }
        }
    }

    if (!closest || dist > 0x18000l)
    {
        // missed
        return;
    }

    gamestate.weaponShotsHit++;
    // hit something
    DamageActor (closest,US_RndT() >> 4);
}


Now we want to set the values to 0 on a NewGame, so we go to WL_MAIN.CPP, the NewGame function:

::: CODE :::
void NewGame (int difficulty,int episode)
{
    memset (&gamestate,0,sizeof(gamestate));
    gamestate.difficulty = difficulty;
    gamestate.weapon = gamestate.bestweapon
            = gamestate.chosenweapon = wp_pistol;
    gamestate.health = 100;
    gamestate.ammo = STARTAMMO;
    gamestate.lives = 3;
    gamestate.nextextra = EXTRAPOINTS;
    gamestate.episode=episode;
    gamestate.weaponShotsFired =
    gamestate.weaponShotsHit = 0;


    startgame = true;
}


Now, it needs to be reset when the player dies, so open WL_GAME.CPP and go to the Died function:

::: CODE :::

    if (gamestate.lives > -1)
    {
        gamestate.health = 100;
        gamestate.weapon = gamestate.bestweapon
            = gamestate.chosenweapon = wp_pistol;
        gamestate.ammo = STARTAMMO;
        gamestate.keys = 0;
        pwallstate = pwallpos = 0;
        gamestate.attackframe = gamestate.attackcount =
            gamestate.weaponframe = 0;

        gamestate.weaponShotsFired =
        gamestate.weaponShotsHit = 0;



And down to the GameLoop function, we want to reset the counts every level:

::: CODE :::

                gamestate.oldscore = gamestate.score;
                gamestate.weaponShotsFired =
                gamestate.weaponShotsHit = 0;


#ifndef SPEAR
                //
                // COMING BACK FROM SECRET LEVEL
                //


Now that all of the logic to record the values is done, we can go to WL_INTER.CPP to the LevelCompleted function, and add in how we will see the stats. I just replaced the "Floor X Completed" text for demonstration purposes, so I'm sure you'll find a way to make it fit on the screen.

::: CODE :::

#ifndef SPEAR
    if (mapon < 8)
#else
    if (mapon != 4 && mapon != 9 && mapon != 15 && mapon < 17)
#endif
    {
#ifndef JAPAN
#ifdef SPANISH
        Write (14, 2, "piso\ncompletado");
#else
        //Write (14, 2, "floor\ncompleted");
        Write (14,2, "Accuracy    %");

        char accuracyStr[3];
        int accuracy = ((float)gamestate.weaponShotsHit / gamestate.weaponShotsFired) * 100;
        itoa(accuracy, accuracyStr, 10);
        int offset = 38 - (int) strlen(accuracyStr) * 2;
        Write(offset,2,accuracyStr);


#endif

        Write (14, 7, STR_BONUS "     0");
        Write (16, 10, STR_TIME);
        Write (16, 12, STR_PAR);


#ifdef SPANISH
        Write (11, 14, STR_RAT2KILL);
        Write (11, 16, STR_RAT2SECRET);
        Write (11, 18, STR_RAT2TREASURE);
#else
        Write (9, 14, STR_RAT2KILL);
        Write (5, 16, STR_RAT2SECRET);
        Write (1, 18, STR_RAT2TREASURE);
#endif

        //Write (26, 2, itoa (gamestate.mapon + 1, tempstr, 10));
#endif


I hope this finds use for someone! If there are any suggestions, comments, let me know

#2: Re: [Tutorial] Weapon accuracy stat on Level screen Author: linuxwolf PostPosted: Thu Sep 07, 2017 6:39 pm
    —
This is a good start. Its still missing the 10000 bonus points when you achieve 100% accuracy. However I'll say it would be very difficult to achieve 100% accuracy anyway. Remember hits are randomized:

::: CODE :::

void    GunAttack (objtype *ob)
{
...
        if ( (US_RndT() / 12) < dist)           // missed
...
}

#3: Re: [Tutorial] Weapon accuracy stat on Level screen Author: Aryan_Wolf3D PostPosted: Fri Sep 08, 2017 2:02 am
    —
linuxwolf wrote:
This is a good start. Its still missing the 10000 bonus points when you achieve 100% accuracy. However I'll say it would be very difficult to achieve 100% accuracy anyway. Remember hits are randomized:

Yeah, getting 100% accuracy would be practically impossible, so anything > 50% accuracy would probably be enough.

#4: Re: [Tutorial] Weapon accuracy stat on Level screen Author: insurrectionmanLocation: Wisconsin PostPosted: Sat Sep 09, 2017 7:26 am
    —
Aryan_Wolf3D wrote:
linuxwolf wrote:
This is a good start. Its still missing the 10000 bonus points when you achieve 100% accuracy. However I'll say it would be very difficult to achieve 100% accuracy anyway. Remember hits are randomized:

Yeah, getting 100% accuracy would be practically impossible, so anything > 50% accuracy would probably be enough.


I forgot about the random part when I came up with this idea. I think also, I had the ray-tracing bullets also in mind, so it wasn't a random chance of hit/miss. Although the ray trace also is random on where it'll go (if you have a spread or something to that effect).



DieHard Wolfers -> SDL Code Crackers


output generated using printer-friendly topic mod. All times are GMT

Page 1 of 1

Powered by phpBB © 2001, 2005 phpBB Group