warcraft-3-w3ts-utils 0.1.1

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.
@@ -0,0 +1,179 @@
1
+ import { Effect, MapPlayer, Timer, Unit } from "w3ts";
2
+
3
+ type ProperColors = "goldenrod" | "magenta" | "green" | "yellow" | "red" | "player1-red" | "player2-blue" | "player3-teal" | "player4-purple" | "player5-yellow" | "player6-orange";
4
+
5
+ export function tColor(text: string | number, color?: ProperColors | null, hex?: PlayerColorHex | string | null, alpha?: string) {
6
+ if (color) {
7
+ return `|cff${properColorHexes.get(color) || "FFFFFF"}${alpha || ""}${text}|r`;
8
+ } else if (hex) {
9
+ return `|cff${hex}${alpha || ""}${text}|r`;
10
+ }
11
+
12
+ return String(text);
13
+ }
14
+
15
+ /**
16
+ * Colorizes the string according to the map player
17
+ */
18
+ export function ptColor(player: MapPlayer, text: string) {
19
+ return `${tColor(text, undefined, playerColors[player.id])}`;
20
+ }
21
+
22
+ const properColorHexes = new Map<ProperColors, string>([
23
+ ["goldenrod", "ffcc00"],
24
+ ["green", "00FF00"],
25
+ ["yellow", "FFFF00"],
26
+ ["red", "FF0000"],
27
+ ["magenta", "FF00FF"],
28
+ ["player1-red", "ff0303"],
29
+ ["player2-blue", "0042ff"],
30
+ ["player3-teal", "1ce6b9"],
31
+ ["player4-purple", "540081"],
32
+ ["player5-yellow", "fffc00"],
33
+ ["player6-orange", "fe8a0e"],
34
+ ]);
35
+ // enum PlayerColors {
36
+ // Player1 = "ff0303", // Red
37
+ // Player2 = "0042ff", // Blue
38
+ // Player3 = "1be7ba", // Teal
39
+ // Player4 = "550081", // Purple
40
+ // Player5 = "fefc00", // Yellow
41
+ // Player6 = "fe890d", // Orange
42
+ // Player7 = "21bf00", // Green
43
+ // Player8 = "e45caf", // Pink
44
+ // Player9 = "939596", // Gray
45
+ // Player10 = "7ebff1", // Light Blue
46
+ // Player11 = "106247", // Dark Green
47
+ // Player12 = "4f2b05", // Brown
48
+ // Player13 = "9c0000", // Maroon
49
+ // Player14 = "0000c3", // Navy
50
+ // Player15 = "00ebff", // Turquoise
51
+ // Player16 = "bd00ff", // Violet
52
+ // Player17 = "ecce87", // Wheat
53
+ // Player18 = "f7a58b", // Peach
54
+ // Player19 = "bfff81", // Mint
55
+ // Player20 = "dbb8eb", // Lavender
56
+ // Player21 = "4f5055", // Coal
57
+ // Player22 = "ecf0ff", // Snow
58
+ // Player23 = "00781e", // Emerald
59
+ // Player24 = "a56f34", // Peanut
60
+ // PlayerNeutral = "2e2d2e", // Black
61
+ // }
62
+
63
+ export enum PlayerColorHex {
64
+ Red = "ff0303",
65
+ Blue = "0042ff",
66
+ Teal = "1be7ba",
67
+ Purple = "550081",
68
+ Yellow = "fefc00",
69
+ Orange = "fe890d",
70
+ Green = "21bf00",
71
+ Pink = "e45caf",
72
+ Gray = "939596",
73
+ LightBlue = "7ebff1",
74
+ DarkGreen = "106247",
75
+ Brown = "4f2b05",
76
+ Maroon = "9c0000",
77
+ Navy = "0000c3",
78
+ Turquoise = "00ebff",
79
+ Violet = "bd00ff",
80
+ Wheat = "ecce87",
81
+ Peach = "f7a58b",
82
+ Mint = "bfff81",
83
+ Lavender = "dbb8eb",
84
+ Coal = "4f5055",
85
+ Snow = "ecf0ff",
86
+ Emerald = "00781e",
87
+ Peanut = "a56f34",
88
+ Black = "2e2d2e",
89
+ }
90
+
91
+ const playerColors = [
92
+ "ff0303", // Player 1: Red
93
+ "0042ff", // Player 2: Blue
94
+ "1be7ba", // Player 3: Teal
95
+ "550081", // Player 4: Purple
96
+ "fefc00", // Player 5: Yellow
97
+ "fe890d", // Player 6: Orange
98
+ "21bf00", // Player 7: Green
99
+ "e45caf", // Player 8: Pink
100
+ "939596", // Player 9: Gray
101
+ "7ebff1", // Player 10: Light Blue
102
+ "106247", // Player 11: Dark Green
103
+ "4f2b05", // Player 12: Brown
104
+ "9c0000", // Player 13: Maroon
105
+ "0000c3", // Player 14: Navy
106
+ "00ebff", // Player 15: Turquoise
107
+ "bd00ff", // Player 16: Violet
108
+ "ecce87", // Player 17: Wheat
109
+ "f7a58b", // Player 18: Peach
110
+ "bfff81", // Player 19: Mint
111
+ "dbb8eb", // Player 20: Lavender
112
+ "4f5055", // Player 21: Coal
113
+ "ecf0ff", // Player 22: Snow
114
+ "00781e", // Player 23: Emerald
115
+ "a56f34", // Player 24: Peanut
116
+ "2e2d2e", // Player Neutral: Black
117
+ ];
118
+
119
+ /**
120
+ * Standardized format for notifying player of events.
121
+ */
122
+ export function notifyPlayer(msg: string) {
123
+ print(`${tColor("!", "goldenrod")} - ${msg}`);
124
+ }
125
+
126
+ export function displayError(msg: string) {
127
+ print(`[ ${tColor("WARNING", "red")} ]: ${msg}`);
128
+ }
129
+
130
+ /**
131
+ * Returns degrees or radians?
132
+ */
133
+ export function getRelativeAngleToUnit(unit: Unit, relativeUnit: Unit) {
134
+ const locA = GetUnitLoc(unit.handle);
135
+ const locB = GetUnitLoc(relativeUnit.handle);
136
+ const angle = AngleBetweenPoints(locA, locB);
137
+
138
+ RemoveLocation(locA);
139
+ RemoveLocation(locB);
140
+
141
+ return angle;
142
+ }
143
+
144
+ /**
145
+ * Manages state of effects in this context so you don't have to!
146
+ */
147
+ export function useEffects() {
148
+ const effects: Effect[] = [];
149
+
150
+ return {
151
+ addEffect: (effect: Effect | undefined) => {
152
+ if (effect) {
153
+ effects.push(effect);
154
+ }
155
+ },
156
+ /**
157
+ * @returns reference to effects array
158
+ */
159
+ getEffects: () => {
160
+ return effects;
161
+ },
162
+ destroyAllEffects: () => {
163
+ effects.forEach((e) => {
164
+ e.destroy();
165
+ });
166
+ },
167
+ };
168
+ }
169
+
170
+ export function useTempEffect(effect: Effect | undefined, duration: number = 1.5) {
171
+ if (effect) {
172
+ const timer = Timer.create();
173
+
174
+ timer.start(duration, false, () => {
175
+ effect.destroy();
176
+ timer.destroy();
177
+ });
178
+ }
179
+ }
@@ -0,0 +1,295 @@
1
+ // import { WTS_Units } from "src/enums/WTS_Enums";
2
+ // import { PlayerIndex } from "src/player/player-data";
3
+ import { Effect, Timer, Unit } from "w3ts";
4
+ import { OrderId, Players } from "w3ts/globals";
5
+
6
+ interface ApplyForceConfig {
7
+ /**
8
+ * Default: 0
9
+ */
10
+ sustainedForceDuration?: number;
11
+ /**
12
+ * Default: -2400 units per second
13
+ */
14
+ frictionConstant?: number;
15
+ /**
16
+ * The unit will not phase through unpathable areas
17
+ */
18
+ obeyPathing?: boolean;
19
+ whileActive?: (currentSpeed?: number, timeElapsed?: number) => void;
20
+ onStart?: (currentSpeed?: number, timeElapsed?: number) => void;
21
+ onEnd?: (currentSpeed?: number, timeElapsed?: number) => void;
22
+ /**
23
+ *
24
+ */
25
+ targetSpeed?: number;
26
+ /**
27
+ * In seconds
28
+ */
29
+ accelerationTime?: number;
30
+ /**
31
+ * This will be used to decide if the unit the force is applied on is allowed to deviate from their current path.
32
+ */
33
+ strictPathing?: boolean;
34
+ animationIndexNumber?: number;
35
+ dummyUnitPlayerIndex: number;
36
+ dummyUnitFourCC: number;
37
+ }
38
+
39
+ /**
40
+ * @param angle degrees
41
+ * @param unit
42
+ * @param initialSpeed meters per second
43
+ * @param affectHeight determines whether or not to change unit height whilst force is applied
44
+ */
45
+ export function applyForce(
46
+ angle: number,
47
+ unit: Unit,
48
+ initialSpeed: number,
49
+ config: ApplyForceConfig
50
+ ) {
51
+ const timer = Timer.create();
52
+ const refreshInterval = 0.01;
53
+ const updatesPerSecond = 1 / refreshInterval;
54
+ const frictionConstant = 4800; //meters per second friction decay
55
+ let currentSpeed = initialSpeed;
56
+ let timeElapsed = 0;
57
+
58
+ const clickMoveOrder = 851971;
59
+ const moveOrders = [
60
+ OrderId.Move,
61
+ OrderId.Attackground,
62
+ OrderId.Patrol,
63
+ OrderId.Attack,
64
+ clickMoveOrder,
65
+ ];
66
+
67
+ let forceDummyUnit: Unit | undefined = undefined;
68
+ const defaultX = 11800;
69
+ const defaultY = -5700;
70
+
71
+ //Cancel unit commands - if a unit already has a move command and are applied a force, they will bug out sometimes and walk in the opposite direction
72
+ // unit.issueImmediateOrder(OrderId.Stop);
73
+
74
+ /**
75
+ * Prematurely end the force effect
76
+ */
77
+ function destroyForceEffect(runOnEnd: boolean = false) {
78
+ if (runOnEnd && config?.onEnd) {
79
+ config.onEnd();
80
+ }
81
+
82
+ ResetUnitAnimation(unit.handle);
83
+ forceDummyUnit?.destroy();
84
+ timer.destroy();
85
+ }
86
+
87
+ /**
88
+ * These are the positions the unit would be if there were no minor disturbances
89
+ */
90
+ let theoreticalX = unit.x;
91
+ let theoreticalY = unit.y;
92
+
93
+ /**
94
+ * The distance the true unit position can be from the theoretical position. Helps stop sliding against walls, etc.
95
+ */
96
+ // const BREAK_DISTANCE = 100;
97
+
98
+ if (!forceDummyUnit) {
99
+ forceDummyUnit = Unit.create(
100
+ Players[config.dummyUnitPlayerIndex],
101
+ config.dummyUnitPlayerIndex,
102
+ defaultX,
103
+ defaultY
104
+ );
105
+ }
106
+
107
+ // unit.issueImmediateOrder(OrderId.Stop);
108
+
109
+ if (config?.animationIndexNumber) {
110
+ ResetUnitAnimation(unit.handle);
111
+ SetUnitAnimationByIndex(unit.handle, config.animationIndexNumber);
112
+ }
113
+
114
+ timer.start(refreshInterval, true, () => {
115
+ //If for whatever reason the unit no longer exists.
116
+ if (!unit) {
117
+ destroyForceEffect();
118
+ return destroyForceEffect;
119
+ }
120
+
121
+ // if (distanceBetweenPoints(unit.x, unit.y, theoreticalX, theoreticalY) > BREAK_DISTANCE) {
122
+ // print("unit was to far away from theoretical distance");
123
+ // const e = Effect.create("Abilities\\Spells\\Other\\TalkToMe\\TalkToMe", theoreticalX, theoreticalY);
124
+ // e?.setScaleMatrix(2, 2, 2);
125
+ // unit.issueImmediateOrder(OrderId.Stop);
126
+
127
+ // destroyForceEffect(true);
128
+ // return;
129
+ // }
130
+
131
+ //if the unit's move speed vector is greater than the remaining applied force vector then we may stop the applied force function; should only run while the unit has the move order
132
+ // if (config?.obeyPathing && !config.strictPathing && currentSpeed > unit.moveSpeed) {
133
+ // unit.issueImmediateOrder(OrderId.Stop);
134
+ // }
135
+
136
+ //should probably only do strict pathing so nothing else interfers with the unit moving
137
+ if (forceDummyUnit && config?.strictPathing) {
138
+ const isWindWalked = UnitHasBuffBJ(forceDummyUnit.handle, FourCC("BOwk"));
139
+
140
+ if (!isWindWalked) {
141
+ forceDummyUnit.issueImmediateOrder(OrderId.Windwalk);
142
+ }
143
+ // forceDummyUnit.issueImmediateOrder(OrderId.Stop);
144
+
145
+ //Move dummy to the position we want the unit to be in
146
+ forceDummyUnit.x = unit.x;
147
+ forceDummyUnit.y = unit.y;
148
+
149
+ forceDummyUnit.issueImmediateOrder(OrderId.Stop);
150
+
151
+ //flooring values since it can be off sometimes by a thousandth
152
+ if (
153
+ Math.floor(forceDummyUnit.x) !== Math.floor(unit.x) ||
154
+ Math.floor(forceDummyUnit.y) !== Math.floor(unit.y)
155
+ ) {
156
+ // print(`Collision detected! Dummy x: ${forceDummyUnit.x} y: ${forceDummyUnit.y} Unit x: ${unit.x} y: ${unit.y}`);
157
+ const e = Effect.create(
158
+ "Abilities\\Spells\\Other\\TalkToMe\\TalkToMe",
159
+ forceDummyUnit.x,
160
+ forceDummyUnit.y
161
+ );
162
+ e?.setScaleMatrix(2, 2, 2);
163
+ destroyForceEffect(true);
164
+ return;
165
+ }
166
+ }
167
+
168
+ const xVelocity =
169
+ (currentSpeed / updatesPerSecond) * Math.cos(Deg2Rad(angle));
170
+ const yVelocity =
171
+ (currentSpeed / updatesPerSecond) * Math.sin(Deg2Rad(angle));
172
+
173
+ //Complete execution when current speed of the initial force has decayed
174
+ if (currentSpeed <= 0) {
175
+ destroyForceEffect(true);
176
+ return;
177
+ }
178
+
179
+ //Runs when the force is first applied
180
+ if (config?.onStart && currentSpeed === initialSpeed) {
181
+ config.onStart(currentSpeed, timeElapsed);
182
+ }
183
+
184
+ //Runs at any point while the function is executing
185
+ if (config?.whileActive) {
186
+ config.whileActive(currentSpeed, timeElapsed);
187
+ }
188
+
189
+ theoreticalX += xVelocity;
190
+ theoreticalY += yVelocity;
191
+
192
+ //basically the same thing now since we are no longer issuing the stop command on the unit.
193
+ //also the moment another force is applied to the unit the previous force will stop being applied.
194
+ if (config?.strictPathing) {
195
+ unit.x = theoreticalX;
196
+ unit.y = theoreticalY;
197
+ } else {
198
+ unit.x += xVelocity;
199
+ unit.y += yVelocity;
200
+ }
201
+
202
+ timeElapsed += refreshInterval;
203
+
204
+ if (
205
+ config?.sustainedForceDuration &&
206
+ timeElapsed <= config.sustainedForceDuration
207
+ ) {
208
+ return;
209
+ }
210
+
211
+ currentSpeed -= frictionConstant / updatesPerSecond;
212
+ });
213
+
214
+ return { destroyForceEffect };
215
+ }
216
+
217
+ /**
218
+ * @param angle degrees
219
+ * @param effect
220
+ * @param initialSpeed meters per second
221
+ * @param affectHeight determines whether or not to change unit height whilst force is applied
222
+ */
223
+ export function applyForceForEffect(
224
+ angle: number,
225
+ effect: Effect,
226
+ initialSpeed: number,
227
+ config?: ApplyForceConfig
228
+ ) {
229
+ const timer = Timer.create();
230
+ const refreshInterval = 0.01;
231
+ const updatesPerSecond = 1 / refreshInterval;
232
+ const frictionConstant = 1200; //meters per second friction decay
233
+ let currentSpeed = initialSpeed;
234
+ let timeElapsed = 0;
235
+
236
+ //Cancel unit commands - if a unit already has a move command and are applied a force, they will bug out sometimes and walk in the opposite direction
237
+
238
+ timer.start(refreshInterval, true, () => {
239
+ //if the unit's move speed vector is greater than the remaining applied force vector then we may stop the applied force function; should only run while the unit has the move order
240
+ const xVelocity =
241
+ (currentSpeed / updatesPerSecond) * Math.cos(Deg2Rad(angle));
242
+ const yVelocity =
243
+ (currentSpeed / updatesPerSecond) * Math.sin(Deg2Rad(angle));
244
+
245
+ //On end hook runs before the timer is destroyed and the function ends
246
+ if (config?.onEnd && currentSpeed <= 0) {
247
+ config.onEnd(currentSpeed, timeElapsed);
248
+ }
249
+
250
+ //Complete execution when current speed of the initial force has decayed
251
+ if (currentSpeed <= 0) {
252
+ timer.destroy();
253
+ return;
254
+ }
255
+
256
+ //Runs when the force is first applied
257
+ if (config?.onStart && currentSpeed === initialSpeed) {
258
+ config.onStart(currentSpeed, timeElapsed);
259
+ }
260
+
261
+ //Runs at any point while the function is executing
262
+ if (config?.whileActive) {
263
+ config.whileActive(currentSpeed, timeElapsed);
264
+ }
265
+
266
+ effect.x += xVelocity;
267
+ effect.y += yVelocity;
268
+
269
+ timeElapsed += refreshInterval;
270
+
271
+ if (
272
+ config?.sustainedForceDuration &&
273
+ timeElapsed <= config.sustainedForceDuration
274
+ ) {
275
+ return;
276
+ }
277
+
278
+ currentSpeed -= frictionConstant / updatesPerSecond;
279
+ });
280
+
281
+ function destroyForceEffect() {
282
+ timer.destroy();
283
+ }
284
+
285
+ return { destroyForceEffect };
286
+ }
287
+
288
+ // const unitIsMovingVector_x = (unit.moveSpeed / updatesPerSecond) * Math.cos(Deg2Rad(unit.facing));
289
+ // const unitIsMovingVector_y = (unit.moveSpeed / updatesPerSecond) * Math.sin(Deg2Rad(unit.facing));
290
+ // if ((moveOrders.includes(unit.currentOrder) && unitIsMovingVector_x > xVelocity) || unitIsMovingVector_y > yVelocity) {
291
+ // print("moving velocity exceeded applied force velocity");
292
+ // timer.destroy();
293
+
294
+ // return;
295
+ // }
@@ -0,0 +1,214 @@
1
+ import { Group, MapPlayer, Rectangle, Trigger, Unit } from "w3ts";
2
+ import { Players } from "w3ts/globals";
3
+
4
+ /**
5
+ * Does a callback for every unit of the player that has the ability
6
+ * @param player
7
+ * @param abilityId
8
+ * @param cb
9
+ */
10
+ export function forEachUnitOfPlayerWithAbility(player: MapPlayer, abilityId: number, cb: (unit: Unit) => void) {
11
+ forEachUnitOfPlayer(player, (u) => {
12
+ for (let x = 0; x < 12; x++) {
13
+ const currentAbility = u.getAbilityByIndex(x);
14
+
15
+ if (currentAbility && currentAbility === u.getAbility(abilityId) && u.isAlive()) {
16
+ cb(u);
17
+ }
18
+ }
19
+ });
20
+ }
21
+
22
+ /**
23
+ * Calls a function for each player playing and is an ally of red.
24
+ * @warning specific to map
25
+ */
26
+ export function forEachAlliedPlayer(cb: (player: MapPlayer, index: number) => void) {
27
+ Players.forEach((player, index) => {
28
+ //For testing purposes, include player[9] (the human ally) so their units can also be included when iterating the units OR i should make a separate function for all units.
29
+ if (player.slotState === PLAYER_SLOT_STATE_PLAYING && player.isPlayerAlly(Players[0]) && player != Players[25] && player != Players[27]) {
30
+ cb(player, index);
31
+ }
32
+ });
33
+ }
34
+
35
+ export function forEachPlayer(cb: (player: MapPlayer, index: number) => void) {
36
+ Players.forEach((p, index) => {
37
+ cb(p, index);
38
+ });
39
+ }
40
+
41
+ /**
42
+ * Executes the callback function for each unit matching the unit type for the player
43
+ * @param unitType Unit Type Id or the Unit Type String "hcas", etc
44
+ */
45
+ export function forEachUnitTypeOfPlayer(unitType: number | string, player: MapPlayer, cb: (unit: Unit) => void) {
46
+ if (typeof unitType === "string") {
47
+ unitType = FourCC(unitType);
48
+ }
49
+
50
+ const g = Group.create();
51
+
52
+ g?.enumUnitsOfPlayer(player, () => {
53
+ const unit = Group.getFilterUnit();
54
+
55
+ if (unit && unit?.typeId === unitType) {
56
+ cb(unit);
57
+ }
58
+
59
+ return true;
60
+ });
61
+
62
+ g?.destroy();
63
+ }
64
+
65
+ /**
66
+ * Doesn;'t appear to actually work?
67
+ * @broken
68
+ * @param unitName
69
+ * @param unitType
70
+ * @param fn
71
+ */
72
+ export function forEachUnitOfType(unitName: string, unitType: number | string, fn: (unit: Unit) => void) {
73
+ if (typeof unitType === "string") {
74
+ unitType = FourCC(unitType);
75
+ }
76
+
77
+ print(unitName);
78
+
79
+ const g = Group.create();
80
+
81
+ g?.enumUnitsOfType(unitName, () => {
82
+ const unit = Group.getFilterUnit();
83
+
84
+ if (unit && unit?.typeId === unitType) {
85
+ fn(unit);
86
+ }
87
+
88
+ return true;
89
+ });
90
+
91
+ print(`Total units of name ${unitName} found: ${g?.size}`);
92
+
93
+ g?.destroy();
94
+ }
95
+
96
+ /**
97
+ * @param unitType Unit Type Id or the Unit Type String "hcas", etc
98
+ */
99
+ export function forEachUnitOfPlayer(player: MapPlayer, cb: (unit: Unit) => void) {
100
+ const g = Group.create();
101
+
102
+ g?.enumUnitsOfPlayer(player, () => {
103
+ const unit = Group.getFilterUnit();
104
+
105
+ if (!unit) {
106
+ print("Enumerating over a unit that doesn't exist!");
107
+ }
108
+ if (unit) {
109
+ cb(unit);
110
+ }
111
+
112
+ return true;
113
+ });
114
+
115
+ g?.destroy();
116
+ }
117
+
118
+ /**
119
+ * Executes the callback function for each unit matching the unit type for the player
120
+ * @param unitType Unit Type Id or the Unit Type String "hcas", etc
121
+ */
122
+ export function forEachUnitInRectangle(rectangle: Rectangle, cb: (unit: Unit) => void) {
123
+ const g = Group.create();
124
+
125
+ g?.enumUnitsInRect(rectangle, () => {
126
+ const unit = Group.getFilterUnit();
127
+
128
+ if (unit) {
129
+ cb(unit);
130
+ }
131
+
132
+ return true;
133
+ });
134
+
135
+ g?.destroy();
136
+ }
137
+
138
+ export function isPlaying(player: MapPlayer | player) {
139
+ if (player instanceof MapPlayer) {
140
+ return player.slotState === PLAYER_SLOT_STATE_PLAYING;
141
+ }
142
+
143
+ return GetPlayerSlotState(player) === PLAYER_SLOT_STATE_PLAYING;
144
+ }
145
+
146
+ export function isUser(player: MapPlayer | player) {
147
+ if (player instanceof MapPlayer) {
148
+ return GetPlayerController(player.handle) === MAP_CONTROL_USER;
149
+ }
150
+
151
+ return GetPlayerController(player) === MAP_CONTROL_USER;
152
+ }
153
+
154
+ export function isComputer(player: MapPlayer) {
155
+ if (player instanceof MapPlayer) {
156
+ return GetPlayerController(player.handle) === MAP_CONTROL_COMPUTER;
157
+ }
158
+
159
+ return GetPlayerController(player) === MAP_CONTROL_COMPUTER;
160
+ }
161
+
162
+ export function isPlayingUser(player: MapPlayer | player) {
163
+ return isUser(player) && isPlaying(player);
164
+ }
165
+
166
+ export function adjustPlayerState(player: MapPlayer, whichState: playerstate, amount: number) {
167
+ player.setState(whichState, player.getState(whichState) + amount);
168
+ }
169
+
170
+ export function adjustGold(player: MapPlayer, amount: number) {
171
+ player.setState(PLAYER_STATE_RESOURCE_GOLD, player.getState(PLAYER_STATE_RESOURCE_GOLD) + amount);
172
+ }
173
+
174
+ export function adjustLumber(player: MapPlayer, amount: number) {
175
+ player.setState(PLAYER_STATE_RESOURCE_LUMBER, player.getState(PLAYER_STATE_RESOURCE_LUMBER) + amount);
176
+ }
177
+
178
+ export function adjustFoodCap(player: MapPlayer, amount: number) {
179
+ player.setState(PLAYER_STATE_RESOURCE_FOOD_CAP, player.getState(PLAYER_STATE_RESOURCE_FOOD_CAP) + amount);
180
+ }
181
+
182
+ export function adjustFoodUsed(player: MapPlayer, amount: number) {
183
+ player.setState(PLAYER_STATE_RESOURCE_FOOD_USED, player.getState(PLAYER_STATE_RESOURCE_FOOD_USED) + amount);
184
+ }
185
+
186
+ export function playerHasResources(player: MapPlayer, data: { gold?: number; lumber?: number }) {
187
+ let hasGold = true;
188
+ let hasLumber = true;
189
+
190
+ if (data.gold && data.gold != 0) {
191
+ hasGold = player.getState(PLAYER_STATE_RESOURCE_GOLD) >= data.gold;
192
+ }
193
+
194
+ if (data.lumber && data.lumber != 0) {
195
+ hasLumber = player.getState(PLAYER_STATE_RESOURCE_LUMBER) >= data.lumber;
196
+ }
197
+
198
+ return hasGold && hasLumber;
199
+ }
200
+
201
+ export function setPlayerName() {
202
+ const t = Trigger.create();
203
+ forEachPlayer((p) => {
204
+ t.registerPlayerChatEvent(p, "-playername ", false);
205
+
206
+ t.addAction(() => {
207
+ const str = GetEventPlayerChatString();
208
+ const newName = str?.replace("-playername", "");
209
+ if (newName) {
210
+ SetPlayerName(p.handle, newName);
211
+ }
212
+ });
213
+ });
214
+ }