ThePix / QuestJS

A major re-write of Quest that is written in JavaScript and will run in the browser.
MIT License
74 stars 15 forks source link

Expansion of RPG module LIMITED_AMMO_WEAPON: RELOADABLE_AMMO_WEAPON #98

Open Kln95130 opened 1 year ago

Kln95130 commented 1 year ago

Hello Pix,

I experiment this week-end on your RPG module. I thought that mixing what I had worked in my previous project with your LIMITED_AMMO_WEAPON would be interesting.

As it is, this template allows the use of ammo, but once the ammo is spent, the weapon is useless. Behold, the RELOADABLE_AMMO_WEAPON template and its associated command, which now allows rogues to resplenish their quivers, and special agents to reload their handguns.

Feel free to use the code in any way, shape, or form.

lang.verbs.reload = "Reload";

commands.push(
    new Cmd('Reload', {
        regex:/^(?:reload) (.+)$/,
        rules:[cmdRules.isHeld, cmdRules.testManipulate],
        objects:[
          {scope:parser.isHeld, multiple:true},
        ],
        script:function(objects) {
            const weapon = objects[0][0];
            if (!weapon.ammoName) {
                msg("{nv:item:do:true} not have any valid ammunition.", {item: weapon});
                return world.FAILED;
            }
            const ammo = w[weapon.ammoName];
            const ammoHeld = ammo.countAtLoc("me");
            if (ammoHeld == 0) {
                msg("{pv:player:do:true} not have any {nm:item} on you.", {item: ammo});
                return world.FAILED;
            }
            const quantityToFill = weapon.capacity - weapon.ammo;
            if (ammoHeld >= quantityToFill) {
                ammo.countableLocs['me'] -= quantityToFill;
                weapon.ammo = weapon.capacity;
                msg("{pv:player:do:true} use {show:quantity} {nm:ammo} to reload {nm:weapon}.", {weapon: weapon, ammo: ammo, quantity: quantityToFill})
            } else {
                ammo.countableLocs['me'] = false;
                weapon.ammo += ammoHeld;
                msg("{pv:player:do:true} use your {show:quantity} remaining {nm:ammo} to reload {nm:weapon}.", {weapon: weapon, ammo: ammo, quantity: ammoHeld})
            }

            return  world.SUCCESS
        },
    })
)

const RELOADABLE_AMMO_WEAPON = function(damage, ammo){
    const res = Object.assign({}, LIMITED_AMMO_WEAPON(damage, ammo))

    res.capacity=ammo;
    res.ammoName = false;

    res.afterCreation =  function(o) {
        //o.oldRpgOnCreation(o)
        o.verbFunctions.push(function(o, verbList) {
          if (o.isAtLoc(player)) {
            verbList.push(lang.verbs.reload)
          }
        })
    }

    return res;
}

createItem("handgun", RELOADABLE_AMMO_WEAPON("1d3+3", 6),  {
    loc: "me",
    synonyms: ['gun', 'pistol'],
    examine: "a nifty gun. It has {show:handgun:ammo} ammunition",
    ammoName: "handgunAmmo",
});

createItem("handgunAmmo", COUNTABLE({me: 7}),  {
    regex:/^(\d+ )?(handgun round|rounds)?$/,

    alias: "handgun round",
    examine: "ammunition for the gun.",
});