Sync On:Sync Rate 60:Autocam OFf
Type Coordinates
   x as FLoat
   y as Float
   z as Float
Endtype
Global Object as Coordinates
Global Target as Coordinates
 
#Constant MaxRU = 20
 
Type RUnit
   Energy as Integer
   Resource as Integer
   WayX as Float
   WayZ as Float
   ObjectID as DWord
EndType
Dim R2(MaxRU) as RUnit
 
 
Position Camera 250,325,250
Pitch Camera Down 90
 
Make Matrix 1,500,500,20,20
 
 
New_R2(250,2.5,250)
New_Energy()
 
KD=0
Money=200
Do
   For i = 0 To MaxRU
      If R2(i).ObjectID>0
         Head_To_Way(i)
      Endif
   Next i
 
   If KD<1 And Lower$(Inkey$())="e" And Money>0
      New_Energy()
      KD=20
      Money=Money-10
   Endif
   If KD<1 And Lower$(Inkey$())="r" And Money>0
      New_Resource(-1,-1,-1)
      KD=20
      Money=Money-10
   Endif
   If KD<1 And Lower$(Inkey$())="s"
      Flag=0:KD=20
      For i=1 to 20
         RU=Rnd(20)
         If R2(RU).ObjectID>0
            Flag=1
            Delete Object R2(RU).ObjectID
            R2(RU).ObjectID=0
            Money=Money+100
            Exit
         Endif
      Next i
      If Flag=0 Then Sold$="No Buyers." Else Sold$="Sold!!!"
   Endif
   If KD>0
      KD=KD-1
      Clear Entry Buffer
      If Sold$<>""
         Set Cursor 0,20
         Print Sold$
      Endif
   Else
      Sold$=""
   Endif
   Set Cursor 0,0:Print "Money: ";Money
   Sync
Loop
 
Function New_Energy()
   For i = 101 to 199
      If Object Exist(i)=0 Then Exit
   Next i
   If i >199 then ExitFunction
   Make Object Sphere i,5
   Do
      Position Object i,rnd(490)+5,2.5,rnd(490)+5
      If Object Collision (i,0)=0 Then Exit
   Loop
EndFunction
 
