PLEX Source Code Version 0.03

Leave a comment Standard

Build ScreenI’ve done several things this week. The game now checks to see if you have a correct login, and only lets you login if it matches a login found in the game database. Once the player is logged in it checks to see if they are standing in a valid location (ie no block on top of them, and no block that will hurt their health like a water block) and moves them if they are not in a valid location to a valid one. Then the board and graphics for the build game are loaded onto the screen. So far the player cannot select and move blocks yet, but that will be the next thing I tackle.

One of the problems I was having had to do with the online board. It’s 100 x 100 and 5 levels deep. At first I was manually typing all of the blocks in and then I realized to do that would be incredibly stupid (at least I didn’t realize it half way through adding the blocks in!) so I wrote the script in PHP below to automatically populate the database for me.

<?php

/*******************

* File Name: plex_functions.php

* Author: G. J. Krafsig

* Date: October 16, 2007

* Purpose: populate the plex database with random blocks

*		   for the online board

*******************//*******************

*          !!! IMPORTANT !!!

*

* DO NOT USE A ROOT LOGIN TO CONNECT

* TO AN ONLINE DATABASE. THIS IS FOR A

* CONNECTION TO AN OFFLINE DATABASE FOR

* TESTING PURPOSES ONLY!

*******************/

mysql_connect("localhost", "root", "") or die ('Database connection error: ' . mysql_error());

//use the database for this game for the queries below

mysql_select_db("plex");

//call the function to fill the database table

//populate();

for ($i = 0; $i < 100; $i++)

 for ($j = 0; $j < 100; $j++)

 	mysql_query("INSERT INTO board (x, y, depth, type) VALUES ('$i', '$j', '4', '-1')")

 	or die ('cannot insert board blocks');

/*******************

* Purpose: Loop for all the depths, EXCEPT THE TOP ONE,

*		   to fill the board with blocks

* Precondition: database connection is open, database named plex exists, table named board exists

*

* SQL for board table:

* 		CREATE TABLE `board` (

*			`id` int(11) NOT NULL auto_increment,

*			`x` int(11) NOT NULL,

*			`y` int(11) NOT NULL,

*			`depth` int(11) NOT NULL,

*			`type` int(11) NOT NULL,

*			`locked` tinyint(1) NOT NULL,

*			UNIQUE KEY `id` (`id`)

*		) ENGINE=MyISAM AUTO_INCREMENT=0;

*

* Postcondition: online board table is now fully populated with values

*******************/

function populate()

{

 for ($depth = 0; $depth <= 3; $depth++)

 	for ($i = 0; $i <= 100; $i++)

 		for ($j = 0; $j <= 100; $j++)

 		{

//blocks at depth 0 should all be the same kind originally

 			if ($depth == 0)

 			{

 				$block = 1;

 				mysql_query("INSERT INTO board (x, y, type, depth) VALUES ('$i', '$j', '$block', '$depth')")

 				or die ('cannot insert a block here');

 			}

 			//for all other depths

 			else

 			{

//generate a random block

 				$block = rand(0, 9);

//no tall block below this one taking up two spaces on the board, add a block here

 				if (!getTallBlock($i, $j, $depth-1))

 					mysql_query("INSERT INTO board (x, y, type, depth) VALUES ('$i', '$j', '$block', '$depth')")

 					or die ('cannot insert a block here');

else //there is a tall block in this spot, put a NOBLOCK value there (-1)

 					mysql_query("INSERT INTO board (x, y, type, depth) VALUES ('$i', '$j', '-1', '$depth')")

 					or die ('cannot insert a block here');

 			} //end else

 		} //end for loop

} //end function populate

/*******************

* Purpose: check to see if the block at this location and depth

*		   is tall and takes up two spaces on the board

* Precondition: the x and y position, and the depth of the block

* Postcondition: return true if the block here is tall and takes up 2 depth spaces

*******************/

function getTallBlock($x, $y, $depth)

{

 //get the type of block from the database

 $result = mysql_query("SELECT type FROM board WHERE x='$x' AND y='$y' AND depth='$depth'")

 		  or die ("cannot select block in this place");

//store the results

 $row = mysql_fetch_array($result);

//if this is a tall block, return true, otherwise return false

 switch ($row['type'])

 {

 	case 5: case 7: return 1; break;

 	default: return 0; break;

 }

}

?>

Another problem I had was with the images. A lot of them have drop shadows, so when I put the transparent pink color makecol(255, 0, 255) behind them they still bleed pink around the shadows and edges of the pictures. To correct this I have been going in manually and editing all of the images which takes forever. So I tried to make a function that manually goes through all of the colors close to makecol(255, 0, 255) and replace them with shades of gray and black. I had some success with this, but because I’m a stickler for perfection and I couldn’t get it replacing 100% of the bleeding pink I’m going back to fixing them all manually. But I’m including the code here because it is a pretty nifty function and someone else down the road might find some use for it:

You can download the new source code for PLEX here.

/***************
* Purpose: Remove the pink bleed from images with transparent background
* Precondition: The image to change
* Postcondition: The pink has been removed
***************/
BITMAP *removePink(BITMAP *image)
{
    int oldpixel, newpixel;
    int r, g, b;

    for (int i = 0; i < image->w; i++)
        for (int j = 0; j < image->h; j++)
        {

            oldpixel = getpixel(image, i, j);
            r = getr(oldpixel);
            g = getg(oldpixel);
            b = getb(oldpixel);

            //replace pixels that are close to 255, 0, 255
            //ignore pixels where g is not set to 0
            if (g == 0) //make these pixels shades of gray
            {

                //almost black
                if (r >= 55 && r < 65 && b >= 55 && b < 65)
                    newpixel = makecol(0, 0, 0);

                //dark dark gray
                else if (r >= 65 && r < 85 && b >= 65 && b < 85)
                    newpixel = makecol(25, 25, 25);

                //dark medium gray
                else if (r >= 85 && r < 100 && b >= 85 && b < 100)
                    newpixel = makecol(35, 35, 35);

                //dark light gray
                else if (r >= 100 && r < 125 && b >= 100 && b < 125)
                    newpixel = makecol(55, 55, 55);

                //medium dark gray
                else if (r >= 125 && r < 155 && b >= 125 && b < 155)
                    newpixel = makecol(75, 75, 75);

                //medium medium gray
                else if (r >= 155 && r < 185 && b >= 155 && b < 185)
                    newpixel = makecol(105, 105, 105);

                //medium light gray
                else if (r >= 185 && r < 200 && b >= 185 && b < 200)
                    newpixel = makecol(115, 115, 115);

                //dark gray
                else if (r >= 200 && r < 215 && b >= 200 && b < 215)
                    newpixel = makecol(125, 125, 125);

                //medium gray
                else if (r >= 215 && r < 235 && b >= 215 && b < 235)
                    newpixel = makecol(135, 135, 135);

                //light gray
                else if (r >= 235 && r < 255 && b >= 235 && b < 255)
                    newpixel = makecol(155, 155, 155);

                //only replace semi-transparent blocks
                if (r != 255 && b != 255 && newpixel)
                {
                    putpixel(image, i, j, newpixel);
                    newpixel = 0;
                }

                oldpixel = 0;

            }

        }
    return image;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s