TypeScriptAddonTemplate icon indicating copy to clipboard operation
TypeScriptAddonTemplate copied to clipboard

Extending the API

Open GniLudio opened this issue 3 years ago • 0 comments

It would be nice to have a file for extending the API. Here is a example for extending CDOTA_BaseNPC:

export {}
declare global {
    interface CDOTA_BaseNPC {
        GetAllAbilities(): CDOTABaseAbility[];
        HasTalent(talentName: string): boolean;
        GetTalent(talentName: string): number;
        HasShard(): boolean,
        GetMissingMana(): number,
        GetAllAbilities(): CDOTABaseAbility[];
        HasTalent(talentName: string): boolean;
        GetTalent(talentName: string): number;
        FindItemByName(itemName: string): CDOTA_Item | undefined;
        RemoveItemByName(itemName: string): void;
        IsRoshan(): boolean;
        HasShard(): boolean;
    }
    interface CScriptParticleManager {
        FireParticle(particleName:string, particleAttach: ParticleAttachment, owner: CBaseEntity|undefined): void;
    }
    interface CDOTA_Buff {
        IsNull(): boolean;
    }
    interface CDOTA_PlayerResource  {
        GetAllPlayers() : CDOTAPlayer[],
    }

}

CDOTA_BaseNPC.GetAllAbilities = function() {
    let abilities: CDOTABaseAbility[] = [];
    for (let i=0; i<DOTA_MAX_ABILITIES; i++) {
        let ability = this.GetAbilityByIndex(i);
        if (ability) {
            abilities.push(ability);
        }
    }
    return abilities;
}
CDOTA_BaseNPC.HasTalent = function (talentName: string) {
    let talent = this.FindAbilityByName(talentName)
    return talent ? talent.GetLevel()>0 : false;
}
CDOTA_BaseNPC.GetTalent = function (talentName: string) {
    let talent = this.FindAbilityByName(talentName);
    return talent ? talent.GetSpecialValueFor("value") : 0;
}
CDOTA_BaseNPC.HasShard = function() {
    return this.HasModifier("modifier_item_aghanims_shard");
}
CDOTA_BaseNPC.GetMissingMana = function() {
    return this.GetMaxMana() - this.GetMana();
}
CDOTA_BaseNPC.FindItemByName = function(itemName: string) {
    for (let slot=0; slot<=16; slot++) {
        const item = this.GetItemInSlot(slot);
        if (item) return item;
    }
    return undefined;
}
CDOTA_BaseNPC.RemoveItemByName = function(itemName: string) {
    const item = this.FindItemByName(itemName);
    if (item) this.RemoveItem(item);
}
CDOTA_BaseNPC.IsRoshan = function() {
    return this.GetName() == "npc_dota_roshan";
}
CScriptParticleManager.FireParticle = function(particleName: string, particleAttach: ParticleAttachment, owner: CBaseEntity | undefined) {
    let particle = ParticleManager.CreateParticle(particleName, particleAttach, owner);
    ParticleManager.ReleaseParticleIndex(particle);
}
CDOTA_PlayerResource.GetAllPlayers = function() {
    const players = new Array<CDOTAPlayer>();
    for (let i=0; i<DOTA_MAX_PLAYERS; i++) {
        if (PlayerResource.IsValidPlayer(i)) {
            const player = PlayerResource.GetPlayer(i);
            if (player) players.push();
        }
    }
    return players;
}

P.S.: I import the extended_api-file in the GameMode.ts-file. (import "./utils/extended_api";)

GniLudio avatar Nov 24 '21 15:11 GniLudio