Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

An example of making a change to Caveman...

An example of making a change to Caveman...

Norman Barrows
Caveman · · 2 min read
1,572 0
An example of making a change to Caveman...

This is a quick description of what its like to make a change to the game.

I just finished the new bandmember AI. Now it was time to make bandmember AI use sprint running speed.

First I checked [color=#0000ff]movement_rate()[/color] to figure out what the speeds were. sneak is 1/2 walk speed. run (its more like cross country jogging)
is 2x walk speed, and sprint is 4x walk speed. walk speed is 1/3 foot per frame at 15 fps (5 ft/sec).

The existing code only supported sprint for the current bandmember (the one the player is controlling at the moment).
and it used the state of the sprint input control (left shift by default) to determine movement rate and fatigue.
What was required was a way to know if any given bandmember was sprinting. this called for a new is_sprinting variable.

I have a checklist i use for adding a new variable to a program:
declare - declare the new variable
init - initialize it
load - add code to load it if needed
save - add code to save it if needed
change - add code that changes its value
use - add code that uses its value

so first i added an "[color=#0000ff]int is_sprinting[/color]" to the cavemanrec struct declaration. this is the struct that holds all the info for a bandmember.

the code is already setup to init a cavemanrec to zero. so as long as i declare variables so their initial values should be zero, i don't
have to write any new init code. so the code already initialized is_sprinitng to zero (false) automatically.

for load and save, i load and save the individual fields of a struct, instead of just reading or writing the whole struct. that way i can add
variables to a struct, and the existing load code and savegames still work. any new fields are initialized before load. the process is simple,
add the code to save the new variable. run the game. load then save any savegames you want to convert. then add the code to read
the new variable and you're done. the code loads and saves the new variable, and the savegames have been converted to the new
file format.

to change the variable, for the player, i needed to add one line of code to the input processing routine:
[color=#0000ff]if (control_pressed(IN_SPRINT)) cm[cm0].is_sprinting=1; else cm[cm0].is_sprinting=0;[/color]
then i added [color=#0000ff]cm[].is_sprinting=1[/color] before the calls to [color=#0000ff]movement_rate[/color] in the bandmember AI code. This appeared in perhaps half
a dozen places. This made the bandmember AI use sprint speeds.


to use it, i did a search on [color=#0000ff]IN_SPRINT[/color], and replaced:
[color=#0000ff]if (control_pressed(IN_SPRINT))[/color]
with:
[color=#0000ff]if (cm[].is_sprinting)[/color]
this appeared in maybe four places.
that made the movement rate and fatigue code use is_sprinting for all bandmembers (both the player controlled one,
and any AI controlled ones) instead of just using [color=#0000ff]control_pressed(IN_SPRINT)[/color] for the player controlled bandmember.

And thats all there was to it. Don't even think it took an hour.

Now its time to make the bandmember AI play sneak and sprint animations!

Discussion

Loading comments...