Source

Selador / tilelevel.php

Full commit
<?php

/* tilelevel.php - Back-end utility code that draws the level numbers around cities for the land view
 *
 * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
 *
 * This file is part of Selador, a browser-based fantasy strategy game
 *
 * This program is distributed under the terms of the GNU Affero General Public License.
 *
 *
 *   Selador is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   Selador is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with Selador.  If not, see <http://www.gnu.org/licenses/>.
 **/

$cwd = getcwd ();
ini_set("include_path", $cwd."/php:".$cwd."/php/base:".$cwd."/var/php:.");

require_once ("php/base/world.inc.php");
require_once ("php/base/config.inc.php");
require_once ("php/base/user.inc.php");
// require_once ("auxiliary.php");

// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.')."/fonts");

$font="Boisterb";
$fontsize=24;

$world = new World;
$world->ConnectWorld();
session_start ();

$me = $_SESSION['me'];

function LevelPic_Text ($pic, $x, $y, $num, $fontcolor)
{
	global $font, $fontsize;

	$size = imagettfbbox($fontsize, 0, $font, (int)$num);

	$xsize = $size[4] - $size[6];
	$ysize = $size[3] - $size[5];

	// imagestring($pic, 5, 28, 19,  $num, $color);
	ImageTTFText($pic, $fontsize, 0, 64 - (int)($xsize/2) + $x, 64 - (int)($ysize/2) + $y, $fontcolor, $font, $num);
	// return $pic;
}

function ReserveGraph ($pic, $x, $y, $reserve, $greencolor, $redcolor)
{
	if (!FINITE_RESSOURCES)
		return true;

	$redpart = (32 - round ($reserve / RESS_RESERVE * 32));

	// echo $reserve.": ".(32 - ((int) ((float)$reserve / 1000000.0 * 32)))." ".((float)$reserve / 1000000.0)." .... ";

	imagefilledrectangle ($pic, $x+48, $y+24, $x+$redpart + 48, $y+26, $redcolor);
	imagefilledrectangle ($pic, $x+48+$redpart, $y+24, $x+80, $y+26, $greencolor);
}

