Sunday, 22 April 2012

Object Description

To add an object description, we need to add more to each object line in the data file standard.txt.  This will go at the end, after the icon number.

In order to find the end of the icon number and the start of the description, another delimiter needs to be used.

For this I have chosen the colon (:), which would not occur in either the name or the icon value.

With some mildly humorous sample information added, the file would look something like this.
tool,-1
resource,-2
device,-3
job,-4
process,-5
storage,-6
tree,7:Home to Birds and Monkeys
stone,8:Small hard piece of planet
stone axe,9:Primative chopping device
log,10:Rough tree part, might burn well
kindling,11:Basis of a good fire
spark,12:Warning - fire Hazard
flame,13:Fire's younger sibling
fire,14:Good source of heat and light
This would still work with the existing code as the Val() command would stop evaluating the number at the colon and the same value.

To use the description, we need another field in MainObjectType.
type MainObjectType
   name$
   detail$
   icon
   isKnown
   isGroup
endtype
This time, rather than using the obvious name description, I've gone for the more ambiguous detail.  This is to allow it to be used for groups, where it will have a different purpose.

The field needs a return function.
function ObjectDetail( thisObject )
if ObjectValid( thisObject )
   thisString$ = MainObject [ thisObject ].detail$
else
   thisString$ = ""
endif
endfunction thisString$
Which is very similar to the ObjectName() function, which could be copied and changed accordingly.

The first step in converting the data is finding the delimiter - the colon, which we do at the same time we find the comma, by adding the following immediately after.
thisColon = FindSubString( thisString$ , ":" , thisComma + 1 )
This performs a similar search function, but starts after the found comma position so that the search doesn't need to recheck the string up to that point.

If the comma was not found - heaven forbid, thisComma will be zero and adding 1 will still be a valid start point for this search.

Within the If block, just after the name is assigned to the array, we now add the following.
if thisColon > 0
   MainObject[ i ].detail$ = right( thisString$ , thisLen - thisColon )
   thisLen = thisColon - 1
   thisString$ = left( thisString$ , thisLen)
else
   MainObject[ i ].detail$ = ""
endif
If a colon was found, this transfers the string after it to the the detail$ field.

Then is sets thisLen to be the position before the colon and chops the string down to that size.

This makes the string the same as it would have been without the colon and description, so the rest of the routine works unaltered.

If the colon was not found, the field is set to an empty string.

In the test code and the main loop, the line.
print( ", " + chr(34) + "" + chr( 34 ))
Is now changed to include the description field.
print( ", " + chr(34) + ObjectDetail( i ) + chr( 34 ))
And can be tested.
The LoadObjectData() function currently looks like this.
function LoadObjectData( thisName$ )
   // Pass the name without extension
   thisTextName$ = thisName$ + ".txt"
   thisImageName$ = FindImageFile( thisName$ )
   if getFileExists( thisTextName$ ) and thisImageName$ <> ""
      // Both data files exist
      dim tempArray[ 1000 ] as string
      thisArraySize = 0
      thisFile = openToRead( thisTextName$ )
      thisString$ = readLine( thisFile )
      repeat
         if thisString$ <> ""
            inc thisArraySize , 1
            tempArray[ thisArraySize ]  = thisString$
         endif
         thisString$ = readLine( thisFile )
      until fileEof( thisFile ) > 0
      closefile( thisFile )
      // Convert Data to Array
      game.topObject = thisArraySize
      dim MainObject[ topObject() ] as MainObjectType
      for i=1 to topObject()
         thisString$ = tempArray[ i ]
         thisLen = len( thisString$ )
         thisComma = FindSubString( thisString$ , "," , 1 )
         thisColon = FindSubString( thisString$ , ":" , thisComma + 1 )
         if thisComma > 0
            MainObject[ i ].name$ = left( thisString$ , thisComma - 1)
            if thisColon > 0
               MainObject[ i ].detail$ = right( thisString$ , thisLen - thisColon )
               thisLen = thisColon - 1
               thisString$ = left( thisString$ , thisLen)
            else
               MainObject[ i ].detail$ = ""
            endif
            thisString$ = right( thisString$ , thisLen - thisComma )
            thisValue = val( thisString$ )
            MainObject[ i ].icon = abs( thisValue )
            MainObject[ i ].isKnown = 0
            if thisValue < 0
               MainObject[ i ].isGroup = 1
            else
               MainObject[ i ].isGroup = 0
            endif
         endif
      next i



      dim tempArray[ 0 ]
   endif
endfunction thisArraySize

No comments:

Post a Comment