@supalosa/chronodivide-bot 0.5.3 → 0.6.4

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.
Files changed (134) hide show
  1. package/.env.template +4 -4
  2. package/.github/workflows/npm-publish.yml +24 -0
  3. package/README.md +108 -97
  4. package/dist/bot/bot.js +105 -105
  5. package/dist/bot/bot.js.map +1 -1
  6. package/dist/bot/logic/awareness.js +136 -136
  7. package/dist/bot/logic/building/antiAirStaticDefence.js +42 -42
  8. package/dist/bot/logic/building/antiGroundStaticDefence.js +34 -30
  9. package/dist/bot/logic/building/antiGroundStaticDefence.js.map +1 -1
  10. package/dist/bot/logic/building/{ArtilleryUnit.js → artilleryUnit.js} +18 -18
  11. package/dist/bot/logic/building/basicAirUnit.js +19 -19
  12. package/dist/bot/logic/building/basicBuilding.js +26 -26
  13. package/dist/bot/logic/building/basicGroundUnit.js +19 -19
  14. package/dist/bot/logic/building/buildingRules.js +175 -174
  15. package/dist/bot/logic/building/buildingRules.js.map +1 -1
  16. package/dist/bot/logic/building/common.js +19 -18
  17. package/dist/bot/logic/building/common.js.map +1 -1
  18. package/dist/bot/logic/building/harvester.js +16 -16
  19. package/dist/bot/logic/building/powerPlant.js +20 -20
  20. package/dist/bot/logic/building/queueController.js +183 -183
  21. package/dist/bot/logic/building/resourceCollectionBuilding.js +36 -36
  22. package/dist/bot/logic/common/scout.js +126 -126
  23. package/dist/bot/logic/common/utils.js +95 -85
  24. package/dist/bot/logic/common/utils.js.map +1 -1
  25. package/dist/bot/logic/composition/alliedCompositions.js +12 -12
  26. package/dist/bot/logic/composition/common.js +1 -1
  27. package/dist/bot/logic/composition/sovietCompositions.js +12 -12
  28. package/dist/bot/logic/map/map.js +44 -44
  29. package/dist/bot/logic/map/sector.js +137 -137
  30. package/dist/bot/logic/mission/actionBatcher.js +91 -91
  31. package/dist/bot/logic/mission/mission.js +122 -122
  32. package/dist/bot/logic/mission/missionController.js +321 -321
  33. package/dist/bot/logic/mission/missionFactories.js +12 -12
  34. package/dist/bot/logic/mission/missions/attackMission.js +214 -214
  35. package/dist/bot/logic/mission/missions/defenceMission.js +82 -82
  36. package/dist/bot/logic/mission/missions/engineerMission.js +63 -63
  37. package/dist/bot/logic/mission/missions/expansionMission.js +60 -60
  38. package/dist/bot/logic/mission/missions/retreatMission.js +33 -33
  39. package/dist/bot/logic/mission/missions/scoutingMission.js +133 -133
  40. package/dist/bot/logic/mission/missions/squads/combatSquad.js +115 -115
  41. package/dist/bot/logic/mission/missions/squads/common.js +57 -57
  42. package/dist/bot/logic/mission/missions/squads/squad.js +1 -1
  43. package/dist/bot/logic/threat/threat.js +22 -22
  44. package/dist/bot/logic/threat/threatCalculator.js +73 -73
  45. package/dist/exampleBot.js +100 -112
  46. package/dist/exampleBot.js.map +1 -1
  47. package/package.json +32 -29
  48. package/src/bot/bot.ts +161 -161
  49. package/src/bot/logic/awareness.ts +245 -245
  50. package/src/bot/logic/building/antiAirStaticDefence.ts +64 -64
  51. package/src/bot/logic/building/antiGroundStaticDefence.ts +55 -51
  52. package/src/bot/logic/building/artilleryUnit.ts +39 -39
  53. package/src/bot/logic/building/basicAirUnit.ts +39 -39
  54. package/src/bot/logic/building/basicBuilding.ts +49 -49
  55. package/src/bot/logic/building/basicGroundUnit.ts +39 -39
  56. package/src/bot/logic/building/buildingRules.ts +250 -247
  57. package/src/bot/logic/building/common.ts +21 -23
  58. package/src/bot/logic/building/harvester.ts +31 -31
  59. package/src/bot/logic/building/powerPlant.ts +32 -32
  60. package/src/bot/logic/building/queueController.ts +297 -297
  61. package/src/bot/logic/building/resourceCollectionBuilding.ts +52 -52
  62. package/src/bot/logic/common/scout.ts +183 -183
  63. package/src/bot/logic/common/utils.ts +120 -112
  64. package/src/bot/logic/composition/alliedCompositions.ts +22 -22
  65. package/src/bot/logic/composition/common.ts +3 -3
  66. package/src/bot/logic/composition/sovietCompositions.ts +21 -21
  67. package/src/bot/logic/map/map.ts +66 -66
  68. package/src/bot/logic/map/sector.ts +174 -174
  69. package/src/bot/logic/mission/actionBatcher.ts +124 -124
  70. package/src/bot/logic/mission/mission.ts +232 -232
  71. package/src/bot/logic/mission/missionController.ts +413 -413
  72. package/src/bot/logic/mission/missionFactories.ts +51 -51
  73. package/src/bot/logic/mission/missions/attackMission.ts +336 -336
  74. package/src/bot/logic/mission/missions/defenceMission.ts +151 -151
  75. package/src/bot/logic/mission/missions/engineerMission.ts +113 -113
  76. package/src/bot/logic/mission/missions/expansionMission.ts +104 -104
  77. package/src/bot/logic/mission/missions/retreatMission.ts +54 -54
  78. package/src/bot/logic/mission/missions/scoutingMission.ts +186 -186
  79. package/src/bot/logic/mission/missions/squads/combatSquad.ts +160 -160
  80. package/src/bot/logic/mission/missions/squads/common.ts +63 -63
  81. package/src/bot/logic/mission/missions/squads/squad.ts +19 -19
  82. package/src/bot/logic/threat/threatCalculator.ts +100 -100
  83. package/src/exampleBot.ts +111 -124
  84. package/tsconfig.json +73 -73
  85. package/dist/bot/logic/building/building.js +0 -82
  86. package/dist/bot/logic/building/massedAntiGroundUnit.js +0 -20
  87. package/dist/bot/logic/building/queues.js +0 -19
  88. package/dist/bot/logic/knowledge.js +0 -1
  89. package/dist/bot/logic/mission/basicMission.js +0 -26
  90. package/dist/bot/logic/mission/behaviours/combatSquad.js +0 -124
  91. package/dist/bot/logic/mission/behaviours/combatSquad.js.map +0 -1
  92. package/dist/bot/logic/mission/behaviours/common.js +0 -56
  93. package/dist/bot/logic/mission/behaviours/common.js.map +0 -1
  94. package/dist/bot/logic/mission/behaviours/engineerSquad.js +0 -39
  95. package/dist/bot/logic/mission/behaviours/engineerSquad.js.map +0 -1
  96. package/dist/bot/logic/mission/behaviours/expansionSquad.js +0 -46
  97. package/dist/bot/logic/mission/behaviours/expansionSquad.js.map +0 -1
  98. package/dist/bot/logic/mission/behaviours/retreatSquad.js +0 -31
  99. package/dist/bot/logic/mission/behaviours/retreatSquad.js.map +0 -1
  100. package/dist/bot/logic/mission/behaviours/scoutingSquad.js +0 -94
  101. package/dist/bot/logic/mission/behaviours/scoutingSquad.js.map +0 -1
  102. package/dist/bot/logic/mission/expansionMission.js +0 -32
  103. package/dist/bot/logic/mission/missions/basicMission.js +0 -13
  104. package/dist/bot/logic/mission/missions/basicMission.js.map +0 -1
  105. package/dist/bot/logic/mission/missions/missionBehaviour.js +0 -2
  106. package/dist/bot/logic/mission/missions/missionBehaviour.js.map +0 -1
  107. package/dist/bot/logic/mission/missions/oneTimeMission.js +0 -27
  108. package/dist/bot/logic/mission/missions/oneTimeMission.js.map +0 -1
  109. package/dist/bot/logic/squad/behaviours/actionBatcher.js +0 -36
  110. package/dist/bot/logic/squad/behaviours/actionBatcher.js.map +0 -1
  111. package/dist/bot/logic/squad/behaviours/attackSquad.js +0 -82
  112. package/dist/bot/logic/squad/behaviours/combatSquad.js +0 -106
  113. package/dist/bot/logic/squad/behaviours/combatSquad.js.map +0 -1
  114. package/dist/bot/logic/squad/behaviours/common.js +0 -55
  115. package/dist/bot/logic/squad/behaviours/common.js.map +0 -1
  116. package/dist/bot/logic/squad/behaviours/defenceSquad.js +0 -48
  117. package/dist/bot/logic/squad/behaviours/engineerSquad.js +0 -38
  118. package/dist/bot/logic/squad/behaviours/engineerSquad.js.map +0 -1
  119. package/dist/bot/logic/squad/behaviours/expansionSquad.js +0 -45
  120. package/dist/bot/logic/squad/behaviours/expansionSquad.js.map +0 -1
  121. package/dist/bot/logic/squad/behaviours/retreatSquad.js +0 -31
  122. package/dist/bot/logic/squad/behaviours/retreatSquad.js.map +0 -1
  123. package/dist/bot/logic/squad/behaviours/scoutingSquad.js +0 -93
  124. package/dist/bot/logic/squad/behaviours/scoutingSquad.js.map +0 -1
  125. package/dist/bot/logic/squad/behaviours/squadExpansion.js +0 -31
  126. package/dist/bot/logic/squad/behaviours/squadScouters.js +0 -8
  127. package/dist/bot/logic/squad/squad.js +0 -126
  128. package/dist/bot/logic/squad/squad.js.map +0 -1
  129. package/dist/bot/logic/squad/squadBehaviour.js +0 -6
  130. package/dist/bot/logic/squad/squadBehaviour.js.map +0 -1
  131. package/dist/bot/logic/squad/squadBehaviours.js +0 -7
  132. package/dist/bot/logic/squad/squadBehaviours.js.map +0 -1
  133. package/dist/bot/logic/squad/squadController.js +0 -215
  134. package/dist/bot/logic/squad/squadController.js.map +0 -1
