Showing posts with label tech. Show all posts
Showing posts with label tech. Show all posts

Tuesday, August 18, 2009

Status Report: ReXML

So far, I have successfully created a system that uses XML pseudofiles (which I will explain and document in a later post) to store data in a database, and the code to read the XML data and turn it into something meaningful for PHP is finished. Tonight, I work on the system that turns everything back into XML data and updates the database. I'm using a system of flags in an array called $REXML to assist in this process. It should be fun to program.

One of the biggest obstacles in picking this project back up is remembering the syntax. For example, dungeon data is stored in $GCHAR["d_data"]. Player data is $GCHAR["d_data"]->player, and the player's current HP is stored in $GCHAR["d_data"]->player->hp. Remembering the differences between arrays and SimpleXML objects is a bit of a pain.

Incidentally, one of my characters has a $GCHAR["d_data"] that looks like this:
<?xml version="1.0"?>
<ddata>
<player>
<hp>7</hp>
<sp>12</sp>
<encounter>0</encounter>
<initiative>0</initiative>
<x>0</x>
<y>0</y>
<dungeon>0</dungeon>
</player>
</ddata>


While another's looks like this:

<?xml version="1.0"?>
<ddata>
<player>
<hp>17</hp>
<sp>2</sp>
<encounter>0</encounter>
<initiative>16</initiative>
<x>0</x>
<y>2</y>
<dungeon>1</dungeon>
</player>
<cohort>
<id>1</id>
<name>Generic</name>
<type>human</type>
<hp>30</hp>
<sp>10</sp>
<initiative>11</initiative>
<attack>
<min>1</min>
<max>4</max>
<properties>
<prop>martial</prop>
</properties>
<closerange>1</closerange>
<midrange>0</midrange>
<farrange>0</farrange>
</attack>
<xpdrop>100</xpdrop>
<gpdrop>300</gpdrop>
<itemdrops>
<item>
<id>1</id>
<rate>20</rate>
</item>
<item>
<id>2</id>
<rate>1</rate>
</item>
</itemdrops>
<stats>
<str>14</str>
<dex>10</dex>
<con>12</con>
<int>10</int>
<wis>10</wis>
<cha>10</cha>
</stats>
</cohort>
<monster>
<id>3</id>
<name>Zombah!</name>
<type>undead</type>
<type>human</type>
<range>1</range>
<hp>2</hp>
<sp>10</sp>
<initiative>12</initiative>
<attack>
<min>1</min>
<max>4</max>
<properties>
<prop>martial</prop>
</properties>
<closerange>1</closerange>
<midrange>0</midrange>
<farrange>0</farrange>
</attack>
<attack>
<min>2</min>
<max>6</max>
<spcost>2</spcost>
<properties>
<prop>fire</prop>
</properties>
<spcost>0</spcost>
<closerange>0</closerange>
<midrange>1</midrange>
<farrange>1</farrange>
</attack>
<xpdrop>100</xpdrop>
<gpdrop>30</gpdrop>
<itemdrops>
<item>
<id>1</id>
<rate>20</rate>
</item>
<item>
<id>2</id>
<rate>1</rate>
</item>
<item>
<id>4</id>
<rate>1000</rate>
</item>
</itemdrops>
<stats>
<str>10</str>
<dex>6</dex>
<con>0</con>
<int>12</int>
<wis>8</wis>
<cha>9</cha>
</stats>
</monster>
</ddata>


Things can get messy, but everything you see should be auto-generated by the server.

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.