Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random crash
#1
ok, this is code for a game im making, just trying to write a little scripting thingo.. i cant figure out why its crashing, it runs for about 3 or 4 mins TOTALLY normally and then randomly crashes... the code is massive... here you can run it and see:


(everything i write is an fsm, i think ;p)


Code:
dim shared inputlock as byte
dim shared moverate
dim shared threadnode

TYPE RoomHeader FIELD=1

  dimensions AS ushort
  numofdoors AS UBYTE
  numoflayers as ubyte
  doors as integer ptr
  layout as ushort ptr ptr
  numofenemies as byte
  enemyids as ushort ptr

END TYPE

type maptype field=1

  numofrooms AS UBYTE
  tilesetname AS string * 12
  rooms as roomheader ptr
  numoftiles as short
  tilesizex as ubyte
  tilesizey as ubyte
  tileset as short ptr

end type

TYPE PalType FIELD=1

  b AS ubyte
  g AS ubyte
  r AS ubyte
  pad as ubyte
  
END TYPE

TYPE SpriteFrame FIELD=1

  max AS short
  rate AS short
  current AS short

END TYPE

TYPE ThreadType FIELD=1

  max AS short
  node AS short

END TYPE

type scripttype field=1
    
  comflag as byte
  comloc as ushort
  lft2wlk as byte
  wlklgth as byte
  wait as double
  
end type
  

type chartype field=1

  x as ushort
  y as ushort
  direction as byte
  movedflag as byte
  stillmoving as byte
  arraysize as ushort
  frame as spriteframe
  currentroom as byte
  tilesizex as ubyte
  tilesizey as ubyte
  tileset as short ptr
  states as byte
  script as string'ubyte ptr
  scriptcontrol as scripttype
end type





SUB LoadSprite (FileName$, sprite AS short ptr, spritepal() AS PalType, numberoftiles%)


  DIM howmuchhere AS ubyte

  OPEN FileName$ FOR BINARY AS #1
                          '((((16 * 16) \ 2) + 2) * numberoftiles%) - 1
    FOR grab2bytes% = 0 TO ((((16 * 24) \ 2) + 2) * numberoftiles%) - 1
      GET #1, , sprite[grab2bytes%]

    NEXT

    FOR grab4bytes% = 0 TO 255
      GET #1, , spritepal(grab4bytes%)

    NEXT

  CLOSE


END SUB

SUB PutPalette (Pal() AS PalType)


  DIM rgbhold&(255)

  FOR colorconvert = 0 TO 255
    rgbhold&(colorconvert) = (Pal(colorconvert).r \ 4) OR (( Pal(colorconvert).g \ 4) SHL 8)  OR ( (Pal(colorconvert).b \ 4)  SHL 16)

  NEXT

  PALETTE USING rgbhold&(0)


END SUB

function rotate45 (torotate%, direction as byte)


  select case torotate%
    
    case is < 0
      tempdirection% = direction - torotate%
      if tempdirection < 0 then rotate45 = ((5 * ((abs(torotate%) \ 4) + 1) + torotate%)) mod 4
        
    case is > 0
      rotate45 = (direction + torotate%) mod 4

    end select


end function


function randomwalk (avgwalk%, rndmdftl%)
' ------------------------------------------------------------    randomwalk = avgwalk% +- (rndmdftl% \ 2)


'gennmbr% = int(rnd * (rndmdftl% + 1))'---------------------------------------    plus one factors in zero.

span% = rndmdftl% * 2'-------------------------------------------    the diameter of possible walk lengths.

randomwalk = avgwalk% - rndmdftl% + int(rnd * (span% + 1))'--------------------    plus one factors in zero.


end function


sub moveobject (objectx as ushort, objecty as ushort, direction as byte)
    
    select case direction
        
    case 0
        if objecty > 0 then objecty -= 1
    case 1
        if objectx < 319 - 16 then objectx += 1
    case 2
        if objecty < 199 - 16 then objecty += 1
    case 3
        if objectx > 0 then objectx -= 1
        
    end select
    
end sub


