my approach

BlitzMax Forums/BlitzMax Programming/my approach

Cruis.In(Posted 2006) [#1]
hey everyone. I've been about to add some form of code so the ship in the space game can "sustain" damage.

this is what I've come up with. In pseudo code only :)
just want to see if my approach is ok, or if its done some other way in general.

ok i have a field in tplayership.
engine_health

on being hit
check status of shields.
if down

'5 could be a variable that changes with rand, with a specified inbetween range so it doesn't ge too big.
engine_health :- 5


now in my code where my ships speed is modified, im thinking.

if engine_health < (certain number) 

'down from what the max speed is, if 3, then say 2.5
then ship.max_speed = (reduced speed here) 


so as the engine health drops, i can drop the max capability of the ship based directly on that.

so like if engine_health out of 100 is 70. probably reduce 1/4 of the speed the ship is capable of.

would this constitute a proper approach? or a newbie's only way he could conceive to do something.

also for something such as invisibility. this is my simple approach.
'When AI is firing at player it
check player invisibilty.

'this is the first statement of the AI "fire" code. so it checks whether player is invisible or not up front. If it evaluates to true, the code won't run. If false, then the player can be seen so it runs. or "fires at the player"

if invisible = false


now my thing is that will include lots of booleans basically. is it wrong or bad to have a lot of boolean variables? variables that you are relying on simply to check if something is true or false? Or is it normal for a game to have a lot, because the game constantly has to be checking for things in order to behave correctly, or behave the way you want it to.

In this case:

"dont fire on the playe if his invisibility = true."

is my approachs sound? or will i run into trouble?


SculptureOfSoul(Posted 2006) [#2]
Seems like a sound approach to me. One thing to consider is that you could make the speed of the ship a direct function of the health. So instead of having multiple cases such as

const MAXSPEED = 10
field current_max_speed:int

If health < 100 AND health > 80
setmaxspeed( MAXSPEED * .8)

if health <= 80 AND health > 60
setmaxspeed( MAXSPEED  *.6)


You could instead make it a function so that instead of having maybe 5 or 10 cases, you simply evaluate your function. e.g.:
setmaxspeed( MAXSPEED * (current_health/max_health))


This would set the players current_max_speed variable to the MAXSPEED of the ship times the players health percentage. So at 70% health, their maximum speed is 70% of the maximum max speed (ugh, too many max's in that sentence :).

Of course, the approach I outlined isn't better per se, just different. Sometimes it makes sense to have multiple unique cases, and sometimes it makes more sense to utilize a function equation. It's up to you and whatever will work better with your game design.

Regarding the AI code, that seems like a fair approach. If you want to take things further, look into "FSM"s, or "finite state machines". You could for example, have a "state" in the enemy ship that gets activated when a player suddenly goes invisible while inside of the enemies view radius. That state might be a "panic state", where the enemy suddenly starts firing at random or perhaps plays it defensively and tries a random 'escape route'.

Within each 'state' there are certain checks and conditionals that either resolve to a choice or a change of state. Further detailing the 'panic state', which I'll call INVIS_PLAYER in the code below, you might end up with pseudo code like this
select MY_STATE

case INVIS_PLAYER
    if health < 25%
        state = FLEE
    endif
   
if not HavePowerup( INVIS_SENSOR )
'if we don't have a sensor that lets us see
'the invisible player, lets look for one

 local Powerup:TPowerup
 Powerup = FINDNEARBYPOWERUP( INVIS_SHIP_SENSOR, 100 )
     'this function returns a refrence to the nearest specified powerup
     '(parameter 1) if one  found within a distance of parameter 2
If Powerup 'if we found one, lets start moving towards it
SetLocationTarget( Powerup )
      'this function will cause the ship to start moving
      'towards the powerup
endif
endif


'more checks and such here...


You could go quite a bit further of course, and have every action that happens to a player or enemy get 'sent' to it's current state in a manner akin to events being passed to a window - and then allowing each separate state to react in it's own way to an event. Anyhow, hope this helps.


Cruis.In(Posted 2006) [#3]
very helpful, thanks a lot.