I think it's easier to use a flat curve and than displace the wall based on the terrain. The starting setup I would use has mesh object with an array modifier and curve modifier pointing to a 2D curve. Then we have the 3D terrain as a separate mesh:
Pick the castle wall object and add a node modifer. First thing we need to do is to "apply" the modifier stack to the geometry of the object, so we add the Realize Istances node:
Then we need to displace the geometry of the wall up above, so we add a "Set Position" node that has as input the position field:
The offset value can't be a single value, or the walls will go up all by the same amount. We should sample wall geometry points to find the nearest point of the surface and transfer the only the Z value (see the position field multiplied by 0,0,1).
Sampling node may introduce some imprecisions whit this simple setup. You can achive better results by taking into accounts only the projection of the surface like this:
Finish by adding another node setup to check for faces that looks up (normal = 0,0,1) and scale them by zero on the Z axis
Final result: