Ok, folks, now it's for real.
This community has given me so much knowledge and useful tips and now it's time for me to contribute with something.
It is not a big deal, no complex coding involved, but I think these functions are good enough to be helpful.
Since I use many functions that generate stuff for me, I was thinking "why not sharing them with the world?"
I'll put them in a bug code snippet in the end of this post, and will take some time explaining each one and how it is used and what it does, OK?
Enjoy!
IMPORTANT: some of these functions require Matrix1Utils plugin.
1. NAME GENERATOR FUNCTION KIT
return string = random_name(size as int,alphabet as string)
It returns a string that is a random name produced with the alphabet string provided. The alphabet string is a set of characters that are used to produce the name, obviously.
Size is not perfect, because it only says how many times it will iterate the generator, the final size can be greater than 1.5x the size in the worst of hypothesis.
To provide the alphabet, user have 2 choices:
1) write his/her own string, useful when you want to specify certain characters for the name generated
2) use the next function to do the dirty work
return string = alphabet_make(vowels as int, consonants as int)
What you see is what you'll get. Tell the function how many vowels you want for your alphabet, then how many consonants and it will give you a string containing what was randomly picked using your parameters.
This function has a fail-safe dumb-proof section in its code that randomly pick another vowel/consonant values if the given ones are not valid, to avoid someone from - for example - telling it that the alphabet has 6 or 7 vowels, something that is impossible.
2. PERCENTAGE FUNCTION:
If you want to know how many % of a value is some other value, this one will do it for you.
return float = percent(value as int, total as int)
Value and total are the ones you will use to find the percentage value, I think there is nothing else to explain since you all are smart enough to deduce how it work.
3. SAFE DELETION OF IMAGES:
In fact you could modify this to anything - sprites, matrices, objects...) but I made one for images and was too laze to make the others (someone can make them, it would be great).
It works in a way so simple that you will say "how did i never thought in something like that?"
All it does is check if the resource exists before trying to delete it. If it does, the function will delete it, but if it doesn't, nothing will be done, avoiding errors such "image does not exist".
safe_del_img(image_number as int)
4. CAPITALIZATION OF A STRING:
I know that there is the UPPER$ and LOWER$ functions that does the work of passing all chars of a string to upper or lower case.
It works great, but when you want to capitalize a string, you need to do more than just put it inside a function and wait for the work to be done.
This function does it for you in a easy way, quite like UPPER$ and LOWER$. Check out some examples:
STRING => String
STRING TO TRANSFORM => String To Transform
sTRING => String
sTrInG => String
In fact, it dosn't care if the string is upper or lower case, it will make it look like the examples above, no matter how large it is.
return string = caps(string)
5.PROCEDURAL SPACE INVADERS-LIKE SHIP:
A little thing I use for some variated purposes and now I am sharing with you. It does that: it makees an Space Invaders spaceship and store it in an image you can retrieve later.
space_invader(bitmap_number as int, image_number as int, rows as int, lines as int)
bitmap_number is the bitmap the function will use to draw the ship onto. Be sure of choosing one not in use, because the function will delete it after making its work.
image_number is the number of the image the function will use.
rows, lines are the x and y size of the space invaders ship in pixels.
Remember this: the generated image will be REALLY SMALL, since it will be drawn with few pixels. Useful to use with sprites, since it is small and eats little memory!
6. RANDOM COLOR GENERATOR:
Useful when you need to pick a color but don't want to lose your time thinking about that.
The only problem is that this function requires a little mental math (or the Windows calculator) to be fully used. I'll explain why in the function parameters' explanation.
random_color(color_step as int, no_black as boolean)
color_step is the one that'll need further calculation, since it is the number that will be divided by 255 to define how many shades of red, green and blue will used to make your color in the end.
For instance, a color_step of 2 will give you about 128 shades of each of the three colors, a 4 will give you around 64 and so on. A 128 color_step will give you similar color palletes you could get on old MS-DOS.
no_black is a boolean used to produce non-black colors - when you want colors that won't be transparent. If it is set to 1, if the color generated is black - rgb(0,0,0) - it will turn it in an almost-black - rgb(1,1,1) - that is not black but it is almost.
7. DISTANCE CALCULATION
For when you need to know how far 2 points are, there are 2 variables - 1D and 2D.
return integer = distance1d(x1,y1)
return integer = distance2d(x1,y1,x2,y2)
I think they are very self-explanative.
8. ANGULAR LINE DRAWING:
Easy as killing rabbits with an axe.
angleline(x as int,y as int,angle as int,lenght as int, clock as boolean)
x and y are the starting point of the line.
angle is, obviously, the angle of the line in degrees.
lenght is the lenght of the line in pixels (duh).
clock is a special parameter to allow you to use this line function to make a clock, if set to 0, the 0 degrees line will point to the right of the screen, but with a value of 1, it will point up like the clock hand at 12:00 o'clock.
9. LIFE BARS:
To make things easier, a little life bar function you can use anywhere and that doesn't require external media to work.
bar(x1,y1,x2,y2,color1 as dword,color2 as dword,current_value,max_value,mirror as boolean)
x1 and y1 are the top-left corner of the life bar.
x2 and y2 are the bottom right corner of the bar.
color1 is the color of the bar itself, a dword that stors an rgb value.
color2 is the background color
current_value is the current value - the current life your character has.
max_value is the total life points your character has.
mirror is a boolean, a 0 is normal, an 1 mirrors the life bar.
10. CLICKABLE BUTTON:
A function that tracks if mouse was clicked within a rectangular area. Useful for buttons or anything that requires you click on a certain spot.
return boolean = button_click(x1,y1,x2,y2)
x1,y1 are the top-left corner of the clickable area
x2 and y2 are the bottom-right.
It will return 1 if mouse was clicked and 0 if it hasn't click.
Easy, huh?
OK, now the huge snippet i promised. It is not very well indented or spaced, but I tried to make it readable enough to be edited and used easily by anyone.
function alphabet_make(vowels,consonants)
//for use with random_name() function
//it makes an alphabet
//with random number of vowels
//and consonants
//and if the parameters are invalid
//it randomly defines valid ones
if vowels < 1 or vowels > 5 then vowels = rnd(4)+1
if consonants < 1 or consonants > 21 then consonants = rnd(20)+1
vowel$ = "aeiou"
consonant$ = "bcdfghjklmnpqrstvwxyz"
while len(vowel$) <> vowels
vowel$ = remove$(vowel$,rnd(len(vowel$)-1)+1,1)
endwhile
while len(consonant$) <> consonants
consonant$ = remove$(consonant$,rnd(len(consonant$)-1)+1,1)
endwhile
alphabet$ = vowel$ + consonant$
ENDFUNCTION alphabet$
function percent(value,total)
//returns how many % the first value is of the second
//in floating point
perc# = (value/total)*100
ENDFUNCTION perc#
function safe_del_img(img_number)
//delete image if it exists, avoiding annoying errors
if image exist(img_number) = 1
delete image img_number
ENDIF
ENDFUNCTION
function caps$(string$)
//Your String Will Be Formatted Like This
for x = 1 to len(string$)
if l$ = "" or l$ = " "
l$ = UPPER$(mid$(string$,x))
else
l$ = LOWER$(mid$(string$,x))
endif
new_string$ = new_string$ + l$
NEXT
ENDFUNCTION new_string$
function random_name(size,include$)
//include$ = alphabet to be used
exclude$ = lower$(exclude$)
letter = rnd(1) //defines if it will start with vowel or consonant
vowels$ = "aeiou"
consonant$ = "bcdfghjklmnpqrstvwxyz" //letter pools
initialsize = size
repeat
if letter = 0
which = rnd(len(vowels$)-1)+1 //get a vowel
l$ = mid$(vowels$,which)
if rnd(5) = 0 and size < initialsize //1 chance in 6 of two vowels together
which = rnd(len(vowels$)-1)+1 //get a vowel
l$ = l$ + mid$(vowels$,which)
ENDIF
endif
if letter = 1 //get consonant
which = rnd(len(consonant$)-1)+1
l$ = mid$(consonant$,which)
//consonant combinations & conditions
if size = initialsize-1
if l$ = "q"
l$ = l$ + "u"
ENDIF
ENDIF
if size < initialsize and rnd(3) = 3
if l$ = "c"
add = rnd(4)
if add = 0 then l$ = l$ + "k"
if add = 1 then l$ = l$ + "h"
if add = 2 then l$ = l$ + "l"
if add = 3 then l$ = l$ + "r"
if add = 4 then l$ = l$ + "t"
ENDIF
if l$ = "b" or l$ = "d" or l$ = "f" or l$ = "g" or l$ = "p" or l$ = "t" or l$ = "v"
add = rnd(4)
if add = 0 then l$ = l$ + "r"
if add = 0 then l$ = l$ + "l"
if add = 0 then l$ = l$ + "h"
ENDIF
if l$ = "l" or l$ = "m" or l$ = "n" or l$ = "r" or l$ = "s"
l$ = l$ + l$
ENDIF
ENDIF
endif
matches = 0
for x = 1 to len(include$) //check for alph matches
for y = 1 to len(l$)
if mid$(include$,x) = mid$(l$,y)
inc matches
endif
NEXT
NEXT
if matches = len(l$)
name$ = name$ + l$
inc letter
if letter > 1 then letter = 0
dec size
endif
until size = 0
ENDFUNCTION name$
function space_invader(bitmap_to_draw_onto,image_number,columns,lines)
//draws a space invaders-esque ship
create bitmap bitmap_to_draw_onto,columns,lines
for y = 0 to lines
for x = 0 to ceil(columns/2)
if rnd(1) = 1
dot x,y
dot (columns-1)-x,y
endif
NEXT
NEXT
IF IMAGE EXIST(image_number) = 1 then delete image image_number
get image image_number,0,0,columns,lines
delete bitmap bitmap_to_draw_onto
ENDFUNCTION
function random_color(num_step as integer,no_black as boolean )
amount = int(255/num_step) //get number o of possible values for rgb
r = rnd(amount)*num_step //randomize rgb values
g = rnd(amount)*num_step
b = rnd(amount)*num_step
color as dword //declaring dword for color storing
color = rgb(r,g,b)
if no_black = 1 and color = rgb(0,0,0) //in case of noblack = 1
color = rgb(1,1,1) //transforms black in 'almost black'
ENDIF
ENDFUNCTION color
function distance1d(x1,x2)
d = abs(x1-x2)
ENDFUNCTION d
function distance2d(x1,y1,x2,y2)
dx = abs(x2 - x1)
dy = abs(y1 - y2)
dt = abs(dx-dy)
ENDFUNCTION dt
function angleline(x,y,angle as float,lenght,clock as boolean)
//make lines based in angle given
//clock parameter set to 1 to make angle 0 point up
if clock = 1
angle = wrapvalue(angle-90)
ENDIF
nx = (cos(angle)*lenght)+x
ny = (sin(angle)*lenght)+y
line x,y,nx,ny
ENDFUNCTION
function bar(x1,y1,x2,y2,color1 as dword,color2 as dword,current_value,max_value,mirror as boolean)
//use it for life bars or progress bars
box x1,y1,x2,y2,color1
scope = abs(x1 - x2)
perc = (current_value*scope)/max_value
if mirror = 0
box x1,y1,x1+perc,y2,color2
else
box x2-perc,y1,x2,y2,color2
endif
ENDFUNCTION
function button_click(x1,y1,x2,y2)
//look for mouseclick in the delimited area
//useful for buttons
if mousex() >= x1 and mousex() <= x2 and mousey() >= y1 and mousey() <= y2
inside = 1
else
inside = 0
endif
if mouseclick() = 1 and inside
clicked = 1
else
clicked = 0
ENDIF
ENDFUNCTION clicked
Now, if someone could transform these functions into a DLL for me, I would be eternally grateful.
<b><i>The mind is the compiler of dreams.</i></b>