Code 1
[============MAIN MENU============]

sync on : sync rate 0           `Setup the sync rate
set display mode 1024,768,32    `Set the resolution of our program
Restart:                        `This is where we'll be taken when we win, die, or exit.
set text size 24

cls       `Clear the screen
CleanUp() `Call up our cleanup function to make sure no objects exist.

menu=10                                 `Variable holding our menu image
load image "Menu Screen/menu.bmp",menu `Our menu image

ink rgb(255,255,255),1 `Set the ink color
do `Our main loop for the main menu
paste image menu,0,0 `Paste our main menu image to the screen

`A text statement to show the controls of the game
text 5,5,"arrowkeys - move    left mouseclick - fire    Tab - Game info    ` - Console    w - wireframe terrain"

`A text statement to show what options the user has
text 5,screen height()-20,"1 - Start Game    2 - Exit"

`When the "1" key is pressed, goto the game
if keystate(2)=1
 delete image menu
 goto Game
endif
`When the "2" key is pressed, delete everything, then exit the game.
`It's always a good idea to delete everything before quitting, to ensure all the memory
`is free.
if keystate(3)=1
  CleanUp()
  end
endif

sync
loop
`[============MAIN MENU============]

Game:

sync on : sync rate 60       `Set the refresh rate to 60
set display mode 1024,768,32 `Set the resolution of our program
autocam off                  `Tell our camera not to be repositioned after creating an
                             `object.

mapsize =500                    `This defines how big our terrain will be.
segments= 64                    `This is the number of segements we want in our matrix.
make matrix 1,mapsize,mapsize,segments,segments `Here is our matrix, which will soon
                                                `be molded by our heightmap.
load image "heightmap.bmp",1    `This is our heightmap image.
size=256                        `Since we know our heightmap is 256 by 256 pixels, we set
                                `a variable for this.
load image "texture.bmp",2      `This is our texture for the terrain
prepare matrix texture 1,2,1,1  `This command applies the texture to the matrix.

`This next step is the most important, as we'll begin reading our heightmap data.
cls `Clear the screen, to make sure we can read the data correctly.
paste image 1,0,0 `Paste the image to the screen.
modifier=size/segments `Since our image has more pixels than we have segments, we need
                       `to make sure we can account for that difference, by getting every
                       `height after 2 pixels(256/128=2) rather than every pixel.
for x = 0 to segments
for y = 0 to segments
  pixelcolor#=rgbr(point(x*modifier,y*modifier)) `This will return the color value.
  set matrix height 1,x,y,pixelcolor#            `With the color value, we then set the
