Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2   Go Down

Author Topic: How to use Bank and combine it with an array?  (Read 11073 times)

0 Members and 3 Guests are viewing this topic.

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
How to use Bank and combine it with an array?
« on: October 10, 2013, 11:34:56 PM »

I have being reading the manual of AMOS about bank and it seems interesting. My favourite part is that I can place variables only on the FAST ram and anything to do with graphics, audio must go on the CHIP side of things. However as I read through it I am confused in how to use it in my project?

For example this is what I want to do....

NewType boardsteps
   int X1
   int Y1
   int X2
   int Y2
   boolean twolinesformed
End NewType

Deftype.boardsteps Board(32,21)

Then for example I can do this:

Board(0,0)\X1=12, Board(0,0)\Y1=14,
Board(0,0)\X2=114, Board(0,0)\Y2=13, Board(0,0)\twolinesformed=false


Why do I need to do that? Simple, in the board there are 30 dots from left to right and 20 dots from top to bottom giving a total of 600 dots in the entire board.....so if I have something like this:

O1 O2 O3
O4 O5 O6
O7 O8 O9

If the user clicks on O1 it stores in board(0,0)\X1=14, board(0,0)\Y1=4 and then a red dot is plotted on O1. Now the user have another round to plot another dot..so he selects on O2.

There the board(0,0)\X2=18, board(0,0)\Y2=4 is stored...then the game checks to see if board(0,0) and board(0,0) X1, Y1, X2, Y2 plot are in the valid location...if they are...then it stores the X2 and Y2 in board(0,0) accordingly. After that..it plots the red dot on the second dot selected...then it forms a red line between the first do and the second dot. Then in that board(0,0)\twolinesformed=true.

Now turn 2 starts and it is the second player...here the player selects again another dot on the board and instead of being board(0,0), the game checks to see if board(0,0) is used..if it is..it increments the second index board(1,0) if that is used it increments again and becomes board(2,0) if that is not used then it adds the values there and repeats.

Now this is how I want to achieve it..the question comes in how do I do that with banks and array and how do I declare an array with type bank like I showed you before and how do I store all that in FAST RAM only?

Thanks in advance.
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #1 on: October 11, 2013, 03:18:55 PM »

How big of an int do you need to use?  If it's between 0 and 255 at all times, an unsigned byte will be sufficient.  A boolean should be a byte also.  That being the case, I'll make a tile-map with a header that stores the size of the array and the array itself.

