Scripts
Wereldgenerator
Een script die een wereld genereert met aansluitend water en land. Ook geeft hij de coördinaten per vakje aan. Plaatjes moet je zelf toevoegen in de map: 'images'. Noem de plaatjes: texture_grass.jpg,texture_water.jpg,texture_sand.jpg. Maak een database aan genaamd 'worldmap' en voer daar de sql code uit.
map_generator.php
<!-- php wereldmaker -->
<?php
include_once('dbconnect.php');
include_once('config.php');
/*
* Gemaakt door Michael R
*
*/
?>
<html>
<form name = "generate" method = "post" action = "<?php echo($_SERVER['PHP_SELF']); ?>">
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
while($y <= $max_y) {
while($x <= $max_x) {
$field_texture[$x][$y] = $texture[rand(0,2)];
$last_water[$x][$y] = false;
$last_grass[$x][$y] = false;
mysqli_query($worldmap,"INSERT INTO map (x,y,texture) VALUES (".$x.",".$y.",'".$field_texture[$x][$y]."')") or die(mysqli_error($worldmap));
$x++;
}
$x = 0;
$y++;
}
$y = 0;
// Controle loop
while($y <= $max_y) {
while($x <= $max_x) {
if($min_x < $x && $max_x > $x && $max_y > $y && $min_y < $y) {
if($field_texture[$x][$y] == "water") {
if(!$last_water[$x][$y]) {
$field_texture[$x + 1][$y] = "water";
mysqli_query($worldmap,"UPDATE map SET texture = 'water' WHERE x = ".($x + 1)." AND y = ".$y."");
$last_water[$x + 1][$y] = true;
}
} elseif($field_texture[$x][$y] == "grass") {
if(!$last_grass[$x][$y]) {
if($field_texture[$x][$y - 1] != "water") {
$field_texture[$x][$y - 1] = "grass";
$last_grass[$x][$y - 1] = true;
}
}
}
}
$x++;
}
$x = 0;
$y++;
}
$y = 0;
// De wereld is gegenereerd
header('Location: map_viewer.php');
} else {
echo("<input type = 'submit' value = 'Genereer wereld!' />");
}
?>
</form>
</html>
worldmap.sql
-- phpMyAdmin SQL Dump
-- version 4.2.7.1
-- http://www.phpmyadmin.net
--
-- Machine: 127.0.0.1
-- Gegenereerd op: 21 jan 2015 om 20:20
-- Serverversie: 5.6.20
-- PHP-versie: 5.5.15
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Databank: `worldmap`
--
-- --------------------------------------------------------
--
-- Tabelstructuur voor tabel `map`
--
CREATE TABLE IF NOT EXISTS `map` (
`id` int(11) NOT NULL,
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`texture` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Indexen voor geëxporteerde tabellen
--
--
-- Indexen voor tabel `map`
--
ALTER TABLE `map`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT voor geëxporteerde tabellen
--
--
-- AUTO_INCREMENT voor een tabel `map`
--
ALTER TABLE `map`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
map_viewer.php
<?php
include_once('dbconnect.php');
//Map size
if(isset($_GET['x_max']) && isset($_GET['y_max']) && isset($_GET['x_min']) && isset($_GET['y_min'])) {
$x_min = $_GET['x_min'];
$x_max = $_GET['x_max'];
$y_min = $_GET['y_min'];
$y_max = $_GET['y_max'];
$x = $x_min;
$y = $y_min;
if($x_min < 0) {
header("Location: map_viewer.php?x_min=".($x_min + 1)."&x_max=".($x_max + 1)."&y_min=".$y_min."&y_max=".$y_max);
} elseif($x_max > 20) {
header("Location: map_viewer.php?x_min=".($x_min - 1)."&x_max=".($x_max - 1)."&y_min=".$y_min."&y_max=".$y_max);
} else {
if($y_min < 0) {
header("Location: map_viewer.php?x_min=".$x_min."&x_max=".$x_max."&y_min=".($y_min + 1)."&y_max=".($y_max + 1));
} elseif($y_max > 10) {
header("Location: map_viewer.php?x_min=".($x_min - 1)."&x_max=".($x_max - 1)."&y_min=".$y_min."&y_max=".$y_max);
}
}
} else {
$x_min = 0;
$x_max = 6;
$y_min = 0;
$y_max = 6;
$x = 0;
$y = 0;
}
?>
<html>
<!-- Wereld kaart -->
<table border = 1>
<?php
while($y <= $y_max) {
echo("<tr>");
while($x <= $x_max) {
$texture = mysqli_fetch_array(mysqli_query($worldmap,"SELECT * FROM map WHERE x = ".$x." AND y = ".$y." LIMIT 1"));
echo("<td>");
echo("<img src='images/texture_".$texture['texture'].".jpg' height = 40 width = 40 title = '(".$x.",".$y.")'/>");
echo("</td>");
$x++;
}
$x = $x_min;
echo("</tr>");
$y++;
}
$y = 0;
?>
</table>
<!-- -->
<table border = 1>
<tr>
<td></td><td><center><a href = "map_viewer.php?x_min=<?php echo($x_min); ?>&x_max=<?php echo($x_max); ?>&y_min=<?php echo($y_min - 1); ?>&y_max=<?php echo($y_max - 1); ?>">+++</a></center></td><td></td>
</tr>
<tr>
<td><center><a href = "map_viewer.php?x_min=<?php echo($x_min - 1); ?>&x_max=<?php echo($x_max - 1); ?>&y_min=<?php echo($y_min); ?>&y_max=<?php echo($y_max); ?>">+++</a></center></td><td></td><td><center><a href = "map_viewer.php?x_min=<?php echo($x_min + 1); ?>&x_max=<?php echo($x_max + 1); ?>&y_min=<?php echo($y_min); ?>&y_max=<?php echo($y_max); ?>">+++</a></center></td>
</tr>
<tr>
<td></td><td><center><a href = "map_viewer.php?x_min=<?php echo($x_min); ?>&x_max=<?php echo($x_max); ?>&y_min=<?php echo($y_min + 1); ?>&y_max=<?php echo($y_max + 1); ?>">+++</a></center></td><td></td>
</tr>
</table>
</html>
dbconnect.php
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "worldmap";
$worldmap = mysqli_connect($host,$user,$password,$database) or die("Couldn't connect to the database");
?>
config.php
<?php
// Instellingen
$min_x = 0;
$min_y = 0;
$max_x = 20;
$max_y = 10;
$x = $min_x;
$y = $min_y;
$texture = array("grass","sand","water");
//
?>
Reacties
0