Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, August 19, 2009

Dungeon Combat System: 33% Complete!

Hazzah! I have completed the hardest part of the system: Determining who attacks first, collecting a large script together from each combatant to determine the damage dealt and other such effects, and displaying the output for the player to read.

Zombah! strikes voodooKobra with his fist for 2 damage.
Zombah! strikes voodooKobra with his fist for 1 damage.
Generic Cohort strikes Zombah! with his fist for 4 damage.


<?php
$GCHAR["d_data"]->player->hp -= 2;
$GCHAR["d_data"]->player->hp -= 1;
$GCHAR["d_data"]->cohort[0]->hp -= 4;
?>


Both of the above are generated by the server and executed in a single battle turn. You wouldn't believe how difficult I've made the process by which the server generates this in the name of flexibility, and the entire thing only clocks in at about 32 KB so far, but that's still a lot of code. And I'm not an efficient programmer, either.

Still to do:

  • Attack rolls, AC, etc.
  • Saving throws
  • Display the combat interface
  • Interpret player input
  • Specify -1 as the flag for "attack all enemies" and make it work
  • Spell effects
  • Appending to $COMBAT["script"]


Then, I'll move on to leveling up and character development, and FINALLY begin the dungeon itself. You guys are gonna love it!

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.