Author Topic: Help with some array maths  (Read 3464 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Help with some array maths
« on: March 05, 2020, 12:48:42 pm »
So in Dragon Warrior the monster type encounter is a grid of 16x16 tiles per zone, and there are 8x8 zones in the world(128x128).
I've found an error in my setup that causes the zones to be skewed by +1 each zone. so zone 0,0 is perfect but zone 1,0 is off by 1 tile in x, and zone 0,1 is off 1 tile in y.  This pattern continues lineally, so  zone 5,0 is off +5 in x, and so forth.  but yet there is no gap in the zones, so there is no 0 value tile between zone 1,0 and zone 2,0.

Here is the code to generate the encounter zones,
Code: QB64: [Select]
  1. TYPE WorldData
  2.  Territory AS _BYTE
  3.  
  4. DIM World(127,127) AS WorldData
  5.  
  6. FOR i%% = 0 TO 7
  7.  FOR j%% = 0 TO 7
  8.   X1%% = 16 * j%%
  9.   Y1%% = 16 * i%%
  10.   Z%% = INT(RND *19)' dummy data for this thread  READ Z%% 'encouter territory
  11.   FOR Y%% = 0 TO 15
  12.    FOR X%% = 0 TO 15
  13.     World(X1%% + X%%, Y1%% + Y%%).Territory = Z%%
  14.   NEXT X%%, Y%%
  15. NEXT j%%, i%%

Can anyone see right off why its growing by an extra +1 each time?
I'm counting 0 to 15, so I do want to start on 16 for the next set right? 16+0(16) to 16+15(31) then 32+0(32) to 32+15(47)
where is that extra tile coming from?

I've attached the a visualization of this setup to show what I'm trying to reproduce.
https://gamefaqs.gamespot.com/nes/563408-dragon-warrior/map/5948?raw=1
Granted after becoming radioactive I only have a half-life!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Help with some array maths
« Reply #1 on: March 05, 2020, 01:07:06 pm »
Did you try?

Code: QB64: [Select]
  1.   X1%% = 15 * j%%
  2.   Y1%% = 15 * i%%
  3.  

Just flinging that out there as something that kind of jumped out at me...

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with some array maths
« Reply #2 on: March 05, 2020, 01:37:53 pm »
Not sure I understand problem but no gaps or overlaps here:
Code: QB64: [Select]
  1. TYPE WorldData
  2.     Territory AS _BYTE
  3. SCREEN _NEWIMAGE(600, 600, 32)
  4. DIM World(127, 127) AS WorldData
  5.  
  6. FOR i%% = 0 TO 7
  7.     FOR j%% = 0 TO 7
  8.         X1%% = 16 * j%%
  9.         Y1%% = 16 * i%%
  10.         Z%% = INT(RND * 19) ' dummy data for this thread  READ Z%% 'encouter territory
  11.         'PRINT X1, Y1
  12.         'LINE (X1%%, Y1%%)-STEP(15, 15), &H88FFFFFF, B '<<< using step( 15, 15) no overlap, using step(16, 16) get overlap
  13.         FOR Y%% = 0 TO 15
  14.             FOR X%% = 0 TO 15
  15.                 PSET (X1%% + X%%, Y1%% + Y%%), &H550000FF
  16.                 ' World(X1%% + X%%, Y1%% + Y%%).Territory = Z%%
  17.         NEXT X%%, Y%%
  18. NEXT j%%, i%%
  19.  
  20.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with some array maths
« Reply #3 on: March 05, 2020, 03:04:39 pm »
This looks odd
Quote
16+0(16) to 16+15(31) then 32+0(32) to 32+15(47)

0 to 15, 16 to 31, 32 to 47, 48 to 63, 64 to 79
= 16(n) + 0 to 16(n) + 15  here is formula I get, n = 0 to 7

or for x = 0 to 15
x1 = 16(n) + x

and that looks like what you are doing in example given.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Help with some array maths
« Reply #4 on: March 08, 2020, 11:46:49 am »
Did you try?

Code: QB64: [Select]
  1.   X1%% = 15 * j%%
  2.   Y1%% = 15 * i%%
  3.  

Just flinging that out there as something that kind of jumped out at me...

the only problem with that is 15*7 =105 + 15 = 120
needs to make it to 127 (map is 0-127)
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with some array maths
« Reply #5 on: March 08, 2020, 12:02:47 pm »
Zone 7 is 7 * 16, last tile is + 15 = 112 + 15 = 127,  0 to 127 is 128 tiles

Could go
Code: QB64: [Select]
  1. for tileY = 0 to 127
  2. for tileX = 0 to 127
  3.     world(tileX, tileY) = rndTerr
and skip the confusing double loop math
« Last Edit: March 08, 2020, 12:14:02 pm by bplus »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Help with some array maths
« Reply #6 on: March 09, 2020, 11:11:22 am »
Zone 7 is 7 * 16, last tile is + 15 = 112 + 15 = 127,  0 to 127 is 128 tiles

Could go
Code: QB64: [Select]
  1. for tileY = 0 to 127
  2. for tileX = 0 to 127
  3.     world(tileX, tileY) = rndTerr
and skip the confusing double loop math

save for this line I think you missed this
Z%% = INT(RND * 19) ' dummy data for this thread READ Z%% 'encounter territory

see where I commented out the READ, it doesn't actually get a random value assigned.
there is data that has to be read to assign the correct zone to the correct area. right now that data is only 1 line with 64 values.
while I probably could use that code, I would end up with 127 lines of data with 127 values each! dang thats a lot more of typing!

I do thank you for at least looking at this.  I think I have found a compromise that seems to work for 98% of the area, and the lowest zones will be slightly off but they are the most difficult so I don't think too many players will notice. it was just glaringly off in the weaker zones as that is where you spend the most time leveling up.

Have you checked Dragon Warrior out? Would love to know what you think of it.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with some array maths
« Reply #7 on: March 09, 2020, 11:51:58 am »
Quote
Have you checked Dragon Warrior out? Would love to know what you think of it.

I am not aficionado of this sort of game but man I appreciate all the effort you put into your games, I still remember what you did with eRATication, you made it into real game with splash screens, menus and music, the works! :)

