[QUOTE=Yoshi da Sniper,Aug 21 2004, 01:18 PM] PHP -> Hypertext Preprocessor.
php = pre hypertext prosessor. I may be in high school, but my 4.8 gpa allows me to take colege classes
so
CODE
<?php
/*
// This is a conversion of the QS universe generator to attempt a map creator for ******
// It's obviously not going to be a perfect conversion - an enhanced version will follow
// Chances are this script will fail to work until fully finished
*/
/*
// Dev notes
//
// 1. replace admin check with a permission check
// 2. remove reference to a preview
// 3. remove capacity for empty links
*/
require_once("core.inc.php");
require_once("includes/print_funcs_defunct.inc.php");
$db_name = "srbase_game";
set_time_limit(0); //a full minute to run it.
print(str_repeat(" ",300)."\n");flush(); //Make sure IE's 300 byte buffer is filled
// create the default success/error strings
// these are simply provisional code - will be replaced by the template print functions
// set default heading for success
$success_str = "
<div class=\"h4\" style=\"color: blue; text-align: center; padding: 0px;\">
******* 0.0.1b
<br />World Map and Location Generator
<br />
<br />
</div>
";
// set default heading for success
$error_str = "
<div class=\"h4\" style=\"color: blue; text-align: center; padding: 0px;\">
****** 0.0.1b
<br />Error! in World Generator
<br />
<br />
</div>
";
// only allow a user with Deity rank to run this script
if($user_perm['god'] != 1)
{
$text .= "<div align=\"center\">You possess no Rank with the authority to access this script!<br /><br /><a href=\"$CONFIG[url_prefix]/core/location.php\">Return to Location</a><br /><br /><a href=\"$CONFIG[url_prefix]/index.php\">Return to CMS</a></div>";
print_fullpage("Shadows Rising: World Generator",$error_str,$text);
}
elseif(empty($_POST['sure']) && (isset($_GET['delete_maps']) || isset($_GET['build_universe'])))
{
if(isset($_GET['delete_maps']))
{
$sure_str = "Are you sure you want to delete the maps for the world images?";
}
else
{
$sure_str = "Are you sure you want to build a new world? This may take some time.";
}
get_confirm("Shadows Rising :: World Generator","build_world_new.php",$sure_str);
}
elseif(isset($_GET['process']) || isset($_POST['process']))
{
// start building! First send the page header
print_header("Build Universe");
// second check if a map directory for the images exists
if(!file_exists("$CONFIG[map_path]/$db_name"))
{
mkdir("$CONFIG[map_path]/$db_name");
}
// seed our random number generator
mt_srand((float)microtime()*1000000);
// create an array of some startup options for colouring, size, etc.
$UNI = array();
/*************************************************/
/********************VARIABLES********************/
$extinfo = 1;
/*************************************************/
$UNI['size'] = 700;
$UNI['numsystems'] = 300; //sets total number of waypoints on map
$UNI['numlsystems'] = 50; // sets number of "populated locations" - villages/towns etc.
$UNI['mindist'] = 75;
$UNI['map_layout'] = 1; //set to 0 for random layout, 1 for grid layout
$UNI['link_dist'] = ""; //maximum distance between linked star systems (pixels).
$UNI['map_border'] = 50; //border on all sides around the image (stops numbers going off the edge) (pixels).
$UNI['minlinks'] = 4; //miniumum number of links a system may have.2
$UNI['maxlinks'] = 4; //maximum number of links a system may have.3
$UNI['num_size'] = 2; //font size for system numbers (on map).
$UNI['bg_color'] = array(0,0,0); //background colour of map
$UNI['link_color'] = array(0,0,0); //colour of links between systems90/90/90
//$UNI['num_color'] = array(0,255,255);//Most system numbers
$UNI['num_color'] = array(130,100,30);//Most system numbers //brown
$UNI['num_color2'] = array(0,255,0);//Current system number
$UNI['num_color3'] = array(255,0,0);//Sol color
$UNI['star_color'] = array(255,255,255);
$UNI['worm_one_way_color'] = array(230,230,64); //yellow
$UNI['worm_two_way_color'] = array(0,230,0); //green
$UNI['label_color'] = array(0, 255, 0);
$UNI['print_bg_color'] = array(255,255,255); //background colour of printable map.
$UNI['print_link_color'] = array(200,200,200); //link colour for printable map
$UNI['print_num_color'] = array(0,0,0);//Most system numbers for printably map
$UNI['print_star_color'] = array(0,0,0); //star colour for printable map
$UNI['print_label_color'] = array(0, 0, 0);
$UNI['localmapwidth'] = 196; //width of 'local area' map.
$UNI['localmapheight'] = 196; //height of 'local area' map.
$systems = array( array('num' => 0, 'x_loc' => $UNI['size']/2, 'y_loc' => $UNI['size']/2, 'links' => '', 'name' => 'The Capital') );
if(isset($extinfo))
{
$extinfo = true;
}
else
{
$extinfo = false;
}
if(isset($_POST['build_universe']))
{
isset($_POST['build_universe']) ? print("Generating Systems...<br />"):1; flush();
make_systems_1($systems);
isset($_POST['build_universe']) ? print("Linking Systems...<br />"):1; flush();
link_systems_1($systems);
if(isset($_POST['build_universe']))
{
//generating a new universe
print("Saving Universe...<br />"); flush();
save_universe_se1($systems, "${gameinstance}_locations", true);
set_time_limit(0);
print("<br />Deleting old images...<br />"); flush();
clear_images("$CONFIG[map_path]/$db_name");
print("Rendering global map...<br />"); flush();
render_global_se1($db_name);
print("Rendering local maps...<br />"); flush();
render_local_se1($db_name);
print("<div id='done'>Finished.<script>document.all.done.scrollIntoView();</script></div>"); flush();
print_footer();
}
else
{
//previewing universes
render_global_se1($db_name);
}
}
}
else
{
$out_str = "Choose something to do with the universe generator.<br />Only the bottom choice will re-generate the universe!";
$out_str .= "<p><a href=$_SERVER[PHP_SELF]?preview=1&process=1>Preview</a> a universe that uses your present variable settings. This won't do anything to the present game!!!";
$out_str .= "<p><br />Generate a <a href=$_SERVER[PHP_SELF]?build_universe=1&process=1>new universe</a>!<br />";
print_fullpage('Universe Generation',$out_str);
}
/*************************************************/
/********************FUNCTIONS********************/
/*************************************************/
//get's a variable from the database, and returns it's value
function get_db_var($name)
{
global $db_name;
db(__FILE__,__LINE__,"select value from ${db_name}_db_vars where name = '$name'");
$result = dbr(1);
return $result['value'];
}
//deletes all image files.
function clear_images($path)
{
$dir = opendir($path);
while($filename = readdir($dir))
{
if(eregi("\\.png$", $filename))
{
unlink("$path/$filename");
}
}
closedir($dir);
}
//save the universe
function save_universe_se1(&$systems, $table_stars, $delete=true) {
global $UNI,$db_name;
if($delete)
{
if(!empty($table_stars))
{
dbn(__FILE__,__LINE__,"delete from $table_stars");
}
}
if(!empty($table_stars))
{
foreach($systems as $system)
{
$link_arr = array();
if($system['links'] != "")
{
//don't link all systems to 1 automatically.
$link_arr = array_map("plus_one", explode(',', $system['links']));
}
$link_arr = array_pad($link_arr, $UNI['maxlinks'], 0);
dbn(__FILE__,__LINE__,"insert into $table_stars set loc_id = ".($system['num'] + 1).", loc_name = \"".addslashes($system['name'])."\", x_loc = $system[x_loc], y_loc = $system[y_loc], link_1 = '$link_arr[0]', link_2 = '$link_arr[1]', link_3 = '$link_arr[2]', link_4 = '$link_arr[3]', link_5 = '$link_arr[4]', link_6 = '$link_arr[5]'");
}
}
}
//create the star systems
function make_systems_1(&$systems) {
global $UNI,$extinfo,$tables;
db(__FILE__,__LINE__,"select * from se_svr_star_names order by rand()");
$centre = round($UNI['size'] / 2); //centre of map
$do_this = 0;
if($UNI['map_layout'] == 1)
{
//grid layout
$rows = round(sqrt($UNI['numsystems']));
$row_dist = round($UNI['size'] / $rows);
$per_col = round($UNI['numsystems'] / $rows);
$col_dist = round($UNI['size'] / $per_col);
$row_count = 0;
$col_count = 0;
}
$named_locs = 0;
while(($count = count($systems)) < $UNI['numsystems'])
{
$name_chance = mt_rand(1,round($UNI['numsystems'] / $UNI['numlsystems']));
if($name_chance == 1 && $named_locs < $UNI['numlsystems'])
{
$result = dbr();
$newname = $result['name'];
$named_locs += 1;
}
else
{
$newname = "";
}
$newsystem = array('num' => $count, 'x_loc' => mt_rand(0,$UNI['size']), 'y_loc' => mt_rand(0,$UNI['size']), 'links' => '', 'name' => $newname);
if($UNI['map_layout'] == 1) {
//grid layout
if($row_count > $rows)
{
//create a new column
$row_count = 0;
$col_count++;
}
$newsystem['x_loc'] = $col_dist * $col_count;
$newsystem['y_loc'] = $row_dist * $row_count;
$row_count++;
while(system_too_close($newsystem,$systems,$UNI['mindist']) && !empty($newsystem['name']))
{
$newsystem['x_loc'] = mt_rand(0,$UNI['size']);
$newsystem['y_loc'] = mt_rand(0,$UNI['size']);
}
}
else
{
// random layout
while(system_too_close($newsystem,$systems,$UNI['mindist']))
{
$newsystem['x_loc'] = mt_rand(0,$UNI['size']);
$newsystem['y_loc'] = mt_rand(0,$UNI['size']);
}
}
if($extinfo)
{
print("<div id=\"makesys1$count\" name=\"makesys1$count\">-Location #".($count+1)." created.<script>document.all.makesys1$count.scrollIntoView();</script></div>");
flush();
}
$systems[] = $newsystem;
}
}
//link the systems
function link_systems_1(&$systems) {
global $UNI,$extinfo;
foreach($systems as $system)
{
$numlinks = mt_rand($UNI['minlinks'],$UNI['maxlinks']);
foreach(get_closest_systems($system,$systems,$numlinks) as $linksys)
{
make_link($systems[$system['num']],$systems[$linksys['num']]);
}
if($extinfo)
{
print("<div id='linksys1$system[num]'>-Created $numlinks links in system #".($system['num']+1)."<script>document.all.linksys1$system[num].scrollIntoView();</script></div>");
flush();
}
}
}
//function that determines if it's ok to link to a system
function ok_to_link($sys1, $sys2) {
global $UNI;
//linking to itself.
if($sys1['num'] == $sys2['num'])
{
return false;
}
$sys2_links = explode(',', $sys2['links']);
//return o.k. if target still has empty links || already linked.
if((count($sys2_links) < $UNI['maxlinks']) || in_array($sys1['num'], $sys2_links) || (count($sys2_links) >= $UNI['minlinks']))
{
return true;
}
else
{
return false;
}
}
//find the closest systems to link to.
function get_closest_systems($sys,$systems,$howmany) {
global $UNI;
$dists = array();
foreach($systems as $system)
{
if(ok_to_link($sys, $system))
{
$dists[$system['num']] = get_sys_dist($sys,$system);
}
}
reset($dists);
asort($dists,SORT_NUMERIC);
$systems_to_link = array();
while(count($systems_to_link) < $howmany)
{
//find as many systems as allowed.
if(!$present_sys = each($dists))
{
//get a system out of the dist array. Return results if none.
return $systems_to_link;
}
//too far away to be linked to (Sol System excepted).
if($present_sys['value'] > $UNI['link_dist'] && $UNI['link_dist'] > 0 && $sys['num'] != 0)
{
return $systems_to_link;
}
$systems_to_link[] = $systems[$present_sys['key']];
}
return $systems_to_link;
}
//work out if a system is too close to another system
function system_too_close($sys,&$systems,$within) {
foreach($systems as $system)
{
if($system['num'] == $sys['num'])
{
//same system
continue;
}
if($dist = get_sys_dist($sys,$system) < $within)
{
//too close
return true;
}
}
return false;
}
//make a single link between two systems.
function make_link(&$sys1,&$sys2) {
if((string)$sys1['links'] != "")
{
$sys1warps = explode(',',$sys1['links']);
if(!in_array($sys2['num'],$sys1warps))
{
$sys1warps[] = $sys2['num'];
$sys1['links'] = implode(',',$sys1warps);
}
}
else
{
$sys1['links'] = $sys2['num'];
}
if((string)$sys2['links'] != "")
{
$sys2warps = explode(',',$sys2['links']);
if(!in_array($sys1['num'],$sys2warps))
{
$sys2warps[] = $sys1['num'];
$sys2['links'] = implode(',',$sys2warps);
}
}
else
{
$sys2['links'] = $sys1['num'];
}
}
//work out the distance (in pixels) between two star systems.
function get_sys_dist(&$sys1,&$sys2)
{
return (int)round(sqrt(pow($sys1['x_loc']-$sys2['x_loc'],2) + pow($sys1['y_loc']-$sys2['y_loc'],2)));
}
//generate the three global maps.
function render_global_se1($db_name) {
global $UNI,$extinfo,$games_dir, $systems, $preview, $CONFIG;
$uv_show_warp_numbers = 1;
$transparency = 1;
$size = $UNI['size'] + ($UNI['map_border'] * 2);
$numsize = $UNI['num_size'];
$offset_x = $UNI['map_border'];
$offset_y = $UNI['map_border'];
$im = ImageCreate($size,$size);
$color_bg = ImageColorAllocate($im, $UNI['bg_color'][0], $UNI['bg_color'][1], $UNI['bg_color'][2]);
$color_st = ImageColorAllocate($im, $UNI['num_color'][0], $UNI['num_color'][1], $UNI['num_color'][2]);
$color_sd = ImageColorAllocate($im, $UNI['star_color'][0], $UNI['star_color'][1], $UNI['star_color'][2] );
$color_sl = ImageColorAllocate($im, $UNI['link_color'][0], $UNI['link_color'][1], $UNI['link_color'][2] );
$color_sh = ImageColorAllocate($im, $UNI['num_color3'][0], $UNI['num_color3'][1], $UNI['num_color3'][2] );
$color_l = ImageColorAllocate($im, $UNI['label_color'][0], $UNI['label_color'][1], $UNI['label_color'][2] );
$white = ImageColorAllocate($im, 255,255,255);
$grey = ImageColorAllocate($im,90,90,90);
$lgrey = ImageColorAllocate($im,150,150,150);
$black = ImageColorAllocate($im, 0,0,0);
$red = ImageColorAllocate($im, 255,0,0);
$green = ImageColorAllocate($im, 0,255,0);
$blue = ImageColorAllocate($im, 100,200,200);
$yellow = ImageColorAllocate($im, 255,255,64);
$aliengreen = ImageColorAllocate($im, 0,181,0);
//process stars
foreach($systems as $star)
{
if($star['links'] != "")
{
//don't link all systems to 1 automatically.
$star_links = array_map("plus_one", explode(',', $star['links']));
$star_id = $star['num'] + 1;
foreach($star_links as $link)
{
//make star links
if($link < 1)
{
continue 1;
}
$other_star = $systems[$link -1];
imageline($im, ($star['x_loc'] + $offset_x), ($star['y_loc'] + $offset_y), ($other_star['x_loc'] + $offset_x), ($other_star['y_loc'] + $offset_y), $color_sl);
}
}
}
foreach($systems as $star)
{
$star_id = $star['num'] + 1;
$star_name = $star['name'];
if($star_id == 1)
{
//Place and Highlight sol
if($uv_show_warp_numbers == 1)
{
imagestring($im, $numsize, ($star['x_loc'] + $offset_x + 3), ($star['y_loc'] + $offset_y - 4), $star_name, $color_sh);
}
imagesetpixel($im, ($star['x_loc'] + $offset_x), ($star['y_loc'] + $offset_y), $color_sh);
}
else
{
//place normal Star
if($uv_show_warp_numbers == 1)
{
imagestring($im, $numsize, ($star['x_loc'] + $offset_x + 3), ($star['y_loc'] + $offset_y - 4), $star_name, $color_st);
}
imagesetpixel($im, ($star['x_loc'] + $offset_x), ($star['y_loc'] + $offset_y), $color_sd);
}
}
if(isset($transparency))
{
$ref_im = ImageCreate($size,$size);
$color_ty = ImageColorAllocate($bb_im, $UNI['bg_color'][0], $UNI['bg_color'][1], $UNI['bg_color'][2]);
ImageColorAllocate($im, $UNI['bg_color'][0], $UNI['bg_color'][1], $UNI['bg_color'][2]);
ImageCopy($ref_im, $im, 0, 0, 0, 0, $size, $size);
$aliengreen = ImageColorAllocate($ref_im, 0,181,0);
imagecolortransparent($ref_im, $color_ty);
imagestring($ref_im, 5, (($size/2)-80), 5, "Reference World Map - For Player Use", $aliengreen);
}
//for just a preview we can quite while we're ahead.
if(isset($preview)){
Header("Content-type: image/png");
ImagePng($im);
ImageDestroy($im);
exit;
}
//Draw title
imagestring($im, 5, (($size/2)-80), 5, "World Map", $color_l);
//Create buffer image
$bb_im = ImageCreate(($UNI['size'] + $UNI['localmapwidth']), ($UNI['size'] + $UNI['localmapheight']));
ImageColorAllocate($im, $UNI['bg_color'][0], $UNI['bg_color'][1], $UNI['bg_color'][2]);
ImageCopy($bb_im, $im, ($UNI['localmapwidth'] / 2), ($UNI['localmapheight'] / 2), $offset_x, $offset_y, $UNI['size'], $UNI['size']);
//Create printable map
$p_im = ImageCreate($size,$size);
ImageColorAllocate($p_im, $UNI['print_bg_color'][0], $UNI['print_bg_color'][1], $UNI['print_bg_color'][2]);
ImageCopy($p_im, $im, 0, 0, 0, 0, $size, $size);
//Replace colors
$index = ImageColorExact($p_im, $UNI['bg_color'][0], $UNI['bg_color'][1], $UNI['bg_color'][2]);
ImageColorSet($p_im, $index, $UNI['print_bg_color'][0], $UNI['print_bg_color'][1], $UNI['print_bg_color'][2]);
$index = ImageColorExact($p_im, $UNI['link_color'][0], $UNI['link_color'][1], $UNI['link_color'][2]);
ImageColorSet($p_im, $index, $UNI['print_link_color'][0], $UNI['print_link_color'][1], $UNI['print_link_color'][2]);
$index = ImageColorExact($p_im, $UNI['num_color'][0], $UNI['num_color'][1], $UNI['num_color'][2]);
ImageColorSet($p_im, $index, $UNI['print_num_color'][0], $UNI['print_num_color'][1], $UNI['print_num_color'][2]);
$index = ImageColorExact($p_im, $UNI['num_color3'][0], $UNI['num_color3'][1], $UNI['num_color3'][2]);
ImageColorSet($p_im, $index, $UNI['print_num_color'][0], $UNI['print_num_color'][1], $UNI['print_num_color'][2]);
$index = ImageColorExact($p_im, $UNI['star_color'][0], $UNI['star_color'][1], $UNI['star_color'][2]);
ImageColorSet($p_im, $index, $UNI['print_star_color'][0], $UNI['print_star_color'][1], $UNI['print_star_color'][2]);
//Draw new label
ImageFilledRectangle($p_im, 0, 0, $size, $UNI['map_border'], ImageColorExact($p_im, $UNI['print_bg_color'][0], $UNI['print_bg_color'][1], $UNI['print_bg_color'][2]));
imagestring($p_im, 5, (($size/2)-80), 5, "Printable World Map", ImageColorExact($p_im, $UNI['print_label_color'][0], $UNI['print_label_color'][1], $UNI['print_label_color'][2]));
//Save map and finish
ImagePng($im, "$CONFIG[map_path]/$db_name/sm_full.png");
ImagePng($bb_im, "$CONFIG[map_path]/$db_name/bb_full.png");
ImagePng($p_im, "$CONFIG[map_path]/$db_name/psm_full.png");
if(isset($transparency))
{
ImagePng($ref_im, "$CONFIG[map_path]/$db_name/ref_full.png");
}
if($extinfo)
{
print("<br /><br /><br /><hr><img src='$CONFIG[url_prefix]/core/maps/$db_name/sm_full.png' onLoad='this.scrollIntoView();'>");
flush();
print("<br /><br /><br /><hr><img src='$CONFIG[url_prefix]/core/maps/$db_name/ref_full.png' onLoad='this.scrollIntoView();'>");
flush();
}
ImageDestroy($im);
ImageDestroy($bb_im);
ImageDestroy($p_im);
}
//draw the local maps.
function render_local_se1($db_name) {
global $UNI, $extinfo, $games_dir, $CONFIG, $gameinstance;
$full_map = imagecreatefrompng("$CONFIG[map_path]/$db_name/bb_full.png");
db(__FILE__,__LINE__,"select * from ${gameinstance}_locations");
while($star = dbr())
{
$im = imagecreate($UNI['localmapwidth'], $UNI['localmapheight']);
$color_bg = $color_bg = ImageColorAllocate($im, $UNI['bg_color'][0], $UNI['bg_color'][1], $UNI['bg_color'][2]);
$color_ht = ImageColorAllocate($im, $UNI['num_color2'][0], $UNI['num_color2'][1], $UNI['num_color2'][2]);
$color_hd = ImageColorAllocate($im, $UNI['num_color2'][0], $UNI['num_color2'][1], $UNI['num_color2'][2]);
$black = ImageColorAllocate($im,0,0,0);
imagecopy($im, $full_map, 0, 0, $star['x_loc'], $star['y_loc'], $UNI['localmapwidth'], $UNI['localmapheight']);
if(strlen($star['loc_name']) > 0)
{
imagestring($im, $UNI['num_size'], (($UNI['localmapwidth'] / 2) + 3), (($UNI['localmapheight'] / 2) - 4), "$star[loc_name]", $color_ht);
}
imagesetpixel($im, ($UNI['localmapwidth'] / 2), ($UNI['localmapheight'] / 2), $color_hd);
imagecolortransparent($im,$color_bg);
ImagePng($im, "$CONFIG[map_path]/$db_name/sm$star[loc_id].png");
if($extinfo)
{
print("<br /><img src='$CONFIG[url_prefix]/core/maps/$db_name/sm$star[loc_id].png' onLoad='this.scrollIntoView();'>");
flush();
}
ImageDestroy($im);
}
ImageDestroy($full_map);
}
function plus_one($a)
{
return $a + 1;
}
?>
so
i worked a long time to get that to work right. it still has some glitches but Maugrim<- holds the copright for this project
has done a lot on it too.