GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 Re: Script Submission » Hamming Distance and Decimal Length » 2008-09-13 02:48:20

I needed the base 10 version of the script for an assignment in CS class... the binary version is something I'm thinking about using in an AI which will compare various recordings and tell you if they are of the same piece of music or not.
Oddly enough, I've never actually used the hamming distance for error detection (though, not being a professional in the field, I have never really had the need to detect errors in anything).

#2 Re: Script Submission » Hamming Distance and Decimal Length » 2008-09-13 02:13:08

Oh right, sorry. Using strings would allow for an arbitrary base, but it still makes the engineering side of me shutter.

#3 Re: Script Submission » Hamming Distance and Decimal Length » 2008-09-13 01:42:42

Ah, yes, in regards to 10^... you're absolutely right; I meant "ten to the power of" and simply wrote it wrong (I tested this function in MATLAB, not GML).
As for the hamming distance normally being used for strings, I would argue that it is mainly used for comparing binary numbers, however, that comes from very limited experiences with the function.

EDIT: Regarding memory, you're right again... I was forgetting GM stores everything as doubles... so again, not an issue.

#4 Re: Script Submission » Hamming Distance and Decimal Length » 2008-09-13 01:18:42

Heh, I've had several people say that since I wrote this script. I just didn't think it was a good idea mixing and matching data types like that; doing it mathematically seems so much more... pure?
It is also more memory efficient this way and I assume there is a mathematical way that is more efficient all around as well (mine's not so bad, but I'm sure there is a better way to do the decimal compare).

EDIT: Of course, it would also be useful to have a separate function for doing this to strings. I wasn't working with strings, but of course it would be a useful script too.

#5 Script Submission » Hamming Distance and Decimal Length » 2008-09-13 00:08:15

leif902
Replies: 8

