PDA

View Full Version : A Super-annoying, Rand()-related problem



Nimono
02-16-2007, 04:00 PM
I don't know the rules about double-posting here, so I'm just making a new thread. Sorry.

I'm having a problem with Rand() in this script:

// Shovel- Well, what do you THINK it does? :P It digs up the ground directly in front of you,
// provided it's not solid or a certain combo. The combos it can't dig up are yours to decide!
// However, I give you three solid combos it CAN dig up, so use them wisely.
// Variables:
// ComboDug- Combo that appears when you dig in a diggable spot.
// Combo1- First Combo you can't dig in.
// Combo2- 2nd Combo you can't dig in.
// Combo3- 3rd No-Dig Combo.
// Combo4- 4th No-Dig Combo.
// Combo5- 5th No-Dig Combo.
// ComboS1- First Solid Combo you can dig in.
// ComboS2- 2nd Solid Dig Combo.
// ComboS3- 3rd Solid Dig Combo.
// Random/Random2/Random3- Don't mess with these.
// BuriedItem- What Special Item you get from certain combos.
// ComboX & ComboY- Leave this alone.
// SpclItmCmb1-Cmb4- These 4 combos are the combos that give you special items when dug up! :D
// ModItem- Leave this alone.
// NewItem- Leave this alone.
// ModNPC- See Above.
// NewNPC- See above, also.
// Note: Wow! So many variables! Ummm... Nothing I can do about it. =/

import "std.zh"
item script Shovel
{
int ComboDug = 55;
int Combo1 = 47;
int Combo2 = 0;
int Combo3 = 0;
int Combo4 = 0;
int Combo5 = 0;
int ComboS1 = 322;
int ComboS2 = 0;
int ComboS3 = 0;
int ComboX = 0;
int ComboY = 0;
int ComboSDug = 1;
int Random = 0;
int Random2 = 0;
int Random3 = 0;
int BuriedItem = 0;
int SpclItmCmb1 = 0;
int SpclItmCmb2 = 0;
int SpclItmCmb3 = 0;
int SpclItmCmb4 = 0;
int ModItem = 0;
int NewItem = 0;
int ModNPC = 0;
int NewNPC = 0;

void run()
{
if (Link->Dir == 0)
{
int BlockCheckY = Link->Y - 16;
int BlockCheckX = Link->X;
float BlockRmndrX = BlockCheckX % 16;
if (BlockRmndrX <= 8)
{
BlockCheckX = BlockCheckX - BlockRmndrX;
}
else
{
BlockCheckX = BlockCheckX + 16 - BlockRmndrX;
}
float BlockRmndrY = BlockCheckY % 16;
if (BlockRmndrY <= 8)
{
BlockCheckY = BlockCheckY - BlockRmndrY;
}
else
{
BlockCheckY = BlockCheckY + 16 - BlockRmndrY;
}
int BlockPosition = (BlockCheckY & 240)+(BlockCheckX>>4);

if ( (Screen->ComboD [BlockPosition] == Combo1 || Screen->ComboD [BlockPosition] == Combo2 || Screen->ComboD [BlockPosition] == Combo3 || Screen->ComboD [BlockPosition] == Combo4 || Screen->ComboD [BlockPosition] == Combo5 || Screen->ComboD [BlockPosition] ==ComboDug) || (Screen->ComboS [BlockPosition] == 15 && (Screen->ComboD [BlockPosition] != ComboS1 && Screen->ComboD [BlockPosition] != ComboS2 && Screen->ComboD [BlockPosition] != ComboS3)) )
{
Game->PlaySound(6);
Quit();
}
ComboY = Link->Y - 16;
ComboX = Link->X;
float remainder = ComboX % 16;
if (remainder <= 8)
{
ComboX = ComboX - remainder;
}
else
{
ComboX = ComboX + 16 - remainder;
}
float remainderY = ComboY % 16;
if (remainderY <= 8)
{
ComboY = ComboY - remainderY;
}
else
{
ComboY = ComboY + 16 - remainderY;
}
int ComboPosition = (ComboY & 240)+(ComboX>>4);
if (Screen->ComboD [BlockPosition] == ComboS1 || Screen->ComboD [BlockPosition] == ComboS2 || Screen->ComboD [BlockPosition] == ComboS3)
{
Screen->ComboD [ComboPosition] = ComboSDug;
}
else
{
Screen->ComboD [ComboPosition] = ComboDug;
}
}
else if (Link->Dir == 1)
{
int BlockCheckY = Link->Y + 16;
int BlockCheckX = Link->X;
float BlockRmndrX = BlockCheckX % 16;
if (BlockRmndrX <= 8)
{
BlockCheckX = BlockCheckX - BlockRmndrX;
}
else
{
BlockCheckX = BlockCheckX + 16 - BlockRmndrX;
}
float BlockRmndrY = BlockCheckY % 16;
if (BlockRmndrY <= 8)
{
BlockCheckY = BlockCheckY - BlockRmndrY;
}
else
{
BlockCheckY = BlockCheckY + 16 - BlockRmndrY;
}
int BlockPosition = (BlockCheckY & 240)+(BlockCheckX>>4);
if ( (Screen->ComboD [BlockPosition] == Combo1 || Screen->ComboD [BlockPosition] == Combo2 || Screen->ComboD [BlockPosition] == Combo3 || Screen->ComboD [BlockPosition] == Combo4 || Screen->ComboD [BlockPosition] == Combo5 || Screen->ComboD [BlockPosition] == ComboDug) || (Screen->ComboS [BlockPosition] == 15 && (Screen->ComboD [BlockPosition] != ComboS1 && Screen->ComboD [BlockPosition] != ComboS2 && Screen->ComboD [BlockPosition] != ComboS3)) )
{
Game->PlaySound(6);
Quit();
}
ComboY = Link->Y + 16;
ComboX = Link->X;
float remainder = ComboX % 16;
if (remainder <= 8)
{
ComboX = ComboX - remainder;
}
else
{
ComboX = ComboX + 16 - remainder;
}
float remainderY = ComboY % 16;
if (remainderY <= 8)
{
ComboY = ComboY - remainderY;
}
else
{
ComboY = ComboY + 16 - remainderY;
}
int ComboPosition = (ComboY & 240)+(ComboX>>4);
if (Screen->ComboD [BlockPosition] == ComboS1 || Screen->ComboD [BlockPosition] == ComboS2 || Screen->ComboD [BlockPosition] == ComboS3)
{
Screen->ComboD [ComboPosition] = ComboSDug;
}
else
{
Screen->ComboD [ComboPosition] = ComboDug;
}
}
else if (Link->Dir == 2)
{
int BlockCheckY = Link->Y;
int BlockCheckX = Link->X - 16;
float BlockRmndrX = BlockCheckX % 16;
if (BlockRmndrX <= 8)
{
BlockCheckX = BlockCheckX - BlockRmndrX;
}
else
{
BlockCheckX = BlockCheckX + 16 - BlockRmndrX;
}
float BlockRmndrY = BlockCheckY % 16;
if (BlockRmndrY <= 8)
{
BlockCheckY = BlockCheckY - BlockRmndrY;
}
else
{
BlockCheckY = BlockCheckY + 16 - BlockRmndrY;
}
int BlockPosition = (BlockCheckY & 240)+(BlockCheckX>>4);
if ( (Screen->ComboD [BlockPosition] == Combo1 || Screen->ComboD [BlockPosition] == Combo2 || Screen->ComboD [BlockPosition] == Combo3 || Screen->ComboD [BlockPosition] == Combo4 || Screen->ComboD [BlockPosition] == Combo5 || Screen->ComboD [BlockPosition] == ComboDug) || (Screen->ComboS [BlockPosition] == 15 && (Screen->ComboD [BlockPosition] != ComboS1 && Screen->ComboD [BlockPosition] != ComboS2 && Screen->ComboD [BlockPosition] != ComboS3)) )
{
Game->PlaySound(6);
Quit();
}
ComboY = Link->Y;
ComboX = Link->X - 16;
float remainder = ComboX % 16;
if (remainder <= 8)
{
ComboX = ComboX - remainder;
}
else
{
ComboX = ComboX + 16 - remainder;
}
float remainderY = ComboY % 16;
if (remainderY <= 8)
{
ComboY = ComboY - remainderY;
}
else
{
ComboY = ComboY + 16 - remainderY;
}
int ComboPosition = (ComboY & 240)+(ComboX>>4);
if (Screen->ComboD [BlockPosition] == ComboS1 || Screen->ComboD [BlockPosition] == ComboS2 || Screen->ComboD [BlockPosition] == ComboS3)
{
Screen->ComboD [ComboPosition] = ComboSDug;
}
else
{
Screen->ComboD [ComboPosition] = ComboDug;
}
}
else if (Link->Dir == 3)
{
int BlockCheckY = Link->Y;
int BlockCheckX = Link->X + 16;
float BlockRmndrX = BlockCheckX % 16;
if (BlockRmndrX <= 8)
{
BlockCheckX = BlockCheckX - BlockRmndrX;
}
else
{
BlockCheckX = BlockCheckX + 16 - BlockRmndrX;
}
float BlockRmndrY = BlockCheckY % 16;
if ( BlockRmndrY <= 8 )
{
BlockCheckY = BlockCheckY - BlockRmndrY;
}
else
{
BlockCheckY = BlockCheckY + 16 - BlockRmndrY;
}
int BlockPosition = (BlockCheckY & 240)+(BlockCheckX>>4);
if ( (Screen->ComboD [BlockPosition] == Combo1 || Screen->ComboD [BlockPosition] == Combo2 || Screen->ComboD [BlockPosition] == Combo3 || Screen->ComboD [BlockPosition] == Combo4 || Screen->ComboD [BlockPosition] == Combo5 || Screen->ComboD [BlockPosition] == ComboDug) || (Screen->ComboS [BlockPosition] == 15 && (Screen->ComboD [BlockPosition] != ComboS1 && Screen->ComboD [BlockPosition] != ComboS2 && Screen->ComboD [BlockPosition] != ComboS3)) )
{
Game->PlaySound(6);
Quit();
}
ComboY = Link->Y;
ComboX = Link->X + 16;
float remainder = ComboX % 16;
if (remainder <= 8)
{
ComboX = ComboX - remainder;
}
else
{
ComboX = ComboX + 16 - remainder;
}
float remainderY = ComboY % 16;
if (remainderY <= 8)
{
ComboY = ComboY - remainderY;
}
else
{
ComboY = ComboY + 16 - remainderY;
}
int ComboPosition = (ComboY & 240)+(ComboX>>4);
if (Screen->ComboD [BlockPosition] == ComboS1 || Screen->ComboD [BlockPosition] == ComboS2 || Screen->ComboD [BlockPosition] == ComboS3)
{
Screen->ComboD [ComboPosition] = ComboSDug;
}
else
{
Screen->ComboD [ComboPosition] = ComboDug;
}
}
Game->PlaySound(61);
Random = Floor(Random);
Random = Rand(19);
Random2 = Floor(Random2);
Random2 = Rand (4);
int ComboPosition = (ComboY & 240)+(ComboX>>4);
if (Screen->ComboD [ComboPosition] != SpclItmCmb1 && Screen->ComboD [ComboPosition] != SpclItmCmb2 && Screen->ComboD [ComboPosition] != SpclItmCmb3 && Screen->ComboD [ComboPosition] != SpclItmCmb4 && Screen->ComboD [ComboPosition] != ComboS1 && Screen->ComboD [ComboPosition] != ComboS2 && Screen->ComboD [ComboPosition] != ComboS3)
{
if (Random == 0 || Random == 1 || Random == 2 || Random == 3 || Random == 4 || Random == 11)
{
Quit();
}
else if (Random == 5 || Random == 6 || Random == 7 || Random == 8 || Random == 16 || Random == 17)
{
if (Random2 == 0 || Random2 == 1 || Random2 == 2)
{
Screen->CreateItem(0);
NewItem = Screen->NumItems();
item ModItem = Screen->LoadItem(NewItem);
ModItem->X = ComboX;
ModItem->Y = ComboY;
}
else if (Random2 == 3)
{
Screen->CreateItem(2);
NewItem = Screen->NumItems();
item ModItem = Screen->LoadItem(NewItem);
ModItem->X = ComboX;
ModItem->Y = ComboY;
}
}
else if (Random == 9 || Random == 10)
{
Screen->CreateItem(5);
ModItem = Screen->NumItems();
item NewItem = Screen->LoadItem(ModItem);
NewItem->X = ComboX;
NewItem->Y = ComboY;
}
else if (Random == 12 || Random == 13)
{
Screen->CreateNPC(44);
ModNPC = Screen->NumNPCs();
npc NewNPC = Screen->LoadNPC(ModNPC);
NewNPC->X = ComboX;
NewNPC->Y = ComboY;
}
else if (Random == 14)
{
Screen->CreateItem(40);
NewItem = Screen->NumItems();
item ModItem = Screen->LoadItem(NewItem);
ModItem->X = ComboX;
ModItem->Y = ComboY;
}
else if (Random == 15)
{
Screen->CreateNPC(80);
ModNPC = Screen->NumNPCs();
npc NewNPC = Screen->LoadNPC(ModNPC);
NewNPC->X = ComboX;
NewNPC->Y = ComboY;
}
}
else if (Screen->ComboD [ComboPosition] == ComboS1 || Screen->ComboD [ComboPosition] != ComboS2 || Screen->ComboD [ComboPosition] != ComboS3)
{
Random3 = Floor(Random3);
Random3 = Rand(13);
if (Random3 == 0 || Random3 == 1 || Random3 == 2 || Random3 == 3 || Random3 == 4 || Random3 == 5 || Random3 == 6)
{
Quit();
}
else if (Random3 == 7 || Random3 == 8 || Random3 == 9 || Random3 == 10 || Random3 == 11 || Random3 == 12)
{
if (Random2 == 0 || Random2 == 1 || Random2 == 2)
{
Screen->CreateItem(0);
NewItem = Screen->NumItems();
item ModItem = Screen->LoadItem(NewItem);
ModItem->X = ComboX;
ModItem->Y = ComboY;
}
else if (Random2 == 3)
{
Screen->CreateItem(2);
NewItem = Screen->NumItems();
item ModItem = Screen->LoadItem(NewItem);
ModItem->X = ComboX;
ModItem->Y = ComboY;
}
}
}
}
}
I asked Koopa for help earlier, and he told me to use "Random=Floor(Random)" to turn the Random variables back into ints, but.... It's not working. The last bit of code that begins all the randomizing and stuff is supposed to randomly create certain things when you use it on a diggable combo. Problem is, NOTHING HAPPENS. Koopa says that Rand() keeps turning out 0, but.... I can't fix it, and Koopa left me NO instructions on how to use it. (He also said he fixed the code and got it to work, but never sent it to me. :() So, I'm at a loss. Yes, this is my Shovel script, but it'll never be complete until it can produce items. Does anyone know what I'm doing wrong?

blue_knight
02-16-2007, 05:04 PM
Why are you doing this?


Random = Floor(Random);
Random = Rand(19);
Random2 = Floor(Random2);
Random2 = Rand (4);


What you are doing is taking the floor of the contents of Random, then setting it to a new random number right afterward. I'm guessing you meant to do this:



Random = Rand(19);
Random = Floor( Random );
Random2 = Rand(4);
Random2 = Floor( Random2 );


This set Random to a random number and then take the floor of it. You could even shorten it to:



Random = Floor( Rand(19) );
Random2 = Floor( Rand(4) );


You have other places in the code that do this too, this was just an example.

Nimono
02-16-2007, 05:26 PM
Why are you doing this?


Random = Floor(Random);
Random = Rand(19);
Random2 = Floor(Random2);
Random2 = Rand (4);


What you are doing is taking the floor of the contents of Random, then setting it to a new random number right afterward. I'm guessing you meant to do this:



Random = Rand(19);
Random = Floor( Random );
Random2 = Rand(4);
Random2 = Floor( Random2 );


This set Random to a random number and then take the floor of it. You could even shorten it to:



Random = Floor( Rand(19) );
Random2 = Floor( Rand(4) );


You have other places in the code that do this too, this was just an example.

I did that originally, and THAT didn't work! And I have no clue why! ;_;

Edit: Oh crud! It's doing it AGAIN for another script! This time, I'm trying to randomize the position of something on the screen. Problem is, even with everything right, NOTHING HAPPENS. I even used "Floor" for the Rand()s. It's not working. And no, I will NOT post the script, for I intend for this script to remain secret until I release it. :)

blue_knight
02-17-2007, 01:47 AM
Ok you need to see what kind of numbers Rand() is returning. Use the Trace function to output the results to the "allegro.log". Try doing this:

Trace( Rand(19) );

or whatever number you want. Then test your script in the viewer, after that look in the "allegro.log" file and let me know what values are in there. We'll figure out what's going on :)

Nimono
02-17-2007, 11:17 AM
And then I'm back to square one. Koopa did that, and told me it was going from an int to a float, and told me to use Floor to turn it back into an int. So what good is it going to do to retrace his steps ALL OVER AGAIN?

Edit: I tried Trace, but.... Problem is, I can never make heads or tails of the ZASM output. What exactly am I looking for? Do I have to USE the script, and not just compile it?

Fire Wizzrobe
02-17-2007, 05:05 PM
Disregard this post.

Nimono
02-17-2007, 05:23 PM
Uhhh.... I didn't even SEE the original post. O.o

Fire Wizzrobe
02-17-2007, 06:54 PM
I wish we could delete posts here. It was just an unessary question.

DarkDragon
02-17-2007, 07:24 PM
Has this been sorted out, or are there still questions/bugs?
For the record Rand(), for positive arguments, should ALWAYS return an integer.

Nimono
02-17-2007, 07:49 PM
Has this been sorted out, or are there still questions/bugs?
For the record Rand(), for positive arguments, should ALWAYS return an integer.

Bugs. This is 2.11 b16c, though. Even FLOORING Rand() won't make it work. I haven't tested it out yet, but on my surprise special script (which I really don't want to reveal, for some reason), I have a variable that accesses a Randomized variable in a scope INSIDE the scope the Rand. variable is in. Problem being? Both variables are global, so it shouldn't matter, right? I'm going to try moving things around a bit, though, just in case them being global has no bearing on the matter.... =/ I'll get back to you soon.

Edit: O_o Nope. Moving it didn't work. I have NO clue what is wrong here....

DarkDragon
02-17-2007, 09:09 PM
There was some funky business back around those betas where script functions returned floats instead of ints. Is Rand still broken on the latest alpha?

Nimono
02-17-2007, 09:16 PM
There was some funky business back around those betas where script functions returned floats instead of ints. Is Rand still broken on the latest alpha?

Oh. *heh* I'll go check. I generally don't look at those.... XD

Edit: Yes, it's somewhat bugged. It works fine when randomizing..... Sorta. I set up a screen surrounded by "trees" with some in the center. Without revealing too much, I'll just say that the script randomizes the location of something. This "something", for some reason, doesn't get its location randomized, but instead is ALWAYS thrown into the center of two trees. And if I'm not mistaken.... THAT'S THE EXACT CENTER OF THE SCREEN! O.o And using the item again does nothing! No..... Wait.... Maybe it's Link->X... I have a variable set to Link->X, so if, when the variable sends this something into a solid combo, it is instead sent to Link's location instead. Problem is.... That location seems to be somewhere OTHER than Link's true location.... Sigh.... I don't want to reveal the script on the forum yet, but I'd be glad to PM it to you, DarkDragon. Would you like me to do so?