Rem Project: AI Challenge
Rem Created: 03/04/2005 14:52:41
Rem Best viewed in a 800x600 window
 
Rem Do I really need to tell you?
set window on : set window title "AI Challenge" : set window size 800,600
sync on : sync rate 0 : autocam off
 
Rem Constants for images made from memblocks,  for the gui
#constant GUI_IMAGE_1 2
#constant GUI_IMAGE_2 3
#constant GUI_IMAGE_3 4
#constant GUI_IMAGE_4 5
#constant GUI_IMAGE_5 6
#constant GUI_IMAGE_6 7
#constant GUI_IMAGE_7 8
#constant GUI_IMAGE_8 9
#constant GUI_IMAGE_9 10
 
Rem Pet guis
#constant OB_PET 1000
#constant OB_PET_IMAGE 1
 
Rem Constants for the Gui
#constant GUI_WINDOW 1 : Rem Constant for a window..
#constant GUI_BUTTON 2 : Rem Is a button
#constant GUI_PROGRESS 3 : Rem A progress bar
 
Rem The UDT for the widgets
type widget_GUI
   gui_draw as integer : Rem Draw it or not
   gui_number as integer : Rem The number of sprites to start from, this is the main limitation
   gui_title as string : Rem The widgets title or caption
   gui_position_x as integer : Rem The x position
   gui_position_y as integer : Rem ^ y
   gui_width as integer : Rem The width...duh
   gui_height as integer : Rem The height..ffs thats obvious
   gui_type as integer : Rem The type, defined by constants
   gui_action as integer : Rem If the widget is acting or not. So far only buttons act.
   gui_parent as integer : Rem The widgets parent, where it belongs. 0 is the background.
   gui_extra as string : Rem Anything else needed for the widget. Depends on type.
endtype
 
Rem The array using the above type
dim gui_widget(100) as widget_GUI
 
Rem Define the window data. The first few bits will be commented :|
gui_widget(1).gui_draw = 1
gui_widget(1).gui_number = 100
gui_widget(1).gui_title = "AI Challenge"
gui_widget(1).gui_position_x = 0
gui_widget(1).gui_position_y = 0
gui_widget(1).gui_width = 110
gui_widget(1).gui_height = 48
gui_widget(1).gui_type = GUI_WINDOW
gui_widget(1).gui_action = 0
gui_widget(1).gui_parent = 0
gui_widget(1).gui_extra = "STATIC" : Rem Means teh bastage trying to break my GUI cant (it can't be moved)..
 
gui_widget(2).gui_draw = 1
gui_widget(2).gui_number = 104
gui_widget(2).gui_title = "Exit"
gui_widget(2).gui_position_x = 4
gui_widget(2).gui_position_y = 4
gui_widget(2).gui_width = 102
gui_widget(2).gui_height = 21
gui_widget(2).gui_type = GUI_BUTTON
gui_widget(2).gui_action = 0
gui_widget(2).gui_parent = 1
gui_widget(2).gui_extra = "EXIT" : Rem The action is to quit
 
 
gui_widget(6).gui_draw = 1
gui_widget(6).gui_number = 200
gui_widget(6).gui_title = "Pet Data"
gui_widget(6).gui_position_x = 0
gui_widget(6).gui_position_y = 47
gui_widget(6).gui_width = 110
gui_widget(6).gui_height = 125
gui_widget(6).gui_type = GUI_WINDOW
gui_widget(6).gui_action = 0
gui_widget(6).gui_parent = 0
gui_widget(6).gui_extra = "STATIC"
 
gui_widget(7).gui_draw = 1
gui_widget(7).gui_number = 204
gui_widget(7).gui_title = "Health"
gui_widget(7).gui_position_x = 4
gui_widget(7).gui_position_y = 4
gui_widget(7).gui_width = 102
gui_widget(7).gui_height = 21
gui_widget(7).gui_type = GUI_PROGRESS
gui_widget(7).gui_action = 0
gui_widget(7).gui_parent = 6
gui_widget(7).gui_extra = str$(GUI_IMAGE_5) : Rem The background image it should use.
 
gui_widget(8).gui_draw = 1
gui_widget(8).gui_number = 208
gui_widget(8).gui_title = "Stanima"
gui_widget(8).gui_position_x = 4
gui_widget(8).gui_position_y = 29
gui_widget(8).gui_width = 102
gui_widget(8).gui_height = 21
gui_widget(8).gui_type = GUI_PROGRESS
gui_widget(8).gui_action = 0
gui_widget(8).gui_parent = 6
gui_widget(8).gui_extra = str$(GUI_IMAGE_6)
 
gui_widget(9).gui_draw = 1
gui_widget(9).gui_number = 212
gui_widget(9).gui_title = "Level [1]"
gui_widget(9).gui_position_x = 4
gui_widget(9).gui_position_y = 54
gui_widget(9).gui_width = 102
gui_widget(9).gui_height = 21
gui_widget(9).gui_type = GUI_PROGRESS
gui_widget(9).gui_action = 0
gui_widget(9).gui_parent = 6
gui_widget(9).gui_extra = str$(GUI_IMAGE_8)
 
gui_widget(10).gui_draw = 1
gui_widget(10).gui_number = 216
gui_widget(10).gui_title = "Happiness"
gui_widget(10).gui_position_x = 4
gui_widget(10).gui_position_y = 79
gui_widget(10).gui_width = 102
gui_widget(10).gui_height = 21
gui_widget(10).gui_type = GUI_PROGRESS
gui_widget(10).gui_action = 0
gui_widget(10).gui_parent = 6
gui_widget(10).gui_extra = str$(GUI_IMAGE_9)
 
 
gui_widget(11).gui_draw = 1
gui_widget(11).gui_number = 300
gui_widget(11).gui_title = "Actions"
gui_widget(11).gui_position_x = 0
gui_widget(11).gui_position_y = 171
gui_widget(11).gui_width = 110
gui_widget(11).gui_height = 125
gui_widget(11).gui_type = GUI_WINDOW
gui_widget(11).gui_action = 0
gui_widget(11).gui_parent = 0
gui_widget(11).gui_extra = "STATIC"
 
gui_widget(12).gui_draw = 1
gui_widget(12).gui_number = 304
gui_widget(12).gui_title = "Add Food"
gui_widget(12).gui_position_x = 4
gui_widget(12).gui_position_y = 4
gui_widget(12).gui_width = 102
gui_widget(12).gui_height = 21
gui_widget(12).gui_type = GUI_BUTTON
gui_widget(12).gui_action = 0
gui_widget(12).gui_parent = 11
gui_widget(12).gui_extra = "FOOD"
 
 
Rem Define the type for the pet. I'm not going over this..
type petData
   health as integer
   stamina as integer
   level as integer
   level_exp as integer
   happy as integer
   x as integer
   y as integer
endtype
 
Rem Data var
global pet_data as petData
pet_data.health = 500
pet_data.stamina = 100
pet_data.level = 1
pet_data.happy = 250
pet_data.x = 200
pet_data.y = 200
 
type fooodness
   exists as integer
   x as integer
   y as integer
endtype
 
food as fooodness
 
Rem Globals for mouse positions, allows me to take them into functions, without being gay.
global mox : global moy
Rem For the AI
global loops2
 
Rem Create the images for the GUI
createGuiSprites()
 
Rem Environment set up:
backdrop on
color backdrop 0,rgb(70,122,175)
 
Rem pet set up:
sprite OB_PET,pet_data.x,pet_data.y,OB_PET_IMAGE
stretch sprite OB_PET,3500,3500
set sprite OB_PET,0,0
 
Rem Sync all this to screen and begin the loop.
sync
do
   Rem Update the mouse position
   mox = mousex() : moy = mousey()
 
   Rem Print dt and the FPS to the screen.
   set cursor 200,200 : ink rgb(255,255,255),0
   print screen FPS();" ";pet_data.happy
 
   Rem Control the pet
   if loops=0
      pet()
      inc loops
   else
      loops=0
   endif
   Rem Handle the gui
   handleGui()
   Rem Draw him!
   sprite OB_PET,pet_data.x,pet_data.y,OB_PET_IMAGE
   set sprite OB_PET,0,0
 
   Rem Sync and loop
   sync
loop
 
Rem End. The short and sweet command..
end
 
REM --------------------------------------
REM GENERAL FUNCTIONS
REM --------------------------------------
 
function lose()
 
   exit prompt "Next time round you better treat your poor, little, well-coloured, square pet better, otherwise it'll be a case of Mr Psycotic-Neofish on your back!!1","You lose!!!!!11onetwo!"
   end
 
endfunction
 
REM --------------------------------------
REM AI FUNCTIONS
REM --------------------------------------
 
function pet()
 
   Rem You lose if pet has no health
   if pet_data.health = 0 then lose()
 
   Rem Move it away if in the last 20 pixels of screen:
   if pet_data.x > 760 then dec pet_data.x,5
   if pet_data.x < 5 then inc pet_data.x,5
   if pet_data.y < 5 then inc pet_data.y,5
   if pet_data.y > 560 then dec pet_data.y,5
 
   Rem Basic, non script control - makes pet tired as it struggles
   if rightkey()=1
      inc pet_data.x,5
      forcedMovement()
   endif
   if leftkey()=1
      dec pet_data.x,5
      forcedMovement()
   endif
   if upkey()=1
      dec pet_data.y,5
      forcedMovement()
   endif
   if downkey()=1
      inc pet_data.y,5
      forcedMovement()
   endif
 
   Rem These things must happen less than other stuff
   if loops2<>6 then inc loops2
   select loops2
 
      case 1:
 
         Rem Inc the experience
         if pet_data.happy > 250 then inc pet_data.level_exp
         if spacekey()=1 then inc pet_data.happy,2
         if pet_data.level_exp > (100 * pet_data.level)-1 or pet_data.happy > 499
 
            Rem Give him level, reset exp, life and stamina
            inc pet_data.level
            pet_data.level_exp = 0
            pet_data.health = 500
            pet_data.stamina = 100
            pet_data.happy = 200
 
            Rem Change the level progress bar's caption, and delete the image so it updates
            gui_widget(9).gui_title = "Level ["+str$(pet_data.level)+"]"
            num = gui_widget(9).gui_number + 3
            if image exist(num)=1 then delete image num
 
         endif
      endcase
 
      case 3:
         Rem Make his stamina go up if its down :)
         if pet_data.stamina <> 100 then inc pet_data.stamina
      endcase
 
      case 6
         Rem revert it so those pieces of gay go get to cycle more than once
         loops2 = 0
      endcase
 
   endselect
 
endfunction
 
Rem Pet grows tired and gets hurt, teh poor thing.
function forcedMovement()
 
   if pet_data.stamina <> 0 then dec pet_data.stamina
 
   if pet_data.stamina = 0
      if pet_data.health <> 0 then dec pet_data.health
      if pet_data.happy <> 0 then dec pet_data.happy
      if pet_data.health = 0 and pet_data.health > 1 then dec pet_data.health
   endif
 
endfunction
 
Rem Adds food that the pet should run to :)
function addFood()
 
   food.exists = 1
 
   x = rnd (300)+200
   y = rnd (200)+100
 
   food.x = x
   food.y = y
 
endfunction
 
REM --------------------------------------
REM GUI FUNCTIONS
REM --------------------------------------
 
Rem Loads the images for the sprites.
function createGuiSprites()
 
   Remstart
      Memblock images: 3 dwords and 4 bytes (for a 1 pixel image)
      which makes 4x3 + 4  = 16
   Remend
   size = 16
 
   Rem GUI_IMAGE_1 - Blue
   make memblock GUI_IMAGE_1,size
   write memblock dword GUI_IMAGE_1,0,1     : `width
   write memblock dword GUI_IMAGE_1,4,1     : `height
   write memblock dword GUI_IMAGE_1,8,32    : `depth
   write memblock byte GUI_IMAGE_1,12,204   : `blue
   write memblock byte GUI_IMAGE_1,13,102   : `green
   write memblock byte GUI_IMAGE_1,14,0     : `red
   write memblock byte GUI_IMAGE_1,15,255   : `alpha
   make image from memblock GUI_IMAGE_1,GUI_IMAGE_1 : Rem Make the image
 
   Rem GUI_IMAGE_2 - Grey
   make memblock GUI_IMAGE_2,size
   write memblock dword GUI_IMAGE_2,0,1     : `width
   write memblock dword GUI_IMAGE_2,4,1     : `height
   write memblock dword GUI_IMAGE_2,8,32    : `depth
   write memblock byte GUI_IMAGE_2,12,221   : `blue
   write memblock byte GUI_IMAGE_2,13,221   : `green
   write memblock byte GUI_IMAGE_2,14,221   : `red
   write memblock byte GUI_IMAGE_2,15,255   : `alpha
   make image from memblock GUI_IMAGE_2,GUI_IMAGE_2
 
   Rem GUI_IMAGE_3 - Black
   make memblock GUI_IMAGE_3,size
   write memblock dword GUI_IMAGE_3,0,1     : `width
   write memblock dword GUI_IMAGE_3,4,1     : `height
   write memblock dword GUI_IMAGE_3,8,32    : `depth
   write memblock byte GUI_IMAGE_3,12,0     : `blue
   write memblock byte GUI_IMAGE_3,13,0     : `green
   write memblock byte GUI_IMAGE_3,14,0     : `red
   write memblock byte GUI_IMAGE_3,15,255   : `alpha
   make image from memblock GUI_IMAGE_3,GUI_IMAGE_3
 
   Rem GUI_IMAGE_4 - Light Grey
   make memblock GUI_IMAGE_4,size
   write memblock dword GUI_IMAGE_4,0,1     : `width
   write memblock dword GUI_IMAGE_4,4,1     : `height
   write memblock dword GUI_IMAGE_4,8,32    : `depth
   write memblock byte GUI_IMAGE_4,12,241   : `blue
   write memblock byte GUI_IMAGE_4,13,241   : `green
   write memblock byte GUI_IMAGE_4,14,241   : `red
   write memblock byte GUI_IMAGE_4,15,255   : `alpha
   make image from memblock GUI_IMAGE_4,GUI_IMAGE_4
 
   Rem GUI_IMAGE_4 - Bright Red
   make memblock GUI_IMAGE_5,size
   write memblock dword GUI_IMAGE_5,0,1     : `width
   write memblock dword GUI_IMAGE_5,4,1     : `height
   write memblock dword GUI_IMAGE_5,8,32    : `depth
   write memblock byte GUI_IMAGE_5,12,0     : `blue
   write memblock byte GUI_IMAGE_5,13,0     : `green
   write memblock byte GUI_IMAGE_5,14,240   : `red
   write memblock byte GUI_IMAGE_5,15,255   : `alpha
   make image from memblock GUI_IMAGE_5,GUI_IMAGE_5
 
   Rem GUI_IMAGE_6 - Bright Blue
   make memblock GUI_IMAGE_6,size
   write memblock dword GUI_IMAGE_6,0,1     : `width
   write memblock dword GUI_IMAGE_6,4,1     : `height
   write memblock dword GUI_IMAGE_6,8,32    : `depth
   write memblock byte GUI_IMAGE_6,12,240   : `blue
   write memblock byte GUI_IMAGE_6,13,0     : `green
   write memblock byte GUI_IMAGE_6,14,0     : `red
   write memblock byte GUI_IMAGE_6,15,255   : `alpha
   make image from memblock GUI_IMAGE_6,GUI_IMAGE_6
 
   Rem GUI_IMAGE_7 - White
   make memblock GUI_IMAGE_7,size
   write memblock dword GUI_IMAGE_7,0,1     : `width
   write memblock dword GUI_IMAGE_7,4,1     : `height
   write memblock dword GUI_IMAGE_7,8,32    : `depth
   write memblock byte GUI_IMAGE_7,12,255   : `blue
   write memblock byte GUI_IMAGE_7,13,255   : `green
   write memblock byte GUI_IMAGE_7,14,255   : `red
   write memblock byte GUI_IMAGE_7,15,255   : `alpha
   make image from memblock GUI_IMAGE_7,GUI_IMAGE_7
 
   Rem GUI_IMAGE_8 - Bright Green
   make memblock GUI_IMAGE_8,size
   write memblock dword GUI_IMAGE_8,0,1     : `width
   write memblock dword GUI_IMAGE_8,4,1     : `height
   write memblock dword GUI_IMAGE_8,8,32    : `depth
   write memblock byte GUI_IMAGE_8,12,0     : `blue
   write memblock byte GUI_IMAGE_8,13,255   : `green
   write memblock byte GUI_IMAGE_8,14,0     : `red
   write memblock byte GUI_IMAGE_8,15,255   : `alpha
   make image from memblock GUI_IMAGE_8,GUI_IMAGE_8
 
   Rem GUI_IMAGE_9 - Bright Orange
   make memblock GUI_IMAGE_9,size
   write memblock dword GUI_IMAGE_9,0,1     : `width
   write memblock dword GUI_IMAGE_9,4,1     : `height
   write memblock dword GUI_IMAGE_9,8,32    : `depth
   write memblock byte GUI_IMAGE_9,12,0     : `blue
   write memblock byte GUI_IMAGE_9,13,255   : `green
   write memblock byte GUI_IMAGE_9,14,255   : `red
   write memblock byte GUI_IMAGE_9,15,255   : `alpha
   make image from memblock GUI_IMAGE_9,GUI_IMAGE_9
 
   Rem OB_PET_IMAGE - Black
   make memblock OB_PET_IMAGE,size
   write memblock dword OB_PET_IMAGE,0,1     : `width
   write memblock dword OB_PET_IMAGE,4,1     : `height
   write memblock dword OB_PET_IMAGE,8,32    : `depth
   write memblock byte OB_PET_IMAGE,12,0     : `blue
   write memblock byte OB_PET_IMAGE,13,0     : `green
   write memblock byte OB_PET_IMAGE,14,0     : `red
   write memblock byte OB_PET_IMAGE,15,255   : `alpha
   make image from memblock OB_PET_IMAGE,OB_PET_IMAGE
 
endfunction
 
Rem Handle the gui...duh
function handleGui()
 
   Rem Repeat through the array items, drawing them and making them act if necessary
   a=1
   repeat
      if gui_widget(a).gui_draw = 1 then drawGui(a)
      guiAction(a)
      inc a
   until a=array count(gui_widget(0))+1
 
endfunction
 
Rem Draw a window based on the info passed in. This will be tedious :'(
function drawGui(index as integer)
 
   Rem Don't go be calling the array all the time (shorter names).
   num = gui_widget(index).gui_number
   title$ = gui_widget(index).gui_title
   x = gui_widget(index).gui_position_x
   y = gui_widget(index).gui_position_y
   width = gui_widget(index).gui_width
   height = gui_widget(index).gui_height
   wtype = gui_widget(index).gui_type
   parent = gui_widget(index).gui_parent
   extra$ = gui_widget(index).gui_extra
 
   Rem Select a case based on what it wants to draw (a pony!).
   select wtype
 
      case GUI_WINDOW
 
         Rem If the image for the caption/title of the widget doesn't exist, create it.
         if image exist(num+3)=0
            Rem Set the text teh right
            ink rgb(255,255,255),0 : set text opaque : set text font "verdana" : set text size 14
            text x,y,title$ : set image colorkey 0,0,0
            Rem Get the image
            get image num+3,x,y,x+text width(title$),y+text height(title$),1
         endif
 
         Rem Make the black background sprite
         sprite num,x,y,GUI_IMAGE_3
         Rem Make it faster
         set sprite num,0,0
         Rem Stretch it to the right size
         stretch sprite num,100*width,100*height
 
         Rem Make the blue title sprite
         sprite num+1,x+1,y+1,GUI_IMAGE_1
 
         set sprite num+1,0,0
         stretch sprite num+1,100*(width-2),1900
 
         Rem Make the grey main bit sprite
         sprite num+2,x+1,y+21,GUI_IMAGE_2
         set sprite num+2,0,0
         stretch sprite num+2,100*(width-2),100*(height-22)
 
         Rem Make the title sprite
         sprite num+3,x+4,y+4,num+3
         set sprite num+3,0,1
 
      endcase
 
      case GUI_BUTTON
      Rem I'm only going to comment bits that have changed!
 
         if image exist(num+2)=0
            ink rgb(10,10,10),0 : set text opaque : set text font "verdana" : set text size 14
            text x,y,title$ : set image colorkey 0,0,0
            get image num+2,x,y,x+text width(title$),y+text height(title$),1
         endif
 
         Rem Get teh parent positions, so the position is relative.
         parent_x = gui_widget(parent).gui_position_x
         parent_y = gui_widget(parent).gui_position_y + 20
         x = x + parent_x
         y = y + parent_y
 
         Rem Rollover image
         if (mox => x and mox <= x+width) and (moy => y and moy <= y+20)
            image = GUI_IMAGE_7
         else
            image = GUI_IMAGE_4
         endif
 
         sprite num,x,y,GUI_IMAGE_3
         set sprite num,0,0
         stretch sprite num,100*width,100*height
 
         sprite num+1,x+1,y+1,image
         set sprite num+1,0,0
         stretch sprite num+1,100*(width-2),100*(height-2)
 
         sprite num+2,x+4,y+4,num+2
         set sprite num+2,0,1
 
      endcase
 
      case GUI_PROGRESS
      Rem This one is the hard one: progress bars
 
         if image exist(num+3)=0
            ink rgb(10,10,10),0 : set text opaque : set text font "verdana" : set text size 14
            text x,y,title$ : set image colorkey 0,0,0
            get image num+3,x,y,x+text width(title$),y+text height(title$),1
         endif
 
         parent_x = gui_widget(parent).gui_position_x
         parent_y = gui_widget(parent).gui_position_y + 20
         x = x + parent_x
         y = y + parent_y
 
         Rem Get the correct image for the particular bar and the right data
         image = val(extra$)
         select image
 
            case GUI_IMAGE_5:
               prog = pet_data.health / 5
            endcase
 
            case GUI_IMAGE_6:
               prog = pet_data.stamina
            endcase
 
            case GUI_IMAGE_8:
               prog = pet_data.level_exp / pet_data.level
            endcase
 
            case GUI_IMAGE_9:
               prog = pet_data.happy / 5
            endcase
 
         endselect
 
         sprite num,x,y,GUI_IMAGE_3
         set sprite num,0,0
         stretch sprite num,100*width,100*height
 
         Rem Draw the coloured bar bit
         sprite num+1,x+1,y+1,image
         set sprite num+1,0,0
         stretch sprite num+1,100*prog,100*(height-2)
 
         Rem Draw the grey bar to fill the gap
         sprite num+2,x+1+prog,y+1,GUI_IMAGE_2
         set sprite num+2,0,0
         stretch sprite num+2,100*(100-prog),100*(height-2)
 
         sprite num+3,x+4,y+4,num+3
         set sprite num+3,0,1
 
      endcase
 
   endselect
 
endfunction
 
Rem Run the widget actions.
function guiAction(index as integer)
 
   Rem Load the widget data
   x = gui_widget(index).gui_position_x
   y = gui_widget(index).gui_position_y
   width = gui_widget(index).gui_width
   Rem if its a button find its actual position
   if gui_widget(index).gui_type = GUI_BUTTON
      parent = gui_widget(index).gui_parent
      parent_x = gui_widget(parent).gui_position_x
      parent_y = gui_widget(parent).gui_position_y + 20
      x = x + parent_x
      y = y + parent_y
   endif
 
   Rem Left clicking on the widget will activate it.
   if (mox => x and mox <= x+width) and (moy => y and moy <= y+20) and mouseclick()=1
      gui_widget(index).gui_action = 1
   endif
 
   Rem Run the button's action
   if gui_widget(index).gui_action = 1 and gui_widget(index).gui_type = GUI_BUTTON
      Rem Exit the piece of gay
      if gui_widget(index).gui_extra = "EXIT" then end
      Rem Reset the action
      gui_widget(index).gui_action = 0
   endif
 
endfunction
 
Rem Well I'm finished and off for a coffee. You're welcome to join me if you so wish, or just bask in the 133bness of this code O_o