You are not logged in.    Login    New User    Forum Home    Search

Location:
JBPLAY  \  Meteor 2  \  Editing  \  Square root in Meteor 2 scripting?

Back to Threads

Viewing Thread: Square root in Meteor 2 scripting?

 

Dthdealer

Joined: 10 March 2007
Posts: 476
11 April 2010 07:19 (UK time)

Hey guys,

I'm working on a scripted boss meteor mod, and I need to find the distance between an object and the player. To do so I need to use pythag, which in turn requires me to be able to find square roots.

Is it possible in Meteor 2 scripting and how, or do I just have to write a looping 'halve difference every time' script? I hope I don't need the latter option, as I'll need to do this every few seconds of the boss fight.

You'll be amazed at what is possible with the limited variety of ( Hooks? ) script commands M2 has. The only limitation is pathfinding, as writing a dynamic waypoint ( or even better - area pathfinding ) script would take ages.

I might try anyway. A missing useful function to note here is a goto coordinate command, but I'll just substitute with a 1px object the other object goes to if need be.

Regards, William
Also known as Veyrdite
My website: http://halestrom.net/darksleep@Occasionally you may meet me when you sleep. In the darkest corners of your dreams, where your phone stops working and you don't know what to do. I'll be there; telling you that I don't support your device.

"Can't you just fix-"
NO! I told you when you bought this, when it goes wrong I can't fix it. It's filled with glue. The Greek goddess of glue Araldities herself would be impressed with this.

Please stop asking me for technical support during your dreams. Can't we just tour some surreal landscapes instead?

"How do I do that?"

Count your fingers. Keep trying. Make sure there's ten.

Then summon me. I am large and covered in wires. Clouds of baking soda pour into your world and a slightly weak yellowy glow appears from the LED strips behind me.

If you can see this message then you're already part of the way there. Keep all your styles disabled and you might see the dream within.

"But there's so much javascript everywhere! I can't see you. Help! I've forgotten to count my fingers! Where do I find them?"

The LED strip behind me pops and goes out. Dark spots appear in the centre of each light. I lift my finger to my lips. The smell of tantalum capacitors fills the air.

Ssh. Don't worry about that. Behind those frameworks there's another person sleeping.

Some day the javascript will sleep too.


You need to login to create posts in this thread.

Dthdealer

Joined: 10 March 2007
Posts: 476
11 April 2010 09:51 (UK time)

I'm encountering some pretty mean memory errors. When using integers in M2 scripting their values are getting really screwed up. For example a value equals ( all of these, depending on what Is calling it ( eg gamemessage, setobjecthits etc )) 0, 627 and -842150451. Even odder is the fact it is impossible for the variable to be negative, as I have this in my code.
 If ( variableA > variable B ) int theculpritvariable = variableA - variableB;
else int theculpritvariable = variableB - variableA;

Only some variables are affected ( generally the last declared in a code block ). Using staticflags instead of variable fixes this problem. Odd eh?

EDIT: *Hits self over head* Damn local variables! I'm an idiot!


None the less, the original topic question still stands. Any help?

Regards, William
Also known as Veyrdite
My website: http://halestrom.net/darksleep@Occasionally you may meet me when you sleep. In the darkest corners of your dreams, where your phone stops working and you don't know what to do. I'll be there; telling you that I don't support your device.

"Can't you just fix-"
NO! I told you when you bought this, when it goes wrong I can't fix it. It's filled with glue. The Greek goddess of glue Araldities herself would be impressed with this.

Please stop asking me for technical support during your dreams. Can't we just tour some surreal landscapes instead?

"How do I do that?"

Count your fingers. Keep trying. Make sure there's ten.

Then summon me. I am large and covered in wires. Clouds of baking soda pour into your world and a slightly weak yellowy glow appears from the LED strips behind me.

If you can see this message then you're already part of the way there. Keep all your styles disabled and you might see the dream within.

"But there's so much javascript everywhere! I can't see you. Help! I've forgotten to count my fingers! Where do I find them?"

The LED strip behind me pops and goes out. Dark spots appear in the centre of each light. I lift my finger to my lips. The smell of tantalum capacitors fills the air.

Ssh. Don't worry about that. Behind those frameworks there's another person sleeping.

Some day the javascript will sleep too.


Edited: 11 April 2010 10:25


You need to login to create posts in this thread.

me_mantis

