Hi everyone, Demolicious here... I am just messing around with a 2d tile based multiplayer system.
Im using multisync(you can download this from 'program announcements') and using some code from another person in a forum(i cant find out who they were

)
All that i have done is combined these two techniques to try and make the host object to display in the client world.
but the client object doesnt display in the host world(this is just for starters)
im nearly there, please have a look at this code and let me know what i have one wron, or how i could do it better.
HOST CODE--------------
`****************************************************************************************
`*********************************** Start of program ***********************************
`****************************************************************************************
global PACKETS_SENT as integer
global connected as boolean
global newPlayer as integer
global stateP1 as boolean
global playerMade as boolean
`gosubs are here instead of actual code, just to make things look neat
IP$ = "192.168.0.3"
`Act as server
connected = net host(20,IP$)
net stack on
if connected = 0
text 0,100,"Failed to connect"
wait 2000
else
text 0,100,"Connected as host"
wait 2000
endif
gosub screensetup
gosub DeclareVariables
gosub createimages
gosub loaddata
gosub makeplayer
`start the main loop
do
gosub controls
gosub collision
gosub ScrollScreen
gosub displayplayer
gosub player
gosub lan
gosub DisplayDebugInfo
`update screen
sync
loop
gosub cleanup
`end the program
end
`****************************************************************************************
`************************************* End of program ***********************************
`****************************************************************************************
screensetup:
`delete anything stored in video memory
flush video memory
`set screen resolution
set display mode 800,600,32
`setup syncing
sync on
sync rate 150
return
DeclareVariables:
`store how many horizontal/verticle tiles there are
NumOfTilesX = 40
NumOfTilesY = 40
`store the size (in pixels) of a tile
TileSizeX = 40
TileSizeY = 40
`store the speed of the tile
SpeedX = 2
SpeedY = 2
`store the coordinates that will make the player be at the centre of the screen
ScreenCentreX = 380
ScreenCentreY = 280
`store predefined coordinates of player
X1 = ScreenCentreX
Y1 = ScreenCentreY
`store predefined coordinates of map
MapX = 0
MapY = 0
return
createimages:
`create a green box
box 0,0,TileSizeX,TileSizeY,rgb(0,255,0),rgb(0,225,0),rgb(0,225,0),rgb(0,200,0)
get image 1,0,0,TileSizeX,TileSizeY,1
`create a yellow/orange box as the player
box 0,0,TileSizeX,TileSizeY,rgb(255,255,0),rgb(255,128,0),rgb(255,128,0),rgb(255,255,0)
get image 2,0,0,TileSizeX,TileSizeY,1
`create a red box
cls rgb(255,0,0)
get image 10,0,0,TileSizeX,TileSizeY,1
`create a blue box
box 0,0,TileSizeX,TileSizeY,rgb(0,0,255),rgb(0,0,225),rgb(0,0,225),rgb(0,0,200)
get image 11,0,0,TileSizeX,TileSizeY,1
return
loaddata:
`create an array called TileMap - we will use this to store the tile data.
`an array is like a file cabinet - in each drawer of the file cabinet there are
`folders that hold our data. Therefore in our "file cabinet" we have 20 drawers; and
`inside of each drawer we 15 folders; which hold data we want to access.
`Also arrays can have up to 5 dimensions (ours has 2 dimensions) - you can visualise
`this as being more folders inside of the other folders and so on; and of course you
`can just have 1 dimension array as well (in other words just the drawers)
dim TileMap(NumOfTilesX - 1,NumOfTilesY - 1)
`load data into the array - basically the computer will read 20 bits of data from one
`data statment (at the end of the program), then move onto the next data statment
`until all 15 data statments are read. Note: all 15 data statments could have been
`combined into one large data statment of 300 items; the reason why its not currently
`like that is its hard to read also you can kind of see the map the way its currently
`written.
for y = 0 to NumOfTilesY - 1
for x = 0 to NumOfTilesX - 1
read TileMap(x,y)
next x
next y
return
Player:
newPlayer = net new player()
`If there is a new player, make its object
if newPlayer = 1
playerMade = 1
endif
stateP1 = net get player state(1)
return
makeplayer:
`make the player
sprite 1,X1,Y1,2
`this sets the sprites properties; by placing a 0 in the backsave flag the background
`behind sprite will not be saved by the computer, hence will speed up our program a
`little. We are not using the backsave feature because we will be replacing the
`background ourselves; this is due to when a sprite is created the 3d backdrop is
`automatically created (a blue screen wiping any 2d images previously draw to the
`screen) and cannot be turned off as long as you have a sprite on screen.
set sprite 1,0,1
return
`MULTIPLAYER CODE-----------------------------------------------------------------------
lan:
state = net get player state(1)
X1# = X1
Y1# = Y1
`Send the host coordinates to all players
if state
net push float X1#
net push float Y1#
net send 1
inc PACKETS_SENT
endif
return
`END OF MULTIPLAYER CODE--------------------------------------------------------------------
controls:
`store the map's x and y coordinates into the variables OldMapX and OldMapY
OldMapX = MapX
OldMapY = MapY
`store the x and y coordinates of the player into the variables oldX1 and oldY1
OldX1 = X1
OldY1 = Y1
`makes the player move by altering the sprites coordinates
`makes the map move by altering the maps coordinates, if the player is roughly at the
`centre of the screen
if leftkey() = 1
if MapX < X1 - (ScreenCentreX - 5) and MapX > X1 - (ScreenCentreX + 5) then MapX = MapX - SpeedX
X1 = X1 - SpeedX
endif
if rightkey() = 1
if MapX < X1 - (ScreenCentreX - 5) and MapX > X1 - (ScreenCentreX + 5) then MapX = MapX + SpeedX
X1 = X1 + SpeedX
endif
if upkey() = 1
if MapY < Y1 - (ScreenCentreY - 5) and MapY > Y1 - (ScreenCentreY + 5) then MapY = MapY - SpeedY
Y1 = Y1 - SpeedY
endif
if downkey() = 1
if MapY < Y1 - (ScreenCentreY - 5) and MapY > Y1 - (ScreenCentreY + 5) then MapY = MapY + SpeedY
Y1 = Y1 + SpeedY
endif
return
collision:
`NOTE : you may have noticed the player sticking to the blocks, to stop this make the
` collision area of the player smaller and edit the players image to the same
` same size (so it doesn't look weird). You have to make the change in the
` players x and y sizes greater or equal to their speed or you wont notice a
` difference.
`the following 8 lines of code stores the old and new left,right,top,bottom edges of
`the sprite in the respective variables
`gets which tiles the current left and right co-ordinates of the player are on
LeftTileNum = int(X1 / TileSizeX)
RightTileNum = int((X1 + (TileSizeX - 1)) / TileSizeX)
`gets which tiles the current top and bottom co-ordinates of the player are on
TopTileNum = int(Y1 / TileSizeY)
BottomTileNum = int((Y1 + (TileSizeY - 1)) / TileSizeY)
`gets which tiles the old left and right co-ordinates of the player are on
OldLeftTileNum = int(oldX1 / TileSizeX)
OldRightTileNum = int((oldX1 + (TileSizeX - 1)) / TileSizeX)
`gets which tiles the old top and bottom co-ordinates of the player are on
OldTopTileNum = int(oldY1 / TileSizeY)
OldBottomTileNum = int((oldY1 + (TileSizeY - 1)) / TileSizeY)
`this is where we check for those 4 collision points; if the collision point is on a
`tile you are not able to pass through (in our case a tile that has an image number
`of 10 or greater), then the old coordinates of the sprite are restored.
`Note: to have sliding collision we must check the the current x tile against the
`old y tile and vice versa.
`checks for top-left collision point
if TileMap(LeftTileNum, OldTopTileNum) >= 10 then X1 = oldX1 : MapX = OldMapX
if TileMap(OldLeftTileNum, TopTileNum) >= 10 then Y1 = oldY1 : MapY = OldMapY
`checks for top-right collision point
if TileMap(RightTileNum, OldTopTileNum) >= 10 then X1 = oldX1 : MapX = OldMapX
if TileMap(OldRightTileNum, TopTileNum) >= 10 then Y1 = oldY1 : MapY = OldMapY
`checks for bottom-left collision point
if TileMap(LeftTileNum, OldBottomTileNum) >= 10 then X1 = oldX1 : MapX = OldMapX
if TileMap(OldLeftTileNum, BottomTileNum) >= 10 then Y1 = oldY1 : MapY = OldMapY
`checks for bottom-right collision point
if TileMap(RightTileNum, OldBottomTileNum) >= 10 then X1 = oldX1 : MapX = OldMapX
if TileMap(OldRightTileNum, BottomTileNum) >= 10 then Y1 = oldY1 : MapY = OldMapY
`stops player from scrolling past the edges of the map by resetting the maps
`global coordinates to their maximum values. The maximum values are calculated by
`(TileSizeX * NumOfTilesInRow) - SpeedX - for MapX
`(TileSizeY * NumOfTilesInColumn) - SpeedY - for MapX
if MapX < 0 then MapX = 0
if MapX >= 798 then MapX = 798
if MapY < 0 then MapY = 0
if MapY >= 598 then MapY = 598
return
ScrollScreen:
`works out the tile number within array to start displaying tiles at
TileNumX = MapX / NumOfTilesX
TileNumY = MapY / NumOfTilesY
`cache calculation to tempX and TempY variables for a minor speed increase
`this calculation works out how many pixels to offset the position where the tile
`images should be placed.
TempX = MapX - (TileNumX * TileSizeX)
TempY = MapY - (TileNumY * TileSizeY)
`this pastes the images to screen in the correct order by working out where to start
`pasting the images from in the array, then displaying the images at the correct
`screen coordinates
for y = 0 to 15
for x = 0 to 20
paste image TileMap(x + TileNumX,y + TileNumY),(x * TileSizeX) - TempX,(y * TileSizeY) - TempY
next x
next y
return
displayplayer:
`display sprite
sprite 1,X1 - MapX,Y1 - MapY,2
return
DisplayDebugInfo:
text 0, 0,"FPS " + str$(screen fps())
text 0,20,"MapX " + str$(MapX)
text 0,40,"MapY " + str$(MapY)
text 0,60,"X1 " + str$(X1)
text 0,80,"Y1 " + str$(Y1)
text 0,110,"Packets sent: "+str$(PACKETS_SENT)
stateP1 = net get player state(1)
text 0,140,"Player 1 exists?: "+str$(stateP1)
`exit the loop if space bar is pressed - effectively ending the program
`Note: it has to be place here and not in a gosub as it wont work
text 100, 0,"Press spacebar to exit"
return
cleanup:
`delete any media involved in this program
for x = 1 to 11
if image exist(x) then delete image x
if sprite exist(x) then delete sprite x
next x
`delete anything thats left in the video memory - if anything
flush video memory
return
`store tile data at the end of the program so its out of the way
data 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1,10
data 10, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1,11, 1,11, 1, 1, 1, 1, 1, 1, 1, 1,11, 1,11, 1, 1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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1,10
data 10, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
Client Code-----------------
`****************************************************************************************
`*********************************** Start of program ***********************************
`****************************************************************************************
`gosubs are here instead of actual code, just to make things look neat
global connected as boolean
global PACKETS_RECEIVED as integer
gosub join
gosub screensetup
gosub DeclareVariables
gosub createimages
gosub loaddata
gosub makeplayer
`start the main loop
do
`exit the loop if space bar is pressed - effectively ending the program
`Note: it has to be place here and not in a gosub as it wont work
if spacekey() = 1 then exit
gosub controls
gosub collision
gosub ScrollScreen
gosub displayplayer
gosub DisplayDebugInfo
gosub lan
`update screen
sync
loop
gosub cleanup
`end the program
end
`****************************************************************************************
`************************************* End of program ***********************************
`****************************************************************************************
join:
connected = net join("192.168.0.3")
if connected = 0
text 0,100,"Failed to connect"
wait 2000
end
else
text 0,100,"Connected to IP"
wait 2000
endif
return
screensetup:
`delete anything stored in video memory
flush video memory
`set screen resolution
set display mode 800,600,32
`setup syncing
sync on
sync rate 150
return
DeclareVariables:
`store how many horizontal/verticle tiles there are
NumOfTilesX = 40
NumOfTilesY = 40
`store the size (in pixels) of a tile
TileSizeX = 40
TileSizeY = 40
`store the speed of the tile
SpeedX = 2
SpeedY = 2
`store the coordinates that will make the player be at the centre of the screen
ScreenCentreX = 380
ScreenCentreY = 280
`store predefined coordinates of player
XX1 = ScreenCentreX
YY1 = ScreenCentreY
X1 = 380
Y1= 280
`store predefined coordinates of map
MapX = 0
MapY = 0
return
createimages:
`create a green box
box 0,0,TileSizeX,TileSizeY,rgb(0,255,0),rgb(0,225,0),rgb(0,225,0),rgb(0,200,0)
get image 1,0,0,TileSizeX,TileSizeY,1
`create a yellow/orange box as the player
box 0,0,TileSizeX,TileSizeY,rgb(255,255,0),rgb(255,128,0),rgb(255,128,0),rgb(255,255,0)
get image 2,0,0,TileSizeX,TileSizeY,1
`create a yellow/orange box as the host player
box 0,0,TileSizeX,TileSizeY,rgb(255,255,0),rgb(255,128,0),rgb(255,128,0),rgb(255,255,0)
get image 3,0,0,TileSizeX,TileSizeY,1
`create a red box
cls rgb(255,0,0)
get image 10,0,0,TileSizeX,TileSizeY,1
`create a blue box
box 0,0,TileSizeX,TileSizeY,rgb(0,0,255),rgb(0,0,225),rgb(0,0,225),rgb(0,0,200)
get image 11,0,0,TileSizeX,TileSizeY,1
return
loaddata:
`create an array called TileMap - we will use this to store the tile data.
`an array is like a file cabinet - in each drawer of the file cabinet there are
`folders that hold our data. Therefore in our "file cabinet" we have 20 drawers; and
`inside of each drawer we 15 folders; which hold data we want to access.
`Also arrays can have up to 5 dimensions (ours has 2 dimensions) - you can visualise
`this as being more folders inside of the other folders and so on; and of course you
`can just have 1 dimension array as well (in other words just the drawers)
dim TileMap(NumOfTilesX - 1,NumOfTilesY - 1)
`load data into the array - basically the computer will read 20 bits of data from one
`data statment (at the end of the program), then move onto the next data statment
`until all 15 data statments are read. Note: all 15 data statments could have been
`combined into one large data statment of 300 items; the reason why its not currently
`like that is its hard to read also you can kind of see the map the way its currently
`written.
for y = 0 to NumOfTilesY - 1
for x = 0 to NumOfTilesX - 1
read TileMap(x,y)
next x
next y
return
makeplayer:
`make the player
sprite 1,XX1,YY1,2
`this sets the sprites properties; by placing a 0 in the backsave flag the background
`behind sprite will not be saved by the computer, hence will speed up our program a
`little. We are not using the backsave feature because we will be replacing the
`background ourselves; this is due to when a sprite is created the 3d backdrop is
`automatically created (a blue screen wiping any 2d images previously draw to the
`screen) and cannot be turned off as long as you have a sprite on screen.
set sprite 1,0,1
return
`MULTIPLAYER CODE-----------------------------------------------------------------------
lan:
if net receive(1)
inc PACKETS_RECEIVED
msgAmount = net get msg amount()
for x=1 to msgAmount
net get msg
Y1# = net pop float()
X1# = net pop float()
next x
Y1# = Y1
X1# = X1
sprite 2,X1,Y1,3
set sprite 2,0,1
endif
return
`END OF MULTIPLAYER CODE--------------------------------------------------------------------
controls:
`store the map's x and y coordinates into the variables OldMapX and OldMapY
OldMapX = MapX
OldMapY = MapY
`store the x and y coordinates of the player into the variables oldX1 and oldY1
OldX1 = XX1
OldY1 = YY1
`makes the player move by altering the sprites coordinates
`makes the map move by altering the maps coordinates, if the player is roughly at the
`centre of the screen
if leftkey() = 1
if MapX < XX1 - (ScreenCentreX - 5) and MapX > XX1 - (ScreenCentreX + 5) then MapX = MapX - SpeedX
XX1 = XX1 - SpeedX
endif
if rightkey() = 1
if MapX < XX1 - (ScreenCentreX - 5) and MapX > XX1 - (ScreenCentreX + 5) then MapX = MapX + SpeedX
XX1 = XX1 + SpeedX
endif
if upkey() = 1
if MapY < YY1 - (ScreenCentreY - 5) and MapY > YY1 - (ScreenCentreY + 5) then MapY = MapY - SpeedY
YY1 = YY1 - SpeedY
endif
if downkey() = 1
if MapY < YY1 - (ScreenCentreY - 5) and MapY > YY1 - (ScreenCentreY + 5) then MapY = MapY + SpeedY
YY1 = YY1 + SpeedY
endif
return
collision:
`NOTE : you may have noticed the player sticking to the blocks, to stop this make the
` collision area of the player smaller and edit the players image to the same
` same size (so it doesn't look weird). You have to make the change in the
` players x and y sizes greater or equal to their speed or you wont notice a
` difference.
`the following 8 lines of code stores the old and new left,right,top,bottom edges of
`the sprite in the respective variables
`gets which tiles the current left and right co-ordinates of the player are on
LeftTileNum = int(XX1 / TileSizeX)
RightTileNum = int((XX1 + (TileSizeX - 1)) / TileSizeX)
`gets which tiles the current top and bottom co-ordinates of the player are on
TopTileNum = int(YY1 / TileSizeY)
BottomTileNum = int((YY1 + (TileSizeY - 1)) / TileSizeY)
`gets which tiles the old left and right co-ordinates of the player are on
OldLeftTileNum = int(oldX1 / TileSizeX)
OldRightTileNum = int((oldX1 + (TileSizeX - 1)) / TileSizeX)
`gets which tiles the old top and bottom co-ordinates of the player are on
OldTopTileNum = int(oldY1 / TileSizeY)
OldBottomTileNum = int((oldY1 + (TileSizeY - 1)) / TileSizeY)
`this is where we check for those 4 collision points; if the collision point is on a
`tile you are not able to pass through (in our case a tile that has an image number
`of 10 or greater), then the old coordinates of the sprite are restored.
`Note: to have sliding collision we must check the the current x tile against the
`old y tile and vice versa.
`checks for top-left collision point
if TileMap(LeftTileNum, OldTopTileNum) >= 10 then XX1 = oldX1 : MapX = OldMapX
if TileMap(OldLeftTileNum, TopTileNum) >= 10 then YY1 = oldY1 : MapY = OldMapY
`checks for top-right collision point
if TileMap(RightTileNum, OldTopTileNum) >= 10 then XX1 = oldX1 : MapX = OldMapX
if TileMap(OldRightTileNum, TopTileNum) >= 10 then YY1 = oldY1 : MapY = OldMapY
`checks for bottom-left collision point
if TileMap(LeftTileNum, OldBottomTileNum) >= 10 then XX1 = oldX1 : MapX = OldMapX
if TileMap(OldLeftTileNum, BottomTileNum) >= 10 then YY1 = oldY1 : MapY = OldMapY
`checks for bottom-right collision point
if TileMap(RightTileNum, OldBottomTileNum) >= 10 then XX1 = oldX1 : MapX = OldMapX
if TileMap(OldRightTileNum, BottomTileNum) >= 10 then YY1 = oldY1 : MapY = OldMapY
`stops player from scrolling past the edges of the map by resetting the maps
`global coordinates to their maximum values. The maximum values are calculated by
`(TileSizeX * NumOfTilesInRow) - SpeedX - for MapX
`(TileSizeY * NumOfTilesInColumn) - SpeedY - for MapX
if MapX < 0 then MapX = 0
if MapX >= 798 then MapX = 798
if MapY < 0 then MapY = 0
if MapY >= 598 then MapY = 598
return
ScrollScreen:
`works out the tile number within array to start displaying tiles at
TileNumX = MapX / NumOfTilesX
TileNumY = MapY / NumOfTilesY
`cache calculation to tempX and TempY variables for a minor speed increase
`this calculation works out how many pixels to offset the position where the tile
`images should be placed.
TempX = MapX - (TileNumX * TileSizeX)
TempY = MapY - (TileNumY * TileSizeY)
`this pastes the images to screen in the correct order by working out where to start
`pasting the images from in the array, then displaying the images at the correct
`screen coordinates
for y = 0 to 15
for x = 0 to 20
paste image TileMap(x + TileNumX,y + TileNumY),(x * TileSizeX) - TempX,(y * TileSizeY) - TempY
next x
next y
return
displayplayer:
`display sprite
sprite 1,XX1 - MapX,YY1 - MapY,2
sprite 2,X1 - MapX,Y1 - MapY,3
return
DisplayDebugInfo:
text 0, 0,"FPS " + str$(screen fps())
text 0,20,"MapX " + str$(MapX)
text 0,40,"MapY " + str$(MapY)
text 0,60,"X1 " + str$(XX1)
text 0,80,"Y1 " + str$(YY1)
text 0,110,"Packets PACKETS_RECEIVED: "+str$(PACKETS_RECEIVED)
text 100, 0,"Press spacebar to exit"
text 0,130,"X1 " + str$(X1)
text 0,140,"Y1 " + str$(Y1)
return
cleanup:
`delete any media involved in this program
for x = 1 to 11
if image exist(x) then delete image x
if sprite exist(x) then delete sprite x
next x
`delete anything thats left in the video memory - if anything
flush video memory
return
`store tile data at the end of the program so its out of the way
data 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
data 10, 1, 1, 1, 1, 1, 1,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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
i think the code is all over the place, i had to rush to do it today soz...
sEriously..