Skip to content

Commit

Permalink
cleanup ability cost into separate cost file
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelprograms committed Mar 8, 2025
1 parent 08f756c commit a303ce5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
16 changes: 2 additions & 14 deletions lib/std/ability.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 40 additions & 0 deletions lib/std/ability/cost.c
Original file line number Diff line number Diff line change
@@ -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 = ([
Expand All @@ -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)));
}
}

0 comments on commit a303ce5

Please sign in to comment.