gfxgfx
 
Please login or register.

Login with username, password and session length
 
gfx gfx
gfxgfx
 
Members, please note that our guidelines are located here.
 
gfx gfx
gfx
484586 Posts in 28410 Topics by 2464 Members - Latest Member: austinrobert July 11, 2020, 07:02:38 PM
*
gfx* Home | Help | Calendar | Login | Register | gfx
gfx
The Gaming Universe  |  The Archives  |  Archived Projects  |  Open Zelda  |  OZ Help (Moderator: red_team316)  |  Saving/Restoring Map ver 2, trying to understand bits
gfx
gfxgfx
 

Author Topic: Saving/Restoring Map ver 2, trying to understand bits  (Read 945 times)

Offline KingOfHumans

  • Messiah
  • ******
  • Posts: 17,866
    • View Profile
Saving/Restoring Map ver 2, trying to understand bits
« on: June 14, 2011, 09:44:22 PM »
Is there a difference between just using a number (new val = 10) and using a binary format(new val = 0b1010)?
While I haven't got around to using &, >>,<<... etc for binary. I did learn that you can use basic math symbols that might get you the same results which is confusing at the same time.

new val = 0b100; //8 in binary
val *= 0b10;//2 in binary
From what I outputted it equals to
0b1000//16 in binary.


What throws me off is the fact that when I use whole numbers I get the same results for the function ToBinary.

Source Code for OZ Community
Code: [Select]
static cell AMX_NATIVE_CALL ToBinary(AMX *amx, cell *params)
{
    char szString[33];
cell *cptr;
ConvertBit(params[1],szString);
amx_GetAddr(amx,params[2],&cptr);
    amx_SetString(cptr, szString, 0);
return 0;
}

...................................
void ConvertBit(int bin, char *str)
{
    unsigned int mask;     

    mask = 0x80000000;   
    while (mask)       
    {
        if (bin & mask) 
              *str = '1'; 
          else
              *str = '0';
        str++;
        mask >>= 1;
    }
    *str = 0;
}
I always thought if you use a regular number, it uses up more memory, but it outputs the same?

Anyways I changed some things around in my Dungeon Map script. Instead of having
new Floor6Layout[10][10];
new Floor5Layout[10][10];
new Floor4Layout[10][10];
............
I'm now using
new FloorLayout[10][100];
The first var is the x(0 - 9). The second handles the y and the floor.y(floor / 10).
So 0 - 9 is the very top floor, then 10 - 19, 20 - 29, etc.

I couldn't get any of my methods working so I just used 3 strings. It works, so I'll probably just stick with this method.

Code: [Select]
SaveDunMap()
{
if(GetValue("this",0) == 0)
return;
new num;
if(GetValue("this",0) < 5)
num = GetValue("this",0) * 3;
else
num = (GetValue("this",0) * 3) - 15;
new n;
new string1[500];
new string2[500];
new string3[500];
new slot,x,y;

while(n < 1000)
{
x = n % 10;
y = (n/10);
slot = n / 400;
if(slot == 0)
string1[n] = FloorLayout[x][y] + 48;
else if(slot == 1)
string2[n - 400] = FloorLayout[x][y] + 48;
else if(slot == 2)
string3[n - 800] = FloorLayout[x][y] + 48;
n++;
}

SetString("this",num,string1);
SetString("this",num + 1,string2);
SetString("this",num + 2,string3);
}

RestoreDunMap()
{
if(GetValue("this",0) == 0)
return;
new num;
if(GetValue("this",0) < 5)
num = GetValue("this",0) * 3;
else
num = (GetValue("this",0) * 3) - 15;
new n;
new string1[500];
new string2[500];
new string3[500];
new slot,x,y;

GetString("this",num,string1);
GetString("this",num + 1,string2);
GetString("this",num + 2,string3);

if(isNull(string1) || isNull(string2) || isNull(string3))
return;

while(n < 1000)
{
x = n % 10;
y = (n/10);
slot = n / 400;
if(slot == 0)
FloorLayout[x][y] = string1[n] - 48;
else if(slot == 1)
FloorLayout[x][y] = string2[n - 400] - 48;
else if(slot == 2)
FloorLayout[x][y] = string3[n - 800] - 48;
n++;
}
}

Unless if someone would/could script it in a way I can understand this is probably the best method to use for me.
So far every-time I tried, I aggravated myself so much that no progress gets made.
« Last Edit: June 15, 2011, 01:06:19 PM by KingOfHumans »

Offline Luke

  • Regimental
  • ****
  • Posts: 1,823
    • View Profile
Re: Saving/Restoring Map ver 2, trying to understand bits
« Reply #1 on: June 15, 2011, 06:32:05 PM »
For saving/restoring issue I recommend looking up strtol and sprintf function, they're in C but you should be able convert them to Small. Also for storing it as a string the Hex format is better as it takes up 8 chars instead of 32 for bits and 2-13 for decimals

Saving
Code: [Select]
new string[11];
sprintf(string, "%X", Floor4Layout[10][10])

Restoring
Code: [Select]
new string[11] = "0xDEADBEEF";
Floor4Layout[10][10] = strtol(string, NULL, 16);

On Saving Memory.
It really depends on how many layout options they are, but for is I'm going to say up to 16 , cause it not worth the hassle over that number.

Code: [Select]
stock GetBits( v, p, s )//value, position, size
{
//(v >> p)  shift value left 'p' bits
// & see http://en.wikipedia.org/wiki/Bitwise_operation#AND
// ((1<<s)-1) then s = 4 it would be 0b10000 so - 1 from that gives use 0b01111 and see above why you need to do that.
return (v >> p) & ((1<<s)-1);
}
new value = 0xDEADBEEF; // F is bit 0,1,2,3

//GetBits( value, 0, 16 ) == 0x0000BEEF // that would be room 1 value
//GetBits( value, 16, 16 ) == 0x0000DEAD // that would be room 2 value
printf("%x\n", GetBits( value, 0, 16 ));
printf("%x\n", GetBits( value, 16, 16 ))

As for the best method don't worry about that just do what you fine easy.

Question? Get/SetString does stop then it comes across a \0? And how do they handle packed strings?


Offline KingOfHumans

  • Messiah
  • ******
  • Posts: 17,866
    • View Profile
Re: Saving/Restoring Map ver 2, trying to understand bits
« Reply #2 on: June 16, 2011, 02:28:16 PM »
I didn't use packed strings, I think.
Unless if I made a mistake, it shouldn't read any \0 characters.
The last character it reads should be a 0 or 1 for each string.
It only reads 400 characters for each string.

I'll try your methods in the future, but for now I'll keep it as is.
Thanks!

Offline Luke

  • Regimental
  • ****
  • Posts: 1,823
    • View Profile
Re: Saving/Restoring Map ver 2, trying to understand bits
« Reply #3 on: June 16, 2011, 07:05:31 PM »
Sorry I wasn't clear, the question wasn't about your method, I just has idea but I haven't use those functions in ages.
My idea was this: SetString("this", num, Floor4Layout[10][0]); and GetString("this", num, Floor4Layout[10][0]);

The Gaming Universe  |  The Archives  |  Archived Projects  |  Open Zelda  |  OZ Help (Moderator: red_team316)  |  Saving/Restoring Map ver 2, trying to understand bits
 

gfxgfx
gfx
SMF 2.0.17 | SMF © 2019, Simple Machines Page created in 0.255 seconds with 23 queries.
Helios Multi © Bloc
gfx
Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!
anything