next x                                           `matrix height according to that color.
next y

update matrix 1      `Finally, we must update our matrix to show the changes that've taken
                     `place.

``````Creating the plants``````
load image "tree.bmp",3,1     `Here we load a uv map for our tree object. It's already
                              `premade, so you don't need to worry about it. It'll rely
                              `on transparency for the branches and detail(Go ahead and
                              `take a look at the image).
                              `This builds off the last tutorial, so be sure to not forget
                              `that extra texture flag.
load image "rose.bmp",4,1     `Here's our second plant, the flower(rose).

Plants=940                `This variable defines how many plants we want total. There will
                           `be an even amount of plants created(if plants is 800 then
                           `400 trees and 400 flowers will be made). Be sure to make sure
                           `the plants number is an even number, to keep things simple.

for x = 1 to Plants/2      `This for-next statement uses our plant function to create some
                           `plants. Please see bottom of source code for explanation as
  createplant(x,mapsize,1) `to how the function works.
next x
for x = (Plants/2)+1 to Plants
  createplant(x,mapsize,0)
next x
Code 2
water=5000                              `Our variable for our water object
make object plain water,mapsize,mapsize `We make it as big as our map, to ensure all areas
                                        `get covered.
load image "water.bmp",water            `Load our image we'll use to texture our water.
texture object water,water              `Texture our object
set reflection shading on water         `We give our object reflection properties.
                                        `Be careful, use this command sparingly, as it's
                                        `very system intensive.
set alpha mapping on water,25           `We give our water transparency.
                                        `This way we'll be able to see through our water.
waterheight=15                          `This is where we'll position our water on the
                                        `y-axis.
position object water,mapsize/2,waterheight,mapsize/2   `We position our water.
xrotate object water,-90                `Rotate our water.

`We don't want trees under our water, so we'll get rid of them.
`If some trees are still half under the water, you can adjust the values accordingly.
for x = 1 to Plants
  if object exist(x)           `Check to see if the object exists
    if object position y(x)<=waterheight then delete object x `Delete object if it's under
  endif                                                       `the water.
next x
`As you'll see later in our main loop,(under "[====Water===]"), we'll make it slowly
`move up and down to give it a coastline.
Code 3
sky=5001                            `Our variable for the skysphere.
load image "sky.bmp",sky            `Load our texture for the skysphere.
make object sphere sky,(-mapsize*2) `Here we create our sphere, and ensure it's big enough
                                    `to cover the map, by multiplying it by the mapsize.
texture object sky,sky              `Texture our sky with the sky texture.
position object sky,mapsize/2,0,mapsize/2  `We position our skysphere.
                                    `Note that we divide the mapsize by 2 because our object
                                    `has been rotated. :)
`Later on, in the main loop, we'll rotate the skysphere. This will create the illusion that
`the sky is actually moving like it really does.



`[====The GUI====]
`Lets first start by loading the images we'll need for our GUI.
bullet_image=7000
load image "bullet.bmp",bullet_image
healthbar=7001
load image "healthbar.bmp",healthbar
roundsbar=7002
load image "rounds.bmp",roundsbar
scorebar=7003
load image "scoreboard.bmp",scorebar

`For our GUI, we'll also have a cursor which will show where our bullet fire will go.
cursor=7004
load image "cursor.bmp",cursor

`I'm sure all you artists out there can improve upon the media, feel free to!
`If you haven't guessed by now, we're on our way to making a FPS. :)
`See section "GUI" in our main loop, to see how images are pasted
`[====The GUI====]
Code 4
`[===Our Player & variables===]
`At this point and time, we need to assign some variables for our player.
`We'll need three variables, one for the health, a second for how many bullets we have
`in our round, and a third for how many rounds we have.

Player1_Health=100 `Our player health
Bullets_left=5     `How many bullets we have left in our round
Rounds=5           `How many rounds we have

`We now need to create our character.
`We're going to use the ak-47 model from darkmatter in this tutorial as our main player.
`(This model was freely distrubuted in TGC newsletter 12
`//www.thegamecreators.com/?gf=newsletter_issue_12)
You=11000 `This is you.
load object "H-AK47-Static.x",You    `Load the ak-47.
scale object You,1000,1000,1000      `Make the object 10 times bigger.
yrotate object you,90                `Rotate our object
set object collision on you          `Turn on object collision
set object collision to polygons you `set the collision type to polygons

`For our gun, we'll also need some sounds(All sounds are included with DBP).
gunsound=1
load sound "Sounds/machine gun 2.wav",gunsound   `Sound for when we fire the gun.
reloadsound=2
load sound "Sounds/rifle reload.wav",reloadsound `Sound for reloading and picking up ammo.

`To spice things up a little, we'll add a muzzle flash that'll be pasted to screen
`when we fire our gun.
muzzle=7005
load image "muzzle.bmp",muzzle

`It's also a good idea to set a variable for how fast our character will move.
movement#=1.0
`[===Our Player variables===]
Code 5
`[===The ammo crates===]
`Because we can't have an unlimited supply of ammo, we'll need a way to get more.
`This will be as simple as randomly creating ammo boxes to place around the map.

Number_of_ammo_boxes=20 `This defines how many ammo boxes we'll have in our level.

ammo_image=7006
load image "ammo.bmp",ammo_image                `Our ammo box texture
for x =12000 to 12000+Number_of_ammo_boxes
  make object cube x,1                          `Make our ammo crate
  texture object x,ammo_image                   `Texture our object
  position object x,rnd(mapsize),0,rnd(mapsize) `Randomly position our object
  `Here we find out how high the land is at the objects position.
  y#=get ground height(1,object position x(x),object position z(x))
  `We reposition the object to make sure its viewable on the land.
  position object x,object position x(x),y#+.5,object position z(x)
  set object collision on x                    `Turn collision on
  set object collision to boxes x              `Set the collision mode to boxes
next x

`---Setting up collision---

`[=====The ENEMIES!=====]
`I didn't want to leave you all hanging with nothing to shoot at, so lets create
`some enemies. We'll be using the alien model that was given away in newsletter 12.

Number_of_Enemies=10                `The number of enemies in the game.
dim EnemyHealth(Number_of_Enemies+1)`We create an array for each enemy to define
                                    `their health.
for x = 0 to Number_of_Enemies+1    `We set the enemy health for each of the 30 enemies.
  EnemyHealth(x)=5
next x

for x = 16000 to 16000+Number_of_Enemies
  load object "H-Alien Mutant-Move.x",x         `Our alien model
  set object light x,0                          `Disable lighting on our alien
  scale object x,500,500,500                    `Scale our object
  position object x,rnd(mapsize),0,rnd(mapsize) `Randomly position our object
  `Here we find out how high the land is at the objects position.
  y#=get ground height(1,object position x(x),object position z(x))
  `We reposition the object to make sure its viewable on the land.
  position object x,object position x(x),y#+.5,object position z(x)
  set object collision on x                    `Turn collision on
  set object collision to polygons x           `Set the collision mode to boxes
next x

`See "Kills" section to see how we kill them
`[=====The ENEMIES!=====]
Code 6
`Our dummy variables
Dummy1=99991
Dummy2=99992
Dummy3=99993
Dummy4=99994
`Dummy5 would of course be you(Not that I'm calling you a dummy ;) ).
for x = 99991 to 99994
  make object cube x,100           `Make the dummy object
  hide object x                   `Hide the object
  set object collision on x       `Enable collision
  set object collision to polygons x   `Set collision to boxes
next x

`Now we position our dummy objects accordingly
position object Dummy1,0,get ground height(1,0,mapsize),mapsize
position object Dummy2,mapsize,get ground height(1,mapsize,mapsize),mapsize
position object Dummy3,mapsize,get ground height(1,mapsize,0),0
position object Dummy4,0,get ground height(1,0,0),0

`Next we must create an array, so we can hold information as to what point the enemies
`should be heading towards.
dim Waypoints(16000+Number_of_Enemies)  `The array that holds what waypoint each enemy is heading
                                  `towards
for x = 0 to Number_of_Enemies
  Waypoints(16000+x)=rnd(4)+1   `Assign a waypoint to the enemy.
                                  `Note: The random number function generators integers
                                  `from 0 to the range provided, thus the +1.
next x

dim Distance#(16000+Number_of_Enemies)
Code 7
RainDrops=300     `Number of raindrops we'll have.

`Create 100 raindrops, color them blue,and then ghost them.
`To save processor power, and get just as good results, we just need to ghost them
`(No textures necessary).

for x = 72000 to 72000+RainDrops
  make object plain x,.4,1
  color object x,rgb(0,0,255)
  ghost object on x,2
next x