TGC Codebase Backup



BSP Entities DLL Example by Ideajuice

14th May 2004 14:34
Summary

An example of using the BSP Entities DLL to parse information from a BSP file



Description

Creates spheres and cones representing the lights found in the BSP file sized to match the intensity of the light and colored to match the color. Also dumps out a formatted list of all the entitites found to a text file so you can look at it an add new entity types that you didn't know about.

Contains some useful helper functions to retreive attributes based on key name, and to parse out floats into correctly scaled numbers from the strings returned by the DLL.

This was the first program I played with when I got DB, so there are some things in here I would now do differently.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    global cam_obj
cam_obj = 500

make object sphere cam_obj, 2
hide object cam_obj
position object cam_obj,0,15,0
rotate object cam_obj,0,-90,0

bspfile$ = "casbah.bsp"
global bsp_scale#=(7.5/256.0)

sync on : sync rate 100
backdrop off : hide mouse

rem Select font
set text font "arial" : set text size 16
set text to bold : set text transparent

sync : center text screen width()/2,screen height()/2,str$(screen width())+" "+str$(screen height()) : sync

rem Set camera
set camera range 0.1,500
position camera -10,5,2
rotate camera 0,135,0

global spheres = 0
global ostart = 0
global psx# = 0
global psy# = 0
global psz# = 0
global psya# = 90.

set text size 16
gosub _load_game
gosub _setup_game

set normalization on
MainLight = 1
make light MainLight
color light MainLight, 50,100,200
set light range MainLight, 40
set point light MainLight, 26.,5.,-32.
show light MainLight
set ambient light 5

restart = 1
set text font "arial" : set text size 16

angle = 0
last = timer()
rate = 50
when = last
cur = 0

dim events(20)
dim objects(20)

now = timer()
rem set point light 0,10,30,10
color light 0,100,90,40
show light 0

d# = 10.
inc# = 0.25

global hit = 0

set text font "arial" : set text size 24
ink rgb(255,255,255),0

set bsp camera collision 1,0,10,0
set bsp object collision 1,cam_obj,1,1
set bsp camera 0

sync on
camera = 0
color backdrop camera, rgb(255,0,255)
backdrop on 0
when = now + 1000
set = 1
ping = 0

i = 0
do
   rem -------------------------------------------------------
   now = timer()
   millis = now - last

   rem Control game elements
   gosub _control_player
   gosub _control_stats

   last = timer()

   camx# = object position x(cam_obj) : camy# = object position y(cam_obj) : camz# = object position z(cam_obj)
   camrx# = object angle x(cam_obj)   : camry# = object angle y(cam_obj)   : camrz# = object angle z(cam_obj)
   position camera camera, camx#, camy#, camz#
   rotate camera camera, camrx#, camry#, camrz#
   sync
loop
end

_control_player:

  rotate object cam_obj,object angle x(cam_obj)+(mousemovey()/2.0),object angle y(cam_obj)+(mousemovex()/2.0),0

  acc# = .15
  cx#=object angle x(cam_obj) : cy#=object angle y(cam_obj)
 if upkey()=1 then move object cam_obj, acc#
 if downkey()=1 then move object cam_obj, -acc#
 if leftkey()=1 then yrotate object cam_obj,cy#-90 : move object cam_obj, acc# : yrotate object cam_obj,cy#
 if rightkey()=1 then yrotate object cam_obj,cy#+90 : move object cam_obj,acc# : yrotate object cam_obj,cy#

 if wrapvalue(object angle x(cam_obj))>180 and wrapvalue(object angle x(cam_obj))<280 then xrotate object cam_obj,280
 if wrapvalue(object angle x(cam_obj))>180 and wrapvalue(object angle x(cam_obj))<280 then xrotate object cam_obj,280

rem In case of restart
if restart=1
   restart=0
   rotate object cam_obj,0,psya#,0
   position object cam_obj,psx#,psy#,psz#
endif

return