Code: [Select]
rem assumes int is a byte and boolean is a byte
rem size of boardsteps is 4*int+1=5 bytes
boardsteps=5
rem offsets within the 5 byte space are as follows
x1=0
y1=1
x2=2
y2=3
twolinesformed=4
rem now for the header to store the size of the array
boardheader=2
rem offsets within the header
boardx=0
boardy=1
rem now to allocate the board
board=boardheader+(32*21*boardsteps)
Reserve As Work 15,board
poke start(15)+boardx,32
poke start(15)+boardy,21
def fn boardarray(x,y,offset,banknum) boardheader+offset+((x+y*(peek(start(banknum)+boardx))*boardsteps)
rem here's how to store something in twolinesformed with x=0 and y=3
poke fn boardarray(0,3,twolinesformed,15), TRUE
rem you read it back with
print peek (fn boardarray(0,3,twolinesformed,15)
rem if you want to do a value range check for the array here it is
def fn rngchk(x,y,offset, banknum) (x<peek(start(banknum)+boardx)) and (y<peek(start(banknum)+boardy)) and (offset<boardsteps)
if fn rngchk(0,3,twolinesformed,15) then poke fn boardarray(0,3,twolinesformed,15),FALSE
« Last Edit: October 11, 2013, 03:34:26 PM by SamuraiCrow »
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #2 on: October 11, 2013, 03:29:15 PM »

If you need int to be 16 bits for values between 0 and 65535 it gets trickier but not by much.  You Deek and Doke the int offsets and Peek and Poke the boolean normally.  Also, the size of the record must be even to insure that all of the ints fall on even addresses.

Code: [Select]
rem assumes int is a word and boolean is a byte
rem size of boardsteps is 4*int+1=9 bytes
rem since 9 is not even we add a pad byte that will remain unused.
boardsteps=10
rem offsets within the 10 byte space are as follows
x1=0
y1=2
x2=4
y2=6
twolinesformed=8
rem now for the header to store the size of the array
boardheader=4
rem offsets within the header
boardx=0
boardy=2
rem now to allocate the board
board=boardheader+(32*21*boardsteps)
Reserve As Work 15,board
Doke start(15)+boardx,32
Doke start(15)+boardy,21
def fn boardarray(x,y,offset,banknum) boardheader+offset+((x+y*(Deek(start(banknum)+boardx))*boardsteps)
rem here's how to store something in twolinesformed with x=0 and y=3
poke fn boardarray(0,3,twolinesformed,15), TRUE
rem you read it back with
print peek (fn boardarray(0,3,twolinesformed,15)
rem if you want to do a value range check for the array here it is
def fn rngchk(x,y,offset, banknum) (x<Deek(start(banknum)+boardx)) and (y<Deek(start(banknum)+boardy)) and (offset<boardsteps-1)
if fn rngchk(0,3,twolinesformed,15) then poke fn boardarray(0,3,twolinesformed,15),FALSE
rem to read x1 you do this
print Deek(fn boardarray(0,3,x1,15)
« Last Edit: October 11, 2013, 03:35:10 PM by SamuraiCrow »
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to use Bank and combine it with an array?
« Reply #3 on: October 11, 2013, 06:42:18 PM »

How big of an int do you need to use?  If it's between 0 and 255 at all times, an unsigned byte will be sufficient.  A boolean should be a byte also.  That being the case, I'll make a tile-map with a header that stores the size of the array and the array itself.

Code: [Select]
rem assumes int is a byte and boolean is a byte
rem size of boardsteps is 4*int+1=5 bytes
boardsteps=5
rem offsets within the 5 byte space are as follows
x1=0
y1=1
x2=2
y2=3
twolinesformed=4
rem now for the header to store the size of the array
boardheader=2
rem offsets within the header
boardx=0
boardy=1
rem now to allocate the board
board=boardheader+(32*21*boardsteps)
Reserve As Work 15,board
poke start(15)+boardx,32
poke start(15)+boardy,21
def fn boardarray(x,y,offset,banknum) boardheader+offset+((x+y*(peek(start(banknum)+boardx))*boardsteps)
rem here's how to store something in twolinesformed with x=0 and y=3
poke fn boardarray(0,3,twolinesformed,15), TRUE
rem you read it back with
print peek (fn boardarray(0,3,twolinesformed,15)
rem if you want to do a value range check for the array here it is
def fn rngchk(x,y,offset, banknum) (x<peek(start(banknum)+boardx)) and (y<peek(start(banknum)+boardy)) and (offset<boardsteps)
if fn rngchk(0,3,twolinesformed,15) then poke fn boardarray(0,3,twolinesformed,15),FALSE

Thank you so much for your reply :) I just have couple of questions I need to ask as a learning purpose and understand how it works all together. If I do not understand it...I will always get frustrated, I will always ask people to solve it for me...and I will not feel good and feel like a junior programmer all the time. But ones you teach me how it works....ones I understand it the same way I understand how I am typing these words....I will be able to do it in my own...and I will feel good about myself....not be that annoying monkey in your backs...and at the same time I will have the confident to make better and more complex games that I have always dreamed to do...plus have fun with my code  ;D.

Code: [Select]
rem assumes int is a byte and boolean is a byte
rem size of boardsteps is 4*int+1=5 bytes
boardsteps=5
Is boardsteps the same as the one I have done at the beginning of the post NewType.boardsteps? Is the reason you put five because I have five variables which are the following:  int X1,int Y1,int X2,int Y2,boolean twolinesformed?

Code: [Select]
rem offsets within the 5 byte space are as follows
x1=0
y1=1
x2=2
y2=3

I assume the x1,y1,x2,y2 is obtained from the

NewType.boardsteps
   x1
   y1
   x2
   y2
end newtype

Am I correct so far?

Here in this line of code you said twolinesformed=8. Why 8? Why not True of False?

Code: [Select]
rem now for the header to store the size of the array
boardheader=4

The above code I do not understand. Why you made a variable boardheader and why you set it to 4 and not 2 or 3 or any number, why 4? You said in the commentary that it store the size of the array...but the array I want is board(32,21) with type boardsteps.

Now you lost me in the below code O_O:

Code: [Select]
rem offsets within the header
boardx=0
boardy=2

Why do we need header and why do we need offset?

Now come to the most complicated part of the code
Code: [Select]
rem now to allocate the board
board=boardheader+(32*21*boardsteps)
Reserve As Work 15,board
poke start(15)+boardx,32
poke start(15)+boardy,21

OK you declared a variable called board but what is that formula you made above there where boardheader+(32*21*boardsteps) I do not understand how this work. So boardheader equals to 4 and what is 32 * 21 * boardsteps (where boardsteps equal to 5). I do not understand all how this works at all...and how is that equivalent to board(30, 31).

Now here you have the code Reserve As Work 15, board can I have it as Reserve As Fast 15, board...second of all what is 15 and why you chose board right after 15.?

poke start(15)+boardx,32
poke start(15)+boardy,21

Is that above the same as arrayvariable(32,21)?

Code: [Select]
def fn boardarray(x,y,offset,banknum) boardheader+offset+((x+y*(peek(start(banknum)+boardx))*boardsteps)
rem here's how to store something in twolinesformed with x=0 and y=3
poke fn boardarray(0,3,twolinesformed,15), TRUE
rem you read it back with
print peek (fn boardarray(0,3,twolinesformed,15)
rem if you want to do a value range check for the array here it is
def fn rngchk(x,y,offset, banknum) (x<peek(start(banknum)+boardx)) and (y<peek(start(banknum)+boardy)) and (offset<boardsteps)
if fn rngchk(0,3,twolinesformed,15) then poke fn boardarray(0,3,twolinesformed,15),FALSE

Ok the above code I do understand a single bit of it....can you explain the things above in terms of language I understand...thanks?
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #4 on: October 11, 2013, 10:21:03 PM »

How big of an int do you need to use?  If it's between 0 and 255 at all times, an unsigned byte will be sufficient.  A boolean should be a byte also.  That being the case, I'll make a tile-map with a header that stores the size of the array and the array itself.

Code: [Select]
rem assumes int is a byte and boolean is a byte
rem size of boardsteps is 4*int+1=5 bytes
boardsteps=5
rem offsets within the 5 byte space are as follows
x1=0
y1=1
x2=2
y2=3
twolinesformed=4
rem now for the header to store the size of the array
boardheader=2
rem offsets within the header
boardx=0
boardy=1
rem now to allocate the board
board=boardheader+(32*21*boardsteps)
Reserve As Work 15,board
poke start(15)+boardx,32
poke start(15)+boardy,21
def fn boardarray(x,y,offset,banknum) boardheader+offset+((x+y*(peek(start(banknum)+boardx))*boardsteps)
rem here's how to store something in twolinesformed with x=0 and y=3
poke fn boardarray(0,3,twolinesformed,15), TRUE
rem you read it back with
print peek (fn boardarray(0,3,twolinesformed,15)
rem if you want to do a value range check for the array here it is
def fn rngchk(x,y,offset, banknum) (x<peek(start(banknum)+boardx)) and (y<peek(start(banknum)+boardy)) and (offset<boardsteps)
if fn rngchk(0,3,twolinesformed,15) then poke fn boardarray(0,3,twolinesformed,15),FALSE

Thank you so much for your reply :) I just have couple of questions I need to ask as a learning purpose and understand how it works all together. If I do not understand it...I will always get frustrated, I will always ask people to solve it for me...and I will not feel good and feel like a junior programmer all the time. But ones you teach me how it works....ones I understand it the same way I understand how I am typing these words....I will be able to do it in my own...and I will feel good about myself....not be that annoying monkey in your backs...and at the same time I will have the confident to make better and more complex games that I have always dreamed to do...plus have fun with my code  ;D.
No problem!
Code: [Select]
rem assumes int is a byte and boolean is a byte
rem size of boardsteps is 4*int+1=5 bytes
boardsteps=5
Is boardsteps the same as the one I have done at the beginning of the post NewType.boardsteps? Is the reason you put five because I have five variables which are the following:  int X1,int Y1,int X2,int Y2,boolean twolinesformed?
In my example, boardsteps is the size of of the boardsteps type in bytes.
Code: [Select]
rem offsets within the 5 byte space are as follows
x1=0
y1=1
x2=2
y2=3

I assume the x1,y1,x2,y2 is obtained from the

NewType.boardsteps
   x1
   y1
   x2
   y2
end newtype

Am I correct so far?
Yes.
Here in this line of code you said twolinesformed=8. Why 8? Why not True of False?
It's an offset.  It's not a value, it's a location within the structure type.
Code: [Select]
rem now for the header to store the size of the array
boardheader=4

The above code I do not understand. Why you made a variable boardheader and why you set it to 4 and not 2 or 3 or any number, why 4? You said in the commentary that it store the size of the array...but the array I want is board(32,21) with type boardsteps.

Now you lost me in the below code O_O:

Code: [Select]
rem offsets within the header
boardx=0
boardy=2

Why do we need header and why do we need offset?
The header is used to store the dimensions of the array.  It is used by the other formulas.
Now come to the most complicated part of the code
Code: [Select]
rem now to allocate the board
board=boardheader+(32*21*boardsteps)
Reserve As Work 15,board
poke start(15)+boardx,32
poke start(15)+boardy,21

OK you declared a variable called board but what is that formula you made above there where boardheader+(32*21*boardsteps) I do not understand how this work. So boardheader equals to 4 and what is 32 * 21 * boardsteps (where boardsteps equal to 5). I do not understand all how this works at all...and how is that equivalent to board(30, 31).
Start(15)+boardheader marks the start of the actual array.  Also, I gave two examples and you're mixing up between them.  In the first example boardheader was only 2 bytes of storage.  This is dimensioning an array manually in memory.  Since in the second example, I used word-sized dimensions the array can grow bigger.  Generally I avoid using longword-sized offsets unless they are needed because it slows the system down.  This is how a regular array would work.
Now here you have the code Reserve As Work 15, board can I have it as Reserve As Fast 15, board...second of all what is 15 and why you chose board right after 15.?

poke start(15)+boardx,32
poke start(15)+boardy,21

Is that above the same as arrayvariable(32,21)?
That is storing the dimensions of the array in the header.  Yes.
Code: [Select]
def fn boardarray(x,y,offset,banknum) boardheader+offset+((x+y*(peek(start(banknum)+boardx))*boardsteps)
rem here's how to store something in twolinesformed with x=0 and y=3
poke fn boardarray(0,3,twolinesformed,15), TRUE
rem you read it back with
print peek (fn boardarray(0,3,twolinesformed,15)
rem if you want to do a value range check for the array here it is
def fn rngchk(x,y,offset, banknum) (x<peek(start(banknum)+boardx)) and (y<peek(start(banknum)+boardy)) and (offset<boardsteps)
if fn rngchk(0,3,twolinesformed,15) then poke fn boardarray(0,3,twolinesformed,15),FALSE

Ok the above code I do understand a single bit of it....can you explain the things above in terms of language I understand...thanks?

The def fn command defines a function with a return value of type integer.  It's essentially like a one-line procedure with a return value.  The formulas contained in them are the way to do an array lookup when doing things manually.

If you use arrays in your code, the compiler or interpreter generates all of this for you.  If you do banks of memory in Amos you have to do in manually.  Sometimes this is desirable since you can use integers that are smaller than 4 bytes each, as I have done in these examples.  On an Amiga 500 (or any 16-bit Amiga) the smaller 2-byte integers access at twice the speed of 4-byte integers in addition to taking less memory.

Peek reads a byte from memory.  Poke writes a byte to memory.  Start(15) is the address of bank 15's contents in memory.  Deek reads a 2-byte word from memory if it is properly on an even address (otherwise it causes a bus error).  Likewise Doke writes a word of memory to an even address.

Once you read the answers to the earlier questions, it should make more sense.
« Last Edit: October 11, 2013, 10:23:36 PM by SamuraiCrow »
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to use Bank and combine it with an array?
« Reply #5 on: October 12, 2013, 03:19:22 AM »

OK the first thing you said that twolinesformed=8 is an actual location it is an offset and not a value. My question why would i need to know the location of twolinesformed if I am going to be using the Array(X,Y) with the stype boardsteps. Do I still need to know each variable, x1,y1,x2,y2 and twolinesformed their offset location before placing values in them? How then the concept of array works if I am manually having to locate their value in memory?

The second part where I asked you about boardheader=4 you said that is to store the dminseions of the array...but from the looks of it I am using single array with size 4...so this I need to ignore then completely and just simply focus on the two dimension array with size more than 4. Am I correct so far?

But...maybe I am wrong completely maybe i still need boardheader because you are using it to store the value of board. But why 4? Is 4 enough for what I am using? What if I went larger would they ruin the whole thing?

In this code down below:

Code: [Select]
rem now to allocate the board
board=boardheader+(32*21*boardsteps)
Reserve As Work 15,board
poke start(15)+boardx,32
poke start(15)+boardy,21

You said Start(15)+boardheader marks the start of the actual array...why you choose 15? Why not 0 or 12 or 3 or 6? Does that mean all my arrays they must start at 15?

Now here is the thing that I am little confused...you said boardx and boardy...am I taking it that these size will change based on the mouse location or the cursor location in the board or how exactly are these values changed?

Can boardx=210, boardy=34? Does that mean if the plotted dot on the board is plotted on 210, 34 that these values will actually be stored in boardx and boardy and the size of the array is array(32,21) is that how it work? But what about X2 and Y2...where does that go?

You said poke start(15)+boardx, 32 and poke start(15)+boardy,21 is the storing of the dimensions of the array in the header...you confirmed that with a yes. That is good so far....

So how does this work

Code: [Select]
for y=0 to 21
   for x=0 to 32
        if boardarray(x,y)=0
           board(x,y)\x1=x1:board(x,y)\y1=y1:board(x,y)\x2=x2:board(x,y)\y2=y2
           ' do the necessary flags to quit the entire loop all together               
        end if
   next x
next y     

With what you did up in terms of poke start(15)+boardx, 32 and poke start(15)+boardy, 21, I do not see the x1,y1,x2,y2 in the equation and most importantly I do not see or know what is suppose to increment boardx, and boardy with and by how much and why?


You said this "If you use arrays in your code, the compiler or interpreter generates all of this for you.  If you do banks of memory in Amos you have to do in manually.  Sometimes this is desirable since you can use integers that are smaller than 4 bytes each, as I have done in these examples.  On an Amiga 500 (or any 16-bit Amiga) the smaller 2-byte integers access at twice the speed of 4-byte integers in addition to taking less memory."

What if I do not want to have the "Advanced" version of array...and maybe to some..they need full control of everything and perhaps they can use it to optimize their code and execution to be a junior level like me and who is new in AMOS will find this a bit of an advanced topic. He wants to learn the scope of programming in AMOS and what to feel happy he have released his first Amiga game so he can move on to the next project...now maybe I should learn this in the future because maybe my future projects demand that I go through this approach...now essentially my question to you is...do I have to do it this way as you said below:

def fn boardarray(x,y,offset,banknum) boardheader+offset+((x+y*(peek(start(banknum)+boardx))*boardsteps)

and ther eis no other option behind it? If what you are saying is true...then teach me the above how it works all together, the x,y, offset, banknum with boardheader+offset+x+y, etc...pretend I am a student coming to first year class university. I have no idea what so ever in the above code how it works and function and why you did it this way and why you inclided banknum and why you also included boardsteps and how are these values boardheader, offset, x,y, boardx and boardsteps changes value in during game and why they needed to be changed?


Here is my question if I want to take this code here

Code: [Select]
for y=0 to 21
   for x=0 to 32
        if boardarray(x,y)=0
           board(x,y)\x1=x1:board(x,y)\y1=y1:board(x,y)\x2=x2:board(x,y)\y2=y2
           ' do the necessary flags to quit the entire loop all together               
        end if
   next x
next y     

using the boardstep how do I go about doing that with bank? Forgive me if I sound a little frustrated, it just I am not understanding how this works...and no course in university is going to teach me this and I have no intention of going through the beuracracy of taking a course in college and spending 1000 dollars for four month to teach me this and they are not going to teach me this...they are going to teach me .net and visual studio and html5...so..and no book here in AMOS is going to teach me either...I need someone here in the community to explain this to me...I promise ones you explain it to me (slowly) and I get it...it will stick in my brain and I can zoom clearing without any problem...I just need to understand what you are trying to say with your code.
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #6 on: October 12, 2013, 02:31:51 PM »

OK the first thing you said that twolinesformed=8 is an actual location it is an offset and not a value. My question why would i need to know the location of twolinesformed if I am going to be using the Array(X,Y) with the stype boardsteps. Do I still need to know each variable, x1,y1,x2,y2 and twolinesformed their offset location before placing values in them? How then the concept of array works if I am manually having to locate their value in memory?
There are no custom structure types in AmosPro.  That's why you have to do it manually.  Also, if you know that the values of x, y, and offset are in range, you can skip the range check and it will run faster that an array.  Arrays in AmosPro always use one of the following 3 types:  integer, floating point, or string.  An array access always does a range check whether you ask it to or not and the compiler isn't smart enough to realize that constants can be determined to be in or out of range without having to do a runtime check.
The second part where I asked you about boardheader=4 you said that is to store the dminseions of the array...but from the looks of it I am using single array with size 4...so this I need to ignore then completely and just simply focus on the two dimension array with size more than 4. Am I correct so far?
The header stores 2 integers at 2 bytes each.  2*2=4.  If you want more dimensions or metadata or other information to be stored once then the header is definitely the place for it.

Also there is one more advantage of using a bank over using an array:  You can load or save the bank to or from disk without much effort.
But...maybe I am wrong completely maybe i still need boardheader because you are using it to store the value of board. But why 4? Is 4 enough for what I am using? What if I went larger would they ruin the whole thing?

In this code down below:

Code: [Select]
rem now to allocate the board
board=boardheader+(32*21*boardsteps)
Reserve As Work 15,board
poke start(15)+boardx,32
poke start(15)+boardy,21

You said Start(15)+boardheader marks the start of the actual array...why you choose 15? Why not 0 or 12 or 3 or 6? Does that mean all my arrays they must start at 15?
Wrong.  It means you are using bank number 15.  It is the last bank supported in Amos the Creator and although AmosPro can use higher bank numbers, the lowest bank numbers are normally reserved for images, icons, music, sound effects and other data structures.
Now here is the thing that I am little confused...you said boardx and boardy...am I taking it that these size will change based on the mouse location or the cursor location in the board or how exactly are these values changed?
They are constant offsets within the bank.  The values they contain will change based on the size of the map.  The header itself just contains array dimensions at this point but can be expanded to store additional data as well.
Can boardx=210, boardy=34? Does that mean if the plotted dot on the board is plotted on 210, 34 that these values will actually be stored in boardx and boardy and the size of the array is array(32,21) is that how it work? But what about X2 and Y2...where does that go?
If there were a way to make boardx and boardy constants that never change I would have done so.  The only reason I kept them as variables is to avoid having to explain the structure offset functions of AmosPro which aren't much better than just naming offsets like I did here.
You said poke start(15)+boardx, 32 and poke start(15)+boardy,21 is the storing of the dimensions of the array in the header...you confirmed that with a yes. That is good so far....

So how does this work

Code: [Select]
for y=0 to 21
   for x=0 to 32
        if boardarray(x,y)=0
           board(x,y)\x1=x1:board(x,y)\y1=y1:board(x,y)\x2=x2:board(x,y)\y2=y2
           ' do the necessary flags to quit the entire loop all together               
        end if
   next x
next y     

With what you did up in terms of poke start(15)+boardx, 32 and poke start(15)+boardy, 21, I do not see the x1,y1,x2,y2 in the equation and most importantly I do not see or know what is suppose to increment boardx, and boardy with and by how much and why?
NEVER change a constant.  It isn't supposed to vary.
You said this "If you use arrays in your code, the compiler or interpreter generates all of this for you.  If you do banks of memory in Amos you have to do in manually.  Sometimes this is desirable since you can use integers that are smaller than 4 bytes each, as I have done in these examples.  On an Amiga 500 (or any 16-bit Amiga) the smaller 2-byte integers access at twice the speed of 4-byte integers in addition to taking less memory."

What if I do not want to have the "Advanced" version of array...and maybe to some..they need full control of everything and perhaps they can use it to optimize their code and execution to be a junior level like me and who is new in AMOS will find this a bit of an advanced topic. He wants to learn the scope of programming in AMOS and what to feel happy he have released his first Amiga game so he can move on to the next project...now maybe I should learn this in the future because maybe my future projects demand that I go through this approach...now essentially my question to you is...do I have to do it this way as you said below:

def fn boardarray(x,y,offset,banknum) boardheader+offset+((x+y*(peek(start(banknum)+boardx))*boardsteps)

and ther eis no other option behind it? If what you are saying is true...then teach me the above how it works all together, the x,y, offset, banknum with boardheader+offset+x+y, etc...pretend I am a student coming to first year class university. I have no idea what so ever in the above code how it works and function and why you did it this way and why you inclided banknum and why you also included boardsteps and how are these values boardheader, offset, x,y, boardx and boardsteps changes value in during game and why they needed to be changed?
The reason I named the constants is so that they can be set once in the code and used many times.  They aren't intended to be treated as variables.
Here is my question if I want to take this code here

Code: [Select]
for y=0 to 21
   for x=0 to 32
        if boardarray(x,y)=0
           board(x,y)\x1=x1:board(x,y)\y1=y1:board(x,y)\x2=x2:board(x,y)\y2=y2
           ' do the necessary flags to quit the entire loop all together               
        end if
   next x
next y     

using the boardstep how do I go about doing that with bank?
Code: [Select]
for y=0 to 21
   for x=0 to 32
        if peek(fn board(x,y,value,15))=0
           doke fn board(x,y,x1,15),x1init
           doke fn board(x,y,y1,15),y1init
           doke fn board(x,y,x2,15),x2init
           doke fn board(x,y,y2,15),y2init
           ' do the necessary flags to quit the entire loop all together               
        end if
   next x
next y     

Note:  I had to change the variable names to be different from the offset names so they wouldn't conflict so I added the word "init" to them.  Also, since we don't have an actual value stored in the custom type, you'd have to add the following to the offsets within the boardsteps definition:
Code: [Select]
value=9This uses up the pad byte that was added into the offset values so now all 10 bytes are used in the custom type.

Forgive me if I sound a little frustrated, it just I am not understanding how this works...and no course in university is going to teach me this and I have no intention of going through the beuracracy of taking a course in college and spending 1000 dollars for four month to teach me this and they are not going to teach me this...they are going to teach me .net and visual studio and html5...so..and no book here in AMOS is going to teach me either...I need someone here in the community to explain this to me...I promise ones you explain it to me (slowly) and I get it...it will stick in my brain and I can zoom clearing without any problem...I just need to understand what you are trying to say with your code.

I could teach you .NET too.  But here's the clincher: .NET uses object oriented programming.  You can reuse code and define accessors to get and set values.  The advantage to defining an interface to the code in an OOP fashion is reusability.  It slows things down though.  Arrays that do range checks are easy but slow.  Directly accessing memory is dangerous but fast.  Also, Banks in AmosPro make storage and retrieval of boards from disk to be easier.

I forgot a set of grouping symbols in the example code:
Code: [Select]
board=boardheader+(32*(21*boardsteps))In this case 32 is the x dimension of the manual array.  21 is the y dimension.
« Last Edit: October 12, 2013, 02:38:06 PM by SamuraiCrow »
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to use Bank and combine it with an array?
« Reply #7 on: October 12, 2013, 07:13:23 PM »

Thank you so much!!   :D :D Now I understand what you are trying say...that explains some of the confusions I had earlier.

Just one question and I am done for sure...

Code: [Select]
if peek(fn board(x,y,value,15))=0
the word value there in the peek statement....what is that value? Is it changeable and what value suppose to be there? Thanks in advance.
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #8 on: October 14, 2013, 02:13:49 PM »

If you look below the example code it is an offset within the structure and represents the constant value 9, which is the last of the 10 bytes of structure.  In the earlier example it was used merely as a pad byte.
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #9 on: October 14, 2013, 02:21:16 PM »

Now that you understand how it works, I'll show you an optimization that can make your structure array accesses much MUCH faster!

Code: [Select]
for y=0 to 21
   for x=0 to 32
        if peek(fn board(x,y,value,15))=0
           doke fn board(x,y,x1,15),x1init
           doke fn board(x,y,y1,15),y1init
           doke fn board(x,y,x2,15),x2init
           doke fn board(x,y,y2,15),y2init
           ' do the necessary flags to quit the entire loop all together               
        end if
   next x
next y
becomes
Code: [Select]
for y=0 to 21
   for x=0 to 32
        itemloc=fn board(x,y,0,15)
        if peek(itemloc+value)=0
           doke itemloc+x1,x1init
           doke itemloc+y1,y1init
           doke itemloc+x2,x2init
           doke itemloc+y2,y2init
           ' do the necessary flags to quit the entire loop all together               
        end if
   next x
next y

Do you see how it is much faster to calculate the array index only once and access the members of the structure based on that pointer address?  It's optimizations like this that C programmers use all the time!
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to use Bank and combine it with an array?
« Reply #10 on: October 16, 2013, 01:08:49 AM »

I implemented the code but it is not working at all! The results are always zero! I am going to post the code here as an attachment

Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #11 on: October 16, 2013, 03:28:31 PM »

Code: [Select]
Procedure SETBOARDARRAY[X1INIT,Y1INIT,X2INIT,Y2INIT]
   Shared BOARDSTEPS,BOARDX,BOARDY,BOARD
   For Y=0 To 21
      For X=0 To 32
         Def Fn BOARDARRAY(X,Y,OFFSET,BANKNUM)=BOARDHEADER+OFFSET+((X+Y*(Peek(Start(BANKNUM)+BOARDX))*BOARDSTEPS))
         ITEMLOC= Fn BOARDARRAY(X,Y,0,15+X)
         If Peek(ITEMLOC+10)=0
            Doke ITEMLOC+X1,X1INIT
            Doke ITEMLOC+Y1,Y1INIT
            Doke ITEMLOC+X2,X2INIT
            Doke ITEMLOC+Y2,Y2INIT
            Locate 20,20 : Print Peek( Fn BOARDARRAY(0,0,OFFSET,15+X))
            Wait 30
            Locate 20,20 : Print "               "
            Goto QUIT
         End If
      Next X
  Next Y
QUIT:
End Proc

Ummm... why are you trying to use multiple banks?  Each bank should hold a different board.

Also, when I set up the peek to look at ITEMLOC+9 I was assuming you'd initialize that as your character instead of the other array.  If you want to use the other array, just revert the bit about peeking itemloc+9 and use the array lookup you were using before.

Lastly, ITEMLOC+10 is the high byte of X1 in the next higher element of the array.  Maybe I was wrong to tell you to disable the range checking.  If you had an array of 10 elements and you asked for the one numbered 10 it would give you an array bounds out of range error.  Array indexes and offsets start at 0 and work their way up to N-1 where N is the dimension of the array.
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #12 on: October 21, 2013, 10:35:52 PM »

Code: [Select]
Procedure SETBOARDARRAY[X1INIT,Y1INIT,X2INIT,Y2INIT]
   Shared BOARDSTEPS,BOARDX,BOARDY,BOARD
   For Y=0 To 21
      For X=0 To 32
         Def Fn BOARDARRAY(X,Y,OFFSET,BANKNUM)=BOARDHEADER+OFFSET+((X+Y*(Peek(Start(BANKNUM)+BOARDX))*BOARDSTEPS))
         ITEMLOC= Fn BOARDARRAY(X,Y,0,15)
         If Peek(ITEMLOC+10)=0
            Doke ITEMLOC+X1,X1INIT
            Doke ITEMLOC+Y1,Y1INIT
            Doke ITEMLOC+X2,X2INIT
            Doke ITEMLOC+Y2,Y2INIT
            Locate 20,20 : Print Peek( Fn BOARDARRAY(0,0,OFFSET,15+X))
            Wait 30
            Locate 20,20 : Print "               "
            Goto QUIT
         End If
      Next X
  Next Y
QUIT:
End Proc

That can be simplified and corrected somewhat.  Try this:
Code: [Select]
Procedure SETBOARDARRAY[X1INIT,Y1INIT,X2INIT,Y2INIT]
   Shared BOARDSTEPS,BOARDX,BOARDY,BOARD
   For Y=0 To 21
      For X=0 To 32
         Def Fn BOARDARRAY(X,Y,BANKNUM)=BOARDHEADER+((X+Y*(Peek(Start(BANKNUM)+BOARDX))*BOARDSTEPS))
         ITEMLOC= Fn BOARDARRAY(X,Y,15)
         If Peek(ITEMLOC+9)=0
            Doke ITEMLOC+X1,X1INIT
            Doke ITEMLOC+Y1,Y1INIT
            Doke ITEMLOC+X2,X2INIT
            Doke ITEMLOC+Y2,Y2INIT
            Locate 20,20 : Print Deek( Fn BOARDARRAY(0,0,15)+X1)
            Wait 30
            Locate 20,20 : Print "               "
            Goto QUIT
         End If
      Next X
  Next Y
QUIT:
End Proc

The offset cannot be greater than 9 because the elements start counting from 0 and there are only 10 byte elements in the structure size.  If you use 10 as an offset, it will overwrite the high-byte of X1 (that's the bigger half of the 16-bit word) in the next element.

Also, since we are adding the offsets to ITEMLOC anyway, we can remove the offset parameter from the function, hence the simplification of that formula.  Lastly, if you want to read back a 16-bit word instead of an 8-bit byte, you have to use Deek instead of Peek.
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to use Bank and combine it with an array?
« Reply #13 on: October 24, 2013, 06:56:33 AM »

For some reason it brings out wrong values and it crashes with guru meditation. Let me post the source code here again so you can see my issue.



Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to use Bank and combine it with an array?
« Reply #14 on: October 25, 2013, 02:02:08 PM »

It's probably because you're reading from and writing to uninitialized memory.  In all of my examples I've used bank 15 to store the board.  For some unknown reason there is a bug that seems to have randomly crept into the code that seems to be based on the idea that banks 15 through 46 are the same thing.  They aren't.  Delete all occurrences of 15+X to the bank number in your source code and replace them with the constant 15.  If you want more than one board active at a time, you'll have to initialize the additional banks yourself.
Logged
Pages: [1] 2   Go Up
 

TinyPortal 2.2.2 © 2005-2022