@@ -74,7 +74,7 @@ static inline float LineGroundSquareCol(
7474 const int & xs,
7575 const int & ys)
7676{
77- const bool inMap = (xs >= 0 ) && (ys >= 0 ) && (xs <= gs->mapx ) && (ys <= gs->mapy );
77+ const bool inMap = (xs >= 0 ) && (ys >= 0 ) && (xs <= gs->mapxm1 ) && (ys <= gs->mapym1 );
7878 assert (inMap);
7979 if (!inMap)
8080 return -1 .0f ;
@@ -103,8 +103,8 @@ static inline float LineGroundSquareCol(
103103 fromFacePlaneDist = (from - cornerVertex).dot (faceNormalTL);
104104
105105 if (fromFacePlaneDist != toFacePlaneDist) {
106- const float alpha = fromFacePlaneDist / (fromFacePlaneDist - toFacePlaneDist) ;
107- const float3 col = from * ( 1 . 0f - alpha) + ( to * alpha);
106+ const float alpha = fromFacePlaneDist / toFacePlaneDist;
107+ const float3 col = from * alpha + to * ( 1 . 0f - alpha);
108108
109109 if ((col.x >= cornerVertex.x ) && (col.z >= cornerVertex.z ) && (col.x + col.z <= cornerVertex.x + cornerVertex.z + SQUARE_SIZE)) {
110110 // ! point of intersection is inside the TL triangle
@@ -127,8 +127,8 @@ static inline float LineGroundSquareCol(
127127 fromFacePlaneDist = (from - cornerVertex).dot (faceNormalBR);
128128
129129 if (fromFacePlaneDist != toFacePlaneDist) {
130- const float alpha = fromFacePlaneDist / (fromFacePlaneDist - toFacePlaneDist) ;
131- const float3 col = from * ( 1 . 0f - alpha) + ( to * alpha);
130+ const float alpha = fromFacePlaneDist / toFacePlaneDist;
131+ const float3 col = from * alpha + to * ( 1 . 0f - alpha);
132132
133133 if ((col.x <= cornerVertex.x ) && (col.z <= cornerVertex.z ) && (col.x + col.z >= cornerVertex.x + cornerVertex.z - SQUARE_SIZE)) {
134134 // ! point of intersection is inside the BR triangle
@@ -198,7 +198,7 @@ inline static bool ClampInMapHeight(float3& from, float3& to)
198198 return false ;
199199
200200 const float3 dir = (to - from);
201- if (dir.y > 0 ) {
201+ if (dir.y >= 0 . 0f ) {
202202 // both `from` & `to` are above map's height
203203 from = float3 (-1 .0f , -1 .0f , -1 .0f );
204204 to = float3 (-1 .0f , -1 .0f , -1 .0f );
@@ -236,28 +236,33 @@ float CGround::LineGroundCol(float3 from, float3 to, bool synced) const
236236 }
237237
238238 const float skippedDist = (pfrom - from).Length ();
239+
240+ if (synced) { // TODO do this in unsynced too once the map border rendering is finished?
241+ // check if our start position is underground (assume ground is unpassable for cannons etc.)
242+ const int sx = from.x / SQUARE_SIZE;
243+ const int sz = from.z / SQUARE_SIZE;
244+ const float & h = hm[sz * gs->mapxp1 + sx];
245+ if (from.y <= h) {
246+ return 0 .0f + skippedDist;
247+ }
248+ }
249+
239250 const float dx = to.x - from.x ;
240251 const float dz = to.z - from.z ;
241252 const int dirx = (dx > 0 .0f ) ? 1 : -1 ;
242253 const int dirz = (dz > 0 .0f ) ? 1 : -1 ;
243254
244- const float ffsx = from.x / SQUARE_SIZE;
245- const float ffsz = from.z / SQUARE_SIZE;
246- const float ttsx = to.x / SQUARE_SIZE;
247- const float ttsz = to.z / SQUARE_SIZE;
255+ // Claming is done cause LineGroundSquareCol() operates on the 2 triangles faces each heightmap
256+ // square is formed of.
257+ const float ffsx = Clamp (from.x / SQUARE_SIZE, 0 .0f , (float )gs->mapxm1 );
258+ const float ffsz = Clamp (from.z / SQUARE_SIZE, 0 .0f , (float )gs->mapym1 );
259+ const float ttsx = Clamp (to.x / SQUARE_SIZE, 0 .0f , (float )gs->mapxm1 );
260+ const float ttsz = Clamp (to.z / SQUARE_SIZE, 0 .0f , (float )gs->mapym1 );
248261 const int fsx = ffsx; // a>=0: int(a):=floor(a)
249262 const int fsz = ffsz;
250263 const int tsx = ttsx;
251264 const int tsz = ttsz;
252-
253- if (synced) { // TODO do this in unsynced too once the map border rendering is finished?
254- // check if our start position is underground (assume ground is unpassable for cannons etc.)
255- const float & h = hm[fsz * gs->mapxp1 + fsx];
256- if (from.y <= h) {
257- return 0 .0f + skippedDist;
258- }
259- }
260-
265+
261266 bool keepgoing = true ;
262267
263268 if ((fsx == tsx) && (fsz == tsz)) {
0 commit comments