@@ -9,6 +9,7 @@ import Bodies from './Bodies'
99import { CurrentMap , StaticMap } from './Map'
1010import { Vector } from './Vector'
1111import { Team } from './Game'
12+ import Round from './Round'
1213
1314const applyInRadius = (
1415 map : CurrentMap | StaticMap ,
@@ -40,7 +41,35 @@ const squareIntersects = (check: Vector, center: Vector, radius: number) => {
4041 )
4142}
4243
44+ const checkValidRuinPlacement = ( check : Vector , map : StaticMap , bodies : Bodies ) => {
45+ // Check if ruin is too close to the border
46+ if ( check . x <= 1 || check . x >= map . width - 2 || check . y <= 1 || check . y >= map . height - 2 ) {
47+ return false
48+ }
49+
50+ // Check if this is a valid ruin location
51+ const idx = map . locationToIndex ( check . x , check . y )
52+ const ruin = map . ruins . findIndex ( ( l ) => squareIntersects ( l , check , 4 ) )
53+ const wall = map . walls . findIndex ( ( v , i ) => ! ! v && squareIntersects ( map . indexToLocation ( i ) , check , 2 ) )
54+ const paint = map . initialPaint [ idx ]
55+
56+ let tower = undefined
57+ for ( const b of bodies . bodies . values ( ) ) {
58+ if ( squareIntersects ( check , b . pos , 4 ) ) {
59+ tower = b
60+ break
61+ }
62+ }
63+
64+ if ( tower || ruin !== - 1 || wall !== - 1 || paint ) {
65+ return false
66+ }
67+
68+ return true
69+ }
70+
4371export class WallsBrush extends SymmetricMapEditorBrush < StaticMap > {
72+ private readonly bodies : Bodies
4473 public readonly name = 'Walls'
4574 public readonly fields = {
4675 shouldAdd : {
@@ -54,8 +83,9 @@ export class WallsBrush extends SymmetricMapEditorBrush<StaticMap> {
5483 }
5584 }
5685
57- constructor ( map : StaticMap ) {
58- super ( map )
86+ constructor ( round : Round ) {
87+ super ( round . map . staticMap )
88+ this . bodies = round . bodies
5989 }
6090
6191 public symmetricApply ( x : number , y : number , fields : Record < string , MapEditorBrushField > ) {
@@ -64,7 +94,17 @@ export class WallsBrush extends SymmetricMapEditorBrush<StaticMap> {
6494 const pos = this . map . indexToLocation ( idx )
6595 const ruin = this . map . ruins . findIndex ( ( l ) => squareIntersects ( l , pos , 2 ) )
6696 const paint = this . map . initialPaint [ idx ]
67- if ( ruin !== - 1 || paint ) return true
97+
98+ let tower = undefined
99+ for ( const b of this . bodies . bodies . values ( ) ) {
100+ if ( squareIntersects ( pos , b . pos , 2 ) ) {
101+ tower = b
102+ break
103+ }
104+ }
105+
106+ if ( tower || ruin !== - 1 || paint ) return true
107+
68108 this . map . walls [ idx ] = 1
69109 }
70110
@@ -94,6 +134,7 @@ export class WallsBrush extends SymmetricMapEditorBrush<StaticMap> {
94134}
95135
96136export class RuinsBrush extends SymmetricMapEditorBrush < StaticMap > {
137+ private readonly bodies : Bodies
97138 public readonly name = 'Ruins'
98139 public readonly fields = {
99140 shouldAdd : {
@@ -102,26 +143,14 @@ export class RuinsBrush extends SymmetricMapEditorBrush<StaticMap> {
102143 }
103144 }
104145
105- constructor ( map : StaticMap ) {
106- super ( map )
146+ constructor ( round : Round ) {
147+ super ( round . map . staticMap )
148+ this . bodies = round . bodies
107149 }
108150
109151 public symmetricApply ( x : number , y : number , fields : Record < string , MapEditorBrushField > ) {
110152 const add = ( x : number , y : number ) => {
111- // Check if ruin is too close to the border
112- if ( x <= 1 || x >= this . map . width - 2 || y <= 1 || y >= this . map . height - 2 ) {
113- return true
114- }
115-
116- // Check if this is a valid ruin location
117- const pos = { x, y }
118- const idx = this . map . locationToIndex ( x , y )
119- const ruin = this . map . ruins . findIndex ( ( l ) => squareIntersects ( l , pos , 4 ) )
120- const wall = this . map . walls . findIndex (
121- ( v , i ) => ! ! v && squareIntersects ( this . map . indexToLocation ( i ) , pos , 2 )
122- )
123- const paint = this . map . initialPaint [ idx ]
124- if ( ruin !== - 1 || wall !== - 1 || paint ) {
153+ if ( ! checkValidRuinPlacement ( { x, y } , this . map , this . bodies ) ) {
125154 return true
126155 }
127156
@@ -145,6 +174,7 @@ export class RuinsBrush extends SymmetricMapEditorBrush<StaticMap> {
145174}
146175
147176export class PaintBrush extends SymmetricMapEditorBrush < CurrentMap > {
177+ private readonly bodies : Bodies
148178 public readonly name = 'Paint'
149179 public readonly fields = {
150180 shouldAdd : {
@@ -171,8 +201,9 @@ export class PaintBrush extends SymmetricMapEditorBrush<CurrentMap> {
171201 }
172202 }
173203
174- constructor ( map : CurrentMap ) {
175- super ( map )
204+ constructor ( round : Round ) {
205+ super ( round . map )
206+ this . bodies = round . bodies
176207 }
177208
178209 public symmetricApply ( x : number , y : number , fields : Record < string , MapEditorBrushField > , robotOne : boolean ) {
@@ -217,6 +248,7 @@ export class PaintBrush extends SymmetricMapEditorBrush<CurrentMap> {
217248}
218249
219250export class TowerBrush extends SymmetricMapEditorBrush < StaticMap > {
251+ private readonly bodies : Bodies
220252 public readonly name = 'Towers'
221253 public readonly fields = {
222254 isTower : {
@@ -238,26 +270,20 @@ export class TowerBrush extends SymmetricMapEditorBrush<StaticMap> {
238270 }
239271 }
240272
241- constructor (
242- private readonly bodies : Bodies ,
243- map : StaticMap
244- ) {
245- super ( map )
273+ constructor ( round : Round ) {
274+ super ( round . map . staticMap )
275+ this . bodies = round . bodies
246276 }
247277
248278 public symmetricApply ( x : number , y : number , fields : Record < string , MapEditorBrushField > , robotOne : boolean ) {
249279 const towerType : schema . RobotType = fields . towerType . value
250280 const isTower : boolean = fields . isTower . value
251281
252282 const add = ( x : number , y : number , team : Team ) => {
253- // Check if this is a valid tower location
254283 const pos = { x, y }
255- const idx = this . map . locationToIndex ( x , y )
256- const body = this . bodies . getBodyAtLocation ( x , y )
257- const wall = this . map . walls [ idx ]
258- const ruin = this . map . ruins . findIndex ( ( l ) => squareIntersects ( l , pos , 2 ) )
259-
260- if ( body || wall || ruin !== - 1 ) return null
284+ if ( ! checkValidRuinPlacement ( pos , this . map , this . bodies ) ) {
285+ return null
286+ }
261287
262288 const id = this . bodies . getNextID ( )
263289 this . bodies . spawnBodyFromValues ( id , towerType , team , pos )
0 commit comments