Code: Select all

```
;character initiative ###
.C:5421 A9 01 LDA #$01 ;start with 1st character
.C:5423 85 0D STA $0D ;store character no. in $0D
.C:5425 A5 0D LDA $0D ;load character no. in A
.C:5427 0A ASL A ;double A by moving bit positions to the left
.C:5428 AA TAX ;transfer A to X
.C:5429 BD 8D 09 LDA $098D,X ;load character address (LSB)
.C:542c 85 50 STA $50 ;store it
.C:542e BD 8E 09 LDA $098E,X ;load character address (MSB)
.C:5431 85 51 STA $51 ;store it
.C:5433 A0 00 LDY #$00 ;set y to byte 0 of character
.C:5435 B1 50 LDA ($50),Y ;load first letter of character
.C:5437 D0 03 BNE $543C ;if first letter of character > 0 => jump
.C:5439 4C D4 54 JMP $54D4 ;else jump to slot S initiative creation
.C:543c 20 CE 18 JSR $18CE ;create two random numbers between 00-FF and store them in $5A and $5B
.C:543f A0 3C LDY #$3C ;set y to byte 3C of character
.C:5441 B1 50 LDA ($50),Y ;load MSB of number of won combats
.C:5443 4A LSR A ;half A by moving bit positions to the right
.C:5444 85 3B STA $3B ;store it in $3B
.C:5446 A0 11 LDY #$11 ;set y to byte 11h of character
.C:5448 B1 50 LDA ($50),Y ;load attributes (the byte which includes dex)
.C:544a 29 1F AND #$1F ;AND with 1F (only the 5 bits from the right can be 1)
.C:544c 38 SEC
.C:544d E9 0E SBC #$0E ;substract 0E => dex18=>4;dex17=>3;dex16=>2;dex15=1
.C:544f 90 0A BCC $545B ;if result <= 0 (no bonus) => jump
.C:5451 0A ASL A ;shift A left
.C:5452 0A ASL A ;shift A left
.C:5453 0A ASL A ;shift A left => dex bonus: dex18=>20h;dex17=>18h;dex16=>10h;dex15=>8h
.C:5454 18 CLC
.C:5455 65 3B ADC $3B ;add initiative value ($5444) to A (dex bonus)
.C:5457 B0 63 BCS $54BC ;if > FF (max initiative reached) => jump
.C:5459 85 3B STA $3B ;store result
.C:545b A5 5B LDA $5B ;load random number ($543C) in A
.C:545d 29 1F AND #$1F ;use only the 5 bits from the right (value: 0-31)
.C:545f 18 CLC
.C:5460 65 3B ADC $3B ;add initiative value ($5455) to A (random number)
.C:5462 B0 58 BCS $54BC ;if > FF (max initiative reached) => jump
.C:5464 85 3B STA $3B ;store result
.C:5466 A0 22 LDY #$22 ;set y to byte 22h of character
.C:5468 B1 50 LDA ($50),Y ;load MSB of current character level
.C:546a 85 64 STA $64 ;store it
.C:546c C8 INY ;set y to byte 23h of character
.C:546d B1 50 LDA ($50),Y ;load LSB of current character level
.C:546f 46 64 LSR $64 ;shift A bitwise right
.C:5471 6A ROR A ;rotate A
.C:5472 85 63 STA $63 ;store it
.C:5474 A5 64 LDA $64 ;load MSB of current character level in A
.C:5476 F0 08 BEQ $5480 ;if A = 0 (character level < 256) => jump
.C:5478 A9 00 LDA #$00 ;0 => A
.C:547a 85 64 STA $64 ;set MSB of current level to 0
.C:547c A9 40 LDA #$40 ;40h (64dec) => A (load max level for bonus to A)
.C:547e 85 63 STA $63 ;store max level for bonus in LSB current character level
.C:5480 A5 63 LDA $63 ;load LSB of current character level
.C:5482 C9 41 CMP #$41 ;compare it with 41h (65dec)
.C:5484 90 04 BCC $548A ;if LSB of current character level < 41h (65dec) => jump
.C:5486 A9 40 LDA #$40 ;else: set LSB of current character level to 40h (64dec)
.C:5488 85 63 STA $63 ;store it
.C:548a A0 38 LDY #$38 ;set y to byte 38h of character
.C:548c B1 50 LDA ($50),Y ;load class of character
.C:548e F0 1A BEQ $54AA ;if class = 0 (warrior) => jump
.C:5490 C9 07 CMP #$07 ;compare character class with 07h
.C:5492 F0 16 BEQ $54AA ;if class = 7 (paladin) => jump
.C:5494 C9 08 CMP #$08 ;compare character class with 08h
.C:5496 F0 12 BEQ $54AA ;if class = 8 (hunter) => jump
.C:5498 C9 05 CMP #$05 ;compare character class with 05h
.C:549a 90 13 BCC $54AF ;if class < 5 (conjuror, magician, sorcerer, wizard) => jump
.C:549c C9 09 CMP #$09 ;compare character class with 09h
.C:549e D0 16 BNE $54B6 ;if class <> 9 (no monk) => jump
.C:54a0 A5 63 LDA $63 ;load current character level (LSB; max = 64)
.C:54a2 0A ASL A ;double it (level/class bonus monk => 2*level)
.C:54a3 18 CLC
.C:54a4 65 3B ADC $3B ;add initiative value ($5460) to A (level/class bonus)
.C:54a6 B0 14 BCS $54BC ;if > FF (max initiative reached) => jump
.C:54a8 90 14 BCC $54BE ;else (max initiative not reached) => jump
.C:54aa A5 63 LDA $63 ;load current character level (LSB; max = 64; level/class bonus warrior/paladin/hunter => level)
.C:54ac 4C A3 54 JMP $54A3 ;=> jump
.C:54af A5 63 LDA $63 ;load current character level (LSB; max = 64)
.C:54b1 4A LSR A ;half it
.C:54b2 4A LSR A ;half if (level/class bonus conjuror/magician/sorcerer/wizard => level/4)
.C:54b3 4C A3 54 JMP $54A3 ;=> jump
.C:54b6 A5 63 LDA $63 ;load current character level (LSB; max = 64)
.C:54b8 4A LSR A ;half it (level/class bonus bard/rouge => level/2)
.C:54b9 4C A3 54 JMP $54A3 ;=> jump
.C:54bc A9 FF LDA #$FF ;if max bonus was reached: FF => A
.C:54be A6 0D LDX $0D ;character no. => X
.C:54c0 C9 00 CMP #$00 ;compare A with 0
.C:54c2 D0 02 BNE $54C6 ;if A not 0 (if initiative not 0) => jump
.C:54c4 A9 01 LDA #$01 ;if initiative = 0: 01 (min initiative) => A
.C:54c6 9D 0F 83 STA $830F,X ;store character initiative in table (1-6) [finished for this character]
.C:54c9 E6 0D INC $0D ;increase $0D by 1 (next character)
.C:54cb A5 0D LDA $0D ;load next character no.
.C:54cd C9 07 CMP #$07 ;compare with 7
.C:54cf B0 03 BCS $54D4 ;if character no. = 7 [finished for all characters] => jump
.C:54d1 4C 25 54 JMP $5425 ;else: calculate initiative for next character
;slot S initiative ###
.C:54d4 20 CE 18 JSR $18CE ;create two random numbers between 00-FF and store them in $5A and $5B
.C:54d7 AD 00 CB LDA $CB00 ;load first letter of solt S
.C:54da F0 18 BEQ $54F4 ;if first letter of character = 0 (slot S is empty) => jump
.C:54dc AD 15 CB LDA $CB15 ;load attack value of monster
.C:54df 29 1F AND #$1F ;use only the 5 bits from the right
.C:54e1 0A ASL A ;double it
.C:54e2 0A ASL A ;double it
.C:54e3 8D EC 54 STA $54EC ;store it
.C:54e6 A5 5A LDA $5A ;load random number in A
.C:54e8 29 1F AND #$1F ;use only the 5 bits from the right (value: 0-31)
.C:54ea 18 CLC
.C:54eb 69 00 ADC #$00 ;add modified attack value ($54E3) to the random number
.C:54ed 90 02 BCC $54F1 ;if A <= FF (max initiative not reached) => jump
.C:54ef A9 FF LDA #$FF ;if max initiative was reached: FF => A
.C:54f1 8D 0F 83 STA $830F ;store slot S initiative [finished]
;monster initiative ###
.C:54f4 A9 00 LDA #$00 ;set monster group (0-3) to 0
.C:54f6 85 0D STA $0D ;store it
.C:54f8 A6 0D LDX $0D ;load monster group => X
.C:54fa BC B0 03 LDY $03B0,X ;load monster type => Y
.C:54fd F0 32 BEQ $5531 ;if Y = 0 (no monster in this group) => jump
.C:54ff B9 28 7E LDA $7E28,Y ;load attack value of monster
.C:5502 29 1F AND #$1F ;use only the 5 bits from the right
.C:5504 0A ASL A ;double it
.C:5505 0A ASL A ;double it
.C:5506 8D 24 55 STA $5524 ;store it
.C:5509 BD D3 7C LDA $7CD3,X ;load monster address (LSB)
.C:550c 8D 2D 55 STA $552D ;store it
.C:550f BD D7 7C LDA $7CD7,X ;load monster address (MSB)
.C:5512 8D 2C 55 STA $552C ;store it
.C:5515 BC B8 03 LDY $03B8,X ;load number of monsters in group
.C:5518 F0 17 BEQ $5531 ;if Y = 0 (no monsters in this group) => jump
.C:551a 88 DEY ;decrease number of monsters in group by 1
.C:551b 20 CE 18 JSR $18CE ;create two random numbers between 00-FF and store them in $5A and $5B
.C:551e A5 5A LDA $5A ;load random number in A
.C:5520 29 1F AND #$1F ;use only the 5 bits from the right (value: 0-31)
.C:5522 18 CLC
.C:5523 69 00 ADC #$00 ;add modified attack value ($5506) to the random number
.C:5525 90 02 BCC $5529 ;if A <= FF (max initiative not reached) => jump
.C:5527 A9 FF LDA #$FF ;if max initiative was reached: FF => A
.C:5529 09 01 ORA #$01 ;OR A with 1 => set initiative to a minimum of 1 if it was below
.C:552b 99 00 20 STA $2000,Y ;store monster initiative in address which was created ($550C and $5512)
.C:552e 88 DEY ;decrease number of monsters in group by 1
.C:552f 10 EA BPL $551B ;if Y > 0 (more monsters in group) => jump
.C:5531 A5 0D LDA $0D ;else: load monster group in A
.C:5533 C9 04 CMP #$04 ;compare A with 4
.C:5535 B0 08 BCS $553F ;if A >= 4 (last monster group was reached) => jump
.C:5537 C5 3A CMP $3A ;compare A with # of monster groups (0-3)
.C:5539 B0 04 BCS $553F ;if A >= # of monster groups => jump
.C:553b E6 0D INC $0D ;increase monster group by 1 (next one)
.C:553d D0 B9 BNE $54F8 ;if content of $0D <> 0 => jump
.C:553f 60 RTS ;return
```