diff --git a/lib/std/ability.c b/lib/std/ability.c index 99120313..a51f540f 100644 --- a/lib/std/ability.c +++ b/lib/std/ability.c @@ -412,22 +412,10 @@ private void handle_ability_use (object source, object *targets) { // determine cost cost = query_cost(); - // check source vitals - if (cost["sp"] > 0 && source->query_sp() < cost["sp"]) { - message("action", "You are too drained to " + query_name() + ".", source); + if (!verify_cost(source, cost)) { return; } - if (cost["mp"] > 0 && source->query_mp() < cost["mp"]) { - message("action", "You are too tired to " + query_name() + ".", source); - return; - } - // update source vitals - if (cost["sp"] > 0) { - source->add_sp(-(cost["sp"]*3/4 + (random(cost["sp"]/4) + 1))); - } - if (cost["mp"]) { - source->add_mp(-(cost["mp"]*3/4 + (random(cost["mp"]/4) + 1))); - } + apply_cost(source, cost); // update statuses source->set_busy(1); diff --git a/lib/std/ability/cost.c b/lib/std/ability/cost.c index 7784a7aa..02c6ef54 100644 --- a/lib/std/ability/cost.c +++ b/lib/std/ability/cost.c @@ -1,5 +1,10 @@ // @this_object /std/ability.c +/** + * Determine how much this ability costs to cast. + * + * @returns a mapping of vitals + */ mapping query_cost () { string vitalType; mapping cost = ([ @@ -19,4 +24,39 @@ mapping query_cost () { cost[vitalType] += value; } return cost; +} + +/** + * Verifies the source has enough vitals to cast this ability. + * + * @param {STD_LIVING} source + * @param cost from query_cost function + * @returns 0 or 1 for being able to cast + */ +int verify_cost (object source, mapping cost) { + // check source vitals + if (cost["sp"] > 0 && source->query_sp() < cost["sp"]) { + message("action", "You are too drained to " + this_object()->query_name() + ".", source); + return; + } + if (cost["mp"] > 0 && source->query_mp() < cost["mp"]) { + message("action", "You are too tired to " + this_object()->query_name() + ".", source); + return; + } +} + +/** + * Applies the cost to the source's vitals. + * + * @param {STD_LIVING} source + * @param cost from query_cost function + */ +void apply_cost (object source, mapping cost) { + // update source vitals + if (cost["sp"] > 0) { + source->add_sp(-(cost["sp"]*3/4 + (random(cost["sp"]/4) + 1))); + } + if (cost["mp"]) { + source->add_mp(-(cost["mp"]*3/4 + (random(cost["mp"]/4) + 1))); + } } \ No newline at end of file