diff --git a/DnDEngine/AbilityGenerator.php b/DnDEngine/AbilityHelper.php similarity index 86% rename from DnDEngine/AbilityGenerator.php rename to DnDEngine/AbilityHelper.php index badc3f8..52ef28a 100644 --- a/DnDEngine/AbilityGenerator.php +++ b/DnDEngine/AbilityHelper.php @@ -43,7 +43,7 @@ /** * PHP Template. */ -class AbilityGenerator +class AbilityHelper { /** * Short description for function @@ -54,10 +54,21 @@ class AbilityGenerator * @return object Return description (if any) ... * @access public */ - public static function factory($type) + public static function factoryGenerator($type) { - $class = __NAMESPACE__ . '\\GeneratorHelper_' . ucfirst(strtolower($type)); + $class = __NAMESPACE__ . '\\GeneratorHelper_' . ucfirst($type); return new $class(); } + + public static function getAbilities() { + return array( + Constants\Abilities::STR, + Constants\Abilities::CON, + Constants\Abilities::DEX, + Constants\Abilities::WIS, + Constants\Abilities::INT, + Constants\Abilities::CHA, + ); + } } ?> diff --git a/DnDEngine/Character.php b/DnDEngine/Character.php index d50031e..838e455 100644 --- a/DnDEngine/Character.php +++ b/DnDEngine/Character.php @@ -45,7 +45,7 @@ /** * PHP Template. */ -class Character +class Character implements interfaces\iBeing { /** * Description for protected @@ -106,13 +106,13 @@ class Character * @var unknown * @access protected */ - protected $alignment; + protected $alignment = Constants\Common::ALIGNMENT_NONE; /** * Description for protected * @var unknown * @access protected */ - protected $iniative; + protected $initiative; /** * Description for protected * @var unknown @@ -172,7 +172,7 @@ class Character * @var integer * @access protected */ - protected $actionPoints = 0; + protected $actionPoints = 1; /** * Description for protected * @var integer @@ -534,7 +534,7 @@ public function addFeat(iFeat $feat) $feat->getName() )); if ($feat->isActive()) { - if (!isset($this->activeFeats[$feat->getName() ])) { + if (!isset($this->activeFeats[$feat->getName()])) { $this->activeFeats[$feat->getName() ] = $feat; } } else { @@ -793,6 +793,50 @@ public function getEquipment($name) } return new Equipment_None(); } + + + public function rollInitiative() { + $this->initiative = $this->checkAbility(Constants\Abilities::DEX); + Logger::debug('Character %s rolls initiative of %s.', array( + $this->getName(), + DisplayHelper::displayInt($this->initiative), + )); + } + + public function getInitiative() { + return $this->getInitiative(); + } + + public function checkAbility($ability) { + $modifier = floor($this->getLevel() / 2) + $this->getAbilityModifier($ability); + + $dice = Game::getDice(); + return (int)$dice->roll(20, 1, $modifier); + } + + public function checkSkill($skill, $take10 = FALSE) { + // @todo Improve according to page 178 in Players Handbook. + $modifier = floor($this->getLevel() / 2) + $this->getSkill($skill); + if ($take10) { + return $modifier + 10; + } + else { + $dice = Game::getDice(); + return $dice->roll(20, 1, $modifier); + } + } + + public function getAbilityModifier($ability) { + return (int)floor(($this->getAbility($ability) - 10) / 2); + } + + public function getSkill($skill) { + if (isset($this->skills[$skill])) { + return $this->skills[$skill]; + } + return 0; + } + /** * Short description for function * diff --git a/DnDEngine/Constants/Common.php b/DnDEngine/Constants/Common.php index d472f18..432b3b0 100644 --- a/DnDEngine/Constants/Common.php +++ b/DnDEngine/Constants/Common.php @@ -137,5 +137,15 @@ class Common * Description for const */ const WEAPON_TWOHAND = 'Two-Hand weapon'; + + const ALIGNMENT_GOOD = 'Good'; + + const ALIGNMENT_LAWFUL = 'Lawful Good'; + + const ALIGNMENT_EVIL = 'Evil'; + + const ALIGNMENT_CHAOTIC = 'Chaotic Evil'; + + const ALIGNMENT_NONE = 'Unaligned'; } ?> diff --git a/DnDEngine/Constants/Skills.php b/DnDEngine/Constants/Skills.php index f55609f..9dde636 100644 --- a/DnDEngine/Constants/Skills.php +++ b/DnDEngine/Constants/Skills.php @@ -73,5 +73,7 @@ class Skills * Description for const */ const STREETWISE = 'Streetwise'; + + const PERCEPTION = 'Perception'; } ?> diff --git a/DnDEngine/Dice/Dice.php b/DnDEngine/Dice/Dice.php new file mode 100644 index 0000000..3329f68 --- /dev/null +++ b/DnDEngine/Dice/Dice.php @@ -0,0 +1,88 @@ +sides = $sides; + } + if (!is_null($repeat)) { + $this->repeat = $repeat; + } + if (!is_null($modifier)) { + $this->modifier = $modifier; + } + $result = $this->doRoll(); + $this->reset(); + return $result; + } + + public function reset() { + $this->sides = 20; + $this->modifier = 0; + $this->repeat = 1; + $this->keep = NULL; + $this->use = iDice::USE_HIGH; + } + + public function rollAndKeepSettings() { + return $this->doRoll(); + } + + private function doRoll() { + + $results = array(); + for ($i = 0; $i < $this->repeat; $i++) { + $results[] = $roll = rand(1, $this->sides); + Logger::debug('Rolled %d', array($roll)); + } + + if (!is_null($this->keep)) { + switch ($this->use) { + case iDice::USE_HIGH: + sort($results); + break; + case iDice::USE_LOW: + rsort($results); + break; + } + for ($i = $this->repeat; $i <= $this->keep; $i--) { + array_shift($results); + } + } + $result = array_sum($results) + $this->modifier; + Logger::info('Roll result for %dd%d + %d: %d', array($this->repeat, $this->sides, $this->modifier, $result)); + return $result; + } + + public function setSides($sides) { + $this->sides = $sides; + } + + public function setModifier($modifier) { + $this->modifier = $modifier; + } + + public function setRepeat($repeat) { + $this->repeat = $repeat; + } + + public function setKeep($keep, $use = iDice::USE_HIGH) { + $this->keep = $keep; + $this->use = $use; + } +} +?> diff --git a/DnDEngine/Encounter.php b/DnDEngine/Encounter.php new file mode 100644 index 0000000..7fc9708 --- /dev/null +++ b/DnDEngine/Encounter.php @@ -0,0 +1,45 @@ +involved[] = $being; + } + + public function start() { + Logger::info('Starting an encounter with the following beings:'); + foreach ($this->involved as $being) { + Logger::info(' - %s (Perception: %s, Hitpoints %s)', array( + $being->getName(), + DisplayHelper::displayInt($being->checkSkill(C\Skills::PERCEPTION, TRUE)), + DisplayHelper::displayInt($being->getHitpoints()), + )); + } + + $this->state = Encounter::STATE_ACTIVE; + } +} + +?> diff --git a/DnDEngine/Exception/PrerequisitesNotMet.php b/DnDEngine/Exception/PrerequisitesNotMet.php new file mode 100644 index 0000000..998d432 --- /dev/null +++ b/DnDEngine/Exception/PrerequisitesNotMet.php @@ -0,0 +1,29 @@ +t('Prerequisites not met for %s, requires %s.', array($char->getName(), $prerequisites))); + } + +} + +?> diff --git a/DnDEngine/Feats/Powerattack.php b/DnDEngine/Feats/Powerattack.php index b22a792..928c2cf 100644 --- a/DnDEngine/Feats/Powerattack.php +++ b/DnDEngine/Feats/Powerattack.php @@ -100,7 +100,7 @@ public function arePrerequisitesMet(C $char) */ public function getPrerequisites() { - return Language::singleton()->t('15 Strength'); + return L::singleton()->t('15 Strength'); } /** * Short description for function diff --git a/DnDEngine/Game.php b/DnDEngine/Game.php index 270c47d..93798dd 100644 --- a/DnDEngine/Game.php +++ b/DnDEngine/Game.php @@ -77,6 +77,9 @@ class Game * @static */ protected static $characters = array(); + + protected static $dice = NULL; + /** * Short description for function * @@ -110,5 +113,30 @@ public static function addCharacter($charname) $char = new Character($charname); return self::$characters[$charname] = $char; } + + public static function setDice(interfaces\iDice $dice) { + self::$dice = $dice; + } + + /** + * + * @return interfaces\iDice + */ + public static function getDice() { + if (is_null(self::$dice)) { + self::$dice = new Dice\Dice(); + } + return self::$dice; + } + + public static function startEncounter() { + $encounter = new Encounter(); + foreach (self::$characters as $character) { + $encounter->addBeing($character); + } + return $encounter; + } + + } ?> \ No newline at end of file diff --git a/DnDEngine/GeneratorHelper/RollingScore.php b/DnDEngine/GeneratorHelper/RollingScore.php new file mode 100644 index 0000000..ce55d55 --- /dev/null +++ b/DnDEngine/GeneratorHelper/RollingScore.php @@ -0,0 +1,103 @@ + nor the names of its contributors + * may be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @category DnDEngine + * @package GeneratorHelper_StandardArray + * @author Sascha Grossenbacher + * @copyright 2008 Sascha Grossenbacher + * @license http://www.opensource.org/licenses/bsd-license.php The BSD License + * @version SVN: $Id:$ + * @link http://pear.php.net/package/GeneratorHelper_StandardArray + * @see References to other sections (if any)... + */ + +namespace DnDEngine; +use DnDEngine\Constants; +/** + * Short description for class + * + * Long description (if any) ... + * + * @category DnDEngine + * @package GeneratorHelper_StandardArray + * @author Sascha Grossenbacher + * @copyright 2008 Sascha Grossenbacher + * @license http://www.opensource.org/licenses/bsd-license.php The BSD License + * @version Release: @package_version@ + * @link http://pear.php.net/package/GeneratorHelper_StandardArray + * @see References to other sections (if any)... + */ +class GeneratorHelper_RollingScore +{ + /** + * Description for protected + * @var unknown + * @access protected + */ + protected $abilities; + /** + * Short description for function + * + * Long description (if any) ... + * + * @param array $abilities Parameter description (if any) ... + * @return void + * @access public + */ + public function rollAbilities() + { + $dice = Game::getDice(); + $dice->setSides(6); + $dice->setRepeat(4); + $dice->setKeep(3); + foreach (AbilityHelper::getAbilities() as $ability) { + $this->abilities[$ability] = $dice->rollandKeepSettings(); + } + $dice->reset(); + } + /** + * Short description for function + * + * Long description (if any) ... + * + * @return unknown Return description (if any) ... + * @access public + * @throws Exception_AbilitiesNotSet Exception description (if any) ... + */ + function getAbilities() + { + if (count($this->abilities) !== 6) { + throw new Exception_AbilitiesNotSet(); + } + return $this->abilities; + } +} +?> \ No newline at end of file diff --git a/DnDEngine/GeneratorHelper/Standardarray.php b/DnDEngine/GeneratorHelper/StandardArray.php similarity index 100% rename from DnDEngine/GeneratorHelper/Standardarray.php rename to DnDEngine/GeneratorHelper/StandardArray.php diff --git a/DnDEngine/Level.php b/DnDEngine/Level.php index 85220b8..85fea07 100644 --- a/DnDEngine/Level.php +++ b/DnDEngine/Level.php @@ -198,7 +198,7 @@ public function chooseFeat(iFeat $feat) throw new Exception_NotAviableAtThisLevel($feat); } if (!$feat->arePrerequisitesMet($this->char)) { - throw new Exception_PrerequitesNotMet($feat->getPrerequisites() , $char); + throw new Exception\PrerequisitesNotMet($feat->getPrerequisites() , $this->char); } $this->gain[self::FEAT]--; if ($this->gain[self::FEAT] == 0) { diff --git a/DnDEngine/interfaces/iBeing.php b/DnDEngine/interfaces/iBeing.php new file mode 100644 index 0000000..2a67ecd --- /dev/null +++ b/DnDEngine/interfaces/iBeing.php @@ -0,0 +1,18 @@ + diff --git a/DnDEngine/interfaces/iDice.php b/DnDEngine/interfaces/iDice.php new file mode 100644 index 0000000..2bc4718 --- /dev/null +++ b/DnDEngine/interfaces/iDice.php @@ -0,0 +1,22 @@ + diff --git a/nbproject/private/config.properties b/nbproject/private/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties index 749786d..c049026 100644 --- a/nbproject/private/private.properties +++ b/nbproject/private/private.properties @@ -1,5 +1,6 @@ copy.src.files=false copy.src.target=/var/www/dnd index.file=tests/characterTest.php +interpreter=/usr/bin/php53 run.as=SCRIPT url=http://localhost/dnd/ diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml new file mode 100644 index 0000000..c1f155a --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,4 @@ + + + + diff --git a/nbproject/project.properties b/nbproject/project.properties index e9417de..024c098 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,6 +1,7 @@ include.path=${php.global.include.path} +php.version=PHP_53 source.encoding=UTF-8 src.dir=. tags.asp=false -tags.short=true +tags.short=false web.root=. diff --git a/tests/.characterTest.php.swp b/tests/.characterTest.php.swp new file mode 100644 index 0000000..da2afd1 Binary files /dev/null and b/tests/.characterTest.php.swp differ diff --git a/tests/characterTest.php b/tests/characterTest.php index cd5c134..3645725 100644 --- a/tests/characterTest.php +++ b/tests/characterTest.php @@ -49,6 +49,7 @@ /** * Description for include */ + include '../init.php'; Language::setLanguage('German'); Logger::addListener('StdOut'); @@ -61,21 +62,60 @@ Constants\Skills::ENDURANCE, Constants\Skills::STREETWISE )); -$standardArray = AbilityGenerator::factory('StandardArray'); +$standardArray = AbilityHelper::factoryGenerator('StandardArray'); $standardArray->assignAbilities(array( Constants\Abilities::STR => 16, Constants\Abilities::CON => 14, Constants\Abilities::DEX => 13, Constants\Abilities::WIS => 12, Constants\Abilities::CHA => 11, - Constants\Abilities::INT => 10 + Constants\Abilities::INT => 10, )); $berdir->setAbilityScore($standardArray); +// $rollScore = AbilityHelper::factoryGenerator('RollingScore'); +// $rollScore->rollAbilities(); +// $berdir->setAbilityScore($rollScore); $berdir->build(); + $level = new Level($berdir); $level->chooseFeat(new Feats\Powerattack()); $level->finish(); $berdir->activateFeat(Feats\Powerattack::Name); $berdir->deactivateFeat(Feats\Powerattack::Name); +$berdir->rollInitiative(); Logger::debug($berdir->dump()); + + +$curunair = Game::addCharacter('Curunair'); +$curunair->setRace(new Race\Dwarf()); +$fighter = $curunair->setClass(new Classes\Fighter()); +$fighter->chooseTrainedSkill(array( + Constants\Skills::ATHLETHICS, + Constants\Skills::ENDURANCE, + Constants\Skills::STREETWISE +)); +$standardArray = AbilityHelper::factoryGenerator('StandardArray'); +$standardArray->assignAbilities(array( + Constants\Abilities::STR => 16, + Constants\Abilities::CON => 14, + Constants\Abilities::DEX => 13, + Constants\Abilities::WIS => 12, + Constants\Abilities::CHA => 11, + Constants\Abilities::INT => 10, +)); +$curunair->setAbilityScore($standardArray); +// $rollScore = AbilityHelper::factoryGenerator('RollingScore'); +// $rollScore->rollAbilities(); +// $berdir->setAbilityScore($rollScore); +$curunair->build(); + +$level = new Level($curunair); +$level->chooseFeat(new Feats\Powerattack()); +$level->finish(); +$curunair->rollInitiative(); +Logger::debug($curunair->dump()); + +$encounter = Game::startEncounter(); +$encounter->start(); + ?>

AltStyle によって変換されたページ (->オリジナル) /