Function New_Resource(x#,y#,z#)
   For i = 201 to 299
      If Object Exist(i)=0 Then Exit
   Next i
   If i >299 then ExitFunction
   Make Object Cube i,5
   If y#>-1
      Position Object i,x#,y#,z#
   Else
      Do
         Position Object i,rnd(490)+5,2.5,rnd(490)+5
         If Object Collision (i,0)=0 Then Exit
      Loop
   Endif
EndFunction
 
Function Energy_Look(RU)
   ObjectID=R2(RU).ObjectID
   If Object Exist(2) Then Delete Object 2
   Make Object Sphere 2,1
   Return_Object(ObjectID)
   Position Object 2,Object.x,Object.y,Object.z
   Scale Object 2,10000,10000,10000
   `For i = 1 to 200
   `   x=rnd(screen width()):y=rnd(screen height())
   `   if Pick Object(x,y,2,2)=2 Then Dot x,y
   `Next i
   ec=Object Collision(2,0)
   If ec>100 and ec<200
      Return_Object(ec)
      R2(RU).WayX=Object.x:R2(RU).WayZ=Object.z
   Endif
   Delete Object 2
Endfunction
 
Function Resource_Look(RU)
   ObjectID=R2(RU).ObjectID
   If Object Exist(2) Then Delete Object 2
   Make Object Sphere 2,1
   Return_Object(ObjectID)
   Position Object 2,Object.x,Object.y,Object.z
   Scale Object 2,10000,10000,10000
   `For i = 1 to 100
   `   x=rnd(screen width()):y=rnd(screen height())
   `   if Pick Object(x,y,2,2)=2 Then Dot x,y
   `Next i
   ec=Object Collision(2,0)
   If ec>200 and ec<300
      Return_Object(ec)
      R2(RU).WayX=Object.x:R2(RU).WayZ=Object.z
   Endif
   Delete Object 2
Endfunction
 
Function New_Way(RU)
   If Object Exist(2) Then Delete Object 2
   ObjectID=R2(RU).ObjectID
   Make Object Sphere 2,1
   Scale Object 2,2000,2000,2000
   Do
      Position Object 2,rnd(5000.0)/10.0,2.5,rnd(5000.0)/10.0
      If Object Collision(2,ObjectID)=0 Then Exit
   Loop
   Return_Target(2)
   R2(RU).WayX=Target.x
   R2(RU).WayZ=Target.z
EndFunction
 
Function Head_To_Way(RU)
   If R2(RU).Energy <1
      Return_Object(R2(RU).ObjectID)
      New_Resource(Object.x,Object.y,Object.z)
      Delete Object R2(RU).ObjectID
      R2(RU).ObjectID=0
      ExitFunction
   Endif
   If R2(RU).WayX=0 Then New_Way(RU)
   IF R2(RU).Energy<2500 Then Energy_Look(RU) Else Resource_Look(RU)
   If Object Exist(2) Then Delete Object 2
   ObjectID=R2(RU).ObjectID
   Make Object Sphere 2,1
   Return_Object(ObjectID)
   Position Object 2,Object.x,Object.y,Object.z
   Set Object To Object Orientation 2,ObjectID
   Move Object 2,10
   Point Object 2,R2(RU).WayX,Object.y,R2(RU).WayZ
   Move Object 2,2
   Return_Target(2)
   Point Object ObjectID,Target.x,Object.y,Target.z
   If rnd(20)=1 then Turn Object Right ObjectID,rnd(2)-1
   Move Object ObjectID,1
   Delete Object 2
   R2(RU).Energy=R2(RU).Energy-1
   R2_Collide(RU)
   Return_Object(ObjectID)
   If R2(RU).Resource>499 Then New_R2(Object.x,Object.y,Object.z):R2(RU).Resource=0
   dx#=(Object.x-R2(RU).WayX):dz#=(Object.z-R2(RU).WayZ)
   d#=dx#*dx#+dz#*dz#
   if d#<25 then New_Way(RU)
EndFunction
 
Function R2_Collide(RU)
   ObjectID=R2(RU).ObjectID
   OC=Object Collision(ObjectID,0)
   If OC>100 and OC<200
      Delete Object OC
      R2(RU).Energy=R2(RU).Energy+1000
   Endif
   If OC>200 and OC<300
      Delete Object OC
      R2(RU).Resource=R2(RU).Resource+100
   Endif
Endfunction
 
Function New_R2(x#,y#,z#)
Flag=0
For i=0 to MaxRU
   If R2(i).ObjectID=0
      Flag=1
      Exit
   Endif
Next  i
If Flag = 0 then ExitFunction
R2(i).ObjectID=10+i
Make_R2(10+i)
Position Object 10+i,x#,y#,z#
R2(i).Energy=5000
EndFunction
 
Function Make_R2(ObjectID)
If Object Exist(ObjectID) Then Delete Object ObjectID
If Object Exist(2) Then Delete Object 2
 
`Arms
Make Object Sphere 2,2
Make Mesh From Object 2,2
Delete Object 2
Make Object Cylinder 2,2
Scale Limb 2,0,100,50,100
Add limb 2,1,2
Add Limb 2,2,2
Scale Limb 2,1,100,1,100
Scale Limb 2,2,100,1,100
Offset Limb 2,1,0,.5,0
Offset Limb 2,2,0,-.5,0
Pitch Object Up 2,90
Fix Object Pivot 2
Make Mesh From Object 2,2
Delete Object 2
Make Object 2,2,0
Make Object Box ObjectID,2,5,1
Make Mesh From Object ObjectID,ObjectID
Delete Object ObjectID
Add Limb 2,1,ObjectID
Offset Limb 2,1,0,-2.5,0
Turn Object Right 2,90
Fix Object Pivot 2
Make Mesh From Object 3,2
Delete Object 2
 
`Body
Make Object Sphere ObjectID,5
Make Mesh From Object ObjectID,ObjectID
Delete Object ObjectID
Make Object Cylinder ObjectID,5
Add Limb ObjectID,1,ObjectID
Offset Limb ObjectID,1,0,2.5,0
 
`Leg
Make Object Box 2,1,3,1.5
Make Mesh From Object 4,2
Delete Object 2
 
`Feet
Make Object Box 2,1.5,.8,2.5
Make Mesh From Object 2,2
Delete Object 2
 
 
Add Limb ObjectID,2,4
Offset Limb ObjectID,2,0,-3,1.6
Add Limb ObjectID,3,2
Link Limb ObjectID,2,3
Offset Limb ObjectID,3,0,-1.5,0
 
Add Limb ObjectID,4,3
Offset Limb ObjectID,4,2.8,1,-.2
Add Limb ObjectID,5,2
Link Limb ObjectID,4,5
Offset Limb ObjectID,5,0,-5,0
 
Add Limb ObjectID,6,3
Offset Limb ObjectID,6,-2.8,1,-.2
Add Limb ObjectID,7,2
Link Limb ObjectID,6,7
Offset Limb ObjectID,7,0,-5,0
 
Rotate Limb ObjectID,4,30,0,0
Rotate Limb ObjectID,6,30,0,0
Rotate Limb ObjectID,3,15,0,0
Rotate Limb ObjectID,5,-15,0,0
Rotate Limb ObjectID,7,-15,0,0
 
Pitch Object Up ObjectID,15
Fix Object Pivot ObjectID
EndFunction
 
Function Return_Object(ObjectID)
Object.x=Object Position X(ObjectID):Object.y=Object Position Y(ObjectID):Object.z=Object Position Z(ObjectID)
EndFunction
 
Function Return_Target(TargetID)
Target.x=Object Position X(TargetID):Target.y=Object Position Y(TargetID):Target.z=Object Position Z(TargetID)
EndFunction