What the heck - it was easy to implement
// Project: CreateObjectFromMeshMemblock
// Created: 2018-03-02
// show all errors
SetErrorMode(2)
// Setup the hard coded constants
#constant screenwidth=800
#constant screenheight=600
#constant blocksizex=18 // size of the chunk
#constant blocksizey=1 // size of the chunk
#constant blocksizez=18 // size of the chunk
#constant blocksize#=16 // size of each cube
#constant memblocksize#=1000000
#constant landair = 20 // how many cubes within a layer has air blocks (empty spaces) ratio of 0 to 50
#constant mainchunkair = 20 // how many cubes within a layer has air blocks (empty spaces) ratio of 0 to 50
#constant totalworldchunks=4 // how many of the 16x16 chunks are generated and surrounded
#constant totalfarworldchunks=50 // this is an instance of objects in the far distance
#constant maxtexturemeshes=0 // how many different textured blocks on for each layer
// camera/ player speed
#constant playerspeed#=20
// setup perlinmapdata
#constant perlinw = 256 //power of 2
#constant perlinh = 256 //power of 2
#constant perlinz = 256
// set window properties
SetWindowTitle( "CreateObjectFromMeshMemblock" )
SetWindowSize( screenwidth, screenheight, 1 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( screenwidth,screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 1200, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetDefaultMagFilter(0)
SetDefaultMinFilter(0)
// setup the types - each entity may have multiple of entities to keep track of, these can be stored as Types
type _textures
id // texture store
r,g,b // colors
dr,dg,db // depths (because minecraft tiles is built with rectangular different shades of the color)
texturessize
endtype
type _worldlayers
id
mesh
location as integer[blocksizex,blocksizez] // check if a object already exists in the meshes
taken
endtype
type _perlinmap
r
g
b
a
texture
noise
endtype
type _playblocks
spr
red
green
blue
endtype
global playblocks as _playblocks[10]
type _tree
cones as integer[10]
stump
height#
endtype
global tree as _tree[10000] // how many in the scene
// Initialise Globals
// Inventory Variables
global playerblocks // stored the players object of which will use to add/destroy
global currentselectedplayblock,mouse
// world variables
global perlinmap as _perlinmap[perlinw,perlinh,perlinz]
global perlinx, perlinzz
global worldlayers as _worldlayers[20000] // this stores each the chunks id, used to join them all up together
// use "Totalworldchunks" to set how many joined on the x and z axis
global worldchunks
global chunks, totalcubecount, blockcount, totalblockcount, chunklevelid
// textures
global textures as _textures[50] // used to store textures (not ready yet)
// memblock variables
global face,vertcount,vertoff,vertsize
// Camera Variables
global camerax#,cameray#,cameraz#
global angx#,angy#,startx#,starty#
// Object variables
global treecount=0
global ocean
// Run the Game Title Screen
// title() - Not ready yet
// Initialise Game
InitialiseGame()
chunklevelid=15000
do
movecamerawithmouse()
displayinventoryandhealth()
checkkeypresses()
displaydebuginfo()
if GetRawKeyPressed(48)
endif
if GetRawKeyPressed(48)
layer=random(1,50)
mesh = CreateMemblockFromObjectMesh(worldlayers[layer].id,1)
for b=1 to 1000
cube = random(1,blocksizex*blocksizez)*36
for a=cube to cube+35
SetMeshMemblockVertexPosition(mesh,a,GetMeshMemblockVertexX(mesh,a),GetMeshMemblockVertexy(mesh,a)+blocksize#,GetMeshMemblockVertexz(mesh,a))
next
next
SetObjectMeshFromMemblock(worldlayers[layer].id,1,mesh)
DeleteMemblock(mesh)
endif
if GetRawMouseLeftState()
layer=Random(1,totalcubecount-1)
mesh = CreateMemblockFromObjectMesh(worldlayers[layer].id,1)
cube = random(0,blocksizex*blocksizez)*36
for a=cube to cube+35
SetMeshMemblockVertexPosition(mesh,a,GetMeshMemblockVertexX(mesh,a),GetMeshMemblockVertexy(mesh,a)+blocksize#,GetMeshMemblockVertexz(mesh,a))
next
SetObjectMeshFromMemblock(worldlayers[layer].id,1,mesh)
DeleteMemblock(mesh)
endif
if GetRawMouseRightState()
layer=Random(1,totalcubecount-1)
mesh = CreateMemblockFromObjectMesh(worldlayers[layer].id,1)
cube = random(0,blocksizex*blocksizez)*36
for a=cube to cube+35
SetMeshMemblockVertexPosition(mesh,a,GetMeshMemblockVertexX(mesh,a),GetMeshMemblockVertexy(mesh,a)-blocksize#,GetMeshMemblockVertexz(mesh,a))
next
SetObjectMeshFromMemblock(worldlayers[layer].id,1,mesh)
DeleteMemblock(mesh)
endif
sync()
loop
function InitialiseGame()
createblockpointer()
definetextures()
setupinventoryitems()
setupskyandfog()
// createtree(0)
//createocean()
buildmap()
totalblockcount=1
// generatechunk()
buildchunks()
initialisecamera()
endfunction
function createocean()
ocean=CreateObjectPlane(100000,100000)
SetObjectRotation(ocean,90,0,0)
SetObjectColor(ocean,0,0,50,255)
SetObjectPosition(ocean,getobjectx(ocean),-blocksize#,getobjectz(ocean))
endfunction
function createtree(id)
tree[id].height#=random(1,4)
tree[id].stump = CreateObjectCylinder(tree[id].height#*2,tree[id].height#/2,10)
SetObjectColor(tree[id].stump, 83, 53, 10,255)
for a=0 to tree[id].height#*1.5
tree[id].cones[a] = CreateObjectCone(a,2+a,50)
SetObjectPosition(tree[id].cones[a],0,2+(tree[id].height#)-a,0)
SetObjectColor(tree[id].cones[a],random(50,100),random(120,170),0,255)
fixobjecttoobject(tree[id].cones[a],tree[id].stump)
next
endfunction tree[treecount].stump
function generatechunk(meshid,worldlayer,air)
chunks=100 // memory reference ic
blockcount=1
makememblock(chunks, meshid)
xpos#=0:ypos#=0:zpos#=0
// for y=1 to blocksizey
for z=1 to blocksizez
for x=1 to blocksizex
// pos=random(1,blocksize#)
// pos#=perlinmap[perlinx,perlinzz,0].noise*10
// pos#=Noise2D(x,z) * blocksize#
// pos#=blocksize# * (cos(x) * sin(z)) * 10
// pos# = random2(0,1)
pos#=0
// if worldlayers
//if random(1,50) <landair
addplane(chunks, xpos#,pos#,zpos#,blockcount,x,0,z)
inc blockcount
//endif
worldlayers[worldlayer].location[x,z] = meshid+1
inc totalblockcount
//inc blockcount
inc totalcubecount
xpos#=xpos#+blocksize#
inc perlinx
if perlinx>256 then perlinx=1
next x
zpos#=zpos#+blocksize#
xpos#=0
inc perlinzz
if perlinzz>256 then perlinzz=1
next z
zpos#=0
xpos#=0
ypos#=ypos#+blocksize#
// perlinx=1
// perlinzz=1
//next y
// worldlayers[chks].id
// worldchunks=CreateObjectFromMeshMemblock(chunks)
worldlayers[worldlayer].id=CreateObjectFromMeshMemblock(chunks)
// CreateMemblockFromObjectMesh(worldchunks,1)
// worldlayers[worldlayer].mesh=CreateMemblockFromObjectMesh(worldchunks,1)
endfunction
function buildmap()
// start perlin map data
NoiseInit()
mapimg=generateperlinimage()
mapsprite = CreateSprite(mapimg)
//SetSpriteSize(mapsprite,128,128)
SetSpritePosition(mapsprite,screenwidth-100,10)
endfunction
function buildchunks()
totalcubecount=0
a=1
initialisememblock(100,1)
// you may get memory issues if dont tweak the chunk size - cause having 3 meshes on the memblock will soon build up
// to 100,000 (limit) and causing GPU memory runtime failures. If you do, try reducing the chunk size (at the top of the
// program
// actual chunks that have vertices changes available
for wx=-totalworldchunks to totalworldchunks
for wz=-totalworldchunks to totalworldchunks
for meshid=0 to maxtexturemeshes // multiple textures on each chunk - lets do 3 to start with
generatechunk(meshid+1,a,8)
// worldlayers[a].id = InstanceObject(genchunk)
// AddObjectMeshFromMemblock(worldlayers[a].id, chunk)
SetObjectPosition(worldlayers[a].id,wx*(blocksizex*blocksize#),0,wz*(blocksizez*blocksize#))
SetObjectImage(worldlayers[a].id,textures[meshid].id,0)
//SetObjectColor(worldlayers[a].id,textures[meshid].r,textures[meshid].g,textures[meshid].b,255)
SetObjectVisible(worldlayers[a].id,1)
inc a
// find the centre chunk of starting position, we then can make chunks invisible say 4 surround chunks and render later
// when move around
next
next
next
// generate a flat chunk
instanceobjectid=a
generatechunk(1,instanceobjectid,50)
// generate the far world
for wx=-totalworldchunks-totalfarworldchunks to totalworldchunks+totalfarworldchunks
for wz=-totalworldchunks-totalfarworldchunks to totalworldchunks+totalfarworldchunks
for meshid=0 to maxtexturemeshes // multiple textures on each chunk - lets do 3 to start with
worldlayers[a].id= InstanceObject(worldlayers[instanceobjectid].id)
// worldlayers[a].id = InstanceObject(genchunk)
// AddObjectMeshFromMemblock(worldlayers[a].id, chunk)
SetObjectPosition(worldlayers[a].id,wx*(blocksizex*blocksize#),0,wz*(blocksizez*blocksize#))
SetObjectImage(worldlayers[a].id,textures[1].id,1)
//SetObjectColor(worldlayers[a].id,textures[meshid].r,textures[meshid].g,textures[meshid].b,255)
SetObjectVisible(worldlayers[a].id,1)
inc totalcubecount, blocksizex * blocksizey
inc a
// find the centre chunk of starting position, we then can make chunks invisible say 4 surround chunks and render later
// when move around
next
next
next
// load the chunk (underground)
for y=1 to blocksizey
a=y
for meshid=0 to maxtexturemeshes
generatechunk(meshid,a,mainchunkair)
// worldlayers[a].id = InstanceObject(genchunk)
// AddObjectMeshFromMemblock(worldlayers[a].id, chunk)
inc totalcubecount
SetObjectPosition(worldlayers[a].id,0,0-y * blocksize#,0)
SetObjectImage(worldlayers[a].id,textures[meshid].id,0)
//SetObjectColor(worldlayers[a].id,textures[meshid].r,textures[meshid].g,textures[meshid].b,255)
SetObjectVisible(worldlayers[a].id,1)
next
next
// SetObjectVisible(worldlayers[centrechunk].id,1)
/*
for a=-5 to 5
SetObjectVisible(worldlayers[centrechunk-totalworldchunks-a].id,1)
// SetObjectVisible(worldlayers[centrechunk+totalworldchunks+a].id,1)
SetObjectVisible(worldlayers[centrechunk-totalworldchunks+a].id,1)
// SetObjectVisible(worldlayers[centrechunk-totalworldchunks+1].id,1)
SetObjectVisible(worldlayers[centrechunk+totalworldchunks-a].id,1)
SetObjectVisible(worldlayers[centrechunk+totalworldchunks+a].id,1)
//SetObjectVisible(worldlayers[centrechunk+totalworldchunks+1].id,1)
SetObjectVisible(worldlayers[centrechunk-a].id,1)
// SetObjectVisible(worldlayers[centrechunk+1].id,1)
next
*/
endfunction
function add_a_tree()
// Add a tree
// inc treecount
createtree(treecount)
// if treecount>tree.length-1 then treecount=tree.length-1
// tree[treecount].stump=InstanceObject(tree[0].stump)
treex#=random(1,(blocksizex * totalworldchunks)*blocksize#)
treez#=random(1,(blocksizez * totalworldchunks)*blocksize#)
SetObjectPosition(tree[treecount].stump,treex#,blocksize#,treez#)
inc treecount
endfunction
function initialisecamera()
setcamerarange(1,0.1,10000.0)
setcameraposition(1,getcamerax(1),600,getcameraz(1))
angx#=0:angy#=0:startx#=1024/2:starty#=768/2
SetRawMousePosition(1024/2,768/2)
camerax#=(blocksizex*blocksize#)/2
cameray#=(blocksizey)
cameraz#=(blocksizez * blocksize#)/2
endfunction
function checkkeypresses()
if GetRawKeyState(37) then dec camerax#,playerspeed#//Left
if GetRawKeyState(39) then inc camerax#,playerspeed#//Right
if GetRawKeyState(38) then inc cameraz#,playerspeed#//Forward
if GetRawKeyState(40) then dec cameraz#,playerspeed#//Backward
if GetRawKeyState(87) then inc cameray#,playerspeed#//87 W
if GetRawKeyState(83) then dec cameray#,playerspeed#//87 S
//if cameray#<(blocksizey*10) then cameray#=(blocksizey*10)
if GetRawKeyPressed(27) then end
endfunction
function setupinventoryitems()
for a=0 to 9
swap()
box=CreateObjectBox(.5,.5,.5)
SetObjectRotation(box,0,45,0)
red=random(0,255):playblocks[a].red=red
green=random(0,255):playblocks[a].green=green
blue=random(0,255):playblocks[a].blue=blue
SetObjectColor(box,red,green,blue,255)
SetObjectTransparency(box,1)
render()
playblocks[a].spr=CreateSprite(getimage(screenwidth/2-50,screenheight/2-50,screenwidth/2+50,screenheight/2+50))
DeleteObject(box)
next
DrawLine(0,0,1,1,MakeColor(255,255,255),MakeColor(255,255,255),1)
mouse=createsprite(GetImage(0,0,1,1))
SetRawMouseVisible(0)
endfunction
function displayinventoryandhealth()
SetSpritePosition(mouse,screenwidth/2,screenheight/2)
for a=0 to 9
SetSpritePosition(playblocks[a].spr,(screenwidth/2-(50*5))+a*50,screenheight-150)
next
endfunction
// this creates the centre pointer
function createblockpointer()
swap()
DrawLine(0,5,10,5,MakeColor(255,255,0),MakeColor(255,255,0))
DrawLine(5,0,5,10,MakeColor(255,255,0),MakeColor(255,255,0))
// DrawEllipse(5,5,5,5,MakeColor(255,255,0),MakeColor(255,255,0),0)
render()
mouse = CreateSprite(GetImage(0,0,50,50))
SetRawMousePosition(screenwidth/2,screenheight/2)
SetRawMouseVisible(0)
endfunction
function displaydebuginfo()
print (GetRawLastKey())
print (GetPolygonsDrawn())
print ("Chunk size : " + str(blocksizex) + " x " + str(blocksizey) + " x " + str(blocksizez) )
print ("World size : " + str(totalworldchunks) + " Chunks by " + str(totalworldchunks)+ " Chunks")
print("Potential cubes within : " + str(totalblockcount))
print("Potential chunks within : " + str(totalcubecount))
print("WASD and cursors to move about")
print ("Press Space to lift random cubes on random chunks")
print (ScreenFPS())
endfunction
function movecamerawithmouse()
fDiffX# = (GetPointerX() - startx#)/6.0
fDiffY# = (GetPointerY() - starty#)/6.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
SetCameraPosition(1,camerax#,cameray#,cameraz#)
endfunction
function initialisememblock(chk,meshid)
createobjectplane(999,7,7)
deletememblock(1)
deletememblock(chk)
CreateMemblockFromObjectMesh(1,999,meshid)
creatememblock(chk,memblocksize#)
endfunction
function makememblock(chk,meshid)
rem here we read the memblock to get it's data
off=0
vertcount=GetMemblockInt(1,off)
SetMemblockInt(chk,off,vertcount)
off=off+4
indicecount=GetMemblockInt(1,off)
SetMemblockInt(chk,off,indicecount)
off=off+4
attspervert=GetMemblockInt(1,off) // 3=position, normals & UV data
SetMemblockInt(chk,off,3)
off=off+4
vertsize=GetMemblockInt(1,off)
SetMemblockInt(chk,off,vertsize)
off=off+4
vertoff=GetMemblockInt(1,off)
SetMemblockInt(chk,off,vertoff)
off=off+4
indexoff=GetMemblockInt(1,off)
SetMemblockInt(chk,off,indexoff)
off=off+4
vertdat1=GetMemblockbyte(1,off)
SetMemblockbyte(chk,off,vertdat1)
vertdat2=GetMemblockbyte(1,off+1)
SetMemblockbyte(chk,off+1,vertdat2)
vertdat3=GetMemblockbyte(1,off+2)
SetMemblockbyte(chk,off+2,vertdat3)
vertdat4=GetMemblockbyte(1,off+3)
SetMemblockbyte(chk,off+3,vertdat4)
vertstring$=GetMemblockstring(1,off+4,vertdat4)
SetMemblockstring(chk,off+4,vertstring$)
off=off+4+vertdat4
vertdat5=GetMemblockbyte(1,off)
SetMemblockbyte(chk,off,vertdat5)
vertdat6=GetMemblockbyte(1,off+1)
SetMemblockbyte(chk,off+1,vertdat6)
vertdat7=GetMemblockbyte(1,off+2)
SetMemblockbyte(chk,off+2,vertdat7)
vertdat8=GetMemblockbyte(1,off+3)
SetMemblockbyte(chk,off+3,vertdat8)
vertstring2$=GetMemblockstring(1,off+4,vertdat8)
SetMemblockstring(chk,off+4,vertstring2$)
off=off+4+vertdat8
vertdat9=GetMemblockbyte(1,off)
SetMemblockbyte(chk,off,vertdat9)
vertdat10=GetMemblockbyte(1,off+1)
SetMemblockbyte(chk,off+1,vertdat10)
vertdat11=GetMemblockbyte(1,off+2)
SetMemblockbyte(chk,off+2,vertdat11)
vertdat12=GetMemblockbyte(1,off+3)
SetMemblockbyte(chk,off+3,vertdat12)
vertstring3$=GetMemblockstring(1,off+4,vertdat12)
SetMemblockstring(chk,off+4,vertstring3$)
off=off+4+vertdat12
vertnumchoice=0
off=vertoff+(vertnumchoice*vertsize)
xpos#=GetMemblockFloat(1,off)
ypos#=GetMemblockFloat(1,off+4)
zpos#=GetMemblockFloat(1,off+8)
xnorm#=GetMemblockFloat(1,off+12)
ynorm#=GetMemblockFloat(1,off+16)
znorm#=GetMemblockFloat(1,off+20)
u#=GetMemblockFloat(1,off+24)
v#=GetMemblockFloat(1,off+28)
deleteobject(999)
endfunction chk
function addplane(chk,xoff#,yoff#,zoff#,blockcount,x,z,y)
off=0
SetMemblockInt(chk,off,vertcount*(blockcount*6))
newfacecount=0
rem the front face
//if z>1
//if world[x,z-1,y]=0
for a=0 to 5
off=vertoff+((a+((blockcount-1)*36))*vertsize)
select a
case 0
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 1
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 2
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 3
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 4
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 5
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
endselect
off2=vertoff+(a*vertsize)
setmemblockfloat(chk,off+12,0.0)
setmemblockfloat(chk,off+16,0.0)
setmemblockfloat(chk,off+20,-1.0)
setmemblockfloat(chk,off+24,GetMemblockFloat(1,off2+24))
setmemblockfloat(chk,off+28,GetMemblockFloat(1,off2+28))
facesdrawn=facesdrawn+1
newfacecount=newfacecount+1
next a
//endif
//endif
rem the right face
//if x<chunksize
//if world[x+1,z,y]=0
for a=6 to 11
off=vertoff+((a+((blockcount-1)*36))*vertsize)
select a
case 6
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 7
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 8
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 9
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 10
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 11
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
endselect
off2=vertoff+((a-6)*vertsize)
setmemblockfloat(chk,off+12,1.0)
setmemblockfloat(chk,off+16,0.0)
setmemblockfloat(chk,off+20,0.0)
setmemblockfloat(chk,off+24,GetMemblockFloat(1,off2+24))
setmemblockfloat(chk,off+28,GetMemblockFloat(1,off2+28))
facesdrawn=facesdrawn+1
newfacecount=newfacecount+1
next a
//endif
//endif
rem the rear face
//if z<chunksize
//if world[x,z+1,y]=0
for a=12 to 17
off=vertoff+((a+((blockcount-1)*36))*vertsize)
select a
case 12
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 13
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 14
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 15
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 16
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 17
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
endselect
off2=vertoff+((a-12)*vertsize)
setmemblockfloat(chk,off+12,0.0)
setmemblockfloat(chk,off+16,0.0)
setmemblockfloat(chk,off+20,1.0)
setmemblockfloat(chk,off+24,GetMemblockFloat(1,off2+24))
setmemblockfloat(chk,off+28,GetMemblockFloat(1,off2+28))
facesdrawn=facesdrawn+1
newfacecount=newfacecount+1
next a
//endif
//endif
rem the left face
//if x>1
//if world[x-1,z,y]=0
for a=18 to 23
off=vertoff+((a+((blockcount-1)*36))*vertsize)
select a
case 18
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 19
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 20
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 21
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 22
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 23
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
endselect
off2=vertoff+((a-18)*vertsize)
setmemblockfloat(chk,off+12,-1.0)
setmemblockfloat(chk,off+16,0.0)
setmemblockfloat(chk,off+20,0.0)
setmemblockfloat(chk,off+24,GetMemblockFloat(1,off2+24))
setmemblockfloat(chk,off+28,GetMemblockFloat(1,off2+28))
facesdrawn=facesdrawn+1
newfacecount=newfacecount+1
next a
//endif
//endif
rem the top face
//if y<chunksize
//if world[x,z,y+1]=0
for a=24 to 29
off=vertoff+((a+((blockcount-1)*36))*vertsize)
select a
case 24
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 25
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 26
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 27
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 28
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 29
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#+(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
endselect
off2=vertoff+((a-24)*vertsize)
setmemblockfloat(chk,off+12,0.0)
setmemblockfloat(chk,off+16,1.0)
setmemblockfloat(chk,off+20,0.0)
setmemblockfloat(chk,off+24,GetMemblockFloat(1,off2+24))
setmemblockfloat(chk,off+28,GetMemblockFloat(1,off2+28))
facesdrawn=facesdrawn+1
newfacecount=newfacecount+1
next a
//endif
//endif
rem the bottom face
//if y>2
//if world[x,z,y-1]=0
for a=30 to 35
off=vertoff+((a+((blockcount-1)*36))*vertsize)
select a
case 30
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 31
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 32
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 33
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#)
endcase
case 34
setmemblockfloat(chk,off,xoff#-(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
case 35
setmemblockfloat(chk,off,xoff#+(blocksize#/2))
setmemblockfloat(chk,off+4,yoff#-(blocksize#/2))
setmemblockfloat(chk,off+8,zoff#+blocksize#)
endcase
endselect
off2=vertoff+((a-30)*vertsize)
setmemblockfloat(chk,off+12,0.0)
setmemblockfloat(chk,off+16,-1.0)
setmemblockfloat(chk,off+20,0.0)
setmemblockfloat(chk,off+24,GetMemblockFloat(1,off2+24))
setmemblockfloat(chk,off+28,GetMemblockFloat(1,off2+28))
facesdrawn=facesdrawn+1
newfacecount=newfacecount+1
next a
//endif
//endif
endfunction
function generateperlinimage()
// Generate image from memblock
size = perlinw * perlinh * 4 + 12
mem = CreateMemblock(size)
SetMemblockInt(mem,0,perlinw)
SetMemblockInt(mem,4,perlinh)
SetMemblockInt(mem,8,32)
offset as integer = 12
a as float, b as float
a = 5.0
b = 2.0
for y = 0 to perlinh - 1
for x = 0 to perlinw - 1
a = a + 0.0001
b = b + 0.002
// Try out these two noise methods
//noise = 255.0*Noise2D(x/10.0,y/10.0)
noise = 255.0* Noise2D(x/255.0,y/255.0)
noise = abs(noise)
//if x<chunksize# and y<chunksize# then
perlinmap[x,y,0].noise=noise/13
//clouds
if noise>255
perlinmap[x,y,0].r = noise
perlinmap[x,y,0].g = noise
perlinmap[x,y,0].b = noise
perlinmap[x,y,0].texture = 6 // snow
SetMemblockByte(mem, offset, noise)
SetMemblockByte(mem, offset+1, noise)
SetMemblockByte(mem, offset+2, noise)
SetMemblockByte(mem, offset+3, 255)
endif
//greenary
if noise>100 and noise<=255
perlinmap[x,y,0].r = 0
perlinmap[x,y,0].g = noise
perlinmap[x,y,0].b = 0
perlinmap[x,y,0].texture = 0 // grass
SetMemblockByte(mem, offset, 0)
SetMemblockByte(mem, offset+1, noise)
SetMemblockByte(mem, offset+2, 0)
SetMemblockByte(mem, offset+3, 255)
endif
//sand
if noise>50 and noise<=100
perlinmap[x,y,0].r = noise
perlinmap[x,y,0].g = noise
perlinmap[x,y,0].b = 0
perlinmap[x,y,0].texture = 5 // sand
SetMemblockByte(mem, offset, noise)
SetMemblockByte(mem, offset+1, noise)
SetMemblockByte(mem, offset+2, 0)
SetMemblockByte(mem, offset+3, 255)
endif
// water
if noise>25 and noise<=50
perlinmap[x,y,0].r = 0
perlinmap[x,y,0].g = noise
perlinmap[x,y,0].b = noise
perlinmap[x,y,0].texture = 4 // lightwater
SetMemblockByte(mem, offset, 0)
SetMemblockByte(mem, offset+1, 0)
SetMemblockByte(mem, offset+2, noise)
SetMemblockByte(mem, offset+3, 255)
endif
if noise<=25
perlinmap[x,y,0].r = 0
perlinmap[x,y,0].g = 0
perlinmap[x,y,0].b = noise
perlinmap[x,y,0].texture = 3 // deep water
SetMemblockByte(mem, offset, 0)
SetMemblockByte(mem, offset+1, noise)
SetMemblockByte(mem, offset+2, noise)
SetMemblockByte(mem, offset+3, 255)
endif
offset = offset + 4
next
next
map=CreateImageFromMemblock(mem)
DeleteObject(mem)
endfunction map
// ***************************************************************************************************
// Ken Perlin's Simplex Noise 2D. AGK Version.
// Ported from Stefan Gustavson's Java implementation
// (http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf)
// 2015.02.03
// AGK reference https://forum.thegamecreators.com/thread/213532
// Thank you Thank you
#constant PN3DF2 = 0.5*(sqrt(3.0)-1.0)
#constant PN3DG2 = (3.0-sqrt(3.0))/6.0
Type sPNVECTOR
x as float
y as float
z as float
endtype
Global PNHash as integer[512]
Global PNGrad3 as sPNVECTOR[256]
Function NoiseInit()
Local n as integer, rn as integer
For n=0 To 255
PNHash[n] = n
Next n
For n=0 To 255
rn=Random(0, 255)
PNHash.swap(n,rn)
Next n
For n=0 To 255
PNHash[256 + n] = PNHash[n]
Next n
PNHash[511] = PNHash[0]
For n=0 To 15
PNGrad3[n * 16 + 0].x = 1 : PNGrad3[n * 16 + 0].y = 1 : PNGrad3[n * 16 + 0].z = 0
PNGrad3[n * 16 + 1].x = -1 : PNGrad3[n * 16 + 1].y = 1 : PNGrad3[n * 16 + 1].z = 0
PNGrad3[n * 16 + 2].x = 1 : PNGrad3[n * 16 + 2].y = -1 : PNGrad3[n * 16 + 2].z = 0
PNGrad3[n * 16 + 3].x = -1 : PNGrad3[n * 16 + 3].y = -1 : PNGrad3[n * 16 + 3].z = 0
PNGrad3[n * 16 + 4].x = 1 : PNGrad3[n * 16 + 4].y = 0 : PNGrad3[n * 16 + 4].z = 1
PNGrad3[n * 16 + 5].x = -1 : PNGrad3[n * 16 + 5].y = 0 : PNGrad3[n * 16 + 5].z = 1
PNGrad3[n * 16 + 6].x = 1 : PNGrad3[n * 16 + 6].y = 0 : PNGrad3[n * 16 + 6].z = -1
PNGrad3[n * 16 + 7].x = -1 : PNGrad3[n * 16 + 7].y = 0 : PNGrad3[n * 16 + 7].z = -1
PNGrad3[n * 16 + 8].x = 0 : PNGrad3[n * 16 + 8].y = 1 : PNGrad3[n * 16 + 8].z = 1
PNGrad3[n * 16 + 9].x = 0 : PNGrad3[n * 16 + 9].y = -1 : PNGrad3[n * 16 + 9].z = 1
PNGrad3[n * 16 + 10].x = 0 : PNGrad3[n * 16 + 10].y = 1 : PNGrad3[n * 16 + 10].z = -1
PNGrad3[n * 16 + 11].x = 0 : PNGrad3[n * 16 + 11].y = -1 : PNGrad3[n * 16 + 11].z = -1
PNGrad3[n * 16 + 12].x = 1 : PNGrad3[n * 16 + 12].y = 1 : PNGrad3[n * 16 + 12].z = 0
PNGrad3[n * 16 + 13].x = -1 : PNGrad3[n * 16 + 13].y = 1 : PNGrad3[n * 16 + 13].z = 0
PNGrad3[n * 16 + 14].x = 0 : PNGrad3[n * 16 + 14].y = -1 : PNGrad3[n * 16 + 14].z = 1
PNGrad3[n * 16 + 15].x = 0 : PNGrad3[n * 16 + 15].y = -1 : PNGrad3[n * 16 + 15].z = -1
Next n
endfunction
function Noise2D(xin as float, yin as float)
local n0 as float, n1 as float, n2 as float, s as float, t as float, x0 as float, y0 as float, xs as float, ys as float
local i as integer, j as integer, i1 as integer, j1 as integer, i2 as integer, j2 as integer, gi0 as integer, gi1 as integer, gi2 as integer
local x1 as float, y1 as float, x2 as float, y2 as float, x3 as float, y3 as float, t0 as float, t1 as float, t2 as float
s = (xin + yin) * PN3DF2
xs = xin + s
i = floor(xs)
ys = yin + s
j = floor(ys)
t = (i + j) * PN3DG2
x0 = xin - (i - t)
y0 = yin - (j - t)
if x0>y0
i1=1
j1=0
else
i1=0
j1=1
endif
x1 = x0 - i1 + PN3DG2
y1 = y0 - j1 + PN3DG2
x2 = x0 - 1.0 + 2.0 * PN3DG2
y2 = y0 - 1.0 + 2.0 * PN3DG2
i = i && 255
j = j && 255
gi0 = PNHash[i + PNHash[j]] && 15
gi1 = PNHash[i + i1 + PNHash[j + j1]] && 15
gi2 = PNHash[i + 1 + PNHash[j+ 1]] && 15
t0 = 0.5 - x0*x0-y0*y0
if t0<0
n0 = 0.0
else
t0 = t0 * t0
n0 = t0 * t0 * (PNGrad3[gi0].x * x0 + PNGrad3[gi0].y * y0)
endif
t1 = 0.5 - x1*x1-y1*y1
if t1<0
n1 = 0.0
else
t1 = t1 * t1
n1 = t1 * t1 * (PNGrad3[gi1].x * x1 + PNGrad3[gi1].y * y1)
endif
t2 = 0.5 - x2*x2-y2*y2
if t2<0
n2 = 0.0
else
t2 = t2 * t2
n2 = t2 * t2 * (PNGrad3[gi2].x * x2 + PNGrad3[gi2].y * y2)
endif
endfunction 70.0 * (n0 + n1 + n2)
function createtextures(sizex# as float, sizey# as float,density, toptexture, bottomtexture)
if bottomtexture=0
//do a full block of the same texture
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey# step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
else
// do an half block with a different texture at bottom
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey#/3 step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
texturecount=bottomtexture
for a=0 to sizex# step density
for b=sizey#/3 to sizey# step density
rr=random(0,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
endif
endfunction textures[texturecount].id
function definetextures()
// grass=createtexture(64,64,0,200,0,8)
//grass
textures[0].r=0 :textures[0].dr=50
textures[0].g=255 :textures[0].dg=50
textures[0].b=0 :textures[0].db=50
//dirt
textures[1].r=139 :textures[1].dr=50
textures[1].g=69:textures[1].dg=50
textures[1].b=19:textures[1].db=50
//goldgold=createtexture(8,8,255,215,100,1)
textures[2].r=255 :textures[2].dr=50
textures[2].g=215 :textures[2].dg=50
textures[2].b=100 :textures[2].db=50
// water=createtexture(8,8,0,0,100,1)
textures[3].r=0 :textures[3].dr=50
textures[3].g=0 :textures[3].dg=50
textures[3].b=100 :textures[3].db=50
// lightwater=createtexture(8,8,39,89,45,1)
textures[4].r=0 :textures[4].dr=50
textures[4].g=100 :textures[4].dg=50
textures[4].b=100 :textures[4].db=50
// =createtexture(8,8,39,89,45,1)
textures[5].r=194 :textures[5].dr=50
textures[5].g=178 :textures[5].dg=50
textures[5].b=128 :textures[5].db=50
// create the texturs
textures[0].id = CreateTextures(8,8,1,0,0) // top
textures[1].id = CreateTextures(8,8,1,1,1) // bottom
textures[2].id = CreateTextures(8,8,1,2,2) // sides
textures[3].id = CreateTextures(8,8,1,3,3) // sides
textures[4].id = CreateTextures(8,8,1,4,4) // sides
textures[5].id = CreateTextures(8,8,1,5,5) // sides
endfunction
function setupskyandfog()
// setup a skybox
SetSunActive(1)
SetSkyBoxHorizonSize(4.1,4)
SetSkyBoxHorizonColor(90,132,150)//nice horizon blue
SetSkyBoxSkyColor(0,122,193)//thunder horison thunderblue
SetSkyBoxVisible(1)
SetFogColor(155,155,155)
SetFogRange(1000,20000)
SetFogMode(1)
endfunction
The green is the chunk layers that are verticable changes
The red / brown in the distance are the instanced objects
Enjoy
Catch up tomorrow
EDIT - not the FPS is rubbish now, cause there is no culling in place to look after it - that can be remedied later