Joined: 26 September 2003
Posts: 1152
13 April 2010 19:49 (UK time)

I'm pretty sure I've tried to use this, and it hasn't worked... in the range finding system for my tracking rockets. They basically use a square search pattern now.

James? Any hope from you?

You need to login to create posts in this thread.

arezey

Joined: 16 June 2005
Posts: 1599
14 April 2010 11:20 (UK time)

Does M2 support floats or doubles? I recall it might but not sure.

Anyhow, as M2 does not support square root, make it yourself. Use the Babylonian method:

First pick a starting point (maybe half of the original value or so?). Then iterate the equation of:

(Wikipedia)

where S is the original value you have and xn is your current result (initially the starting point).

With every xn+1 result you get, make it as xn and go over again. Keep going on until the result stops increasing or decreasing (significantly). It's how computers and calculators do it. ;)

Problem though is that if M2 does not support floats or doubles, will integers be accurate enough? :|

Edited: 14 April 2010 11:39


You need to login to create posts in this thread.

Dthdealer

Joined: 10 March 2007
Posts: 476
14 April 2010 12:04 (UK time)

This is the method I was afraid of using. I'll see if it clogs M2 every times it is run ( but of course I do not need to iterate it too much as accuracy is not really needed ) but if not I'll use it.

M2 supports floats and doubles happily. But learn from this lesson

 float foobar = 5 / 3;
SC_GameMessage(" Five divided by three is %d. ", foobar );


You will get the message int happybar = foobar * 100 and putting that in the GameMessage will yield
 float foobar = 5.00 / 3;


Any of the values can have the decimal points. Probably the amount of points determines how many will be in the answer, but I don't know right now. It is 9 pm here in Sydney, and my eyeballs have burnt out from working on the Boss mod I'm going to release by the end of this week ( with only one boss so far mind you ).
Also known as Veyrdite
My website: http://halestrom.net/darksleep@Occasionally you may meet me when you sleep. In the darkest corners of your dreams, where your phone stops working and you don't know what to do. I'll be there; telling you that I don't support your device.

"Can't you just fix-"
NO! I told you when you bought this, when it goes wrong I can't fix it. It's filled with glue. The Greek goddess of glue Araldities herself would be impressed with this.

Please stop asking me for technical support during your dreams. Can't we just tour some surreal landscapes instead?

"How do I do that?"

Count your fingers. Keep trying. Make sure there's ten.

Then summon me. I am large and covered in wires. Clouds of baking soda pour into your world and a slightly weak yellowy glow appears from the LED strips behind me.

If you can see this message then you're already part of the way there. Keep all your styles disabled and you might see the dream within.

"But there's so much javascript everywhere! I can't see you. Help! I've forgotten to count my fingers! Where do I find them?"

The LED strip behind me pops and goes out. Dark spots appear in the centre of each light. I lift my finger to my lips. The smell of tantalum capacitors fills the air.

Ssh. Don't worry about that. Behind those frameworks there's another person sleeping.

Some day the javascript will sleep too.


You need to login to create posts in this thread.

me_mantis

Joined: 26 September 2003
Posts: 1152
14 April 2010 20:01 (UK time)

Lemme know how that sqrt thing works. I'm interested.

You need to login to create posts in this thread.

ParaSait

Joined: 26 June 2007
Posts: 1478
17 April 2010 15:09 (UK time)

me_mantis(keens) wrote:
Lemme know how that sqrt thing works. I'm interested.
I don't remember the M2 script syntax very clearly, but here's a c++ implementation of arezey's method:

float precision = 0.001; // The smaller, the more precise, but the more cpu intensive

float squareroot(float start)
{
float oldnum = start; // Define x0
float newnum = oldnum / 2; // Define x1, taking the half to start with
while(oldnum - newnum > precision) // Keep repeating till the difference between xn and xn+1 is but a fraction
{
oldnum = newnum; // old number is now the previously new one
newnum = 0.5 * (oldnum + (start / oldnum)); // The calculation
}
return newnum; // return the result
}

Haven't tested it, but that should do it I guess.
However, I'm not quite sure how it's going to work in Meteor scripts cause if I understood it right, there are some problems with float division..?


This link is dead. It's only still here because, err... yknow, it's some sort of... memorial.


Edited: 17 April 2010 15:21


You need to login to create posts in this thread.



Forums system (C) 1999-2023 by James Bunting.

Terms of Use