Schrijver
| 16 bit life-counter
|
norakomi msx professional Berichten: 861 | Geplaatst: 02 Juni 2006, 13:03   |
I want to have a routine that decreases a 16 bit value, and gives a flag when 0 is reached.
Example: an endboss has 400 life-points, whenever he gets hit he looses 1 life.
I was thinking about this, but it doesnt look very good/fast:
at startup:
LD HL,400
LD (Bosslife),HL
in game when boss is hit:
XOR A ;reset carry
LD HL,(Bosslife)
LD DE,1
SBC HL,DE
LD (Bosslife),HL
JP Z,BossDies
Does anyone have a faster/better way to do this ?
thank you !
|
|
zeilemaker54 msx lover Berichten: 97 | Geplaatst: 02 Juni 2006, 13:52   |
Quote:
| I want to have a routine that decreases a 16 bit value, and gives a flag when 0 is reached.
Example: an endboss has 400 life-points, whenever he gets hit he looses 1 life.
I was thinking about this, but it doesnt look very good/fast:
at startup:
LD HL,400
LD (Bosslife),HL
in game when boss is hit:
XOR A ;reset carry
LD HL,(Bosslife)
LD DE,1
SBC HL,DE
LD (Bosslife),HL
JP Z,BossDies
Does anyone have a faster/better way to do this ?
thank you !
|
What about:
LD HL,(Bosslife)
DEC HL
LD (Bosslife),HL
LD A,H
OR L
JP Z,BossDies
|
|
BiFi msx guru Berichten: 3142 | Geplaatst: 02 Juni 2006, 14:16   |
why change that routine? this way you can subtract faster by increasing DE... for example depending on the strength of your weapon.
|
|
GhostwriterP msx addict Berichten: 312 | Geplaatst: 02 Juni 2006, 14:19   |
I came up with the following. Note that you have to add 256 to the boss life (ld hl,400+256).
bosslifecheck
ld hl,Bosslife
dec (hl)
ret nz
inc hl
dec (hl)
ret This routine is so fast you could call it a few times if you are fireing with a stonger weapon  |
|
ro msx guru Berichten: 2329 | Geplaatst: 02 Juni 2006, 14:52   |
that routine only decreases bosslife by one... that 'll take long for the player to kill the basterd 
the initial routine will do just fine, but dunno if Z flag is set when substraction crosses 0... |
|
GhostwriterP msx addict Berichten: 312 | Geplaatst: 02 Juni 2006, 14:59   |
Here is a routine that supports stonger 'weapons'.
in: b = strenght
hl -> Bosslife (pointer)
out: carry if zero
lifecheck
ld a,(hl)
sub b
ld (hl),a
jr z,.hup
ret nc
.hup
xor a
inc hl
dec (hl)
jp nz,.hup2
scf
.hup2
retuse it the following way
ld hl,Bosslife
ld b,5
call lifecheck
jp c,BossDead |
|
GhostwriterP msx addict Berichten: 312 | Geplaatst: 02 Juni 2006, 15:12   |
Hmmm does it really work how it is supposed to...
Let's shorten the code a bit:
in: b = strenght
hl -> Bosslife (pointer)
out: carry if zero
lifecheck
ld a,(hl)
sub b
ld (hl),a
ret nc
inc hl
dec (hl)
ret z
ccf
retThis should work fine too. Just shoot one more bullet  (In case you reach exactly zero that is) |
|
norakomi msx professional Berichten: 861 | Geplaatst: 02 Juni 2006, 15:33   |
Quote:
| What about:
LD HL,(Bosslife)
DEC HL
LD (Bosslife),HL
LD A,H
OR L
JP Z,BossDies
|
Doesnt that give a zero flag when H=L instead of when HL=0 ?? Quote:
| I came up with the following. Note that you have to add 256 to the boss life (ld hl,400+256).
bosslifecheck
ld hl,Bosslife
dec (hl)
ret nz
inc hl
dec (hl)
ret
|
this one is great (if it works). I dont quite get it though. DEC (HL) doesnt set any flags... ??? |
|
norakomi msx professional Berichten: 861 | Geplaatst: 02 Juni 2006, 15:36   |
Quote:
| Hmmm does it really work how it is supposed to...
Let's shorten the code a bit:
in: b = strenght
hl -> Bosslife (pointer)
out: carry if zero
lifecheck
ld a,(hl)
sub b
ld (hl),a
ret nc
inc hl
dec (hl)
ret z
ccf
retThis should work fine too. Just shoot one more bullet  (In case you reach exactly zero that is)
|
i get it, that looks good, Im gonna give it a try !! thanx |
|
GhostwriterP msx addict Berichten: 312 | Geplaatst: 02 Juni 2006, 15:48   |
You're welcome, just remember to add 256 to the life meter.
And you could lose all those calls and rets to speed things up.
|
|
GhostwriterP msx addict Berichten: 312 | Geplaatst: 02 Juni 2006, 15:55   |
Quote:
| Doesnt that give a zero flag when H=L instead of when HL=0 ?
|
ld a,h / cp l gives z if h=l. ld a,h / or l is valid for testing hl=0
Quote:
| this one is great (if it works). I dont quite get it though. DEC (HL) doesnt set any flags... ???
|
It does set flags just no carry flag. |
|
norakomi msx professional Berichten: 861 | Geplaatst: 02 Juni 2006, 19:53   |
Great @@  gracias |
|
Anvil msx user Berichten: 36 | Geplaatst: 06 Juni 2006, 16:41   |
Hint: when you want to subtract a constant value from HL like this:
ld de,32
or a
sbc hl,de
... replace with addition of negatief constant:
ld de,-32
add hl,de
This will save you 1 instruction.
|
|
GhostwriterP msx addict Berichten: 312 | Geplaatst: 10 Juni 2006, 16:42   |
Quote:
| This will save you 1 instruction.
|
Thus testing for smaller than zero like:
bit 7,h
jp nz,blablabla
(note that hl must be smaller than 32768 to begin with)
instead of:
jp c,blablabla
But still a very usefull hint  |
|
arnold_m msx lover Berichten: 81 | Geplaatst: 10 Juni 2006, 23:07   |
Anvill suggested adding a negative value rather than subtracting a positive value, but GhostwriterP sees a problem:
Quote:
| Quote:
| This will save you 1 instruction.
|
Thus testing for smaller than zero like:
bit 7,h
jp nz,blablabla
(note that hl must be smaller than 32768 to begin with)
instead of:
jp c,blablabla
But still a very usefull hint 
|
With Anvills trick you can just use
jp nc,blablabla
and hl could start at over 32768.
Such high values for life counters may not be a very good idea though; one can easily create monsters that take too much time to get killed or one can introduce the suggestion of an accuracy that does not really make sense. |
|
|
|
|