Recently, I needed to calculate the hamming distance (the number of different characters, ex. "123" 'and "127" have a distance of 1 because the 3 and the 7 are different) between two numbers.
I first calculated the binary hamming distance:

#define hamming_distance2
var dist, val;
dist = 0;
val = argument0 ^ argument1;

while (val)
{
    dist += 1;
    val &= val - 1;
}

return ( dist );

This worked well enough, until I realized that I needed the hamming distance for decimal numbers:
#define hamming_distance10
var len, dist, i, a, b;
len = ( floor(log10(argument0)) + 1 );
dist = 0;

for(i = 0; i < len; i += 1)
{
    a = floor(argument0 / ( 10 ^ (len - i) )) * 0.1;
    a = floor((a - floor(a)) * 10);

    b = floor(vec2 / ( 10 ^ (len - i) )) * 0.1;
    b = floor((b - floor(b)) * 10);

    dist = dist + (a != b);
}


The base 10 function is very impractical and I'm sure there is a better way to do it mathematically.

Also, the first line of the decimal hamming function " floor(log10(argument0)) + 1;" could be a separate function as it returns the length of the decimal number which is often very useful to know.

#6 Re: Community » Site Suggestion - RSS » 2008-01-09 09:41:54

Heh, fair enough tongue (I hate smiley's)

#7 Re: Community » Site Suggestion - RSS » 2008-01-08 23:01:18

Hah! Sorry for the double post but I just got my internet up and running again (Windstream is a terrible ISP) and saw your post on the main site! (Thanks for the guilt trip, you deserved all those hours working on an automated feed mechanism!)

No really though, thanks a lot, I have subscribed and am lovin' it!
- Leif

#8 Re: Community » Site Suggestion - RSS » 2008-01-06 22:39:20

Excellent, glad to see this implemented (I read somewhere recently that having a feed can boost readership by over 20%). But why oh why did you have to implement it when my home internet is down sad ? I'll definitely try it when I get back online again (I'm on a public (read, slow) dial up connection now).

Thanks,
- Leif

#9 Script Submission » Grid Smoothing and Random Terrain Generation » 2008-01-02 11:14:29

leif902
Replies: 5

Today I decided to take a stab at smoothing out random values in grids, useful in random terrain generation and anywhere else you need random numbers grouped in predictable ways.

Therefore I created the following script for smoothing over grids (Well, I say created, it's a commonly used algorithm):

Expand//
// ds_grid_smooth
// Smooths the randomly generated contents of a grid, useful for
// random terrain generation.
// Arguments
//    argument0 - The grid to populate
//    argument1 - The size of each sub-division (About 4 for optimal results, less than four will take longer to calculate
//                       and give you less of an effect, more than 4 will calculate faster and give you more smoothing)
//

var step, xx, yy, w, h, grid, total, y_local, x_local, average;
grid = argument0;
step = argument1;
w = ds_grid_width(grid);
h = ds_grid_height(grid);

for(yy = 0; yy < h; yy += step)
{
     for(xx = 0; xx < w; xx += step)
     {
          total = 0;
          for(y_local = yy; y_local <= yy + step; y_local += 1)
          {
               for(x_local = xx; x_local <= xx + step; x_local+= 1)
               {
                    total += ds_grid_get(grid,x_local, y_local);
               }
          }
          average = total / sqr( step );
          for(y_local = yy; y_local <= yy + step; y_local += 1)
          {
               for(x_local = xx; x_local <= xx + step; x_local += 1)
               {
                    ds_grid_set(grid, x_local, y_local, average );
               }
          }
     }
}

To test it I populated a grid randomly with values 0 - 3 (Simple enough):

Expandyy = -1;
while ( yy < h )
{
     xx = 0;
     yy += 1;
     while ( xx < w )
     {
          ds_grid_set(grid, xx, yy, floor ( random( 3 ) ) );
          xx += 1;
     }
}

and drew the results by subtracting a normalized version of each grid value from the value of the color black, this is using a step value of 4:
smoothed_grid.png

As opposed to the non-smoothed version (not the same grid):
random_grid.PNG

There are better ways to do this, but this one is easy. I found a better way and created a GML script, but I'm still toying with it as it only semi-smooths it at the moment.

#10 Re: Script Submission » Unary Encoding » 2008-01-02 00:35:05

Ah neat, I didn't realize you could use multiplication with strings. I've never found a use for this script, but I have heard of it being used in various algorithms for discrete probability distribution and geometric distribution. Look up "Unary coding" on Google or Wikipedia and I'm sure you'll find some use for it.

EDIT: Sure enough, Wikipedia has an article on everything... http://en.wikipedia.org/wiki/Unary_coding
EDIT 2: Even if some of them are rather bad...

#11 Script Submission » Unary Encoding » 2008-01-01 20:28:24

leif902
Replies: 2

I can see little purpose for this script, it's simple enough, but I'm sure someone can find a use for it.

Expand// unary_encode(num)
// Returns the unary encoded version of a number as a string.
// ex. unary_encode(5) returns "11110"

var num, unum;
num = argument0;
unum = "";
repeat( num - 1 ) {
     unum += "1";
}
unum += "0";
return ( unum );

(You could of course optimize the repeat loop, but I didn't see a need for it as it will execute fast enough)

#12 Re: Script Submission » Bitmap Conversion (32bit to 24bit) » 2007-12-27 17:36:51

Good point Xot, I like that, a single repeat loop like that should improve the speed a bit as well, I didn't even think about it.
- Leif

EDIT: I'm going to assume the seek performs a dummy byte read from the beginning of the file, in which case it (the dummy byte read) may be faster, but who knows? If I have time later I'll time it and check it out.

EDIT: Just re-read this and it sounded confusing... by "in which case it may be faster" I was not refering to the file_bin_seek function, but a dummy byte read... inserted it for clarity.

#13 Script Submission » Bitmap Conversion (32bit to 24bit) » 2007-12-24 17:11:55

leif902
Replies: 3

This is a small example I wrote for someone on the GMC recently, it does not support compression and should probably be expanded to convert from any bit depth to any other bit depth, but if you would like it here it is.
- Leif

Expand//////////////////////////////////////////////////
// bmp_32_to_24
// Converts uncompressed 32 bit bitmaps to 24
// bit bitmaps.
//
// Arguments:
//    argument0 - The file to convert as a
//                relative or absolute path.
//    argument1 - The file to output as a
//                relative or absolute path.
//
// Returns:
//    Nothing
//
//
//////////////////////////////////////////////////

var in, out, comp, in_size, image_size, out_image_size, i;

in             = file_bin_open(argument0,0);
out            = file_bin_open(argument1,1);
in_size        = file_bin_size(in);
image_size     = in_size - 54;
out_image_size = ( image_size / 4 ) * 3;

file_bin_write_word(out, 2, 0, 19778);
file_bin_write_word(out, 4, 0, out_image_size + 54 );
file_bin_seek(in, 6);
repeat ( 5 ) {
     file_bin_write_word(out, 4, 0, file_bin_read_word(in, 4, 0) );
}
file_bin_write_word(out, 2, 0, file_bin_read_word(in, 2, 0) );
file_bin_write_word(out, 2, 0, 24);
file_bin_seek(in, 30);
comp = file_bin_read_word(in, 4, 0);

if ( comp != 0) {
     // TODO: Add a switch statement here that deals
     //       with different modes of compression.
     show_error("Bitmap File is Compressed.", 0);
}

file_bin_write_word(out, 4, 0, 0);
file_bin_write_word(out, 4, 0, out_image_size);
file_bin_write_word(out, 16, 0, 0);

file_bin_seek(in, 54);
file_bin_seek(out, 54);

for(i = 0; i < image_size / 4; i += 1) {
     repeat ( 3 ) {
          file_bin_write_byte(out, file_bin_read_byte(in) );
     }
     file_bin_seek(in, file_bin_position(in) + 1);
}

file_bin_close(in);
file_bin_close(out);

#14 Re: GML Reference » File Formats » 2007-12-12 17:19:15

Oh believe me, I haven't tried to reverse engineer the GM7 format, if Dr. Overmars doesn't want it known that's good enough for me. And I previously mailed him about the GM6 and he said it was alright.

I should have said, please, this is for informational purposes only, don't do anything illegal with it (That wasn't intended for Yourself obviously, I just mean anyone else reading this topic)

#15 Re: GML Reference » File Formats » 2007-12-12 16:49:07

Oh no, I've checked, it's legal information smile

But if you feel it's inappropriate for GMLScripts that's your call,
- Leif

Oh yes, and: http://hacks.greenmangames.vze.com/ for the GM6 file format, GED file format and LIB file format (Lib and GED by myself, GM6 was partially completed by me and then I found two other people had already done it, so there are three documents)

EDIT: Oh yes, forgot to mention, that was GM6's EULA though, I have not checked since version 7 (when the encryption was put in place).

#16 GML Reference » File Formats » 2007-12-10 21:04:27

leif902
Replies: 4

The new GM7 file format is encrypted but the LIB and extension file formats are still nice and easy to read, perhapses there should be some documentation on these formats on the site?

This would be useful for example in an application to extract bitmap images from libs, or to convert lib actions into GML or to use an extension dynamically without it being installed (by reading the source).

If you'd like, contact me and I'll provide the details of the formats.

#17 Re: Community » Site Suggestion - RSS » 2007-10-19 16:20:37

I'm not entirley (sp?) sure what you mean... when you view an RSS or XML file containing RSS the items in the file are all the reader has access too. You might try it with another reader such as firefox or IE7. I beleive (don't quote me on this) that Google reader indexes and backs up all feeds so that even when the latest items have been removed they still exist on Google servers, this may be what you are seeing, but I am not sure.

Not all items are necissarily shown or linked to from the site either, some sites have RSS feeds and generate their pages and news from the feed, some have a page with news and generate the page from the feed and some maintain separate sites and feeds that contain the same content, there are many ways of doing this. (Personally I maintain a single feed and have the site automatically generate itself from the feed with Javascript, though I imagine this would be even easier in PHP, ASP or another server side language)

Hope that helps,
- Leif

#18 Re: Community » Site Suggestion - RSS » 2007-10-17 18:21:57

Yah, I figure one for each category would be nice so that I don't have to try and remember what scripts are new every time one of my favorite categories is updated smile, and of course, that way I could get automatic notification of new scripts that interest me and notification when the main news is updated, etc.

RSS is mainly useful when you have several (more than 7) sites you visit quite often for updates, it lets me see updates to every site I love one one convenient page and only check out the ones I like.

#19 Re: Community » Site Suggestion - RSS » 2007-10-14 11:10:40

Yah it's really annoying... all those different (incompatible) standards... definitely give Feed burner a whirl, it will select whichever is compatible with your reader for you and automatically translate it, that easy!

It also has lots of other neat services for publication, publicity, compatibility, etc.

#20 Re: Community » Site Suggestion - RSS » 2007-10-14 10:03:42

Alright,

RSS is of course simply a method for syndicating news content through an RSS agrigator. Firefox, IE7, and Google Reader are just a few examples of commonly used RSS agrigators (most likely spelled that wrong...).
There are other formats that are commonly used as well (Atom and the like)... google has a great service called feedburner (feedburner.com) which will automatically translate your RSS feed into Atom or one of the other formats as your reader requires.

A basic site RSS feed should probably just contain the updates, for instance, the last entry might be:

Xot wrote:

Date: October 9, 2007
Author:Xot
Title: What the hell??? An update? This must be the wrong site.
Link: http://gmlscripts.com/ (Unless you make a page for each news item like a blog might do)

Body: "Fear not, True Believers! This week we present a gaggle of graphics scripts. First up, Schreib gives us a script to make a copy a d3d model. Schreib does it again with a new and improved rounded rectangle. Daniel-Dane follows suit with a pair of scripts to draw dashed rectangles. Next up, xot brings to you a pair of scripts to draw spooky, wavy sprites. Teej offers up a script to draw sprites flipped ... but with a twist. Leif902 donates a script to convert a wavelength to a hue. And finally, Keth wraps things up with a really cool motion blur script.

Until next time ... Excelsior! "

Of course, RSS is an XML standard, so it is a little more complicated than that.

You might also choose to just put a small snippit of the news in the RSS and let the reader go to the site for the rest (attracts visitors)

-------------------------------------

It would probably also be practical to have RSS feeds under each of the categories of scripts:

For instance, the { artificial_intelligence } RSS feed would have four items (one of which is below):

Xot wrote:

Title: angle_difference(angle1,angle2)
Link: http://gmlscripts.com/script/angle_difference
Author: EyeGuy; xot
Body: "Returns the difference of the given angles in degress, -180 to 180."

Board footer

Powered by FluxBB