{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Perlin World Generator Test\n", "---\n", "this notebook tests the Perlin world generator" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append(\"../ServerCore\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab ipympl\n", "\n", "import matplotlib.image as mpimg\n", "import scipy.ndimage as ndimage\n", "import matplotlib.pyplot as plt\n", "\n", "import numpy as np\n", "\n", "from WorldManager import WorldManager\n", "from Chunk import Chunk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* variables:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "n_chunks = 3\n", "raw_noise_cell_size = 16\n", "raw_noise_chunk_size = 4\n", "world_seed = 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* create world manager" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "wm = WorldManager(raw_noise_cell_size=raw_noise_cell_size, raw_noise_chunk_size=raw_noise_chunk_size, world_seed=world_seed)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* create world's highmap" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "c = Chunk.CHUNK_SIDELENGTH\n", "\n", "height_map = np.zeros(shape=(n_chunks * c, n_chunks * c), dtype=float)\n", "\n", "for i in range(n_chunks):\n", " for j in range(n_chunks):\n", " height_map[i * c:(i+1)* c, j*c:(j+1)*c] = wm.getPerlinMap(i,j)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* plot height map" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "23dd04daba304f2d9668d861184e7153", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FigureCanvasNbAgg()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig, ax = plt.subplots()\n", "\n", "ax.imshow(height_map, cmap='binary')\n", "ax.set_title(\"stupid interpolated perlin noise map\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* create chunks and fill them with data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "for x in range(n_chunks):\n", " for y in range(2):\n", " for z in range(n_chunks):\n", " wm.createEmptyChunk(x,y,z)\n", " wm.applyPerlinToChunk(x,y,z)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* concatenate to one big array (just for plotting)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "world = np.zeros(shape=(n_chunks * c, 2*c, n_chunks*c), dtype=int)\n", "\n", "for x in range(n_chunks):\n", " for y in range(2):\n", " for z in range(n_chunks):\n", " world[x*c:(x+1)*c, y*c:(y+1)*c, z*c:(z+1)*c] = wm.chunks[x][y][z].block_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* and plot it" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from mpl_toolkits.mplot3d import Axes3D\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "colors = np.zeros(shape=(n_chunks * c, 2*c, n_chunks*c,3))\n", "for x,y,z in np.ndindex(world.shape):\n", " i = y/(c)\n", " colors[x,y,z,:] = [0,i,1-i]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3010f3d5167449b196314981a687108b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FigureCanvasNbAgg()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "fig2 = plt.figure()\n", "ax2 = fig2.gca(projection='3d')\n", "\n", "p = ax2.voxels(np.rollaxis(world,2), facecolors=np.rollaxis(colors,2))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }