Saturday, August 15, 2009

Scripts, subscripts, and subsubscripts!

It's amazing what a short snippet of well-written code can do to make your life easier. For example, take my gfmodulec() function:


function gfmodulec($sdata)
{
global $GCHAR, $GDATA, $charlook, $GSWITCH, $GVAR, $COMBAT, $COMBATORDER, $NUMBER_OF_FLAGS, $NUMBER_OF_ENEMIES, $U, $PLAYABLE, $COOK, $VISIBLE, $SELECTABLE, $socache;
// Create a random file for inclusion and deletion; return its name.
$randomfilename = GAME_ROOT."/temp/".kcenc(microtime()."a".rand(1,10000)).".inc.php";
$fp = fopen($randomfilename, "w");
fwrite($fp, $sdata);
fclose($fp);
include($randomfilename);
unlink($randomfilename);
}


That might look intimidating at first, but it's actually quite simple. First, the "global" statement tells it to import a bunch of crap. All of those are either arrays or SimpleXML objects. Don't worry about them too much.

The $randomfilename is exactly what it sounds like. kcenc() is Kobra's Custom Encryption function, but the final output is an md5() hash, so it doesn't look too strange.

The rest is easy as pie. It creates a file with the data ($sdata) passed to the function, runs that file, then deletes it.

"What's the point in all this?" It's simple. By using data stored in MySQL, I can write weapons, spells, etc. that can do literally ANYTHING! A sword that randomly (1/10,000 chance) replaces your enemy with a level 1 slime but doesn't reduce its EXP or item gains? No problem. I throw some PHP into the SQL value for that weapon's "script" and viola, an interesting relic!

This means I can make abilities that only work in certain worlds, or on certain types of enemies, too. In fact, every skill, spell, feat, or attack (to the engine, though, there are just skills and feats) will be an include()d PHP script.

It's probably not the most optimal way to do things, but it sure as hell makes things interesting.

No comments:

Post a Comment