_control_stats:
   fps$="FPS: "+str$(screen fps())
   text screen width()-20-text width(fps$),screen height()-40,fps$

   x = camera position x(0);
   y = camera position y(0);
   z = camera position z(0);

   fps$="POS: "+str$(x)
   fps$ = fps$ + " " + str$(y)
   fps$ = fps$ + " " + str$(z)
   text screen width()-20-text width(fps$),screen height()-60,fps$

   xr = camera angle x(0)
   yr = camera angle y(0)
   zr = camera angle z(0)
   fps$="ROT: "+str$(xr)
   fps$ = fps$ + " " + str$(yr)
   fps$ = fps$ + " " + str$(zr)
   text screen width()-20-text width(fps$),screen height()-80,fps$

   fps$="FRAMES: "+str$(frames)+" "+str$(frames2)+" "+str$(i)
   text screen width()-20-text width(fps$),screen height()-100,fps$
   fps$="MODE: "+str$(j)+" "+str$(k)
   text screen width()-20-text width(fps$),screen height()-120,fps$
return

_setup_game:
 set camera range 0.1,5000
 autocam off
 restart=1
return

_load_game:
 rem Load BSP world and sky model
 load bsp "","casbah.bsp"
 set bsp multitexturing on

 parse_entities("casbah.bsp")
return

function parse_int(which, string$)
   retvalue = 0
   s = len(string$)
   cur = 0
   n = 1
   while cur <> which
      rem find next space
      while n <= s and mid$(string$,n) <> " "
         n = n + 1
      endwhile
      rem inc past it
      while n <= s and mid$(string$,n) = " "
         n = n + 1
      endwhile
      if n > s
         exitfunction retvalue
      endif

      cur = cur + 1
   endwhile

   retvalue = val(right$(string$,s-n+1))
endfunction retvalue

function parse_float(which , string$)
   bsp_scale#=(7.5/256.0)
   retvalue# = 0.
   s = len(string$)
   cur = 0
   n = 1
   while cur <> which
      while n <= s and mid$(string$,n) <> " "
         n = n + 1
      endwhile
      while n <= s and mid$(string$,n) = " "
         n = n + 1
      endwhile
      if n > s
         exitfunction retvalue#
      endif
      cur = cur + 1
   endwhile

   retvalue# = bsp_scale# * val(right$(string$,s-n+1))
endfunction retvalue#

function key_value(which, query$)
   result$ = ""
   for i = 0 to bsp entity attribute count(which)-1
      if bsp entity attribute name$(which, i) = query$ and len(bsp entity attribute name$(which, i)) = len(query$)
         result$ = bsp entity attribute value$(which,i)
         exitfunction result$
      endif
   next i
endfunction result$