I am guessing your shoulder has recovered and you are back to coding with vengeance, with Dragon Warrior!

I will check it later when all bugs worked out and things get a little sleepy with it.

One last try:
Code: QB64: [Select]
  1. DIM world(127, 127)
  2. FOR tileY = 0 TO 127
  3.     FOR tileX = 0 TO 127
  4.         zoneX = tileX \ 16: zoneY = tileY \ 16 ' 128x128 tiles broken in to 8x8 zones
  5.         zx = tileX MOD 16: zy = tileY MOD 16
  6.         world(zoneX * 16 + zx, zoneY * 16 + zy) = 1 'terr(zoneX, zoneY)
  7.         ' this cycle through all zones and 16 tiles in each and assigns number as function of zone and tile in that zone
  8.         PRINT "world("; ts$(zoneX * 16 + zx); ","; ts$(zoneY * 16 + zy); ") = terr("; ts$(zoneX); ",  "; ts$(zx); ",  "; ts$(zoneY); ",  "; ts$(zy); ")"
  9.         IF zx = 15 THEN INPUT "OK... "; OK$: CLS '<<<<<<<<<<<<<<<<<< Comment out this line to see how it ends
  10.     NEXT
  11.  
  12. FUNCTION ts$ (n)
  13.     ts$ = _TRIM$(STR$(n))

« Last Edit: March 09, 2020, 12:33:22 pm by bplus »

Offline Terrenful

  • Newbie
  • Posts: 1
    • View Profile
Re: Help with some array maths
« Reply #8 on: March 09, 2020, 01:10:33 pm »
How long have you been working on the Dragon Warrior by the way?