Skip to content

Commit

Permalink
feat: Start combat actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Dec 9, 2022
1 parent 64a7e93 commit beea296
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 8 deletions.
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ function mkIifeWrap() {
vars: [
'AltMagicConsumptionID',
'Array',
'AttackTypeID',
'Bank',
'Boolean',
'cdnMedia',
'CombatManager',
'console',
'document',
'Error',
Expand Down
2 changes: 0 additions & 2 deletions src/actions/combat/activate-spell-actions.mts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const mkAction = ({media, registry, ...init}: Init) => {
return out;
};

/* eslint-disable no-new */

mkAction({
label: 'Cast Standard spell',
localID: 'castStdSpell',
Expand Down
1 change: 1 addition & 0 deletions src/actions/combat/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import '../../option-types/option-types.mjs';
import './activate-prayer-action.mjs';
import './activate-spell-actions.mjs';
import './set-attack-style-action.mjs';
import './start-combat-actions.mjs';
95 changes: 95 additions & 0 deletions src/actions/combat/start-combat-actions.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type {CombatArea, Dungeon, Game, NamespaceRegistry} from 'melvor';
import type {TypedKeys} from 'mod-util/typed-keys';
import {InternalCategory} from '../../lib/registries/action-registry.mjs';
import {EMPTY_ARR} from '../../lib/util.mjs';
import {defineLocalAction} from '../../lib/util/define-local.mjs';
import type {ActionNodeDefinition, NodeOption} from '../../public_api';

interface Props {
area: CombatArea;

/** Index in the area's monsters array */
mob?: number;
}

interface Init extends Omit<ActionNodeDefinition<Props>, 'namespace' | 'category' | 'options' | 'execContext'> {
extraOpts?: NodeOption[];

optionLabel: string;

registry: TypedKeys<Game, NamespaceRegistry<CombatArea>>;
}

type This = ReturnType<typeof mkAction>;

function execMob(this: This, {area, mob}: Props) {
game.stopActiveAction();
game.combat.selectMonster(area.monsters[mob!], area);
}

const mkAction = ({extraOpts, registry, optionLabel, ...init}: Init) => {
const def: Pick<ActionNodeDefinition<Props>, 'category' | 'options'> = {
category: InternalCategory.COMBAT,
options: [
{
label: optionLabel,
localID: 'area',
registry,
required: true,
type: 'MediaItem',
},
...(extraOpts || []),
],
};

const out = {
...def,
...init,
} as const;

defineLocalAction(out);

return out;
};

const areaMobOption: [NodeOption] = [{
getAltCostItems: (area: CombatArea) => area.monsters as any[] || EMPTY_ARR,
label: 'Monster',
localID: 'mob',
recipeOption: 'area',
required: true,
showIf: ({area}: Props) => Boolean(area),
type: 'AltRecipeCost',
}];

mkAction({
execute: execMob,
extraOpts: areaMobOption,
label: 'Start Combat Area',
localID: 'startCombatStd',
media: game.combat.media,
optionLabel: 'Area',
registry: 'combatAreas',
});

mkAction({
execute: execMob,
extraOpts: areaMobOption,
label: 'Start Slayer Area',
localID: 'startCombatSlayer',
media: game.slayer.media,
optionLabel: 'Area',
registry: 'slayerAreas',
});

mkAction({
execute({area}) {
game.stopActiveAction();
game.combat.selectDungeon(area as Dungeon);
},
label: 'Start Dungeon',
localID: 'startCombatDung',
media: cdnMedia('assets/media/skills/combat/dungeon.svg'),
optionLabel: 'Dungeon',
registry: 'dungeons',
});
13 changes: 13 additions & 0 deletions types/melvor/game/combat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ export class CombatSkill extends Skill {

}

export class CombatArea extends NamespacedObject {
monsters: Monster[];
get media(): string;
}

export class SlayerArea extends CombatArea {

}

export class Dungeon extends CombatArea {

}

export class Enemy extends Character {
attackType: Exclude<keyof typeof AttackTypeID, 'random' | 'unset'>;

Expand Down
11 changes: 10 additions & 1 deletion types/melvor/game/core.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ActivePrayer, CombatSpell, SpecialAttack} from 'melvor';
import {ActivePrayer, CombatArea, CombatSpell, Dungeon, SlayerArea, SpecialAttack} from 'melvor';
import type {Monster} from './combat';
import {Attack, AttackStyle} from './combat';
import type {EquipmentItem, Item} from './item';
Expand Down Expand Up @@ -148,6 +148,7 @@ export interface Namespace {
}

export class Game {

activeAction?: Skill;

agility: Agility;
Expand All @@ -170,6 +171,8 @@ export class Game {

combat: CombatManager;

combatAreas: NamespaceRegistry<CombatArea>;

cooking: Cooking;

crafting: Crafting;
Expand All @@ -178,6 +181,8 @@ export class Game {

curseSpells: NamespaceRegistry<CombatSpell>;

dungeons: NamespaceRegistry<Dungeon>;

emptyEquipmentItem: EquipmentItem;

firemaking: Firemaking;
Expand Down Expand Up @@ -212,6 +217,10 @@ export class Game {

skills: NamespaceRegistry<Skill>;

slayer: Skill;

slayerAreas: NamespaceRegistry<SlayerArea>;

slayerCoins: SlayerCoins;

smithing: Smithing;
Expand Down
13 changes: 8 additions & 5 deletions types/melvor/game/misc.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import {Enemy} from 'melvor/game/combat';
import {CombatArea, Dungeon, Enemy, Monster} from 'melvor/game/combat';
import type {NamespaceRegistry} from './core';
import type {Item, PotionItem} from './item';
import type {Skill} from './skilling';
import type {Player} from './toon';

export class CombatManager {
isActive: boolean;

enemy: Enemy;

isActive: boolean;

player: Player;

get media(): string;

selectMonster(monster: Monster, area: CombatArea): void;
selectDungeon(dungeon: Dungeon): void;

spawnEnemy(): void;
}

Expand Down Expand Up @@ -54,8 +60,6 @@ export interface Requirement {
[k: string]: any;
}



export class Page extends NamespacedObject {
canBeDefault: boolean;

Expand All @@ -68,7 +72,6 @@ export class Page extends NamespacedObject {
get media(): string;
}


export class ShopPurchase extends NamespacedObject {
public allowQuantityPurchase: boolean;

Expand Down

0 comments on commit beea296

Please sign in to comment.