Archive

Posts Tagged ‘Games’

Custom Scripting Language for Items

June 20th, 2009 No comments

I’ve been working on some new software.. Below is a prototype version of my proprietary itemScript parser, written in PHP. The goal is to have a pseudo-scripting language available for game-items that can be read dynamically by the server, so that new items can be added without additional hard coding.

function scriptToArr($str) {
   # Parse for Whitespace
   $str = preg_replace("#(\r\n|\r|\n|\t|\s)#","",$str);
 
   # Event Handlers
   preg_match_all("#(.*?)\{(.*?)\}(,|)#is",$str,$findEventHandlers);
 
   $arrEvents = array();
   for($j=0;$j<sizeof($findEventHandlers[1]);$j++) {
      # this event
      $thisEvent = $findEventHandlers[1][$j];
 
      # these functions
      $getFunctions = $findEventHandlers[2][$j];
      $allFunctions = explode(";",$getFunctions);
 
      # get all parameters
      foreach($allFunctions as $function) {
         # read and parse function
         preg_match("#(.+?)\((.*)\)#",$function,$functionAndPara);
 
         $thisFunction = $functionAndPara[1];
         $thisParaSet = $functionAndPara[2];               
 
         # parse and arr strings
         while(preg_match("#\'(.+),(.+)\'#",$thisParaSet)) {
            $thisParaSet=preg_replace("#\'(.+?),(.+?)\'#","'$1\0COMMA\0$2'",$thisParaSet);
         }
         $thisParaSet = explode(",",$thisParaSet);
 
         # get each parameter
         foreach($thisParaSet as $parameter) {
            if($addPara = str_replace("\0COMMA\0",",",$parameter))
               $arrEvents[$thisEvent][$thisFunction][] = $addPara;
         }
      }
   }
   return $arrEvents;
}

Here is a sample script I created:

      onDraw {
         setMana( 1, thisPlayer() );
         doOtherThing( 5, thisPlayer() );
      },
      onExert {
         sendToHand( thisTile(), thisPlayer(), x, y, z, 0 );
      },
      onDestroy {
         setMana( 5, thisPlayer() );
      }

The script then gets torn apart and dumped into a multi-dimensional array. See Screenshot.

PHP Item Script Parser

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: PHP Tags: , ,