warcraft-3-w3ts-utils 0.1.11 → 0.1.13
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/dist/dist/tstl_output.lua +11424 -0
- package/dist/dist/tstl_output.lua.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/package.json +40 -39
- package/dist/utils/abilities.d.ts +2 -0
- package/dist/utils/abilities.d.ts.map +1 -0
- package/dist/utils/camera.d.ts +2 -0
- package/dist/utils/camera.d.ts.map +1 -0
- package/dist/utils/chat-command.d.ts +2 -0
- package/dist/utils/chat-command.d.ts.map +1 -0
- package/dist/utils/color.d.ts +2 -103
- package/dist/utils/color.d.ts.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/item.d.ts +2 -0
- package/dist/utils/item.d.ts.map +1 -0
- package/dist/utils/math.d.ts +2 -0
- package/dist/utils/math.d.ts.map +1 -0
- package/dist/utils/minimapIcons.d.ts +2 -0
- package/dist/utils/minimapIcons.d.ts.map +1 -0
- package/dist/utils/misc.d.ts +2 -0
- package/dist/utils/misc.d.ts.map +1 -0
- package/dist/utils/physics.d.ts +2 -0
- package/dist/utils/physics.d.ts.map +1 -0
- package/dist/utils/players.d.ts +2 -0
- package/dist/utils/players.d.ts.map +1 -0
- package/dist/utils/point.d.ts +2 -0
- package/dist/utils/point.d.ts.map +1 -0
- package/dist/utils/quests.d.ts +2 -0
- package/dist/utils/quests.d.ts.map +1 -0
- package/dist/utils/textTag.d.ts +2 -0
- package/dist/utils/textTag.d.ts.map +1 -0
- package/dist/utils/timer.d.ts +2 -0
- package/dist/utils/timer.d.ts.map +1 -0
- package/dist/utils/units.d.ts +2 -0
- package/dist/utils/units.d.ts.map +1 -0
- package/package.json +40 -39
- package/readme.md +1 -7
- package/src/index.ts +1 -0
- package/src/utils/abilities.ts +158 -0
- package/src/utils/camera.ts +63 -0
- package/src/utils/chat-command.ts +22 -0
- package/src/utils/color.ts +139 -0
- package/src/utils/index.ts +15 -0
- package/src/utils/item.ts +163 -0
- package/src/utils/math.ts +14 -0
- package/src/utils/minimapIcons.ts +34 -0
- package/src/utils/misc.ts +179 -0
- package/src/utils/physics.ts +295 -0
- package/src/utils/players.ts +213 -0
- package/src/utils/point.ts +81 -0
- package/src/utils/quests.ts +38 -0
- package/src/utils/textTag.ts +80 -0
- package/src/utils/timer.ts +14 -0
- package/src/utils/units.ts +84 -0
|
@@ -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,213 @@
|
|
|
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
|
+
* @broken
|
|
67
|
+
* @param unitName
|
|
68
|
+
* @param unitType
|
|
69
|
+
* @param fn
|
|
70
|
+
*/
|
|
71
|
+
export function forEachUnitOfType(unitName: string, unitType: number | string, fn: (unit: Unit) => void) {
|
|
72
|
+
if (typeof unitType === "string") {
|
|
73
|
+
unitType = FourCC(unitType);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
print(unitName);
|
|
77
|
+
|
|
78
|
+
const g = Group.create();
|
|
79
|
+
|
|
80
|
+
g?.enumUnitsOfType(unitName, () => {
|
|
81
|
+
const unit = Group.getFilterUnit();
|
|
82
|
+
|
|
83
|
+
if (unit && unit?.typeId === unitType) {
|
|
84
|
+
fn(unit);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return true;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
print(`Total units of name ${unitName} found: ${g?.size}`);
|
|
91
|
+
|
|
92
|
+
g?.destroy();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @param unitType Unit Type Id or the Unit Type String "hcas", etc
|
|
97
|
+
*/
|
|
98
|
+
export function forEachUnitOfPlayer(player: MapPlayer, cb: (unit: Unit) => void) {
|
|
99
|
+
const g = Group.create();
|
|
100
|
+
|
|
101
|
+
g?.enumUnitsOfPlayer(player, () => {
|
|
102
|
+
const unit = Group.getFilterUnit();
|
|
103
|
+
|
|
104
|
+
if (!unit) {
|
|
105
|
+
print("Enumerating over a unit that doesn't exist!");
|
|
106
|
+
}
|
|
107
|
+
if (unit) {
|
|
108
|
+
cb(unit);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return true;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
g?.destroy();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Executes the callback function for each unit matching the unit type for the player
|
|
119
|
+
* @param unitType Unit Type Id or the Unit Type String "hcas", etc
|
|
120
|
+
*/
|
|
121
|
+
export function forEachUnitInRectangle(rectangle: Rectangle, cb: (unit: Unit) => void) {
|
|
122
|
+
const g = Group.create();
|
|
123
|
+
|
|
124
|
+
g?.enumUnitsInRect(rectangle, () => {
|
|
125
|
+
const unit = Group.getFilterUnit();
|
|
126
|
+
|
|
127
|
+
if (unit) {
|
|
128
|
+
cb(unit);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return true;
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
g?.destroy();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function isPlaying(player: MapPlayer | player) {
|
|
138
|
+
if (player instanceof MapPlayer) {
|
|
139
|
+
return player.slotState === PLAYER_SLOT_STATE_PLAYING;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return GetPlayerSlotState(player) === PLAYER_SLOT_STATE_PLAYING;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function isUser(player: MapPlayer | player) {
|
|
146
|
+
if (player instanceof MapPlayer) {
|
|
147
|
+
return GetPlayerController(player.handle) === MAP_CONTROL_USER;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return GetPlayerController(player) === MAP_CONTROL_USER;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function isComputer(player: MapPlayer) {
|
|
154
|
+
if (player instanceof MapPlayer) {
|
|
155
|
+
return GetPlayerController(player.handle) === MAP_CONTROL_COMPUTER;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return GetPlayerController(player) === MAP_CONTROL_COMPUTER;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function isPlayingUser(player: MapPlayer | player) {
|
|
162
|
+
return isUser(player) && isPlaying(player);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function adjustPlayerState(player: MapPlayer, whichState: playerstate, amount: number) {
|
|
166
|
+
player.setState(whichState, player.getState(whichState) + amount);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function adjustGold(player: MapPlayer, amount: number) {
|
|
170
|
+
player.setState(PLAYER_STATE_RESOURCE_GOLD, player.getState(PLAYER_STATE_RESOURCE_GOLD) + amount);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function adjustLumber(player: MapPlayer, amount: number) {
|
|
174
|
+
player.setState(PLAYER_STATE_RESOURCE_LUMBER, player.getState(PLAYER_STATE_RESOURCE_LUMBER) + amount);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function adjustFoodCap(player: MapPlayer, amount: number) {
|
|
178
|
+
player.setState(PLAYER_STATE_RESOURCE_FOOD_CAP, player.getState(PLAYER_STATE_RESOURCE_FOOD_CAP) + amount);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function adjustFoodUsed(player: MapPlayer, amount: number) {
|
|
182
|
+
player.setState(PLAYER_STATE_RESOURCE_FOOD_USED, player.getState(PLAYER_STATE_RESOURCE_FOOD_USED) + amount);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function playerHasResources(player: MapPlayer, data: { gold?: number; lumber?: number }) {
|
|
186
|
+
let hasGold = true;
|
|
187
|
+
let hasLumber = true;
|
|
188
|
+
|
|
189
|
+
if (data.gold && data.gold != 0) {
|
|
190
|
+
hasGold = player.getState(PLAYER_STATE_RESOURCE_GOLD) >= data.gold;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (data.lumber && data.lumber != 0) {
|
|
194
|
+
hasLumber = player.getState(PLAYER_STATE_RESOURCE_LUMBER) >= data.lumber;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return hasGold && hasLumber;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function setPlayerName() {
|
|
201
|
+
const t = Trigger.create();
|
|
202
|
+
forEachPlayer((p) => {
|
|
203
|
+
t.registerPlayerChatEvent(p, "-playername ", false);
|
|
204
|
+
|
|
205
|
+
t.addAction(() => {
|
|
206
|
+
const str = GetEventPlayerChatString();
|
|
207
|
+
const newName = str?.replace("-playername", "");
|
|
208
|
+
if (newName) {
|
|
209
|
+
SetPlayerName(p.handle, newName);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
}
|