function parse_entities(file$)

   bsp_scale#=(7.5/256.0)

   load bsp entities "casbah.bsp"

   foundflag = 0
   foundflag2 = 0
   foundflag3 = 0
   foundflag4 = 0

   global spheres = 0
   global ostart = 0
   global psx# = 0
   global psy# = 0
   global psz# = 0

   ballcolor$=""
   ballorigin$=""
   light$ = ""
   wave$=""
   wav$=""
   angle$=""

   ink rgb(r,g,b),0
   ink rgb(255,255,255),0
   set text size 11
   if file exist("entities.txt") then delete file "entities.txt"
   open to write 1, "entities.txt"

   ostart = 1
   simage = 1
   spheres = ostart

   for i = 0 to bsp entity count()-1
      rem for each entity, there are several attributes - we need to check each one

      class$ = key_value(i, "classname")
      origin$ = key_value(i, "origin")

      if class$ <> "" and origin$ <> ""
         rem start
         light$ = ""
         newobj = 1
         x# = parse_float(0, origin$)
         z# = parse_float(1, origin$)
         y# = parse_float(2, origin$)

         ostring$ = class$ + "@" + origin$
         write string 1,ostring$

   rem output a list of all entities to a file
   for q = 0 to bsp entity attribute count(i)-1
      n$ = bsp entity attribute name$(i, q)
      v$ = bsp entity attribute value$(i,q)
      ostring$ = "    " + n$ + " " + v$
      write string 1,ostring$
   next i

         select class$
            case "light" : rem get an origin, and a color attribute
               lstring$ = "L"
               light$ = key_value(i, "_light")
            endcase
            case "light_spot"
               lstring$ = "S"
               light$ = key_value(i, "_light")
               pitch$ = key_value(i, "pitch")
               icone$ = key_value(i, "_cone")
               ocone$ = key_value(i, "_cone2")
               angles$ = key_value(i, "angles")
               target$ = key_value(i, "target")
            endcase
            case "env_glow"
               lstring$ = "G"
               light$ = key_value(i, "rendercolor")
               light$ = light$ + " " + key_value(i, "renderamt")
            endcase
            case "env_sprite"
               lstring$ = "F"
               light$ = key_value(i, "rendercolor")
               light$ = light$ + " " + key_value(i, "renderamt")
            endcase
            case "ambient_generic"
               lstring$ = "A"
            endcase
            case "func_rotating"
               lstring$ = "R"
               light$ = key_value(i, "rendercolor")
               light$ = light$ + " 200"
            endcase
            case "path_corner"
               lstring$ = "P"
            endcase
            case "env_sound"
               lstring$ = "N"
            endcase
            case "info_player_start"
               lstring$ = "*"
               angles$ = key_value(i, "angles")
               newobj = 0
            endcase
            case "info_player_teamspawn"
               lstring$ = "*"
               angles$ = key_value(i, "angles")
               newobj = 0
            endcase
            case "info_target"
               lstring$ = "T"
               tname$ = key_value(i, "targetname")
            endcase
         endselect

         if light$ <> ""
            r = parse_int(0, light$)
            g = parse_int(1, light$)
            b = parse_int(2, light$)
            d = parse_int(3, light$)
         else
            r = 255
            g = 255
            b = 255
            d = 400
         endif

         if newobj

         select lstring$
            case "L"
               make object sphere spheres,(16.0*bsp_scale#*d)/100
            endcase
            case "S"
               make object cone spheres,(32.0*bsp_scale#*d)/100
            endcase
            case "G"
               make object cone spheres,(16.0*bsp_scale#*d)/100
            endcase
            case "F"
               make object cone spheres,(16.0*bsp_scale#*d)/100
            endcase
            case "A"
               make object cone spheres,(16.0*bsp_scale#*d)/100
            endcase
            case "R"
               make object cone spheres,(16.0*bsp_scale#*d)/100
            endcase
            case "P"
               make object cone spheres,(16.0*bsp_scale#*d)/100
            endcase
            case "N"
               make object cone spheres,(16.0*bsp_scale#*d)/100
            endcase
            case "*"
               make object cone spheres,(16.0*bsp_scale#*d)/100
            endcase
            case default
               make object cone spheres,(16.0*bsp_scale#*d)/100
            endcase
         endselect

            img = simage
            simage = simage + 1
            position object spheres,x#,y#,z#

         ink rgb(0,0,0),0
         box 0,0,screen width()-1,screen height()-1
         ink rgb(255,255,255),0
         ostring$ = class$+" "+origin$+" "+str$(spheres)+" "+str$(img)+" "+str$(x)+" "+str$(y)+" "+str$(z)+" "+str$(int(x#))+" "+str$(int(y#))+" "+str$(int(z#))
         center text screen width()/2,screen height()/2,ostring$ : sync

            ink rgb(r,g,b),0
            box 0,0,128,128
            ink rgb(255-r,255-g,255-b),0
            set text size 32
            text 0,64,lstring$+str$(spheres)

            get image img,0,0,128,128,1
            texture object spheres,img
            set alpha mapping on spheres,50
            set object transparency spheres,1
            spheres = spheres + 1
         endif

         if lstring$ = "*"
            psx# = x#
            psz# = z#
            psy# = y#
rem            psya# = parse_float(3, origin$)

            psya# = parse_int(1, angles$)
         endif
      endif
   next i

   close file 1
   spheres = Free 3DObject(spheres)

   ostring$ = str$(spheres)
   cls:center text screen width()/2,screen height()/2,ostring$ : sync

endfunction