Author Topic: Bonuses, bitvectors, and resistance, oh my!  (Read 780 times)

Offline Lash

  • Acquaintance
  • *
  • Posts: 42
    • View Profile
Bonuses, bitvectors, and resistance, oh my!
« on: December 28, 2015, 01:26:57 PM »
I wanted to add a function to /lib/bonus that will set a resistance type in a player for a limited time:

bonus.c
Code: [Select]
/* /lib/bonus.c
 * from the dead souls mudlib http://www.dead-souls.net
 *
 * modified by Lash (ccoker) for use in The Brass Ring
 * bug fix: bonus object wasn't being destructed
 * 2015-12-28 added functionality for setting resistance
 */

#include <lib.h>
#include ROOMS_H
#include <damage_types.h>

inherit LIB_ITEM;

int SetBonuses();

mapping Skills = ([]);
mapping Stats = ([]);
mapping Points = ([]);

int Duration = 15;
string bonusname;
string brl = " "; //bonusresistance level
int brt = 0;      //bonusresistance type

void create(){
    item::create();
    AddSave( ({ "Skills", "Stats", "Points", "Duration", "bonusname" }) );
    SetInvis(1);
    SetId("bonus_object");
    SetShort("bonus");
    SetLong("A bonus");
}

void init(){
    item::init();
    set_heart_beat(1);
    if(environment(this_object()) && living(environment(this_object())))
        SetBonuses();
}

void heart_beat(){
    if(Duration) Duration--;
    /* modified by Lash - bonus object wasn't being destructed */
    else this_object()->eventDestruct();
    /* end mod */
}

mapping SetStats(mapping arg){
    Stats = copy(arg);
    if(environment(this_object()) && living(environment(this_object())))
        SetBonuses();
    return copy(Stats);
}

mapping GetStats(){
    return copy(Stats);
}

mapping SetSkills(mapping arg){
    Skills = copy(arg);
    return copy(Skills);
}

mapping GetSkills(){
    return copy(Skills);
}

mapping SetPoints(mapping arg){
    Points = copy(arg);
    return copy(Points);
}

mapping GetPoints(){
    return copy(Points);
}

int SetBonusDuration(int i){
    Duration = i;
}

int AddBonusDuration(int i){
    Duration += i;
    if(Duration < 0) Duration = 0;
    return Duration;
}

int GetBonusDuration(){
    return Duration;
}

int SetBonuses(){
    object env = environment();
    if(!env || ! living(env)) return 0;
    if(sizeof(Stats))
        foreach(string key, int val in Stats){
            env->AddStatBonus(key, val);
        }
    if(sizeof(Skills))
        foreach(string key, int val in Skills){
            env->AddSkillBonus(key, val);
        }
    if(sizeof(Points))
        foreach(string key, int val in Points){
            switch(key){
                case "HP" : env->AddHP(val);break;
                case "XP" : env->AddExperiencePoints(val);break;
                case "SP" : env->AddStaminaPoints(val);break;
                case "MP" : env->AddMagicPoints(val);break;
                case "poison" : env->AddPoison(val);break;
                case "caffeine" : env->AddCaffeine(val);break;
                case "food" : env->AddFood(val);break;
                case "drink" : env->AddDrink(val);break;
                default : break;
            }
        }
    env->SetResistance(brt,brl);
         
    return 1;
}

int RemoveBonuses(){
    object env = environment();
    if(!env || ! living(env)) return 0;
    if(sizeof(Stats))
        foreach(string key, int val in Stats){
            env->RemoveStatBonus(key);
        }
    if(sizeof(Skills))
        foreach(string key, int val in Skills){
            env->RemoveSkillBonus(key);
        }
    env->SetResistance(brt,"none");
       
    return 1;
}

int eventDestruct(){
    if(!valid_event(previous_object(), this_object())) return 0;
    RemoveBonuses();
    this_object()->eventMove(ROOM_FURNACE);
    return ::eventDestruct();
}

string GetBonusName(){
    return bonusname;
}

string SetBonusName(string name){
    return bonusname = name;
}

varargs string SetMagicResistance(int type, string level){
    brt = type;
    brl = level;
}

mixed CanGet(object who){ return 0; }
mixed CanGive(object who){ return 0; }
mixed CanSell(object who){ return 0; }
mixed CanDrop(object who){ return 0; }
mixed CanPut(object who){ return 0; }

potion.c
Code: [Select]
/* 2015-12-28
 * modified by ccoker to add functionality for resistance
 */
#include <lib.h>
#include <damage_types.h>

inherit LIB_MEAL;

mapping Skills = ([]);
mapping Stats = ([]);
mapping Points = ([]);
string brl = " ";
int brt = 0;

int Duration;

void create(){
    meal::create();
}

mapping SetStats(mapping arg){
    Stats = copy(arg);
    return copy(Stats);
}

mapping GetStats(){
    return copy(Stats);
}

mapping SetSkills(mapping arg){
    Skills = copy(arg);
    return copy(Skills);
}

mapping GetSkills(){
    return copy(Skills);
}

mapping SetPoints(mapping arg){
    Points = copy(arg);
    return copy(Points);
}

mapping GetPoints(){
    return copy(Points);
}

int SetDuration(int i){
    Duration = i;
    return Duration;
}

int GetDuration(){
    return Duration;
}

varargs string SetMagicResistance(int type, string level){
    brt = type;
    brl = level;
}

mixed eventDrink(object who){
    object ob=new(LIB_BONUS);
    ob->SetPoints(Points);
    ob->SetStats(Stats);
    ob->SetSkills(Skills);
    ob->SetBonusDuration(Duration);
    ob->SetMagicResistance(brt,brl);
    ob->eventMove(who);
    return meal::eventDrink(who);
}

mixed eventEat(object who){
    object ob=new(LIB_BONUS);
    ob->SetPoints(Points);
    ob->SetStats(Stats);
    ob->SetSkills(Skills);
    ob->SetBonusDuration(Duration);
    ob->SetMagicResistance(brt,brl);
    ob->eventMove(who);
    return meal::eventEat(who);
}

Example potion - sets resistance to ALL_EXTERNAL_DAMAGE for 300 heart_beats
Code: [Select]
/*
 * Modified by Lash (Christopher Coker) for use with:
 *
 * The Dead Souls Mud Library version 2
 * developed by Cratylus
 * http://www.dead-souls.net
 */

#include <lib.h>
#include <meal_types.h>
#include <damage_types.h>

inherit LIB_POTION;

static void create() {
    potion::create();
    SetKeyName("potion");
    SetId( ({ "potion","philtrum","purple" }) );
    SetShort("a purple potion");
    SetLong("It looks rather strange!");
    SetMass(1);
    SetMealType(MEAL_DRINK);
    SetStrength(0);
    SetBaseCost("gold",500);
    SetNoCondition(1);
    SetBonusResistance(ALL_EXTERNAL_DAMAGE,"medium");
    SetDuration(300);
    SetProperty("magic", "This potion cures confers the spell 'sanctuary' to the imbiber.");
}

void init(){
    ::init();
}

So far it seems to work. The bitvectors are being moved around correctly as defined in genetics.c and resistance to all external damage (x/2 in eventReceiveDamage() in body.c) is being processed correctly.

The drawback (maybe?!) is that other Resistance bits have the potential to be wiped out once the bonus object is destructed.

(modified to rename functions)
« Last Edit: December 28, 2015, 01:40:16 PM by Lash »

Offline Lash

  • Acquaintance
  • *
  • Posts: 42
    • View Profile
Re: Bonuses, bitvectors, and resistance, oh my!
« Reply #1 on: December 29, 2015, 12:10:49 AM »
Note - modifications:

The functions in bonus.c and potion.c should be changed from SetMagicResistance() to SetBonusResistance() to correspond to the example potion and set them apart from  other "magic resistance" named functions in the mudlib.
« Last Edit: December 29, 2015, 12:12:21 AM by Lash »