function CreateLevelPic ($villageid, $uid, $x=0, $y=0)
{
	global $font, $fontsize, $me, $world;

	if ($villageid)
	{
		$query = "select * from village where villageid=".$villageid." and uid=".$uid;

		$res = mysql_query ($query);
		if (!($vill = mysql_fetch_array ($res)))
		{
			// echo mysql_error ()." ".$query;
			return (0);
		}

		$vx = $vill['x'];
		$vy = $vill['y'];

		if (isset ($me->villages[$villageid]))
		{
			if ($me->villages[$villageid]->expansion_slots > 0)
				$expansion = true;
			else
				$expansion = false;
		}
		else
			$expansion = false;

		$query = "select *,time_to_sec(timediff(now(),lastchange)) as lastchange2 from tileres A, map B where A.x=B.x and A.y=B.y and A.villageid=".$villageid;
		$res = mysql_query ($query);
		while ($row = mysql_fetch_array ($res))
		{
			$level[($row['x']-$vx+2)][($row['y']-$vy+2)] = (int)$row['level'];
			$reserve[($row['x']-$vx+2)][($row['y']-$vy+2)] = $me->display_reserve ($row);
		}
	}
	else
	{
		if ($x && $y)
		{
			$query = "select * from map where x between ".($x-1)." and ".($x+1)." and y between ".($y-1)." and ".($y+1);

			$res = mysql_query ($query);
			while ($row = mysql_fetch_array ($res))
			{
				$reserve[($row['x']-$x+2)][($row['y']-$y+2)] = $row['reserves'];
			}
		}
	}

	header ("Content-type: image/gif");

	$pic = @imagecreate(128*3, 64*3)
		   or die("Cannot Initialize new GD image stream");
//	imagealphablending ($pic, true);
//	$color = ImageColorAllocateAlpha($pic,0,0,0, 30);  // black quite transparent
	$color = ImageColorAllocate($pic,0,0,0);  // black
	// $trans = ImageColorAllocateAlpha ($pic, 255, 255, 255, 127);  // see it
//	$trans = ImageColorAllocateAlpha ($pic, 255, 255, 255, 127);  // see it
	$trans = ImageColorAllocate ($pic, 0, 0, 0);  // see it
	ImageColorTransparent($pic, $trans);
	imagefill ($pic, 0, 0, $trans);

	$fontcolor = ImageColorAllocateAlpha($pic,0,0,0, 30);  // black quite transparent

	$greencolor = ImageColorAllocateAlpha($pic, 0, 130,0, 30);  // green quite transparent
	$redcolor = ImageColorAllocateAlpha($pic, 180, 0, 0, 30);  // red quite transparent

	// if ($expansion || $level[3][0] > 0) LevelPic_Text ($pic, 128, 0, $level[3][0]);
	// if ($expansion || $level[4][1] > 0) LevelPic_Text ($pic, 256, 0, $level[4][1]);
	// if ($expansion || $level[6][2] > 0) LevelPic_Text ($pic, 384, 0, $level[6][2]);

	// if ($expansion || $level[2][0] > 0) LevelPic_Text ($pic, 64, 32, $level[2][0]);

	if (!$uid)
	{
		ReserveGraph ($pic, 128, 0,  $reserve[3][1], $greencolor, $redcolor);
		ReserveGraph ($pic, 64, 32, $reserve[2][1], $greencolor, $redcolor);
		ReserveGraph ($pic, 192, 32, $reserve[3][2], $greencolor, $redcolor);
		ReserveGraph ($pic, 0, 64, $reserve[1][1], $greencolor, $redcolor);
		ReserveGraph ($pic, 256, 64, $reserve[3][3], $greencolor, $redcolor);
		ReserveGraph ($pic, 64, 92, $reserve[1][2], $greencolor, $redcolor);
		ReserveGraph ($pic, 192, 92, $reserve[2][3], $greencolor, $redcolor);
		ReserveGraph ($pic, 128, 128, $reserve[1][3], $greencolor, $redcolor);
	}
	else
	{
		if ($expansion || $level[3][1] > 0)
		{
			LevelPic_Text ($pic, 128, 0, $level[3][1], $fontcolor);
			ReserveGraph ($pic, 128, 0,  $reserve[3][1], $greencolor, $redcolor);
		}
		// if ($expansion || $level[4][2] > 0) LevelPic_Text ($pic, 320, 32, $level[4][2]);
		// LevelPic_Text ($pic, 448, 32, $level[6][3]);

		// if ($expansion || $level[1][0] > 0) LevelPic_Text ($pic, 0, 64, $level[1][0]);
		if ($expansion || $level[2][1] > 0)
		{
			LevelPic_Text ($pic, 64, 32, $level[2][1], $fontcolor);
			ReserveGraph ($pic, 64, 32, $reserve[2][1], $greencolor, $redcolor);
		}

		if ($expansion || $level[3][2] > 0)
		{
			LevelPic_Text ($pic, 192, 32, $level[3][2], $fontcolor);
			ReserveGraph ($pic, 192, 32, $reserve[3][2], $greencolor, $redcolor);
		}
		// if ($expansion || $level[4][3] > 0) LevelPic_Text ($pic, 384, 64, $level[4][3]);
		// LevelPic_Text ($pic, 512, 64, $level[6][4]);
		// LevelPic_Text ($pic, 640, 64, $level[7][5]);

		if ($expansion || $level[1][1] > 0)
		{
			LevelPic_Text ($pic, 0, 64, $level[1][1], $fontcolor);
			ReserveGraph ($pic, 0, 64, $reserve[1][1], $greencolor, $redcolor);
		}
		// if ($expansion || $level[2][2] > 0) LevelPic_Text ($pic, 192, 96, $level[2][2]);
		if ($expansion || $level[3][3] > 0)
		{
			LevelPic_Text ($pic, 256, 64, $level[3][3], $fontcolor);
			ReserveGraph ($pic, 256, 64, $reserve[3][3], $greencolor, $redcolor);
		}
		// LevelPic_Text ($pic, 448, 96, $level[5][4]);

		// if ($expansion || $level[0][1] > 0) LevelPic_Text ($pic, 0, 128, $level[0][1]);
		if ($expansion || $level[1][2] > 0)
		{
			LevelPic_Text ($pic, 64, 92, $level[1][2], $fontcolor);
			ReserveGraph ($pic, 64, 92, $reserve[1][2], $greencolor, $redcolor);
		}

		if ($expansion || $level[2][3] > 0)
		{
			LevelPic_Text ($pic, 192, 92, $level[2][3], $fontcolor);
			ReserveGraph ($pic, 192, 92, $reserve[2][3], $greencolor, $redcolor);
		}
		// if ($expansion || $level[3][4] > 0) LevelPic_Text ($pic, 384, 128, $level[3][4]);
		// LevelPic_Text ($pic, 512, 128, $level[5][5]);
		// LevelPic_Text ($pic, 640, 128, $level[6][6]);

		// if ($expansion || $level[0][2] > 0) LevelPic_Text ($pic, 64, 160, $level[0][2]);
		if ($expansion || $level[1][3] > 0)
		{
			LevelPic_Text ($pic, 128, 128, $level[1][3], $fontcolor);
			ReserveGraph ($pic, 128, 128, $reserve[1][3], $greencolor, $redcolor);
		}
		// if ($expansion || $level[2][4] > 0) LevelPic_Text ($pic, 320, 160, $level[2][4]);
		// LevelPic_Text ($pic, 448, 160, $level[4][5]);

		// if ($expansion || $level[0][3] > 0) LevelPic_Text ($pic, 128, 192, $level[0][3]);
		// if ($expansion || $level[1][4] > 0) LevelPic_Text ($pic, 256, 192, $level[1][4]);
	}

	imagegif($pic);
	// imagedestroy($pic);
}

if (isset ($_REQUEST['village']))
	CreateLevelPic ((int)$_REQUEST['village'], $me->uid);
else
{
	if (isset ($_REQUEST['x']) && isset ($_REQUEST['y']))
	{
		CreateLevelPic (0, 0, (int)$_REQUEST['x'], (int)$_REQUEST['y']);
	}
}

?>