@@ -1,112 +1,120 @@
1
- import { GameObjectData, TechnoRules, UnitData } from "@chronodivide/game-api";
2
-
3
- export type DebugLogger = (message: string, sayInGame?: boolean) => void;
4
-
5
- const SOVIET_COUNTRY_NAMES = ["Africans", "Arabs", "Confederation", "Russians"];
6
-
7
- export const isSoviet = (countryName: string) => SOVIET_COUNTRY_NAMES.includes(countryName);
8
-
9
- export const isOwnedByNeutral = (unitData: UnitData | undefined) => unitData?.owner === "@@NEUTRAL@@";
10
-
11
- // Return if the given unit would have .isSelectableCombatant = true.
12
- // Usable on GameObjectData (which is faster to get than TechnoRules)
13
- export const isSelectableCombatant = (rules: GameObjectData | undefined) =>
14
- !!(rules?.rules as any)?.isSelectableCombatant;
15
-
16
- // Thanks use-strict!
17
- export function formatTimeDuration(timeSeconds: number, skipZeroHours = false) {
18
- let h = Math.floor(timeSeconds / 3600);
19
- timeSeconds -= h * 3600;
20
- let m = Math.floor(timeSeconds / 60);
21
- timeSeconds -= m * 60;
22
- let s = Math.floor(timeSeconds);
23
-
24
- return [...(h || !skipZeroHours ? [h] : []), pad(m, "00"), pad(s, "00")].join(":");
25
- }
26
-
27
- export function pad(n: any, format = "0000") {
28
- let str = "" + n;
29
- return format.substring(0, format.length - str.length) + str;
30
- }
31
-
32
- // So we don't need lodash
33
- export function minBy<T>(array: T[], predicate: (arg: T) => number | null): T | null {
34
- if (array.length === 0) {
35
- return null;
36
- }
37
- let minIdx = 0;
38
- let minVal = predicate(array[0]);
39
- for (let i = 1; i < array.length; ++i) {
40
- const newVal = predicate(array[i]);
41
- if (minVal === null || (newVal !== null && newVal < minVal)) {
42
- minIdx = i;
43
- minVal = newVal;
44
- }
45
- }
46
- return array[minIdx];
47
- }
48
-
49
- export function maxBy<T>(array: T[], predicate: (arg: T) => number | null): T | null {
50
- if (array.length === 0) {
51
- return null;
52
- }
53
- let maxIdx = 0;
54
- let maxVal = predicate(array[0]);
55
- for (let i = 1; i < array.length; ++i) {
56
- const newVal = predicate(array[i]);
57
- if (maxVal === null || (newVal !== null && newVal > maxVal)) {
58
- maxIdx = i;
59
- maxVal = newVal;
60
- }
61
- }
62
- return array[maxIdx];
63
- }
64
-
65
- export function uniqBy<T>(array: T[], predicate: (arg: T) => string | number): T[] {
66
- return Object.values(
67
- array.reduce(
68
- (prev, newVal) => {
69
- const val = predicate(newVal);
70
- if (!prev[val]) {
71
- prev[val] = newVal;
72
- }
73
- return prev;
74
- },
75
- {} as Record<string, T>,
76
- ),
77
- );
78
- }
79
-
80
- export function countBy<T>(array: T[], predicate: (arg: T) => string | undefined): { [key: string]: number } {
81
- return array.reduce(
82
- (prev, newVal) => {
83
- const val = predicate(newVal);
84
- if (val === undefined) {
85
- return prev;
86
- }
87
- if (!prev[val]) {
88
- prev[val] = 0;
89
- }
90
- prev[val] = prev[val] + 1;
91
- return prev;
92
- },
93
- {} as Record<string, number>,
94
- );
95
- }
96
-
97
- export function groupBy<K extends string, V>(array: V[], predicate: (arg: V) => K): { [key in K]: V[] } {
98
- return array.reduce(
99
- (prev, newVal) => {
100
- const val = predicate(newVal);
101
- if (val === undefined) {
102
- return prev;
103
- }
104
- if (!prev.hasOwnProperty(val)) {
105
- prev[val] = [];
106
- }
107
- prev[val].push(newVal);
108
- return prev;
109
- },
110
- {} as Record<K, V[]>,
111
- );
112
- }
1
+ import { GameObjectData, TechnoRules, UnitData } from "@chronodivide/game-api";
2
+
3
+ export enum Countries {
4
+ USA = "Americans",
5
+ KOREA = "Alliance",
6
+ FRANCE = "French",
7
+ GERMANY = "Germans",
8
+ GREAT_BRITAIN = "British",
9
+ LIBYA = "Africans",
10
+ IRAQ = "Arabs",
11
+ CUBA = "Confederation",
12
+ RUSSIA = "Russians",
13
+ }
14
+
15
+ export type DebugLogger = (message: string, sayInGame?: boolean) => void;
16
+
17
+ export const isOwnedByNeutral = (unitData: UnitData | undefined) => unitData?.owner === "@@NEUTRAL@@";
18
+
19
+ // Return if the given unit would have .isSelectableCombatant = true.
20
+ // Usable on GameObjectData (which is faster to get than TechnoRules)
21
+ export const isSelectableCombatant = (rules: GameObjectData | undefined) =>
22
+ !!(rules?.rules as any)?.isSelectableCombatant;
23
+
24
+ // Thanks use-strict!
25
+ export function formatTimeDuration(timeSeconds: number, skipZeroHours = false) {
26
+ let h = Math.floor(timeSeconds / 3600);
27
+ timeSeconds -= h * 3600;
28
+ let m = Math.floor(timeSeconds / 60);
29
+ timeSeconds -= m * 60;
30
+ let s = Math.floor(timeSeconds);
31
+
32
+ return [...(h || !skipZeroHours ? [h] : []), pad(m, "00"), pad(s, "00")].join(":");
33
+ }
34
+
35
+ export function pad(n: any, format = "0000") {
36
+ let str = "" + n;
37
+ return format.substring(0, format.length - str.length) + str;
38
+ }
39
+
40
+ // So we don't need lodash
41
+ export function minBy<T>(array: T[], predicate: (arg: T) => number | null): T | null {
42
+ if (array.length === 0) {
43
+ return null;
44
+ }
45
+ let minIdx = 0;
46
+ let minVal = predicate(array[0]);
47
+ for (let i = 1; i < array.length; ++i) {
48
+ const newVal = predicate(array[i]);
49
+ if (minVal === null || (newVal !== null && newVal < minVal)) {
50
+ minIdx = i;
51
+ minVal = newVal;
52
+ }
53
+ }
54
+ return array[minIdx];
55
+ }
56
+
57
+ export function maxBy<T>(array: T[], predicate: (arg: T) => number | null): T | null {
58
+ if (array.length === 0) {
59
+ return null;
60
+ }
61
+ let maxIdx = 0;
62
+ let maxVal = predicate(array[0]);
63
+ for (let i = 1; i < array.length; ++i) {
64
+ const newVal = predicate(array[i]);
65
+ if (maxVal === null || (newVal !== null && newVal > maxVal)) {
66
+ maxIdx = i;
67
+ maxVal = newVal;
68
+ }
69
+ }
70
+ return array[maxIdx];
71
+ }
72
+
73
+ export function uniqBy<T>(array: T[], predicate: (arg: T) => string | number): T[] {
74
+ return Object.values(
75
+ array.reduce(
76
+ (prev, newVal) => {
77
+ const val = predicate(newVal);
78
+ if (!prev[val]) {
79
+ prev[val] = newVal;
80
+ }
81
+ return prev;
82
+ },
83
+ {} as Record<string, T>,
84
+ ),
85
+ );
86
+ }
87
+
88
+ export function countBy<T>(array: T[], predicate: (arg: T) => string | undefined): { [key: string]: number } {
89
+ return array.reduce(
90
+ (prev, newVal) => {
91
+ const val = predicate(newVal);
92
+ if (val === undefined) {
93
+ return prev;
94
+ }
95
+ if (!prev[val]) {
96
+ prev[val] = 0;
97
+ }
98
+ prev[val] = prev[val] + 1;
99
+ return prev;
100
+ },
101
+ {} as Record<string, number>,
102
+ );
103
+ }
104
+
105
+ export function groupBy<K extends string, V>(array: V[], predicate: (arg: V) => K): { [key in K]: V[] } {
106
+ return array.reduce(
107
+ (prev, newVal) => {
108
+ const val = predicate(newVal);
109
+ if (val === undefined) {
110
+ return prev;
111
+ }
112
+ if (!prev.hasOwnProperty(val)) {
113
+ prev[val] = [];
114
+ }
115
+ prev[val].push(newVal);
116
+ return prev;
117
+ },
118
+ {} as Record<K, V[]>,
119
+ );
120
+ }
@@ -1,22 +1,22 @@
1
- import { GameApi, PlayerData } from "@chronodivide/game-api";
2
- import { MatchAwareness } from "../awareness";
3
- import { UnitComposition } from "./common";
4
-
5
- export const getAlliedCompositions = (
6
- gameApi: GameApi,
7
- playerData: PlayerData,
8
- matchAwareness: MatchAwareness,
9
- ): UnitComposition => {
10
- const hasWarFactory = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "GAWEAP").length > 0;
11
- const hasAirforce =
12
- gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "GAAIRC" || r.name === "AMRADR").length > 0;
13
- const hasBattleLab = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "GATECH").length > 0;
14
-
15
- const includeInfantry = !hasAirforce && !hasBattleLab;
16
- return {
17
- ...(includeInfantry && { E1: 5 }),
18
- ...(hasWarFactory && { MTNK: 3, FV: 2 }),
19
- ...(hasAirforce && { JUMPJET: 6 }),
20
- ...(hasBattleLab && { SREF: 2, MGTK: 3 }),
21
- };
22
- };
1
+ import { GameApi, PlayerData } from "@chronodivide/game-api";
2
+ import { MatchAwareness } from "../awareness";
3
+ import { UnitComposition } from "./common";
4
+
5
+ export const getAlliedCompositions = (
6
+ gameApi: GameApi,
7
+ playerData: PlayerData,
8
+ matchAwareness: MatchAwareness,
9
+ ): UnitComposition => {
10
+ const hasWarFactory = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "GAWEAP").length > 0;
11
+ const hasAirforce =
12
+ gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "GAAIRC" || r.name === "AMRADR").length > 0;
13
+ const hasBattleLab = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "GATECH").length > 0;
14
+
15
+ const includeInfantry = !hasAirforce && !hasBattleLab;
16
+ return {
17
+ ...(includeInfantry && { E1: 5 }),
18
+ ...(hasWarFactory && { MTNK: 3, FV: 2 }),
19
+ ...(hasAirforce && { JUMPJET: 6 }),
20
+ ...(hasBattleLab && { SREF: 2, MGTK: 3 }),
21
+ };
22
+ };
@@ -1,3 +1,3 @@
1
- export type UnitComposition = {
2
- [unitType: string]: number;
3
- };
1
+ export type UnitComposition = {
2
+ [unitType: string]: number;
3
+ };
@@ -1,21 +1,21 @@
1
- import { GameApi, PlayerData } from "@chronodivide/game-api";
2
- import { MatchAwareness } from "../awareness";
3
- import { UnitComposition } from "./common";
4
-
5
- export const getSovietComposition = (
6
- gameApi: GameApi,
7
- playerData: PlayerData,
8
- matchAwareness: MatchAwareness,
9
- ): UnitComposition => {
10
- const hasWarFactory = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "NAWEAP").length > 0;
11
- const hasRadar = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "NARADR").length > 0;
12
- const hasBattleLab = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "NATECH").length > 0;
13
-
14
- const includeInfantry = !hasBattleLab;
15
- return {
16
- ...(includeInfantry && { E2: 10 }),
17
- ...(hasWarFactory && { HTNK: 3, HTK: 2 }),
18
- ...(hasRadar && { V3: 1 }),
19
- ...(hasBattleLab && { APOC: 2 }),
20
- };
21
- };
1
+ import { GameApi, PlayerData } from "@chronodivide/game-api";
2
+ import { MatchAwareness } from "../awareness";
3
+ import { UnitComposition } from "./common";
4
+
5
+ export const getSovietComposition = (
6
+ gameApi: GameApi,
7
+ playerData: PlayerData,
8
+ matchAwareness: MatchAwareness,
9
+ ): UnitComposition => {
10
+ const hasWarFactory = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "NAWEAP").length > 0;
11
+ const hasRadar = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "NARADR").length > 0;
12
+ const hasBattleLab = gameApi.getVisibleUnits(playerData.name, "self", (r) => r.name === "NATECH").length > 0;
13
+
14
+ const includeInfantry = !hasBattleLab;
15
+ return {
16
+ ...(includeInfantry && { E2: 10 }),
17
+ ...(hasWarFactory && { HTNK: 3, HTK: 2 }),
18
+ ...(hasRadar && { V3: 1 }),
19
+ ...(hasBattleLab && { APOC: 2 }),
20
+ };
21
+ };
@@ -1,66 +1,66 @@
1
- import { GameApi, GameMath, MapApi, PlayerData, Size, Tile, UnitData, Vector2 } from "@chronodivide/game-api";
2
- import { maxBy } from "../common/utils.js";
3
-
4
- export function determineMapBounds(mapApi: MapApi): Size {
5
- return mapApi.getRealMapSize();
6
- }
7
-
8
- export function calculateAreaVisibility(
9
- mapApi: MapApi,
10
- playerData: PlayerData,
11
- startPoint: Vector2,
12
- endPoint: Vector2,
13
- ): { visibleTiles: number; validTiles: number } {
14
- let validTiles: number = 0,
15
- visibleTiles: number = 0;
16
- for (let xx = startPoint.x; xx < endPoint.x; ++xx) {
17
- for (let yy = startPoint.y; yy < endPoint.y; ++yy) {
18
- let tile = mapApi.getTile(xx, yy);
19
- if (tile) {
20
- ++validTiles;
21
- if (mapApi.isVisibleTile(tile, playerData.name)) {
22
- ++visibleTiles;
23
- }
24
- }
25
- }
26
- }
27
- let result = { visibleTiles, validTiles };
28
- return result;
29
- }
30
-
31
- export function getPointTowardsOtherPoint(
32
- gameApi: GameApi,
33
- startLocation: Vector2,
34
- endLocation: Vector2,
35
- minRadius: number,
36
- maxRadius: number,
37
- randomAngle: number,
38
- ): Vector2 {
39
- // TODO: Use proper vector maths here.
40
- let radius = minRadius + Math.round(gameApi.generateRandom() * (maxRadius - minRadius));
41
- let directionToEndLocation = GameMath.atan2(endLocation.y - startLocation.y, endLocation.x - startLocation.x);
42
- let randomisedDirection =
43
- directionToEndLocation -
44
- (randomAngle * (Math.PI / 12) + 2 * randomAngle * gameApi.generateRandom() * (Math.PI / 12));
45
- let candidatePointX = Math.round(startLocation.x + GameMath.cos(randomisedDirection) * radius);
46
- let candidatePointY = Math.round(startLocation.y + GameMath.sin(randomisedDirection) * radius);
47
- return new Vector2(candidatePointX, candidatePointY);
48
- }
49
-
50
- export function getDistanceBetweenPoints(startLocation: Vector2, endLocation: Vector2): number {
51
- // TODO: Remove this now we have Vector2s.
52
- return startLocation.distanceTo(endLocation);
53
- }
54
-
55
- export function getDistanceBetweenTileAndPoint(tile: Tile, vector: Vector2): number {
56
- // TODO: Remove this now we have Vector2s.
57
- return new Vector2(tile.rx, tile.ry).distanceTo(vector);
58
- }
59
-
60
- export function getDistanceBetweenUnits(unit1: UnitData, unit2: UnitData): number {
61
- return new Vector2(unit1.tile.rx, unit1.tile.ry).distanceTo(new Vector2(unit2.tile.rx, unit2.tile.ry));
62
- }
63
-
64
- export function getDistanceBetween(unit: UnitData, point: Vector2): number {
65
- return getDistanceBetweenPoints(new Vector2(unit.tile.rx, unit.tile.ry), point);
66
- }
1
+ import { GameApi, GameMath, MapApi, PlayerData, Size, Tile, UnitData, Vector2 } from "@chronodivide/game-api";
2
+ import { maxBy } from "../common/utils.js";
3
+
4
+ export function determineMapBounds(mapApi: MapApi): Size {
5
+ return mapApi.getRealMapSize();
6
+ }
7
+
8
+ export function calculateAreaVisibility(
9
+ mapApi: MapApi,
10
+ playerData: PlayerData,
11
+ startPoint: Vector2,
12
+ endPoint: Vector2,
13
+ ): { visibleTiles: number; validTiles: number } {
14
+ let validTiles: number = 0,
15
+ visibleTiles: number = 0;
16
+ for (let xx = startPoint.x; xx < endPoint.x; ++xx) {
17
+ for (let yy = startPoint.y; yy < endPoint.y; ++yy) {
18
+ let tile = mapApi.getTile(xx, yy);
19
+ if (tile) {
20
+ ++validTiles;
21
+ if (mapApi.isVisibleTile(tile, playerData.name)) {
22
+ ++visibleTiles;
23
+ }
24
+ }
25
+ }
26
+ }
27
+ let result = { visibleTiles, validTiles };
28
+ return result;
29
+ }
30
+
31
+ export function getPointTowardsOtherPoint(
32
+ gameApi: GameApi,
33
+ startLocation: Vector2,
34
+ endLocation: Vector2,
35
+ minRadius: number,
36
+ maxRadius: number,
37
+ randomAngle: number,
38
+ ): Vector2 {
39
+ // TODO: Use proper vector maths here.
40
+ let radius = minRadius + Math.round(gameApi.generateRandom() * (maxRadius - minRadius));
41
+ let directionToEndLocation = GameMath.atan2(endLocation.y - startLocation.y, endLocation.x - startLocation.x);
42
+ let randomisedDirection =
43
+ directionToEndLocation -
44
+ (randomAngle * (Math.PI / 12) + 2 * randomAngle * gameApi.generateRandom() * (Math.PI / 12));
45
+ let candidatePointX = Math.round(startLocation.x + GameMath.cos(randomisedDirection) * radius);
46
+ let candidatePointY = Math.round(startLocation.y + GameMath.sin(randomisedDirection) * radius);
47
+ return new Vector2(candidatePointX, candidatePointY);
48
+ }
49
+
50
+ export function getDistanceBetweenPoints(startLocation: Vector2, endLocation: Vector2): number {
51
+ // TODO: Remove this now we have Vector2s.
52
+ return startLocation.distanceTo(endLocation);
53
+ }
54
+
55
+ export function getDistanceBetweenTileAndPoint(tile: Tile, vector: Vector2): number {
56
+ // TODO: Remove this now we have Vector2s.
57
+ return new Vector2(tile.rx, tile.ry).distanceTo(vector);
58
+ }
59
+
60
+ export function getDistanceBetweenUnits(unit1: UnitData, unit2: UnitData): number {
61
+ return new Vector2(unit1.tile.rx, unit1.tile.ry).distanceTo(new Vector2(unit2.tile.rx, unit2.tile.ry));
62
+ }
63
+
64
+ export function getDistanceBetween(unit: UnitData, point: Vector2): number {
65
+ return getDistanceBetweenPoints(new Vector2(unit.tile.rx, unit.tile.ry), point);
66
+ }