News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_Skunkfish

Arrays in BASIC

Started by Skunkfish, 15:35, 16 November 17

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Skunkfish

I'm designing a hypothetical strategy game, with a map size of 2x2 Mode 1 screens.

Each square of the map will be a standard 8x8 character, giving I believe 1000 squares per screen and 4000 in total.

For each square, I want to store information on the type of object occupying that square and also a numeric value.

I believe that if I define an array of integer values to hold the map data with DIM map%(80,50), then I can hold a 16-bit value for each location which I believe would make the map 8000 bytes?

However, what's the best way of handling the 2 different elements (object type and value) within a single array?
Could I store a pair of HEX values at each location in the array instead of decimal to make this easier to manage?

I don't want to use 2 arrays as I believe this would double the memory usage to 16000 bytes?

Any advice would be appreciated, or if you want to let me know that I'm going about this the wrong way! It's been a long time since I've done any programming of any sort!  ???

Thanks!
An expanding array of hardware available at www.cpcstore.co.uk (and issue 4 of CPC Fanzine!)

Sykobee (Briggsy)

Yes, you can dimension the array as a 2D array of 80x50, or a 1D array of 4000 items.


Using integers indeed gives you two bytes per element.


You are correct that if all your per-cell data can fit in a total of 16-bits, then a single array is the best option. Sadly there's no byte-oriented datatype in CPC BASIC.


So you need to consider the range of the numeric value, and the range of the object types.


If both are under 256, then I'd split it a byte for each, one being the high byte, and the other the low byte, but ultimately you will be ANDing a mask and shifting one of the values until it makes sense, so it doesn't matter too much.

AMSDOS

I'm assuming you've probably progressed deep into your game unfortunately.


I wrote an example of an Array which is just me setting aside some Memory for my Array, but I found that calculating a formula for it was a lot harder than what I initially had and I was actually wasting memory.




10 DEFINT a-z:MEMORY &9FFF
20 MODE 1
30 FOR y=1 TO 25
40   FOR x=1 TO 40
50     n=0:GOSUB 1000
60   NEXT x
70 NEXT y
80 FOR y=1 TO 25
90   FOR x=1 TO 40
100     n=INT(RND*2)+1
110     IF n=1 THEN GOSUB 1000:LOCATE x,y:PRINT CHR$(143);
120   NEXT x
130 NEXT y
140 LOCATE 1,1:END


1000 POKE &9FD7+(x+41*y)-y,n
1010 RETURN


2000 FOR a=&A000 TO &A7A7
2010   IF PEEK(a)=1 THEN PRINT"1"; ELSE PRINT"0";
2020 NEXT a



So in my example I'm setting aside some memory &A000 onwards, which is &3E8 (1000 bytes) which is an array 40,25 in side, there's probably a range of clever ways to setting aside an array like this, for example use MEMORY HIMEM-1000 as the top of the memory, my formula in Line 1000 just assumes the Array begins at &A000, so to get &9FD7 subtract &A000-&29 which is the Width+1. It works by setting x between 1 & 40 and y between 1 & 25. Initially I wasn't subtracting y on the end, and that ended up with an array which was spacing itself out (wasting memory) when y didn't equal 1. Lines 2000 onwards is just a little test program I made and as well as the memory dumps I was using in Winape I could see problems emerging when y was being incremented.


So out of that I was able to produce a byte sized array in memory. Though all that example shows is setting up an array in memory. Obviously to test it PEEK would be used with that formula in it to return the byte value.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

menegator

How many different objects these maps can contain?


For example if there are n different objects you can use n*125 (25*40/8) bytes bitmap to describe objects positions, kinda like the symbol command. If objects are sparsely and algorithmically distributed then you are going to need even less space.


However if you need to store 80x25 numbers, then you need 2000 bytes of memory.

Skunkfish

Quote from: AMSDOS on 04:10, 07 April 18
I'm assuming you've probably progressed deep into your game unfortunately.

Not at all! Thanks for the information, I'm sure it will come in useful when I actually get around to the coding of the game!
An expanding array of hardware available at www.cpcstore.co.uk (and issue 4 of CPC Fanzine!)

Skunkfish

The game will be effectively a simplified settlers/civilization type game working on a turn-based basis.

Each map square could be a player building or a piece of scenery, of which I think I think there will be fewer than 64 types. Each square can either be unclaimed or belong to one of 2 teams. Each square can also be visible or invisible to the player.

The square will also hold some stats: for a mountain square/mine, this would be the quantity of resource remaining.
For a barracks, this would be the quantity and rank of soldiers inside.

For squares that produce resources, there would be a counter as resources would only be produced every x turns (farms, mines, lumberjacks).
Foresters would plant trees that would grow from a sapling into a tree in x turns.

I haven't thought about it in a while and it's still early in the planning stage, but I'm thinking this game should be possible to achieve on the CPC.
I'm not planning any animation or even graphics beyond amending the character set.
An expanding array of hardware available at www.cpcstore.co.uk (and issue 4 of CPC Fanzine!)

Powered by SMFPacks Menu Editor Mod