// Project: meshidtest
// Created: 2018-05-07
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
// set window properties
SetWindowTitle( "meshidtest" )
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
#constant chunksize#=50
// this needs to be square root of a map size
// so, eg = 4 = 2x2 world, 9 = 3x3 world, 16=4x4 world, 25=5x5 world, 36 = 6x6 world, 49 = 7x7 world, 64 = 8x8 world 81 = 9x9 world
#constant numofchunks=49
global blocksize#=2
global vertcount, vertoff, vertsize
global memblocksize#=10000000
type _map
id
chunkobjectid
textureid
height
blockcount
layers as integer[5] // how many textured layers are on this chunk
endtype
global map as _map[1000]
global cubex, cubey
// setup perlinmapdata
type _perlinmap
r
g
b
a
x
y
level
texture
noise
endtype
#constant perlinw = 256 //power of 2
#constant perlinh = 256 //power of 2
#constant perlinz = 0
global perlinmap as _perlinmap[perlinw,perlinh,perlinz]
global perlinx, perlinzz
// end of perlin variables
global camerax#, cameray#,cameraz#,chunks
global playerspeed#=.1
global ang#, angx#, angy#, fDiffY#
global startx#
startx#=screenwidth/2
global starty#
starty#=screenheight/2
// 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
// textures
global textures as _textures[50] // used to store textures (not ready yet)
definetextures()
global loading, loadingstage,mouse, mapspriteimage
Loading = CreateText("Loading")
LoadingStage = CreateText("")
type _playblocks
spr
red
green
blue
endtype
global playblocks as _playblocks[10]
// variables to identify the cube number position on the chunk
global iX# as float
global iY# as float
global iZ# as float
global selectedcube
global totalmeshes=5
// ****************************************
// END OF INITIALISAING VARIABLES
// ****************************************
NoiseInit()
generateperlinimage()
mapimage=CreateSprite(generateperlinimage())
SetSpriteScale(mapimage,.5,.5)
setupinventoryitems()
setupskyandfog()
buildmap()
generatechunk()
positionworld()
DeleteText(loading)
DeleteText(loadingstage)
global obj
obj = CreateObjectbox(blocksize#/2,blocksize#/2,blocksize#/2)
SetObjectVisible(obj,1)
//camerax#=((sqrt(numofchunks)-1)) * chunksize#* numofchunks
//cameraz#=((sqrt(numofchunks)-1)) * chunksize#*numofchunks
cameray#=blocksize#*2
camerax# = (sqrt (numofchunks) -1) * chunksize# + (chunksize#/2)*blocksize#
cameraz# = (sqrt (numofchunks) -1) * chunksize# + (chunksize#/2)*blocksize#
SetCameraRange(1,0.1, 10000)
chunkcount=0:zz=0:xx=0
do
getplayerworldcoordinates()
displayinventoryandhealth()
checkkeypresses()
movecamerawithmouse()
if GetRawKeyState(32)
endif
if GetRawMouseLeftPressed()
for a=0 to chunks
if (map[a].chunkobjectid = mouseOver())
randomchunk=a
endif
next
chunkobjectmem = CreateMemblockFromObjectMesh(map[randomchunk].id,1)
for a=(selectedcube * 36) to (selectedcube*36) + 35
SetMeshMemblockVertexPosition(chunkobjectmem,a,GetMeshMemblockVertexX(chunkobjectmem,a),GetMeshMemblockVertexY(chunkobjectmem,a)+blocksize#,GetMeshMemblockVertexZ(chunkobjectmem,a))
next
SetObjectMeshFromMemblock(map[randomchunk].id,1,chunkobjectmem)
DeleteMemblock(chunkobjectmem)
endif
if GetRawMouseRightState()
deletecube(selectedcube)
// DeleteObject(mouseOver())
endif
Print( ScreenFPS() )
Sync()
loop
function deletecube(cubetodestroy)
randomchunk=(mouseOver()-100000-12)*2
print(randomchunk)
chunkobjectmem = CreateMemblockFromObjectMesh(map[randomchunk].id, 1)
for a=(cubetodestroy*36) to (cubetodestroy*36)+35
SetMeshMemblockVertexPosition(chunkobjectmem, a, 0,0,0)
next
SetObjectMeshFromMemblock(map[randomchunk].id, 1, chunkobjectmem)
DeleteMemblock(chunkobjectmem)
endfunction
// gets the players coordinates, so that we know the height of the block that the player is sitting on
// so can move the camera (on top of the cube)
function getplayerworldcoordinates()
chunkhit=(mouseover()-100000-12) * 2
print ("Chunk hit : - " + str(chunkhit))
print (str(sqrt(numofchunks)-1) + " - " + str(numofchunks))
print ("Cube position - " + str((ix#-1 / blocksize#),0) + " - " + str((iz#-1 / blocksize#),0))
cubex=mod(((ix# / blocksize#)-1),chunksize#)
cubey=mod(((iz# / blocksize#)-1),chunksize#)
print (str(cubex) + " - " + str(cubey))
selectedcube = (cubex * chunksize#) + cubey
print ("Selected Cube - " + str(selectedcube))
endfunction
function mouseOver()
myX as float
myY as float
my3dX as float
my3dY as float
my3dZ as float
rayStartX as float
rayStartY as float
rayStartZ as float
rayEndX as float
rayEndY as float
rayEndZ as float
myX=screenwidth/2
myY=screenheight/2
my3dX=Get3DVectorXFromScreen(myX,myY)
my3dY=Get3DVectorYFromScreen(myX,myY)
my3dZ=Get3DVectorZFromScreen(myX,myY)
rayStartX=my3dX+GetCameraX(1)
rayStartY=my3dY+GetCameraY(1)
rayStartZ=my3dZ+GetCameraZ(1)
rayEndX=800*my3dX+GetCameraX(1)
rayEndY=800*my3dY+GetCameraY(1)
rayEndZ=800*my3dZ+GetCameraZ(1)
theObjectHit= ObjectRayCast(0,rayStartX,rayStartY,rayStartZ,rayEndX,rayEndY,rayEndZ)
// Move camera with WASD
// Rotate Camera with Arrow Keys
MoveCameraLocalX(1,GetRawKeyState(68) - GetRawKeyState(65))
MoveCameraLocalZ(1,GetRawKeyState(87) - GetRawKeyState(83))
RotateCameraGlobalY(1,GetRawKeyState(39) - GetRawKeyState(37))
RotateCameraLocalX(1,GetRawKeyState(40) - GetRawKeyState(38))
cx# = GetCameraX(1)
cy# = GetCameraY(1)
cz# = GetCameraZ(1)
pxRaw# = Get3DVectorXFromScreen(myX,myY)
pyRaw# = Get3DVectorYFromScreen(myX,myY)
pzRaw# = Get3DVectorZFromScreen(myX,myY)
// get pointer vector y added to camera's y position
pyRelative# = pyRaw# + cy#
// find distance between camera's y position and pyRelative#
dist# = cy# - pyRelative#
// calculate scaling value by finding how many times dist# goes into the camera's y position
s# = cy# / dist#
// find the intersection point by multiplying pointer vector by s# and adding it to the camera position.
// ignoring Y in this case since it should be 0 anyway.
ix# = cx# + pxRaw# * s#
iz# = cz# + pzRaw# * s#
// position the object at the intersection point
SetObjectPosition(obj,ix#, blocksize#/2, iz#)
endfunction theObjectHit
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
DrawEllipse(8,8,8,8,MakeColor(255,0,0),MakeColor(255,0,0),1)
for a=1 to 360 step 360/10
xx=8* sin(a)
yy=8* cos(a)
DrawLine(8,8,8+xx,8+yy,MakeColor(255,0,0),MakeColor(255,0,0),1)
next
mouse=createsprite(GetImage(0,0,18,18))
SetRawMouseVisible(0)
endfunction
function displayinventoryandhealth()
// SetSpritePosition(mouse,screenwidth/2-36,screenheight/2-36)
for a=0 to 9
SetSpritePosition(playblocks[a].spr,(screenwidth/2-(50*5))+a*50,screenheight-150)
next
endfunction
function displayloading(stage$)
SetTextSize(loading, 70)
SetTextPosition(loading, screenwidth/2 - GetTextTotalWidth(loading)/2, 100)
SetTextSize(loadingstage,40)
SetTextPosition(loadingstage, screenwidth/2 - GetTextTotalWidth(loadingstage)/2, 500)
SetTextString(loadingstage, stage$)
sync()
endfunction
function positionworld()
chunks=0
for texturesid = 0 to 5
for x=0 to sqrt ( numofchunks ) -1
for z=0 to sqrt (numofchunks)-1
// SetObjectPosition(map[chunks].id, (x * chunksize#) * blocksize#, 0, (z * chunksize#) * blocksize#)
inc chunks
next
next
next
endfunction
// This builds the chunks textures
function buildmap()
for x= 1 to chunksize#
for y=1 to chunksize#
// if perlinmap[x,y,0].noise=1
// perlinmap[x,y,0].texture = 1
// endif
next
next
endfunction
function generatechunk()
chunks=0
px=0
py=0
wrap#=1
chunkid=0
for xx=0 to sqrt ( numofchunks ) -1
for zz=0 to sqrt (numofchunks)-1
map[chunks].id = CreateObjectPlane(001,.001)
initialisememblock(100,1)
for texturesid = 0 to 0
makememblock(100,1)
blockcount=1
for x=1 to chunksize#
for y=1 to chunksize#
// identify which texture / or mesh
// if perlinmap[x,y,0].level=texturesid
//addplane(100,x*blocksize#,(perlinmap[px,py ,0].level+1)*blocksize#,y*blocksize#,blockcount,x,1,y)
// noise = 255.0*Noise2D(px/120.0,py/120.0)
// noise = abs(noise)/50
// addplane(100,x*blocksize#,perlinmap[px,py,0].texture*blocksize#,y*blocksize#,blockcount,x,1,y)
addplane(100,x*blocksize#,0,y*blocksize#,blockcount,x,1,y)
inc blockcount
// endif
inc py
if py>perlinh
py=0
inc px
endif
if px>perlinx
px=0
py=0
endif
next
next
map[chunks].id = CreateObjectFromMeshMemblock(100)
SetObjectImage(map[chunks].id,textures[texturesid].id,1)
// FixObjectToObject( map[chunks].id, obj)
map[chunks].blockcount = blockcount
map[chunks].chunkobjectid = map[chunks].id
map[chunks].textureid = texturesid
displayloading("Generating Chunk " + str(chunks) + "-"+str(map[chunks].id) + " of " + str(numofchunks))
sync()
sleep(10)
SetObjectPosition(map[chunks].id, (xx * chunksize#) * blocksize#, 0, (zz * chunksize#) * blocksize#)
next
inc chunks
DeleteMemblock(100)
next
next
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#//83 S
if cameray#<(blocksize#) then cameray#=(blocksize#)
SetCameraPosition(1,camerax#,cameray#,cameraz#)
if GetRawKeyPressed(27) then end
endfunction
function movecamerawithmouse()
fDiffX# = (GetPointerX() - startx#)/4.0
fDiffY# = (GetPointerY() - starty#)/4.0
newX# = angx# + fDiffY#
if ( newX# > 360 ) then newX# = 360
if ( newX# < -360 ) then newX# = -360
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
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 definetextures()
// grass=createtexture(64,64,0,200,0,8)
//grass
textures[0].r=0 :textures[0].dr=50
textures[0].g=100 :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 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
// ***************************************************************************************************
// 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 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(50,200)
SetFogMode(1)
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/120.0,y/120.0)
noise = abs(noise)
perlinmap[x,y,0].noise=noise
perlinmap[x,y,0].x = x
perlinmap[x,y,0].y = y
//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
perlinmap[x,y,0].level = 1 // deep water
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
perlinmap[x,y,0].level = 2
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
perlinmap[x,y,0].level = 1
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
perlinmap[x,y,0].level = 0 // 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
mapspriteimage = CreateImageFromMemblock(mem)
mapsprite=CreateImageFromMemblock(mem)
endfunction mapsprite
Night folks - catch up in a day or three with more on this