warscript 0.0.1-dev.91da42b → 0.0.1-dev.94cf7fc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,10 @@ export declare abstract class AbilityBehavior<Parameters extends {
36
36
  ]): void;
37
37
  private static MissileLaunchConfig;
38
38
  private get missileLaunchConfig();
39
+ protected launchMissile(source: Unit, ...args: [
40
+ ...pointOrWidget: [x: number, y: number] | [widget: Unit /** TODO: support Widget */],
41
+ ...parameters: NonNullable<Parameters["missileParameters"]>
42
+ ]): void;
39
43
  protected onCreate(): void;
40
44
  onValueChange(_value: ReadonlySubscribableAbilityDependentValue<string | number | boolean>): void;
41
45
  onMissileArrival(...parameters: NonNullable<Parameters["missileParameters"]>): void;
@@ -30,7 +30,14 @@ local ____ability = require("engine.object-field.ability")
30
30
  local AbilityField = ____ability.AbilityField
31
31
  local AbilityLevelField = ____ability.AbilityLevelField
32
32
  local resolveCurrentAbilityDependentValue = ____ability.resolveCurrentAbilityDependentValue
33
+ local ____missile = require("core.types.missile")
34
+ local Missile = ____missile.Missile
33
35
  local createBehaviorFunctionsByAbilityTypeId = {}
36
+ local function invokeOnMissileArrival(_missile, success, abilityBehavior, ...)
37
+ if success then
38
+ abilityBehavior:onMissileArrival(...)
39
+ end
40
+ end
34
41
  local exclusiveOnImpactHandlerAbilityBehaviorByAbility = setmetatable({}, {__mode = "k"})
35
42
  local function createUnitEventListener(key)
36
43
  return function(unit, ability, ...)
@@ -160,6 +167,28 @@ function AbilityBehavior.prototype.flashSpecialEffect(self, xOrWidget, yOrParame
160
167
  )
161
168
  end
162
169
  end
170
+ function AbilityBehavior.prototype.launchMissile(self, source, xOrWidget, yOrParameter, ...)
171
+ if type(xOrWidget) ~= "number" then
172
+ Missile:launch(
173
+ self.missileLaunchConfig,
174
+ source,
175
+ xOrWidget,
176
+ invokeOnMissileArrival,
177
+ self,
178
+ yOrParameter,
179
+ ...
180
+ )
181
+ else
182
+ Missile:launch(
183
+ self.missileLaunchConfig,
184
+ source,
185
+ vec2(xOrWidget, yOrParameter),
186
+ invokeOnMissileArrival,
187
+ self,
188
+ ...
189
+ )
190
+ end
191
+ end
163
192
  function AbilityBehavior.prototype.onCreate(self)
164
193
  end
165
194
  function AbilityBehavior.prototype.onValueChange(self, _value)
@@ -7,7 +7,7 @@ import { Destructor } from "../../../destroyable";
7
7
  import { BehaviorPriority } from "../../behavior";
8
8
  export type StunImmunityUnitBehaviorParameters = {
9
9
  readonly priority?: BehaviorPriority;
10
- buffTypeIds?: LuaSet<BuffTypeId>;
10
+ buffTypeIds?: readonly BuffTypeId[];
11
11
  textTagPreset?: TextTagPreset;
12
12
  textTagText?: string;
13
13
  additionalAction?: (this: void, unit: Unit) => void;
@@ -8,14 +8,15 @@ local UnitBehavior = ____unit.UnitBehavior
8
8
  local ____ability_2Dtype = require("engine.object-data.entry.ability-type")
9
9
  local AbilityType = ____ability_2Dtype.AbilityType
10
10
  local ____arrays = require("utility.arrays")
11
- local flatMapToLuaSet = ____arrays.flatMapToLuaSet
11
+ local distinct = ____arrays.distinct
12
+ local flatMap = ____arrays.flatMap
12
13
  local map = ____arrays.map
13
14
  local ____text_2Dtag = require("engine.text-tag")
14
15
  local TextTag = ____text_2Dtag.TextTag
15
16
  local ____timer = require("core.types.timer")
16
17
  local Timer = ____timer.Timer
17
18
  local DEFAULT_BUFF_TYPE_IDS = postcompile(function()
18
- return flatMapToLuaSet(
19
+ return distinct(flatMap(
19
20
  AbilityType:getAllByBaseIds(map({
20
21
  "AHtb",
21
22
  "AHbh",
@@ -41,11 +42,11 @@ local DEFAULT_BUFF_TYPE_IDS = postcompile(function()
41
42
  "ACcb"
42
43
  }, fourCC)),
43
44
  function(abilityType) return __TS__ArrayFlat(abilityType.buffTypeIds) end
44
- )
45
+ ))
45
46
  end)
46
47
  local function process(behavior)
47
48
  local hasRemovedBuffs = false
48
- for buffTypeId in pairs(behavior.parameters.buffTypeIds or DEFAULT_BUFF_TYPE_IDS) do
49
+ for ____, buffTypeId in ipairs(behavior.parameters.buffTypeIds or DEFAULT_BUFF_TYPE_IDS) do
49
50
  hasRemovedBuffs = hasRemovedBuffs or behavior.unit:removeBuff(buffTypeId)
50
51
  end
51
52
  if hasRemovedBuffs then
@@ -21,6 +21,8 @@ local ____bonus = require("engine.internal.unit.bonus")
21
21
  local addOrUpdateOrRemoveUnitBonus = ____bonus.addOrUpdateOrRemoveUnitBonus
22
22
  local getUnitBonus = ____bonus.getUnitBonus
23
23
  local removeUnitBonus = ____bonus.removeUnitBonus
24
+ local ____linked_2Dmap = require("utility.linked-map")
25
+ local LinkedMap = ____linked_2Dmap.LinkedMap
24
26
  local safeCall = warpack.safeCall
25
27
  local createBehaviorFunctionsByUnitTypeId = {}
26
28
  local behaviorsByOwningPlayerEvent = {}
@@ -79,16 +81,24 @@ function UnitBehavior.prototype.onDestroy(self)
79
81
  end
80
82
  function UnitBehavior.prototype.getUnitBonus(self, bonusType)
81
83
  local ____opt_10 = self._bonusIdByBonusType
82
- local bonusId = ____opt_10 and ____opt_10[bonusType]
84
+ local bonusId = ____opt_10 and ____opt_10:get(bonusType)
83
85
  return bonusId == nil and 0 or getUnitBonus(self.object, bonusType, bonusId)
84
86
  end
85
87
  function UnitBehavior.prototype.addOrUpdateOrRemoveUnitBonus(self, bonusType, value)
86
88
  local bonusIdByBonusType = self._bonusIdByBonusType
87
89
  if bonusIdByBonusType == nil then
88
- bonusIdByBonusType = {}
90
+ bonusIdByBonusType = __TS__New(LinkedMap)
89
91
  self._bonusIdByBonusType = bonusIdByBonusType
90
92
  end
91
- bonusIdByBonusType[bonusType] = addOrUpdateOrRemoveUnitBonus(self.object, bonusType, bonusIdByBonusType[bonusType], value)
93
+ bonusIdByBonusType:put(
94
+ bonusType,
95
+ addOrUpdateOrRemoveUnitBonus(
96
+ self.object,
97
+ bonusType,
98
+ bonusIdByBonusType:get(bonusType),
99
+ value
100
+ )
101
+ )
92
102
  end
93
103
  function UnitBehavior.prototype.registerOwningPlayerEvent(self, event, extractPlayer, listener)
94
104
  local listenerByBehavior = getOrPut(listenerByBehaviorByOwningPlayerEvent, event, mutableLuaMap)
package/engine/buff.d.ts CHANGED
@@ -31,8 +31,10 @@ export type BuffParameters<T extends Buff<any> = Buff> = Buff extends T ? {
31
31
  source?: Unit;
32
32
  behaviorConstructors?: (new (unit: Unit) => UnitBehavior)[];
33
33
  abilityTypeIds?: Record<AbilityTypeId, {
34
- [numberField: (AbilityNumberField | AbilityNumberLevelField) & symbol]: NumberParameterValueType;
35
- } & {
34
+ readonly fields?: [
35
+ AbilityNumberField | AbilityNumberLevelField,
36
+ NumberParameterValueType
37
+ ][];
36
38
  /** Default `true`. */
37
39
  readonly isButtonVisible?: boolean;
38
40
  /** Default is the level of the source ability or 0 if it is absent. */
@@ -269,6 +271,7 @@ export declare class Buff<AdditionalParameters extends Prohibit<Record<string, a
269
271
  get manaRegenerationRateIncreaseFactor(): number;
270
272
  set manaRegenerationRateIncreaseFactor(manaRegenerationRateIncreaseFactor: number);
271
273
  get duration(): number;
274
+ set duration(duration: number);
272
275
  get remainingDuration(): number;
273
276
  set remainingDuration(remainingDuration: number);
274
277
  get abilityCooldownFactor(): number;
@@ -280,6 +283,7 @@ export declare class Buff<AdditionalParameters extends Prohibit<Record<string, a
280
283
  ...parametersOrDuration: [] | [EffectParameters] | [number]
281
284
  ]): void;
282
285
  flashSpecialEffect(...parameters: [...widget: [] | [Widget], ...duration: [] | [number]]): void;
286
+ expire(): void;
283
287
  protected onCreate(): void;
284
288
  protected onDestroy(): Destructor;
285
289
  static apply<T extends Buff<any>, Args extends any[]>(this: BuffConstructor<T, Args>, ...args: Args): T | undefined;
package/engine/buff.lua CHANGED
@@ -42,6 +42,7 @@ local BuffType = ____buff_2Dtype.BuffType
42
42
  local ____unit = require("engine.behaviour.unit")
43
43
  local UnitBehavior = ____unit.UnitBehavior
44
44
  local ____arrays = require("utility.arrays")
45
+ local emptyArray = ____arrays.emptyArray
45
46
  local forEach = ____arrays.forEach
46
47
  local ____event = require("event")
47
48
  local Event = ____event.Event
@@ -55,6 +56,8 @@ local ____ability = require("engine.standard.fields.ability")
55
56
  local COOLDOWN_ABILITY_FLOAT_LEVEL_FIELD = ____ability.COOLDOWN_ABILITY_FLOAT_LEVEL_FIELD
56
57
  local ____ability = require("engine.behaviour.ability")
57
58
  local AbilityBehavior = ____ability.AbilityBehavior
59
+ local ____records = require("utility.records")
60
+ local sortedKeysUnnested = ____records.sortedKeysUnnested
58
61
  local getUnitAbility = BlzGetUnitAbility
59
62
  local stringValueByBuffTypeIdByFieldId = postcompile(function()
60
63
  local stringValueByBuffTypeIdByFieldId = {}
@@ -468,24 +471,22 @@ function Buff.prototype.____constructor(self, _unit, typeIdOrTypeIds, polarityOr
468
471
  abilityTypeIds = {}
469
472
  self._abilityTypeIds = abilityTypeIds
470
473
  end
471
- for abilityTypeId, abilityParameters in pairs(parametersAbilityTypeIds) do
474
+ for ____, abilityTypeId in ipairs(sortedKeysUnnested(parametersAbilityTypeIds)) do
475
+ local abilityParameters = parametersAbilityTypeIds[abilityTypeId]
472
476
  local addedAbility = _unit:addAbility(abilityTypeId)
473
477
  if addedAbility ~= nil then
474
478
  _unit:makeAbilityPermanent(abilityTypeId, true)
475
479
  _unit:setAbilityLevel(abilityTypeId, 1 + (abilityParameters.level or ability and ability.level or 0))
476
- for abilityParameterKey, abilityParameterValue in pairs(abilityParameters) do
477
- if abilityParameterKey == "isButtonVisible" then
478
- if not resolveBooleanValue(ability, level, abilityParameterValue) then
479
- _unit:hideAbility(abilityTypeId, true)
480
- end
481
- elseif abilityParameterKey ~= "level" then
482
- abilityParameterKey:setValue(
483
- addedAbility,
484
- resolveNumberValue(ability, level, abilityParameterValue)
485
- )
486
- end
480
+ if abilityParameters.isButtonVisible == false then
481
+ _unit:hideAbility(abilityTypeId, true)
482
+ end
483
+ for ____, field in ipairs(abilityParameters.fields or emptyArray()) do
484
+ field[1]:setValue(
485
+ addedAbility,
486
+ resolveNumberValue(ability, level, field[2])
487
+ )
487
488
  end
488
- abilityTypeIds[abilityTypeId] = true
489
+ abilityTypeIds[#abilityTypeIds + 1] = abilityTypeId
489
490
  end
490
491
  end
491
492
  local behaviorConstructors = parameters and parameters.behaviorConstructors or defaultParameters.behaviorConstructors
@@ -497,18 +498,20 @@ function Buff.prototype.____constructor(self, _unit, typeIdOrTypeIds, polarityOr
497
498
  self._behaviors = behaviors
498
499
  end
499
500
  end
500
- local additionalParameters = {}
501
- for key, value in pairs(parameters) do
502
- if not buffParametersKeys[key] then
503
- if ability then
504
- additionalParameters[key] = resolveCurrentAbilityDependentValue(ability, value)
505
- else
506
- additionalParameters[key] = value
501
+ if parameters ~= nil then
502
+ local additionalParameters = {}
503
+ for ____, key in ipairs(sortedKeysUnnested(parameters)) do
504
+ if not buffParametersKeys[key] then
505
+ if ability then
506
+ additionalParameters[key] = resolveCurrentAbilityDependentValue(ability, parameters[key])
507
+ else
508
+ additionalParameters[key] = parameters[key]
509
+ end
507
510
  end
508
511
  end
509
- end
510
- if (next(additionalParameters)) ~= nil then
511
- self.parameters = additionalParameters
512
+ if (next(additionalParameters)) ~= nil then
513
+ self.parameters = additionalParameters
514
+ end
512
515
  end
513
516
  end
514
517
  if duration ~= nil and duration > 0 then
@@ -582,6 +585,9 @@ function Buff.prototype.flashSpecialEffect(self, widgetOrDuration, duration)
582
585
  __TS__SparseArraySpread(____array_43)
583
586
  )
584
587
  end
588
+ function Buff.prototype.expire(self)
589
+ expireBuff(self)
590
+ end
585
591
  function Buff.prototype.onCreate(self)
586
592
  end
587
593
  function Buff.prototype.onDestroy(self)
@@ -632,7 +638,7 @@ function Buff.prototype.onDestroy(self)
632
638
  unit:decrementGhostCounter()
633
639
  end
634
640
  if self._abilityTypeIds ~= nil then
635
- for abilityTypeId in pairs(self._abilityTypeIds) do
641
+ for ____, abilityTypeId in ipairs(self._abilityTypeIds) do
636
642
  unit:removeAbility(abilityTypeId)
637
643
  end
638
644
  end
@@ -1272,9 +1278,23 @@ __TS__SetDescriptor(
1272
1278
  __TS__SetDescriptor(
1273
1279
  Buff.prototype,
1274
1280
  "duration",
1275
- {get = function(self)
1276
- return self[103]
1277
- end},
1281
+ {
1282
+ get = function(self)
1283
+ return self[103]
1284
+ end,
1285
+ set = function(self, duration)
1286
+ if duration <= 0 then
1287
+ local timer = self._timer
1288
+ if timer ~= nil then
1289
+ timer:destroy()
1290
+ self._timer = nil
1291
+ end
1292
+ self[103] = 0
1293
+ else
1294
+ self.remainingDuration = self.remainingDuration + (duration - self[103])
1295
+ end
1296
+ end
1297
+ },
1278
1298
  true
1279
1299
  )
1280
1300
  __TS__SetDescriptor(
@@ -74,9 +74,12 @@ ____exports.castAbility = function(nativeUnit, abilityTypeId, prepareAbility, ..
74
74
  unitRemoveAbility(nativeUnit, INVENTORY_ABILITY_TYPE_ID)
75
75
  if latestInventoryAbilityTypeId ~= 0 then
76
76
  unitAddAbility(nativeUnit, latestInventoryAbilityTypeId)
77
- for slot, nativeItem in pairs(nativeItemBySlot) do
78
- unitAddItem(nativeUnit, nativeItem)
79
- unitDropItemSlot(nativeUnit, nativeItem, slot)
77
+ for slot = 0, inventorySize - 1 do
78
+ local nativeItem = nativeItemBySlot[slot]
79
+ if nativeItem ~= nil then
80
+ unitAddItem(nativeUnit, nativeItem)
81
+ unitDropItemSlot(nativeUnit, nativeItem, slot)
82
+ end
80
83
  end
81
84
  end
82
85
  end
@@ -0,0 +1,12 @@
1
+ /** @noSelfInFile */
2
+ import { Event } from "../../../event";
3
+ declare module "../unit" {
4
+ interface Unit {
5
+ readonly unitInRangeEvent: Record<number, Event<[unit: Unit, range: number, unitInRange: Unit]>>;
6
+ }
7
+ }
8
+ declare module "../unit" {
9
+ interface Unit {
10
+ readonly unitOutOfRangeEvent: Record<number, Event<[unit: Unit, range: number, unitOutOfRange: Unit]>>;
11
+ }
12
+ }
@@ -0,0 +1,90 @@
1
+ local ____lualib = require("lualib_bundle")
2
+ local __TS__New = ____lualib.__TS__New
3
+ local __TS__ObjectDefineProperty = ____lualib.__TS__ObjectDefineProperty
4
+ local ____exports = {}
5
+ local ____event = require("event")
6
+ local Event = ____event.Event
7
+ local InitializingEvent = ____event.InitializingEvent
8
+ local TriggerEvent = ____event.TriggerEvent
9
+ local ____unit = require("engine.internal.unit")
10
+ local Unit = ____unit.Unit
11
+ local ____timer = require("core.types.timer")
12
+ local Timer = ____timer.Timer
13
+ local ____attributes = require("attributes")
14
+ local attribute = ____attributes.attribute
15
+ local ____linked_2Dmap = require("utility.linked-map")
16
+ local mutableLinkedMap = ____linked_2Dmap.mutableLinkedMap
17
+ local ____linked_2Dset = require("utility.linked-set")
18
+ local LinkedSet = ____linked_2Dset.LinkedSet
19
+ local mutableLinkedSet = ____linked_2Dset.mutableLinkedSet
20
+ local getTriggerUnit = GetTriggerUnit
21
+ local triggerRegisterUnitInRange = TriggerRegisterUnitInRange
22
+ local eventInvoke = Event.invoke
23
+ __TS__ObjectDefineProperty(
24
+ Unit.prototype,
25
+ "unitInRangeEvent",
26
+ {get = function(self)
27
+ local unit = self
28
+ local handle = self.handle
29
+ local unitInRangeEvent = setmetatable(
30
+ {},
31
+ {__index = function(self, value)
32
+ local event = __TS__New(
33
+ TriggerEvent,
34
+ function(trigger)
35
+ triggerRegisterUnitInRange(trigger, handle, value, nil)
36
+ end,
37
+ function() return unit, value, Unit:of(getTriggerUnit()) end
38
+ )
39
+ rawset(self, value, event)
40
+ return event
41
+ end}
42
+ )
43
+ rawset(self, "unitInRangeEvent", unitInRangeEvent)
44
+ return unitInRangeEvent
45
+ end}
46
+ )
47
+ local units = __TS__New(LinkedSet)
48
+ local unitsInRangeByRangeAttribute = attribute()
49
+ local function registerUnitOfRangeEvent(unit, range, unitInRange)
50
+ units:add(unit)
51
+ unit:getOrPut(unitsInRangeByRangeAttribute, mutableLinkedMap):getOrPut(range, mutableLinkedSet):add(unitInRange)
52
+ end
53
+ __TS__ObjectDefineProperty(
54
+ Unit.prototype,
55
+ "unitOutOfRangeEvent",
56
+ {get = function(self)
57
+ local unit = self
58
+ local unitOutOfRangeEvent = setmetatable(
59
+ {},
60
+ {__index = function(self, value)
61
+ local event = __TS__New(
62
+ InitializingEvent,
63
+ function()
64
+ unit.unitInRangeEvent[value]:addListener(999999, registerUnitOfRangeEvent)
65
+ end
66
+ )
67
+ rawset(self, value, event)
68
+ return event
69
+ end}
70
+ )
71
+ rawset(self, "unitOutOfRangeEvent", unitOutOfRangeEvent)
72
+ return unitOutOfRangeEvent
73
+ end}
74
+ )
75
+ Timer.onPeriod[1]:addListener(function()
76
+ for unit in pairs(units) do
77
+ local unitsInRangeByRange = unit[unitsInRangeByRangeAttribute]
78
+ if unitsInRangeByRange ~= nil then
79
+ for range, unitsInRange in pairs(unitsInRangeByRange) do
80
+ for unitInRange in pairs(unitsInRange) do
81
+ if unit:getDistanceTo(unitInRange) > range then
82
+ unitsInRange:remove(unitInRange)
83
+ eventInvoke(unit.unitOutOfRangeEvent[range], unit, range, unitInRange)
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end)
90
+ return ____exports
@@ -139,6 +139,7 @@ export declare class Unit extends Handle<junit> {
139
139
  private _fields?;
140
140
  private getEvent;
141
141
  constructor(handle: junit);
142
+ private saveData;
142
143
  protected onDestroy(): HandleDestructor;
143
144
  addAttackHandler(condition: AttackHandlerCondition, action: AttackHandlerAction): AttackHandler;
144
145
  removeAttackHandler(handler: AttackHandler): boolean;
@@ -291,7 +292,6 @@ export declare class Unit extends Handle<junit> {
291
292
  set waygateDestination(v: Vec2);
292
293
  get waygateDestination(): Vec2;
293
294
  get abilities(): ReadonlyArray<UnitAbility>;
294
- get onUnitInRange(): Record<number, Event<[Unit]>>;
295
295
  get onManaEqual(): Record<number, Event<[Unit, number]>>;
296
296
  get manaEvent(): Record<Operator, Record<number, Event<[Unit]>>>;
297
297
  get targetAcquiredEvent(): Event;
@@ -64,6 +64,8 @@ local ____ability = require("engine.internal.item.ability")
64
64
  local doUnitAbilityAction = ____ability.doUnitAbilityAction
65
65
  local ____synchronization = require("engine.synchronization")
66
66
  local synchronizer = ____synchronization.synchronizer
67
+ local ____linked_2Dmap = require("utility.linked-map")
68
+ local LinkedMap = ____linked_2Dmap.LinkedMap
67
69
  local match = string.match
68
70
  local ____tostring = _G.tostring
69
71
  local setUnitAnimation = SetUnitAnimation
@@ -381,15 +383,27 @@ local modifiers = {
381
383
  end,
382
384
  armor = function(unit, value)
383
385
  if UnitAddAbility(unit, armorBonusAbilityId) then
384
- assert(UnitMakeAbilityPermanent(unit, true, armorBonusAbilityId))
386
+ assert(
387
+ UnitMakeAbilityPermanent(unit, true, armorBonusAbilityId),
388
+ "armor bonus ability must be made permanent",
389
+ unit
390
+ )
385
391
  end
386
- local ability = assert(BlzGetUnitAbility(unit, armorBonusAbilityId))
387
- assert(BlzSetAbilityRealLevelField(
388
- ability,
389
- armorBonusField,
390
- 0,
391
- BlzGetAbilityRealLevelField(ability, armorBonusField, 0) + value
392
- ))
392
+ local ability = assert(
393
+ BlzGetUnitAbility(unit, armorBonusAbilityId),
394
+ "armor bonus ability must be existing",
395
+ unit
396
+ )
397
+ assert(
398
+ BlzSetAbilityRealLevelField(
399
+ ability,
400
+ armorBonusField,
401
+ 0,
402
+ BlzGetAbilityRealLevelField(ability, armorBonusField, 0) + value
403
+ ),
404
+ "armor bonus ability field must be set",
405
+ unit
406
+ )
393
407
  end
394
408
  }
395
409
  local getters = {
@@ -643,13 +657,17 @@ local fieldSetters = {
643
657
  }
644
658
  local dummies = {}
645
659
  for ____, player in ipairs(Player.all) do
646
- local dummy = assert(createUnit(
647
- player.handle,
648
- dummyUnitId,
649
- 0,
650
- 0,
651
- 270
652
- ))
660
+ local dummy = assert(
661
+ createUnit(
662
+ player.handle,
663
+ dummyUnitId,
664
+ 0,
665
+ 0,
666
+ 270
667
+ ),
668
+ "dummy must be created",
669
+ player
670
+ )
653
671
  ShowUnit(dummy, false)
654
672
  dummies[player] = dummy
655
673
  end
@@ -692,41 +710,57 @@ function Unit.prototype.____constructor(self, handle)
692
710
  nextSyncId = ____nextSyncId_1 + 1
693
711
  self.syncId = ____nextSyncId_1
694
712
  self._owner = Player:of(getOwningPlayer(handle))
695
- assert(unitAddAbility(handle, leaveDetectAbilityId) and UnitMakeAbilityPermanent(handle, true, leaveDetectAbilityId))
696
- assert(unitAddAbility(handle, morphDetectAbilityId))
713
+ assert(
714
+ unitAddAbility(handle, leaveDetectAbilityId) and UnitMakeAbilityPermanent(handle, true, leaveDetectAbilityId),
715
+ "leave detection ability must be added",
716
+ self
717
+ )
718
+ assert(
719
+ unitAddAbility(handle, morphDetectAbilityId),
720
+ "morph detection ability must be added",
721
+ self
722
+ )
697
723
  if unitAddAbility(
698
724
  handle,
699
725
  fourCC("Amrf")
700
726
  ) then
701
- assert(unitRemoveAbility(
702
- handle,
703
- fourCC("Amrf")
704
- ))
727
+ assert(
728
+ unitRemoveAbility(
729
+ handle,
730
+ fourCC("Amrf")
731
+ ),
732
+ "fly ability must be removed after addition",
733
+ self
734
+ )
705
735
  end
706
736
  unitBySyncId[self.syncId] = self
707
737
  local ____ = self.abilities
708
738
  end
709
- function Unit.prototype.getEvent(self, event, collector)
710
- self.events = self.events or ({})
711
- local eventId = GetHandleId(event)
712
- if not self.events[eventId] then
713
- self.events[eventId] = __TS__New(
739
+ function Unit.prototype.getEvent(self, jevent, collector)
740
+ self.events = self.events or __TS__New(LinkedMap)
741
+ local eventId = GetHandleId(jevent)
742
+ local event = self.events:get(eventId)
743
+ if event == nil then
744
+ event = __TS__New(
714
745
  TriggerEvent,
715
746
  function(trigger)
716
- TriggerRegisterUnitEvent(trigger, self.handle, event)
747
+ TriggerRegisterUnitEvent(trigger, self.handle, jevent)
717
748
  end,
718
749
  collector or (function() return {} end)
719
750
  )
751
+ self.events:put(eventId, event)
720
752
  end
721
- return self.events[eventId]
753
+ return event
754
+ end
755
+ function Unit.prototype.saveData(self)
756
+ local handle = self.handle
757
+ self[108] = self[108] or getUnitX(handle)
758
+ self[109] = self[109] or getUnitY(handle)
759
+ self._owner = self._owner or Player:of(getOwningPlayer(handle))
722
760
  end
723
761
  function Unit.prototype.onDestroy(self)
724
762
  local handle = self.handle
725
- self[108] = getUnitX(handle)
726
- self[109] = getUnitY(handle)
727
- if not self._owner then
728
- self._owner = Player:of(getOwningPlayer(handle))
729
- end
763
+ self:saveData()
730
764
  local abilities = self.abilities
731
765
  for ____, ability in ipairs(abilities) do
732
766
  ability:destroy()
@@ -762,7 +796,11 @@ function Unit.prototype.addAttackHandler(self, condition, action)
762
796
  self._attackHandlers = handlers
763
797
  if #handlers == 1 then
764
798
  local handle = self.handle
765
- assert(unitAddAbility(handle, attackHandlerAbilityId) and UnitMakeAbilityPermanent(handle, true, attackHandlerAbilityId))
799
+ assert(
800
+ unitAddAbility(handle, attackHandlerAbilityId) and UnitMakeAbilityPermanent(handle, true, attackHandlerAbilityId),
801
+ "attack handler ability must be added",
802
+ self
803
+ )
766
804
  end
767
805
  return handler
768
806
  end
@@ -1951,30 +1989,6 @@ __TS__SetDescriptor(
1951
1989
  end},
1952
1990
  true
1953
1991
  )
1954
- __TS__SetDescriptor(
1955
- Unit.prototype,
1956
- "onUnitInRange",
1957
- {get = function(self)
1958
- local handle = self.handle
1959
- local onUnitInRange = setmetatable(
1960
- {},
1961
- {__index = function(self, value)
1962
- local event = __TS__New(
1963
- TriggerEvent,
1964
- function(trigger)
1965
- TriggerRegisterUnitInRangeSimple(trigger, value, handle)
1966
- end,
1967
- function() return ____exports.Unit:of(handle) end
1968
- )
1969
- rawset(self, value, event)
1970
- return event
1971
- end}
1972
- )
1973
- rawset(self, "onUnitInRange", onUnitInRange)
1974
- return onUnitInRange
1975
- end},
1976
- true
1977
- )
1978
1992
  __TS__SetDescriptor(
1979
1993
  Unit.prototype,
1980
1994
  "onManaEqual",
@@ -2773,7 +2787,8 @@ Unit.synchronize = synchronizer(
2773
2787
  return
2774
2788
  end
2775
2789
  end
2776
- unit:destroy()
2790
+ unit:saveData()
2791
+ Timer:run(unit, "destroy")
2777
2792
  end)
2778
2793
  end
2779
2794
  end)(Unit)
@@ -0,0 +1,8 @@
1
+ /** @noSelfInFile */
2
+ import { AbilityType, AbilityTypeId } from "../ability-type";
3
+ import { ObjectDataEntryLevelFieldValueSupplier } from "../../entry";
4
+ export declare class ReincarnationAbilityType extends AbilityType {
5
+ static readonly BASE_ID: AbilityTypeId;
6
+ get reincarnationDelay(): number[];
7
+ set reincarnationDelay(reincarnationDelay: ObjectDataEntryLevelFieldValueSupplier<number>);
8
+ }
@@ -0,0 +1,26 @@
1
+ local ____lualib = require("lualib_bundle")
2
+ local __TS__Class = ____lualib.__TS__Class
3
+ local __TS__ClassExtends = ____lualib.__TS__ClassExtends
4
+ local __TS__SetDescriptor = ____lualib.__TS__SetDescriptor
5
+ local ____exports = {}
6
+ local ____ability_2Dtype = require("engine.object-data.entry.ability-type")
7
+ local AbilityType = ____ability_2Dtype.AbilityType
8
+ ____exports.ReincarnationAbilityType = __TS__Class()
9
+ local ReincarnationAbilityType = ____exports.ReincarnationAbilityType
10
+ ReincarnationAbilityType.name = "ReincarnationAbilityType"
11
+ __TS__ClassExtends(ReincarnationAbilityType, AbilityType)
12
+ ReincarnationAbilityType.BASE_ID = fourCC("AOre")
13
+ __TS__SetDescriptor(
14
+ ReincarnationAbilityType.prototype,
15
+ "reincarnationDelay",
16
+ {
17
+ get = function(self)
18
+ return self:getNumberLevelField("Ore1")
19
+ end,
20
+ set = function(self, reincarnationDelay)
21
+ self:setNumberLevelField("Ore1", reincarnationDelay)
22
+ end
23
+ },
24
+ true
25
+ )
26
+ return ____exports
@@ -18,3 +18,4 @@ export declare const random: {
18
18
  <T>(element: T, ...elements: T[]): T;
19
19
  <T>(...elements: T[]): T | undefined;
20
20
  };
21
+ export declare const shuffle: (array: unknown[]) => void;
package/engine/random.lua CHANGED
@@ -28,4 +28,12 @@ ____exports.random = function(...)
28
28
  ...
29
29
  ))
30
30
  end
31
+ ____exports.shuffle = function(array)
32
+ for i = #array - 1, 1 do
33
+ local j = getRandomInt(0, i)
34
+ local value = array[i + 1]
35
+ array[i + 1] = array[j + 1]
36
+ array[j + 1] = value
37
+ end
38
+ end
31
39
  return ____exports
package/engine/unit.d.ts CHANGED
@@ -23,6 +23,7 @@ import "./internal/unit/attributes";
23
23
  import "./internal/unit/fly-height";
24
24
  import "./internal/unit/scale";
25
25
  import "./internal/unit/interrupts";
26
+ import "./internal/unit/range-event";
26
27
  import "./internal/unit/band-aids/ancestral-spirit-cannibalize";
27
28
  export { Unit, DamagingEvent, DamageEvent } from "./internal/unit";
28
29
  export * from "./internal/unit+damage";
package/engine/unit.lua CHANGED
@@ -23,6 +23,7 @@ require("engine.internal.unit.attributes")
23
23
  require("engine.internal.unit.fly-height")
24
24
  require("engine.internal.unit.scale")
25
25
  require("engine.internal.unit.interrupts")
26
+ require("engine.internal.unit.range-event")
26
27
  require("engine.internal.unit.band-aids.ancestral-spirit-cannibalize")
27
28
  do
28
29
  local ____unit = require("engine.internal.unit")
package/objutil/buff.lua CHANGED
@@ -29,6 +29,8 @@ local ____timer = require("core.types.timer")
29
29
  local Timer = ____timer.Timer
30
30
  local ____exception = require("exception")
31
31
  local IllegalStateException = ____exception.IllegalStateException
32
+ local ____linked_2Dmap = require("utility.linked-map")
33
+ local LinkedMap = ____linked_2Dmap.LinkedMap
32
34
  local ____assert = _G.assert
33
35
  local ____pairs = _G.pairs
34
36
  ____exports.BuffDefinition = __TS__Class()
@@ -461,7 +463,7 @@ local buffs = setmetatable(
461
463
  {
462
464
  __mode = "k",
463
465
  __index = function(self, unit)
464
- local ____table = {}
466
+ local ____table = __TS__New(LinkedMap)
465
467
  self[unit] = ____table
466
468
  return ____table
467
469
  end
@@ -494,7 +496,7 @@ function Buff.prototype.____constructor(self, unit, source, duration, level, pre
494
496
  end
495
497
  local constructor = self.constructor
496
498
  local instances = buffs[unit]
497
- local instance = instances[constructor]
499
+ local instance = instances:get(constructor)
498
500
  if instance then
499
501
  instance:destroy()
500
502
  if not instance.destroyed then
@@ -507,7 +509,7 @@ function Buff.prototype.____constructor(self, unit, source, duration, level, pre
507
509
  )
508
510
  end
509
511
  end
510
- instances[constructor] = self
512
+ instances:put(constructor, self)
511
513
  if preset then
512
514
  local unitHandle = unit.handle
513
515
  local slowPoisonMethod = not preset.positive and not preset.magic
@@ -586,13 +588,13 @@ function Buff.apply(self, ...)
586
588
  end
587
589
  end
588
590
  function Buff.getInstance(self, unit)
589
- return buffs[unit][self]
591
+ return buffs[unit]:get(self)
590
592
  end
591
593
  function Buff.isApplied(self, unit)
592
- return buffs[unit][self] ~= nil
594
+ return buffs[unit]:get(self) ~= nil
593
595
  end
594
596
  function Buff.ifApplied(self, unit, action)
595
- local instance = buffs[unit][self]
597
+ local instance = buffs[unit]:get(self)
596
598
  if instance then
597
599
  ____assert(__TS__InstanceOf(instance, self))
598
600
  action(instance)
@@ -615,7 +617,7 @@ function Buff.prototype.destroy(self)
615
617
  if self.preset then
616
618
  self.unit:removeAbility(self.preset.buffId)
617
619
  end
618
- buffs[self.unit][self.constructor] = nil
620
+ buffs[self.unit]:remove(self.constructor)
619
621
  self.destroyed = true
620
622
  end
621
623
  function Buff.prototype.onDispel(self, source)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "warscript",
4
- "version": "0.0.1-dev.91da42b",
4
+ "version": "0.0.1-dev.94cf7fc",
5
5
  "description": "A typescript library for Warcraft III using Warpack.",
6
6
  "keywords": [
7
7
  "warcraft",
@@ -24,7 +24,7 @@
24
24
  "@warscript/language-extensions": "^0.0.1",
25
25
  "@warscript/tstl-plugin": "^0.0.4",
26
26
  "lua-types": "^2.13.1",
27
- "warpack": "0.0.1-dev.efd0770"
27
+ "warpack": "0.0.1-dev.e490aaf"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@typescript-eslint/eslint-plugin": "^8.13.0",
package/patch-lua.lua CHANGED
@@ -1,5 +1,9 @@
1
+ local error = _G.error
1
2
  local getmetatable = _G.getmetatable
2
3
  local ipairs = _G.ipairs
4
+ local select = _G.select
5
+ local tostring = _G.tostring
6
+ local tableconcat = table.concat
3
7
 
4
8
  _G.ipairs = function(t)
5
9
  local metatable = getmetatable(t)
@@ -8,3 +12,14 @@ _G.ipairs = function(t)
8
12
  end
9
13
  return ipairs(t)
10
14
  end
15
+
16
+ _G.assert = function(v, ...)
17
+ if not v then
18
+ local args = {}
19
+ for i = 1, select("#", ...) do
20
+ args[i] = tostring(select(i, ...))
21
+ end
22
+ error(tableconcat(args, " "))
23
+ end
24
+ return v, ...
25
+ end
@@ -0,0 +1,34 @@
1
+ /** @noSelfInFile */
2
+ import { ReadonlyLinkedSet } from "./linked-set";
3
+ type IteratorState<K extends AnyNotNil, V> = {
4
+ n?: K;
5
+ t: LuaMap<K, K>;
6
+ v: LuaMap<K, V>;
7
+ };
8
+ type OneSidedTypeGuard = {
9
+ readonly __oneSidedTypeGuard: unique symbol;
10
+ };
11
+ export interface ReadonlyLinkedMap<K extends AnyNotNil, V> extends LuaPairsIterable<K, V> {
12
+ readonly keys: ReadonlyLinkedSet<K>;
13
+ get(key: K): V | undefined;
14
+ contains(key: AnyNotNil): key is K & OneSidedTypeGuard;
15
+ readonly size: number;
16
+ }
17
+ export interface LinkedMap<K extends AnyNotNil, V> extends LuaPairsIterable<K, V> {
18
+ readonly __linkedSet: unique symbol;
19
+ }
20
+ export declare class LinkedMap<K extends AnyNotNil, V> implements ReadonlyLinkedMap<K, V> {
21
+ private k;
22
+ private v;
23
+ get keys(): ReadonlyLinkedSet<K>;
24
+ get(key: K): V | undefined;
25
+ getOrPut(key: K, defaultValue: (this: void) => V): V;
26
+ put(key: K, value: V): void;
27
+ remove(key: K): boolean;
28
+ contains(key: AnyNotNil): key is K & OneSidedTypeGuard;
29
+ get size(): number;
30
+ protected __pairs(this: LinkedMap<K, V>): LuaIterator<LuaMultiReturn<[K | undefined, V | undefined]>, IteratorState<K, V>>;
31
+ }
32
+ export declare const emptyLinkedMap: <K extends AnyNotNil, V>() => ReadonlyLinkedMap<K, V>;
33
+ export declare const mutableLinkedMap: <K extends AnyNotNil, V>() => LinkedMap<K, V>;
34
+ export {};
@@ -0,0 +1,101 @@
1
+ local ____lualib = require("lualib_bundle")
2
+ local __TS__Class = ____lualib.__TS__Class
3
+ local __TS__New = ____lualib.__TS__New
4
+ local __TS__SetDescriptor = ____lualib.__TS__SetDescriptor
5
+ local __TS__ClassExtends = ____lualib.__TS__ClassExtends
6
+ local ____exports = {}
7
+ local ____linked_2Dset = require("utility.linked-set")
8
+ local LinkedSet = ____linked_2Dset.LinkedSet
9
+ local ____exception = require("exception")
10
+ local UnsupportedOperationException = ____exception.UnsupportedOperationException
11
+ local function linkedMapNext(state)
12
+ local n = state.n
13
+ state.n = state.t[n]
14
+ return n, state.v[n]
15
+ end
16
+ ____exports.LinkedMap = __TS__Class()
17
+ local LinkedMap = ____exports.LinkedMap
18
+ LinkedMap.name = "LinkedMap"
19
+ function LinkedMap.prototype.____constructor(self)
20
+ self.k = __TS__New(LinkedSet)
21
+ self.v = {}
22
+ end
23
+ function LinkedMap.prototype.get(self, key)
24
+ return self.v[key]
25
+ end
26
+ function LinkedMap.prototype.getOrPut(self, key, defaultValue)
27
+ local value = self.v[key]
28
+ if value ~= nil then
29
+ return value
30
+ end
31
+ value = defaultValue()
32
+ self.k:add(key)
33
+ self.v[key] = value
34
+ return value
35
+ end
36
+ function LinkedMap.prototype.put(self, key, value)
37
+ self.k:add(key)
38
+ self.v[key] = value
39
+ end
40
+ function LinkedMap.prototype.remove(self, key)
41
+ if self.k:remove(key) then
42
+ self.v[key] = nil
43
+ return true
44
+ end
45
+ return false
46
+ end
47
+ function LinkedMap.prototype.contains(self, key)
48
+ return self.keys:contains(key)
49
+ end
50
+ function LinkedMap.prototype.__pairs(self)
51
+ return linkedMapNext, {n = self.k.f, t = self.k.n, v = self.v}, nil
52
+ end
53
+ __TS__SetDescriptor(
54
+ LinkedMap.prototype,
55
+ "keys",
56
+ {get = function(self)
57
+ return self.k
58
+ end},
59
+ true
60
+ )
61
+ __TS__SetDescriptor(
62
+ LinkedMap.prototype,
63
+ "size",
64
+ {get = function(self)
65
+ return self.keys.size
66
+ end},
67
+ true
68
+ )
69
+ local emptyIteratorState = {t = {}, v = {}}
70
+ local EmptyLinkedMap = __TS__Class()
71
+ EmptyLinkedMap.name = "EmptyLinkedMap"
72
+ __TS__ClassExtends(EmptyLinkedMap, ____exports.LinkedMap)
73
+ function EmptyLinkedMap.prototype.getOrPut(self)
74
+ error(
75
+ __TS__New(UnsupportedOperationException),
76
+ 0
77
+ )
78
+ end
79
+ function EmptyLinkedMap.prototype.put(self)
80
+ error(
81
+ __TS__New(UnsupportedOperationException),
82
+ 0
83
+ )
84
+ end
85
+ function EmptyLinkedMap.prototype.remove(self)
86
+ error(
87
+ __TS__New(UnsupportedOperationException),
88
+ 0
89
+ )
90
+ end
91
+ function EmptyLinkedMap.prototype.__pairs(self)
92
+ return linkedMapNext, emptyIteratorState, nil
93
+ end
94
+ local EMPTY_LINKED_MAP = __TS__New(EmptyLinkedMap)
95
+ ____exports.emptyLinkedMap = function()
96
+ return EMPTY_LINKED_MAP
97
+ end
98
+ ____exports.mutableLinkedMap = function()
99
+ return __TS__New(____exports.LinkedMap)
100
+ end
101
+ return ____exports
@@ -14,7 +14,7 @@ export interface ReadonlyLinkedSet<T extends AnyNotNil> extends LuaPairsKeyItera
14
14
  next(key: T): T | undefined;
15
15
  previous(key: T): T | undefined;
16
16
  contains(key: AnyNotNil): key is T & OneSidedTypeGuard;
17
- size: number;
17
+ readonly size: number;
18
18
  forEach<Args extends any[]>(action: (value: T, ...args: Args) => void, ...args: Args): void;
19
19
  toArray(): T[];
20
20
  sumOf(selector: ((value: T) => number) | KeysOfType<T, number>): number;
@@ -40,6 +40,7 @@ export declare class LinkedSet<T extends AnyNotNil> implements ReadonlyLinkedSet
40
40
  next(key: T): T | undefined;
41
41
  previous(key: T): T | undefined;
42
42
  add(key: T): boolean;
43
+ protected addBefore(value: T, key: T): boolean;
43
44
  remove(key: T): boolean;
44
45
  contains(key: AnyNotNil): key is T & OneSidedTypeGuard;
45
46
  clear(): void;
@@ -85,6 +85,23 @@ function LinkedSet.prototype.add(self, key)
85
85
  end
86
86
  return true
87
87
  end
88
+ function LinkedSet.prototype.addBefore(self, value, key)
89
+ local n = self.n
90
+ local p = self.p
91
+ local previous = p[value]
92
+ if previous ~= nil then
93
+ n[previous] = key
94
+ p[key] = previous
95
+ elseif value == self.f then
96
+ self.f = key
97
+ else
98
+ return false
99
+ end
100
+ n[key] = value
101
+ p[value] = key
102
+ self.s = self.s + 1
103
+ return true
104
+ end
88
105
  function LinkedSet.prototype.remove(self, key)
89
106
  local n = self.n
90
107
  local next = n[key]
@@ -182,6 +199,7 @@ __TS__SetDescriptor(
182
199
  end},
183
200
  true
184
201
  )
202
+ local emptyIteratorState = {t = {}}
185
203
  local EmptyLinkedSet = __TS__Class()
186
204
  EmptyLinkedSet.name = "EmptyLinkedSet"
187
205
  __TS__ClassExtends(EmptyLinkedSet, ____exports.LinkedSet)
@@ -191,6 +209,9 @@ function EmptyLinkedSet.prototype.add(self)
191
209
  0
192
210
  )
193
211
  end
212
+ function EmptyLinkedSet.prototype.__pairs(self)
213
+ return linkedSetNext, emptyIteratorState, nil
214
+ end
194
215
  local EMPTY_LINKED_SET = __TS__New(EmptyLinkedSet)
195
216
  ____exports.emptyLinkedSet = function()
196
217
  return EMPTY_LINKED_SET
@@ -1,9 +1,28 @@
1
1
  local ____exports = {}
2
+ local ____pairs = _G.pairs
3
+ local tableSort = table.sort
2
4
  ____exports.invertRecord = function(record)
3
5
  local invertRecord = {}
4
- for key, value in pairs(record) do
6
+ for key, value in ____pairs(record) do
5
7
  invertRecord[value] = key
6
8
  end
7
9
  return invertRecord
8
10
  end
11
+ local sortedKeysInternal = {}
12
+ local lastSortedKeysLength = 0
13
+ ---
14
+ -- @internal For use by internal systems only.
15
+ ____exports.sortedKeysUnnested = function(record)
16
+ local length = 0
17
+ for key in ____pairs(record) do
18
+ length = length + 1
19
+ sortedKeysInternal[length] = key
20
+ end
21
+ for i = length + 1, lastSortedKeysLength do
22
+ sortedKeysInternal[i] = nil
23
+ end
24
+ lastSortedKeysLength = length
25
+ tableSort(sortedKeysInternal)
26
+ return sortedKeysInternal
27
+ end
9
28
  return ____exports