warscript 0.0.1-dev.da5fb2e → 0.0.1-dev.db137e7

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.
package/binarywriter.lua CHANGED
@@ -1,6 +1,8 @@
1
1
  local ____lualib = require("lualib_bundle")
2
2
  local __TS__Class = ____lualib.__TS__Class
3
3
  local ____exports = {}
4
+ local ____preconditions = require("utility.preconditions")
5
+ local requireNotNull = ____preconditions.requireNotNull
4
6
  local pack = string.pack
5
7
  local concat = table.concat
6
8
  local ____unpack = table.unpack
@@ -25,60 +27,70 @@ function BinaryWriter.prototype.write(self, fmt, value)
25
27
  self.i = i
26
28
  end
27
29
  function BinaryWriter.prototype.writeBytes(self, value)
30
+ requireNotNull(value)
28
31
  local i = self.i + 1
29
32
  self.f[i] = "c" .. tostring(#value)
30
33
  self.v[i] = value
31
34
  self.i = i
32
35
  end
33
36
  function BinaryWriter.prototype.writeDouble(self, value)
37
+ requireNotNull(value)
34
38
  local i = self.i + 1
35
39
  self.f[i] = "d"
36
40
  self.v[i] = value
37
41
  self.i = i
38
42
  end
39
43
  function BinaryWriter.prototype.writeFloat(self, value)
44
+ requireNotNull(value)
40
45
  local i = self.i + 1
41
46
  self.f[i] = "f"
42
47
  self.v[i] = value
43
48
  self.i = i
44
49
  end
45
50
  function BinaryWriter.prototype.writeInt16(self, value)
51
+ requireNotNull(value)
46
52
  local i = self.i + 1
47
53
  self.f[i] = "h"
48
54
  self.v[i] = value
49
55
  self.i = i
50
56
  end
51
57
  function BinaryWriter.prototype.writeInt32(self, value)
58
+ requireNotNull(value)
52
59
  local i = self.i + 1
53
60
  self.f[i] = "i4"
54
61
  self.v[i] = value
55
62
  self.i = i
56
63
  end
57
64
  function BinaryWriter.prototype.writeInt8(self, value)
65
+ requireNotNull(value)
58
66
  local i = self.i + 1
59
67
  self.f[i] = "b"
60
68
  self.v[i] = value
61
69
  self.i = i
62
70
  end
63
71
  function BinaryWriter.prototype.writeString(self, value)
72
+ requireNotNull(value)
64
73
  local i = self.i + 1
65
74
  self.f[i] = "z"
66
75
  self.v[i] = value
67
76
  self.i = i
68
77
  end
69
78
  function BinaryWriter.prototype.writeUInt16(self, value)
79
+ requireNotNull(value)
70
80
  local i = self.i + 1
71
81
  self.f[i] = "H"
72
82
  self.v[i] = value
73
83
  self.i = i
74
84
  end
75
85
  function BinaryWriter.prototype.writeUInt32(self, value)
86
+ requireNotNull(value)
76
87
  local i = self.i + 1
77
88
  self.f[i] = "I4"
78
89
  self.v[i] = value
79
90
  self.i = i
80
91
  end
81
92
  function BinaryWriter.prototype.writeUInt8(self, value)
93
+ requireNotNull(value)
82
94
  local i = self.i + 1
83
95
  self.f[i] = "B"
84
96
  self.v[i] = value
@@ -19,13 +19,16 @@ local ____frame_2Dcoordinates = require("engine.internal.misc.frame-coordinates"
19
19
  local FRAME_MAX_Y = ____frame_2Dcoordinates.FRAME_MAX_Y
20
20
  local FRAME_MIN_Y = ____frame_2Dcoordinates.FRAME_MIN_Y
21
21
  local getFrameMinXMaxX = ____frame_2Dcoordinates.getFrameMinXMaxX
22
+ local ____playerCamera = require("core.types.playerCamera")
23
+ local frameCoordinatesToWorld = ____playerCamera.frameCoordinatesToWorld
24
+ local worldCoordinatesToFrame = ____playerCamera.worldCoordinatesToFrame
25
+ local ____get_2Dterrain_2Dz = require("engine.internal.misc.get-terrain-z")
26
+ local getTerrainZ = ____get_2Dterrain_2Dz.getTerrainZ
22
27
  local frameClick = BlzFrameClick
23
28
  local frameGetEnable = BlzFrameGetEnable
24
29
  local frameIsVisible = BlzFrameIsVisible
25
30
  local frameSetEnable = BlzFrameSetEnable
26
31
  local frameSetScale = BlzFrameSetScale
27
- local getCameraTargetPositionX = GetCameraTargetPositionX
28
- local getCameraTargetPositionY = GetCameraTargetPositionY
29
32
  local ____rawget = _G.rawget
30
33
  local rawset = _G.rawset
31
34
  local invoke = Event.invoke
@@ -936,16 +939,19 @@ __TS__ObjectDefineProperty(
936
939
  local event = __TS__New(Event)
937
940
  local syncX = 0
938
941
  local syncY = 0
939
- local syncCamX = getCameraTargetPositionX()
940
- local syncCamY = getCameraTargetPositionY()
942
+ local syncFrameX = 0
943
+ local syncFrameY = 0
941
944
  local lastX = syncX
942
945
  local lastY = syncY
943
946
  self.onMouseMove:addListener(function(player, x, y)
944
947
  if player.isLocal then
945
948
  syncX = x
946
949
  syncY = y
947
- syncCamX = getCameraTargetPositionX()
948
- syncCamY = getCameraTargetPositionY()
950
+ syncFrameX, syncFrameY = worldCoordinatesToFrame(
951
+ x,
952
+ y,
953
+ getTerrainZ(x, y)
954
+ )
949
955
  lastX = x
950
956
  lastY = y
951
957
  invoke(event, x, y)
@@ -955,9 +961,8 @@ __TS__ObjectDefineProperty(
955
961
  if syncX == 0 and syncY == 0 then
956
962
  return
957
963
  end
958
- local x = syncX + (getCameraTargetPositionX() - syncCamX)
959
- local y = syncY + (getCameraTargetPositionY() - syncCamY)
960
- if x ~= lastX or y ~= lastY then
964
+ local x, y, ____, isDefinite = frameCoordinatesToWorld(syncFrameX, syncFrameY)
965
+ if isDefinite and (x ~= lastX or y ~= lastY) then
961
966
  lastX = x
962
967
  lastY = y
963
968
  invoke(event, x, y)
@@ -13,6 +13,8 @@ local ____frame_2Dcoordinates = require("engine.internal.misc.frame-coordinates"
13
13
  local FRAME_MAX_Y = ____frame_2Dcoordinates.FRAME_MAX_Y
14
14
  local FRAME_MIN_Y = ____frame_2Dcoordinates.FRAME_MIN_Y
15
15
  local getFrameMinXMaxX = ____frame_2Dcoordinates.getFrameMinXMaxX
16
+ local ____get_2Dterrain_2Dz = require("engine.internal.misc.get-terrain-z")
17
+ local getTerrainZ = ____get_2Dterrain_2Dz.getTerrainZ
16
18
  local getHandleId = GetHandleId
17
19
  local setCameraField = SetCameraField
18
20
  local getCameraField = GetCameraField
@@ -26,6 +28,7 @@ local resetToGameCamera = ResetToGameCamera
26
28
  local cos = math.cos
27
29
  local deg = math.deg
28
30
  local sin = math.sin
31
+ local sqrt = math.sqrt
29
32
  local memoized = {}
30
33
  ____exports.PlayerCamera = __TS__Class()
31
34
  local PlayerCamera = ____exports.PlayerCamera
@@ -233,4 +236,45 @@ ____exports.worldCoordinatesToFrame = function(x, y, z)
233
236
  local frameY = 0.42625 - yCenterScreenShift + (cameraAngleOfAttackSinRotationCos * dx + cameraAngleOfAttackSinRotationSin * dy - cameraAngleOfAttackCos * dz) / xPrime
234
237
  return frameX, frameY, xPrime < 0 and frameX >= frameMinX and frameX <= frameMaxX and frameY >= FRAME_MIN_Y and frameY <= FRAME_MAX_Y
235
238
  end
239
+ ---
240
+ -- @internal For use by internal systems only.
241
+ ____exports.frameCoordinatesToWorld = function(x, y)
242
+ if not isCameraViewPrecalculated then
243
+ precalculateCameraView()
244
+ end
245
+ local a = (x - 0.4) * scaleFactor
246
+ local b = (0.42625 - yCenterScreenShift - y) * scaleFactor
247
+ local nx = 1 / sqrt(1 + a * a + b * b)
248
+ local ny = sqrt(1 - (1 + b * b) * nx * nx)
249
+ local nz = sqrt(1 - nx * nx - ny * ny)
250
+ if a > 0 then
251
+ ny = -ny
252
+ end
253
+ if b < 0 then
254
+ nz = -nz
255
+ end
256
+ local nxPrime = cameraAngleOfAttackCosRotationCos * nx - cameraRotationSin * ny + cameraAngleOfAttackSinRotationCos * nz
257
+ local nyPrime = cameraAngleOfAttackCosRotationSin * nx + cameraRotationCos * ny + cameraAngleOfAttackSinRotationSin * nz
258
+ local nzPrime = -cameraAngleOfAttackSin * nx + cameraAngleOfAttackCos * nz
259
+ local zGuess = getTerrainZ(cameraEyeX, cameraEyeY)
260
+ local xGuess = cameraEyeX + nxPrime * (cameraEyeZ - zGuess) / nzPrime
261
+ local yGuess = cameraEyeY + nyPrime * (cameraEyeZ - zGuess) / nzPrime
262
+ local zWorld = getTerrainZ(xGuess, yGuess)
263
+ local deltaZ = zWorld - zGuess
264
+ zGuess = zWorld
265
+ local zWorldOld = zWorld
266
+ local deltaZOld = deltaZ
267
+ local i = 0
268
+ while (deltaZ > 1 or deltaZ < -1) and i < 50 do
269
+ xGuess = cameraEyeX + nxPrime * (cameraEyeZ - zGuess) / nzPrime
270
+ yGuess = cameraEyeY + nyPrime * (cameraEyeZ - zGuess) / nzPrime
271
+ zWorld = getTerrainZ(xGuess, yGuess)
272
+ deltaZ = zWorld - zGuess
273
+ zGuess = (deltaZOld * zWorld - deltaZ * zWorldOld) / (deltaZOld - deltaZ)
274
+ zWorldOld = zWorld
275
+ deltaZOld = deltaZ
276
+ i = i + 1
277
+ end
278
+ return xGuess, yGuess, zWorld, i < 50
279
+ end
236
280
  return ____exports
@@ -5,6 +5,15 @@ export declare class TileCell implements Readonly<Vec2> {
5
5
  readonly y: number;
6
6
  readonly z: undefined;
7
7
  protected constructor(id: number, x: number, y: number, z: undefined);
8
+ get up(): TileCell;
9
+ get down(): TileCell;
10
+ get left(): TileCell;
11
+ get right(): TileCell;
12
+ get terrainTypeId(): number;
13
+ set terrainTypeId(terrainTypeId: number);
14
+ get terrainVariance(): number;
15
+ set terrainVariance(terrainVariance: number);
16
+ randomizeTerrainVariance(): void;
8
17
  isInRangeOf(x: number, y: number, range: number): boolean;
9
18
  isInRangeOf(tileCell: TileCell, range: number): boolean;
10
19
  static getInRect(minX: number, minY: number, maxX: number, maxY: number): TileCell[];
@@ -1,7 +1,11 @@
1
1
  local ____lualib = require("lualib_bundle")
2
2
  local __TS__Class = ____lualib.__TS__Class
3
3
  local __TS__New = ____lualib.__TS__New
4
+ local __TS__SetDescriptor = ____lualib.__TS__SetDescriptor
4
5
  local ____exports = {}
6
+ local getTerrainType = GetTerrainType
7
+ local setTerrainType = SetTerrainType
8
+ local getTerrainVariance = GetTerrainVariance
5
9
  local abs = math.abs
6
10
  local ____type = math.type
7
11
  local ult = math.ult
@@ -16,6 +20,18 @@ function TileCell.prototype.____constructor(self, id, x, y, z)
16
20
  self.z = z
17
21
  tileCellById[id] = self
18
22
  end
23
+ function TileCell.prototype.randomizeTerrainVariance(self)
24
+ local x = self.x
25
+ local y = self.y
26
+ setTerrainType(
27
+ x,
28
+ y,
29
+ getTerrainType(x, y),
30
+ -1,
31
+ 1,
32
+ 1
33
+ )
34
+ end
19
35
  function TileCell.prototype.isInRangeOf(self, x, y, range)
20
36
  if range == nil then
21
37
  range = y
@@ -97,4 +113,80 @@ function TileCell.of(x, y)
97
113
  nil
98
114
  )
99
115
  end
116
+ __TS__SetDescriptor(
117
+ TileCell.prototype,
118
+ "up",
119
+ {get = function(self)
120
+ return ____exports.TileCell.of(self.x, self.y + 128)
121
+ end},
122
+ true
123
+ )
124
+ __TS__SetDescriptor(
125
+ TileCell.prototype,
126
+ "down",
127
+ {get = function(self)
128
+ return ____exports.TileCell.of(self.x, self.y - 128)
129
+ end},
130
+ true
131
+ )
132
+ __TS__SetDescriptor(
133
+ TileCell.prototype,
134
+ "left",
135
+ {get = function(self)
136
+ return ____exports.TileCell.of(self.x - 128, self.y)
137
+ end},
138
+ true
139
+ )
140
+ __TS__SetDescriptor(
141
+ TileCell.prototype,
142
+ "right",
143
+ {get = function(self)
144
+ return ____exports.TileCell.of(self.x + 128, self.y)
145
+ end},
146
+ true
147
+ )
148
+ __TS__SetDescriptor(
149
+ TileCell.prototype,
150
+ "terrainTypeId",
151
+ {
152
+ get = function(self)
153
+ return getTerrainType(self.x, self.y)
154
+ end,
155
+ set = function(self, terrainTypeId)
156
+ local x = self.x
157
+ local y = self.y
158
+ setTerrainType(
159
+ x,
160
+ y,
161
+ terrainTypeId,
162
+ getTerrainVariance(x, y),
163
+ 1,
164
+ 1
165
+ )
166
+ end
167
+ },
168
+ true
169
+ )
170
+ __TS__SetDescriptor(
171
+ TileCell.prototype,
172
+ "terrainVariance",
173
+ {
174
+ get = function(self)
175
+ return getTerrainVariance(self.x, self.y)
176
+ end,
177
+ set = function(self, terrainVariance)
178
+ local x = self.x
179
+ local y = self.y
180
+ setTerrainType(
181
+ x,
182
+ y,
183
+ getTerrainType(x, y),
184
+ terrainVariance,
185
+ 1,
186
+ 1
187
+ )
188
+ end
189
+ },
190
+ true
191
+ )
100
192
  return ____exports
package/decl/native.d.ts CHANGED
@@ -5259,8 +5259,8 @@ declare function BlzRemoveAbilityStringLevelArrayField(
5259
5259
  level: number,
5260
5260
  value: string,
5261
5261
  ): boolean
5262
- declare function BlzGetItemAbilityByIndex(whichItem: jitem, index: number): jability | null
5263
- declare function BlzGetItemAbility(whichItem: jitem, abilCode: number): jability | null
5262
+ declare function BlzGetItemAbilityByIndex(whichItem: jitem, index: number): jability | undefined
5263
+ declare function BlzGetItemAbility(whichItem: jitem, abilCode: number): jability | undefined
5264
5264
  declare function BlzItemAddAbility(whichItem: jitem, abilCode: number): boolean
5265
5265
  declare function BlzGetItemBooleanField(whichItem: jitem, whichField: jitembooleanfield): boolean
5266
5266
  declare function BlzGetItemIntegerField(whichItem: jitem, whichField: jitemintegerfield): number
@@ -8,6 +8,7 @@ export type StunImmunityUnitBehaviourParameters = {
8
8
  buffTypeIds?: LuaSet<BuffTypeId>;
9
9
  textTagPreset?: TextTagPreset;
10
10
  textTagText?: string;
11
+ additionalAction?: (this: void, unit: Unit) => void;
11
12
  };
12
13
  export declare class StunImmunityUnitBehavior extends UnitBehavior {
13
14
  readonly parameters: Readonly<StunImmunityUnitBehaviourParameters>;
@@ -17,4 +18,5 @@ export declare class StunImmunityUnitBehavior extends UnitBehavior {
17
18
  onDamageReceived(): void;
18
19
  onTargetingAbilityChannelingStart(): void;
19
20
  onTargetingAbilityImpact(): void;
21
+ protected onEffect(): void;
20
22
  }
@@ -48,8 +48,15 @@ local function process(behavior)
48
48
  for buffTypeId in pairs(behavior.parameters.buffTypeIds or DEFAULT_BUFF_TYPE_IDS) do
49
49
  hasRemovedBuffs = hasRemovedBuffs or behavior.unit:removeBuff(buffTypeId)
50
50
  end
51
- if hasRemovedBuffs and behavior.parameters.textTagText ~= nil then
52
- TextTag:flash(TextTag.MISS, behavior.parameters.textTagText, behavior.unit.x, behavior.unit.y)
51
+ if hasRemovedBuffs then
52
+ behavior.onEffect(behavior)
53
+ if behavior.parameters.textTagText ~= nil then
54
+ TextTag:flash(TextTag.MISS, behavior.parameters.textTagText, behavior.unit.x, behavior.unit.y)
55
+ end
56
+ local ____opt_0 = behavior.parameters.additionalAction
57
+ if ____opt_0 ~= nil then
58
+ ____opt_0(behavior.unit)
59
+ end
53
60
  end
54
61
  end
55
62
  ____exports.StunImmunityUnitBehavior = __TS__Class()
@@ -79,5 +86,7 @@ end
79
86
  function StunImmunityUnitBehavior.prototype.onTargetingAbilityImpact(self)
80
87
  process(self)
81
88
  end
89
+ function StunImmunityUnitBehavior.prototype.onEffect(self)
90
+ end
82
91
  StunImmunityUnitBehavior.defaultParameters = {buffTypeIds = DEFAULT_BUFF_TYPE_IDS, textTagPreset = TextTag.MISS, textTagText = nil}
83
92
  return ____exports
@@ -13,7 +13,6 @@ local ____ability = require("engine.internal.item.ability")
13
13
  local abilityActionDummy = ____ability.abilityActionDummy
14
14
  local doAbilityAction = ____ability.doAbilityAction
15
15
  local doAbilityActionForceDummy = ____ability.doAbilityActionForceDummy
16
- local doUnitAbilityAction = ____ability.doUnitAbilityAction
17
16
  local startItemCooldown = ____ability.startItemCooldown
18
17
  local getUnitAbilityLevel = GetUnitAbilityLevel
19
18
  local setUnitAbilityLevel = SetUnitAbilityLevel
@@ -40,8 +39,6 @@ local getItemBooleanField = BlzGetItemBooleanField
40
39
  local setItemBooleanField = BlzSetItemBooleanField
41
40
  local unitHideAbility = BlzUnitHideAbility
42
41
  local unitDisableAbility = BlzUnitDisableAbility
43
- local unitRemoveAbility = UnitRemoveAbility
44
- local itemRemoveAbility = BlzItemRemoveAbility
45
42
  local match = string.match
46
43
  local ____type = _G.type
47
44
  local ____tostring = _G.tostring
@@ -429,7 +426,9 @@ function UnitAbility.prototype.interruptCast(self)
429
426
  self.owner:interruptCast(self.typeId)
430
427
  end
431
428
  function UnitAbility.prototype.onDestroy(self)
432
- doUnitAbilityAction(self.owner.handle, self.typeId, unitRemoveAbility, self.typeId)
429
+ if self.owner.state ~= 2 then
430
+ self.owner:removeAbility(self.typeId)
431
+ end
433
432
  return UnitAbility.____super.prototype.onDestroy(self)
434
433
  end
435
434
  __TS__SetDescriptor(
@@ -525,7 +524,9 @@ function ItemAbility.prototype.interruptCast(self)
525
524
  end
526
525
  end
527
526
  function ItemAbility.prototype.onDestroy(self)
528
- doAbilityAction(self.owner.handle, itemRemoveAbility, self.typeId)
527
+ if self.owner.state ~= 2 then
528
+ self.owner:removeAbility(self.typeId)
529
+ end
529
530
  return ItemAbility.____super.prototype.onDestroy(self)
530
531
  end
531
532
  __TS__SetDescriptor(
@@ -8,12 +8,10 @@ import { AbilityTypeId } from "../object-data/entry/ability-type";
8
8
  import type { ItemTypeId } from "../object-data/entry/item-type";
9
9
  type DefenseType = 0 | 1 | 2 | 3 | 4 | 5;
10
10
  declare const enum ItemPropertyKey {
11
- ABILITIES = 100,
12
- LUA_INDEX_BY_ABILITY_TYPE_ID = 101
11
+ ABILITIES = 100
13
12
  }
14
13
  export declare class Item extends Handle<jitem> {
15
14
  private readonly [ItemPropertyKey.ABILITIES];
16
- private readonly [ItemPropertyKey.LUA_INDEX_BY_ABILITY_TYPE_ID];
17
15
  constructor(handle: jitem);
18
16
  protected onDestroy(): HandleDestructor;
19
17
  static create<T extends Item>(this: typeof Item & (new (handle: jitem) => T), id: number, x: number, y: number, skinId?: number): T;
@@ -29,6 +29,7 @@ local ____vec2 = require("math.vec2")
29
29
  local distance = ____vec2.distance
30
30
  local itemChargesChangeEvent = __TS__New(Event)
31
31
  local itemAddAbility = BlzItemAddAbility
32
+ local itemRemoveAbility = BlzItemRemoveAbility
32
33
  local getItemAbility = BlzGetItemAbility
33
34
  local isItemPowerup = IsItemPowerup
34
35
  local getItemAbilityByIndex = BlzGetItemAbilityByIndex
@@ -138,13 +139,7 @@ Item.name = "Item"
138
139
  __TS__ClassExtends(Item, Handle)
139
140
  function Item.prototype.____constructor(self, handle)
140
141
  Handle.prototype.____constructor(self, handle)
141
- local abilities = doAbilityAction(handle, getItemAbilities, self)
142
- self[100] = abilities
143
- local luaIndexByAbilityTypeId = {}
144
- for i = 1, #abilities do
145
- luaIndexByAbilityTypeId[abilities[i].typeId] = i
146
- end
147
- self[101] = luaIndexByAbilityTypeId
142
+ self[100] = doAbilityAction(handle, getItemAbilities, self)
148
143
  end
149
144
  function Item.prototype.onDestroy(self)
150
145
  local owner = self.owner
@@ -196,31 +191,32 @@ function Item.prototype.addAbility(self, abilityTypeId)
196
191
  if nativeAbility ~= nil then
197
192
  local ability = ItemAbility:of(nativeAbility, abilityTypeId, self)
198
193
  local abilities = self[100]
199
- local luaIndex = #abilities + 1
200
- abilities[luaIndex] = ability
201
- self[101][abilityTypeId] = luaIndex
194
+ abilities[#abilities + 1] = ability
202
195
  return ability
203
196
  end
204
197
  return nil
205
198
  end
206
199
  function Item.prototype.removeAbility(self, abilityTypeId)
207
- local luaIndexByAbilityTypeId = self[101]
208
- local luaIndex = luaIndexByAbilityTypeId[abilityTypeId]
209
- if luaIndex ~= nil then
210
- local abilities = self[100]
211
- abilities[luaIndex]:destroy()
212
- tableRemove(abilities, luaIndex)
213
- luaIndexByAbilityTypeId[abilityTypeId] = nil
214
- return true
200
+ local abilities = self[100]
201
+ for i = 1, #abilities do
202
+ if abilities[i].typeId == abilityTypeId then
203
+ local ability = abilities[i]
204
+ tableRemove(abilities, i)
205
+ ability:destroy()
206
+ return true
207
+ end
215
208
  end
216
- return false
209
+ return doAbilityAction(self.handle, itemRemoveAbility, abilityTypeId)
217
210
  end
218
211
  function Item.prototype.hasAbility(self, abilityTypeId)
219
- return self[101][abilityTypeId] ~= nil
212
+ return doAbilityAction(self.handle, getItemAbility, abilityTypeId) ~= nil
220
213
  end
221
214
  function Item.prototype.getAbility(self, abilityTypeId)
222
- local ability = self[101][abilityTypeId] ~= nil and doAbilityAction(self.handle, getItemAbility, abilityTypeId)
223
- return ability and ItemAbility:of(ability, abilityTypeId, self) or nil
215
+ return ItemAbility:of(
216
+ doAbilityAction(self.handle, getItemAbility, abilityTypeId),
217
+ abilityTypeId,
218
+ self
219
+ )
224
220
  end
225
221
  function Item.getInRange(self, x, y, range)
226
222
  targetCollection = {}
@@ -438,7 +438,7 @@ rawset(
438
438
  condition(function()
439
439
  local unit = Unit:of(getTriggerUnit())
440
440
  if unit ~= nil then
441
- local ability = unit:getAbilityById(abilityTypeId)
441
+ local ability = unit:getAbility(abilityTypeId)
442
442
  if ability ~= nil then
443
443
  eventInvoke(event, unit, ability, orderTypeStringId)
444
444
  end
@@ -271,8 +271,8 @@ export declare class Unit extends Handle<junit> {
271
271
  setAbilityLevel(abilityId: number, level: number): number;
272
272
  getAbilityLevel(abilityId: number): number;
273
273
  hasAbility(abilityId: number): boolean;
274
- getAbilityById(abilityId: number): UnitAbility | undefined;
275
- removeAbility(abilityId: number): boolean;
274
+ getAbility(abilityId: number): UnitAbility | undefined;
275
+ removeAbility(abilityTypeId: number): boolean;
276
276
  hideAbility(abilityId: number, flag: boolean): void;
277
277
  getAbilityRemainingCooldown(abilityId: number): number;
278
278
  startAbilityCooldown(abilityId: number, cooldown: number): void;
@@ -935,20 +935,21 @@ end
935
935
  function Unit.prototype.hasAbility(self, abilityId)
936
936
  return getUnitAbilityLevel(self.handle, abilityId) > 0
937
937
  end
938
- function Unit.prototype.getAbilityById(self, abilityId)
938
+ function Unit.prototype.getAbility(self, abilityId)
939
939
  local ability = doUnitAbilityAction(self.handle, abilityId, getUnitAbility, abilityId)
940
940
  return UnitAbility:of(ability, abilityId, self)
941
941
  end
942
- function Unit.prototype.removeAbility(self, abilityId)
942
+ function Unit.prototype.removeAbility(self, abilityTypeId)
943
943
  local abilities = self.abilities
944
944
  for i = 1, #abilities do
945
- if abilities[i].typeId == abilityId then
946
- abilities[i]:destroy()
945
+ if abilities[i].typeId == abilityTypeId then
946
+ local ability = abilities[i]
947
947
  tremove(abilities, i)
948
+ ability:destroy()
948
949
  return true
949
950
  end
950
951
  end
951
- return false
952
+ return doUnitAbilityAction(self.handle, abilityTypeId, unitRemoveAbility, abilityTypeId)
952
953
  end
953
954
  function Unit.prototype.hideAbility(self, abilityId, flag)
954
955
  BlzUnitHideAbility(self.handle, abilityId, flag)
@@ -1,4 +1,5 @@
1
1
  /** @noSelfInFile */
2
+ import { ReadonlyNonEmptyArray } from "../utility/types";
2
3
  export declare const randomAngle: () => number;
3
4
  export declare const randomInteger: {
4
5
  (upperBound?: number): number;
@@ -9,3 +10,11 @@ export declare const randomFloat: {
9
10
  (lowerBound: number, upperBound: number): number;
10
11
  };
11
12
  export declare const randomXY: (centerX: number, centerY: number, range: number) => LuaMultiReturn<[x: number, y: number]>;
13
+ export declare const randomElement: {
14
+ <T>(array: ReadonlyNonEmptyArray<T>): T;
15
+ <T>(array: ReadonlyArray<T>): T | undefined;
16
+ };
17
+ export declare const random: {
18
+ <T>(element: T, ...elements: T[]): T;
19
+ <T>(...elements: T[]): T | undefined;
20
+ };
package/engine/random.lua CHANGED
@@ -4,6 +4,7 @@ local MAXIMUM_INTEGER = ____math.MAXIMUM_INTEGER
4
4
  local PI = ____math.PI
5
5
  local getRandomInt = GetRandomInt
6
6
  local getRandomReal = GetRandomReal
7
+ local select = _G.select
7
8
  local cos = math.cos
8
9
  local sin = math.sin
9
10
  local sqrt = math.sqrt
@@ -15,4 +16,16 @@ ____exports.randomXY = function(centerX, centerY, range)
15
16
  local t = getRandomReal(0, 1) * 2 * PI
16
17
  return centerX + r * cos(t), centerY + r * sin(t)
17
18
  end
19
+ ____exports.randomElement = function(array)
20
+ return array[getRandomInt(1, #array)]
21
+ end
22
+ ____exports.random = function(...)
23
+ return (select(
24
+ getRandomInt(
25
+ 1,
26
+ select("#", ...)
27
+ ),
28
+ ...
29
+ ))
30
+ end
18
31
  return ____exports
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.da5fb2e",
4
+ "version": "0.0.1-dev.db137e7",
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.78d2c64"
27
+ "warpack": "0.0.1-dev.251db08"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@typescript-eslint/eslint-plugin": "^8.13.0",
@@ -5,6 +5,7 @@ export declare const joinToString: <T>(array: readonly T[], separator: string, t
5
5
  export declare const arrayOfNotNull: <T>(...elements: readonly (T | undefined | null)[]) => T[];
6
6
  export declare const array: <T, N extends number>(length: N, initialize: (index: number) => T) => TupleOf<T, N>;
7
7
  export declare const toLuaSet: <T extends AnyNotNil>(array: readonly T[]) => LuaSet<T>;
8
+ export declare const contains: <T>(array: readonly T[], element: T) => boolean;
8
9
  export declare const forEach: <T, Args extends any[]>(array: readonly T[], consumerOrKey: ((value: T, ...args: Args) => void) | KeysOfType<T, (this: T, ...args: Args) => void>, ...args: Args) => void;
9
10
  export declare const all: {
10
11
  <T>(array: readonly T[], transform: (value: T) => boolean): boolean;
@@ -49,6 +49,14 @@ ____exports.toLuaSet = function(array)
49
49
  end
50
50
  return result
51
51
  end
52
+ ____exports.contains = function(array, element)
53
+ for i = 1, #array do
54
+ if array[i] == element then
55
+ return true
56
+ end
57
+ end
58
+ return false
59
+ end
52
60
  ____exports.forEach = function(array, consumerOrKey, ...)
53
61
  if type(consumerOrKey) == "function" then
54
62
  for i = 1, #array do
@@ -4,6 +4,15 @@ export declare const emptyLuaMap: <K extends AnyNotNil, V>() => ReadonlyLuaMap<K
4
4
  export declare const mutableLuaMap: <K extends AnyNotNil, V>() => LuaMap<K, V>;
5
5
  export declare const mutableWeakLuaMap: <K extends AnyNotNil, V>() => LuaMap<K, V>;
6
6
  export declare const luaMapOf: <K extends AnyNotNil, V>(...pairs: Flatten<TupleOf<[K, V], 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40>>) => LuaMap<K, V>;
7
- export declare const luaMapInvert: <K extends AnyNotNil, V extends AnyNotNil>(luaMap: LuaMap<K, V>) => LuaMap<V, K>;
8
- export declare const mapValues: <K extends AnyNotNil, V1, V2>(luaMap: LuaMap<K, V1>, transform: (value: V1) => V2) => LuaMap<K, V2>;
7
+ export declare const luaMapInvert: <K extends AnyNotNil, V extends AnyNotNil>(luaMap: ReadonlyLuaMap<K, V | undefined | null>) => LuaMap<V, K>;
8
+ export declare const toLuaMap: <K extends PropertyKey, V>(record: Record<K, V>) => LuaMap<K, V>;
9
+ export declare const flattenKeys: <K extends AnyNotNil, V>(luaMap: ReadonlyLuaMap<readonly K[], V> | ReadonlyLuaMap<K[], V>) => LuaMap<K, V>;
10
+ export declare const mapKeys: {
11
+ <K1 extends AnyNotNil, K2 extends AnyNotNil, V>(luaMap: ReadonlyLuaMap<K1, V>, transform: (value: K1) => K2): LuaMap<K2, V>;
12
+ <K1 extends AnyNotNil, K2 extends keyof K1, V>(luaMap: ReadonlyLuaMap<K1, V>, key: K2): K1[K2] extends AnyNotNil ? LuaMap<K1[K2], V> : never;
13
+ };
14
+ export declare const mapValues: {
15
+ <K extends AnyNotNil, V1, V2>(luaMap: ReadonlyLuaMap<K, V1>, transform: (value: V1) => V2): LuaMap<K, V2>;
16
+ <K extends AnyNotNil, V1, V2 extends keyof V1>(luaMap: ReadonlyLuaMap<K, V1>, key: V2): LuaMap<K, V1[V2]>;
17
+ };
9
18
  export declare const getOrPut: <K extends AnyNotNil, V>(luaMap: LuaMap<K, V>, key: K, defaultValue: () => V) => V;
@@ -27,10 +27,41 @@ ____exports.luaMapInvert = function(luaMap)
27
27
  end
28
28
  return invertLuaMap
29
29
  end
30
+ ____exports.toLuaMap = function(record)
31
+ return record
32
+ end
33
+ ____exports.flattenKeys = function(luaMap)
34
+ local result = {}
35
+ for keys, value in pairs(luaMap) do
36
+ for ____, key in ipairs(keys) do
37
+ result[key] = value
38
+ end
39
+ end
40
+ return result
41
+ end
42
+ ____exports.mapKeys = function(luaMap, transform)
43
+ local result = {}
44
+ if type(transform) == "function" then
45
+ for key, value in pairs(luaMap) do
46
+ result[transform(key)] = value
47
+ end
48
+ else
49
+ for key, value in pairs(luaMap) do
50
+ result[key[transform]] = value
51
+ end
52
+ end
53
+ return result
54
+ end
30
55
  ____exports.mapValues = function(luaMap, transform)
31
56
  local result = {}
32
- for key, value in pairs(luaMap) do
33
- result[key] = transform(value)
57
+ if type(transform) == "function" then
58
+ for key, value in pairs(luaMap) do
59
+ result[key] = transform(value)
60
+ end
61
+ else
62
+ for key, value in pairs(luaMap) do
63
+ result[key] = value[transform]
64
+ end
34
65
  end
35
66
  return result
36
67
  end