function runscript (char as chartype ptr, whichchar%)


  if not char[whichchar%].scriptcontrol.comflag then

    select case mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 1)

      case "l", "u"
        char[whichchar%].scriptcontrol.comloc = char[whichchar%].scriptcontrol.comloc + 1
        
        if mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 1) = "k" then'- command "lk" or "uk" -> "lock" or "unlock", respectively
          char[whichchar%].scriptcontrol.comloc = char[whichchar%].scriptcontrol.comloc + 1                '-                                         (controls input)

          inputlock = not inputlock

        else
  
        end if

    
      case "w"
        char[whichchar%].scriptcontrol.comloc = char[whichchar%].scriptcontrol.comloc + 1

        select case mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 1)

          case "t"'----------------    command "wt" -> "wait" (waits for specified number of milliseconds)
            char[whichchar%].scriptcontrol.comloc = char[whichchar%].scriptcontrol.comloc + 1

            waitlength$ = mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 4)
            char[whichchar%].scriptcontrol.comloc = char[whichchar%].scriptcontrol.comloc + 4

            waitnumber# = val(waitlength$) / 1000
            char[whichchar%].scriptcontrol.wait = waitnumber# + timer
  
            char[whichchar%].scriptcontrol.comflag = not 0
    
          case "k"'---------------------------    command "wk -> "walk" (reads (or generates) walk length)
            char[whichchar%].scriptcontrol.comloc = char[whichchar%].scriptcontrol.comloc + 1

            select case mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 3)
              
              case "rnd"'-----------------------------                       sub-command "rnd" -> "random"
                char[whichchar%].scriptcontrol.comloc += 3'----------------    (reads average walking length, and differential)
          
                avg_walk$ = mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 2)
                char[whichchar%].scriptcontrol.comloc += 2
          
                avgwalk% = val(avg_walk$)
          
                rndm_dftl$ = mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 2)
                char[whichchar%].scriptcontrol.comloc += 2
          
                rndmdftl% = val(rndm_dftl$)

                char[whichchar%].scriptcontrol.wlklgth = randomwalk(avgwalk%, rndmdftl%)

                char[whichchar%].scriptcontrol.comflag = not 0
          
              case else
          
                walk$ = mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 2)
                char[whichchar%].scriptcontrol.comloc += 2
          
                char[whichchar%].scriptcontrol.wlklgth = val(walk$)

                char[whichchar%].scriptcontrol.comflag = not 0
          
            end select

        end select

      case "r"
        char[whichchar%].scriptcontrol.comloc += 1

          select case mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 1)
              
            case "t"'-------------    command "rt" -> "rotate" (reads the number of rotations and rotates)
              char[whichchar%].scriptcontrol.comloc += 1
          
              spins$ = mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 1)
              char[whichchar%].scriptcontrol.comloc += 1

              char[whichchar%].direction = rotate45 (val(spins$), char[whichchar%].direction)

          end select
              

      case "t"
        char[whichchar%].scriptcontrol.comloc += 1

        select case mid$(char[whichchar%].script, char[whichchar%].scriptcontrol.comloc, 1)
              
          case "r"'-----    command "tr" -> "turn random" (turn a random direction, excepting 180 degrees)
            char[whichchar%].scriptcontrol.comloc += 1
          
            char[whichchar%].direction = rotate45((int(rnd * 3)) + 3, char[whichchar%].direction)
          
        end select

      case else
        char[whichchar%].scriptcontrol.comloc += 1
      
    end select

  end if


'--------------    on condition of command, unset commandflag. in the mean time, this is a virtual engine.

  if char[whichchar%].scriptcontrol.wait <> 0 then

    if char[whichchar%].scriptcontrol.wait - timer <= 0 then
    char[whichchar%].scriptcontrol.comflag = 0
    char[whichchar%].scriptcontrol.wait = 0
    
    cls

    end if

  end if

  if char[whichchar%].scriptcontrol.wlklgth <> 0 then
    if char[whichchar%].scriptcontrol.lft2wlk = 0 then char[whichchar%].scriptcontrol.lft2wlk = char[whichchar%].scriptcontrol.wlklgth
    if threadnode mod moverate = 0 then
        moveobject(char[whichchar%].x, char[whichchar%].y, char[whichchar%].direction)
  
        IF threadnode MOD char[whichchar%].frame.rate = 0 THEN

            char[whichchar%].frame.current = char[whichchar%].frame.current + 1
            IF char[whichchar%].frame.current = char[whichchar%].frame.max THEN char[whichchar%].frame.current = 0
      
        END IF

        char[whichchar%].scriptcontrol.lft2wlk = char[whichchar%].scriptcontrol.lft2wlk - 1
        
        end if

    if char[whichchar%].scriptcontrol.lft2wlk = 0 then char[whichchar%].scriptcontrol.comflag = 0: char[whichchar%].scriptcontrol.wlklgth = 0: IF char[whichchar%].frame.current <> 0 THEN char[whichchar%].frame.current = 0
'? mid$(script$, comloc%, 1):sleep
  
  end if

  if char[whichchar%].scriptcontrol.comloc = len(char[whichchar%].script) + 2 then runscript = not 0: char[whichchar%].scriptcontrol.comloc = 1': char[whichchar%]


end function

const numoffucks  = 25

dim pal(255) as paltype
dim char as chartype ptr
char = callocate(len(chartype) * numoffucks)

script$ += "0043"
script$ += "wkrnd3025"
script$ += "wt2000"
script$ += "rt1"
script$ += "wt1000"
script$ += "rt2"
script$ += "wt2000"
script$ += "rt1"
script$ += "tr"

for loade = 0 to numoffucks - 1

char[loade].x = int(rnd * 304)
char[loade].y = int(rnd * 184)
char[loade].direction = int(rnd * 4)

char[loade].scriptcontrol.comloc = 1
char[loade].movedflag = 0
char[loade].stillmoving = 0

char[loade].frame.max = 8
char[loade].frame.rate = 10
char[loade].frame.current = 0

char[loade].currentroom = 0

char[loade].tilesizex = 16
char[loade].tilesizey = 24

char[loade].arraysize = 194

char[loade].tileset = callocate(char[loade].arraysize * len(short) * char[loade].frame.max * 4)

LoadSprite "pics\lynnn.spr", char[loade].tileset, pal(), 32

char[loade].states = 1

char[loade].script = script$



next

x% = 160
y% = 100



moverate = 5

screen 13,,2

putpalette pal()

screenset 0,1

randomize timer

do
  for doe = 0 to numoffucks - 1
  unt = runscript(char, doe)
  next
  for doe = 0 to numoffucks -1
'  put (char[doe].x, char[doe].y - 8), @char->tileset[char[doe].direction * (char[doe].frame.max * 194)], trans
  put ((char[doe].x), char[doe].y - 8), @char->tileset[(char[doe].direction * (char[doe].frame.max * 194) + char[doe].arraysize * char[doe].frame.current)], trans

  next

'  if inkey$ <> "" and not inputlock then locate 7:print "hehe not locked..."
  locate 8:? threadnode
  locate 9:? char[0].scriptcontrol.wlklgth, char[1].scriptcontrol.wlklgth, char[2].scriptcontrol.wlklgth,
  locate 10:? "y:"; char[0].y
  locate 11:? "direction:"; char[0].direction
  locate 12:? "commandflag:"; char[0].scriptcontrol.comflag
  locate 13:? "last char:"; mid$(char[0].script, char[0].scriptcontrol.comloc, 1)
  locate 14:? "till time:"; char[0].scriptcontrol.wait
  locate 15:? "left to inc:"; char[0].scriptcontrol.lft2wlk
  if char[0].scriptcontrol.wait > 0 then locate 16:? "time left:"; char[0].scriptcontrol.wait - timer
  locate 17:? "command loc:"; char[0].scriptcontrol.comloc
screencopy

  if threadnode mod 500 = 0 then cls

  threadnode = threadnode + 1
  if threadnode = 5000 then threadnode = 0

t# = timer:do:loop until timer - t# > .001
cls

loop until multikey(1)

? "script executed successfully!!"
screencopy
sleep: end
Reply
#2
i found the problem. i believe the problem was that i had too many pointers in one expression. it worked fine when i had

Code:
put (char[doe].x, char[doe].y - 8), @char->tileset[char[doe].direction * (char[doe].frame.max * 194)], trans

thats 3 pointer references. now when i add

Code:
put ((char[doe].x), char[doe].y - 8), @char->tileset[(char[doe].direction * (char[doe].frame.max * 194) + char[doe].arraysize * char[doe].frame.current)], trans

6 references, it crashes quite fast. the solution was:

Code:
directionframe = char[doe].direction * char[doe].frame.max * char[doe].arraysize
  walkframe = char[doe].arraysize * char[doe].frame.current
  frameindex = directionframe + walkframe
  put (char[doe].x, char[doe].y - 8), @char[0].tileset[frameindex], trans


anywho yeah sorry to post such a huge ass piece of code but maybe someone will find something in it they like who knows =P
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)