A86: Re: Efficiency Problem


[Prev][Next][Index][Thread]

A86: Re: Efficiency Problem






>
>The following is a fragment(?) of code from a game I'm working on called
Nerd
>Quest I. It's the a part that displays the current weapon on the status
>screen. It seems to me that this code is incredibly ineffiecient, and
there's
>gotta be a faster way of doing this. (Perhaps by using that evil
call-counting
>thing mentioned earlier?) BTW, if any of you have suggestions for "nerd"
>weapons, I'd like to hear them! (I'm especially interested in "single-use"
>type ones, e.g. HCl, Test Tube, Inhaler, Bottle Rocket, etc.)
>
>Now, the inefficient code!
>
>GameLoop:
> call CLS ;byte-saving thing
> ...
> ...
> ld a,(Weapon) ;Weapon EQU $F602
> cp 0
> call z,Wp1
> cp 1
> call z,Wp2
> cp 2
> call z,Wp3
> cp 3
> call z,Wp4
> cp 4
> call z,Wp5
> cp 5
> call z,Wp6
> cp 6
> call z,Wp7
> cp 7
> call z,Wp8
> cp 8
> call z,Wp9
> cp 9
> call z,Wp10
> cp 10
> call z,Wp11
> cp 11
> call z,Wp12
> cp 12
> call z,Wp13
> cp 13
> call z,Wp14
> cp 14
> call z,Wp15
> cp 15
> call z,Wp16
> call _puts
> ...
> jp Somewhere
>
>Wp1:
> ld hl,W1
> ret
>
>Wp2:
> ld hl,W2
> ret
>
>Wp3:
> ld hl,W3
> ret
>
>Wp4:
> ld hl,W4
> ret
>
>Wp5:
> ld hl,W5
> ret
>
>Wp6:
> ld hl,W6
> ret
>
>Wp7:
> ld hl,W7
> ret
>
>Wp8:
> ld hl,W8
> ret
>
>Wp9:
> ld hl,W9
> ret
>
>Wp10:
> ld hl,W10
> ret
>
>Wp11:
> ld hl,W11
> ret
>
>Wp12:
> ld hl,W12
> ret
>
>Wp13:
> ld hl,W13
> ret
>
>Wp14:
> ld hl,W14
> ret
>
>Wp15:
> ld hl,W15
> ret
>
>Wp16:
> ld hl,W16
> ret
>
>... ;Whole Lotta Code!

why don't you set up a table like this:

table:
.dw W1
.dw W2
.dw W3

etc

then do this:
 ld a,(Weapon)
 ld hl, table
 add a,a
 ld e,a
 ld d,0
 add hl,de
 call _ldhlind

and hl will point to the right string

>
>;Data Section
>W1: .db "AOL Disk",0
>W2: .db "Pocket Protector",0
>W3: .db "Uniball Pen",0
>W4:  .db "Ruler",0
>W5: .db "Ugly Stick",0
>W6: .db "Old Game Boy",0
>W7: .db "Linux Reference Book",0
>W8: .db "Linux CD",0
>W9: .db "Cokebottle Glasses",0
>W10: .db "AP Calc Textbook",0
>W11: .db "MS Intellimouse",0
>W12: .db "Macintosh LC",0
>W13: .db "Overhead Projector",0
>W14: .db "TI-92+",0
>W15: .db "486 Motherboard",0
>W16: .db "Soldering Iron",0