-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { AbilityType, AbilityTypeId } from "../../object-data/entry/ability-type" | ||
import { DUMMY_ITEM_ID } from "../object-data/dummy-item" | ||
import { INVENTORY_DUMMY_NATIVE_UNIT } from "../misc/dummy-units" | ||
import { checkNotNull } from "../../../utility/preconditions" | ||
import { INVENTORY_ABILITY_TYPE_ID } from "../object-data/dummy-inventory" | ||
import { map, toLuaSet } from "../../../utility/arrays" | ||
|
||
const createItem = CreateItem | ||
const getAbilityId = BlzGetAbilityId | ||
const getItemAbility = BlzGetItemAbility | ||
const getUnitAbilityByIndex = BlzGetUnitAbilityByIndex | ||
const itemAddAbility = BlzItemAddAbility | ||
const setItemBooleanField = BlzSetItemBooleanField | ||
const removeItem = RemoveItem | ||
const unitAddAbility = UnitAddAbility | ||
const unitAddItem = UnitAddItem | ||
const unitDropItemSlot = UnitDropItemSlot | ||
const unitInventorySize = UnitInventorySize | ||
const unitRemoveAbility = UnitRemoveAbility | ||
const unitRemoveItemFromSlot = UnitRemoveItemFromSlot | ||
|
||
const INVENTORY_ABILITY_TYPE_IDS = postcompile(() => { | ||
return toLuaSet( | ||
AbilityType.getAllIdsByBaseIds(map(["AInv", "Aihn", "Aien", "Aion", "Aiun"], fourCC)) | ||
) | ||
}) | ||
|
||
/** @internal For use by internal systems only. */ | ||
export const castAbility = <Args extends any[]>( | ||
nativeUnit: junit, | ||
abilityTypeId: AbilityTypeId, | ||
prepareAbility?: (nativeAbility: jability, ...args: Args) => void, | ||
...args: Args | ||
): boolean => { | ||
const nativeItem = createItem(DUMMY_ITEM_ID, 0, 0) | ||
unitAddItem(INVENTORY_DUMMY_NATIVE_UNIT, nativeItem) | ||
itemAddAbility(nativeItem, abilityTypeId) | ||
const nativeAbility = checkNotNull(getItemAbility(nativeItem, abilityTypeId)) | ||
if (prepareAbility != undefined) { | ||
prepareAbility(nativeAbility, ...args) | ||
} | ||
setItemBooleanField(nativeItem, ITEM_BF_ACTIVELY_USED, true) | ||
setItemBooleanField(nativeItem, ITEM_BF_USE_AUTOMATICALLY_WHEN_ACQUIRED, true) | ||
let success: boolean | ||
if (unitAddItem(nativeUnit, nativeItem)) { | ||
success = true | ||
} else { | ||
let latestInventoryAbilityTypeId = 0 | ||
const nativeItemBySlot = new LuaMap<number, jitem>() | ||
const inventorySize = unitInventorySize(nativeUnit) | ||
if (inventorySize != 0) { | ||
for (const slot of $range(0, inventorySize - 1)) { | ||
nativeItemBySlot.set(slot, unitRemoveItemFromSlot(nativeUnit, slot)) | ||
} | ||
let unitNativeAbility = getUnitAbilityByIndex(nativeUnit, 0) | ||
let i = 1 | ||
while (unitNativeAbility != undefined) { | ||
const abilityTypeId = getAbilityId(unitNativeAbility) as AbilityTypeId | ||
if (INVENTORY_ABILITY_TYPE_IDS.has(abilityTypeId)) { | ||
latestInventoryAbilityTypeId = abilityTypeId | ||
} | ||
unitNativeAbility = getUnitAbilityByIndex(nativeUnit, i) | ||
++i | ||
} | ||
unitRemoveAbility(nativeUnit, latestInventoryAbilityTypeId) | ||
} | ||
unitAddAbility(nativeUnit, INVENTORY_ABILITY_TYPE_ID) | ||
success = unitAddItem(nativeUnit, nativeItem) | ||
unitRemoveAbility(nativeUnit, INVENTORY_ABILITY_TYPE_ID) | ||
if (latestInventoryAbilityTypeId != 0) { | ||
unitAddAbility(nativeUnit, latestInventoryAbilityTypeId) | ||
for (const [slot, nativeItem] of nativeItemBySlot) { | ||
unitAddItem(nativeUnit, nativeItem) | ||
unitDropItemSlot(nativeUnit, nativeItem, slot) | ||
} | ||
} | ||
} | ||
removeItem(nativeItem) | ||
return success | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Unit } from "../unit" | ||
import { castAbility } from "../mechanics/cast-ability" | ||
import { BlinkAbilityType } from "../../object-data/entry/ability-type/blink" | ||
import { MAXIMUM_INTEGER } from "../../../math" | ||
|
||
const getUnitX = GetUnitX | ||
const getUnitY = GetUnitY | ||
const setUnitX = SetUnitX | ||
const setUnitY = SetUnitY | ||
|
||
const BLINK_ABILITY_TYPE_ID = compiletime(() => { | ||
const abilityType = BlinkAbilityType.create() | ||
abilityType.minimumRange = 0 | ||
abilityType.maximumRange = MAXIMUM_INTEGER | ||
abilityType.manaCost = 0 | ||
abilityType.cooldown = 0 | ||
return abilityType.id | ||
}) | ||
|
||
declare module "../unit" { | ||
interface Unit { | ||
detachMissiles(this: Unit): void | ||
} | ||
} | ||
Unit.prototype.detachMissiles = function () { | ||
const nativeUnit = this.handle | ||
const x = getUnitX(nativeUnit) | ||
const y = getUnitY(nativeUnit) | ||
castAbility(nativeUnit, BLINK_ABILITY_TYPE_ID) | ||
setUnitX(nativeUnit, x) | ||
setUnitY(nativeUnit, y) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters