# How can I create a terrain that looks like a staircase in R

I am trying to develop a terrain in R that looks like a stretched out staircase, i.e. the tread is the horizontal parts that I want to place buildings on, while the rise should be gentle sloping (not vertical) to maintain the slope of the terrain. The code below generates the picture presented, which is a bit fine, but i want to eliminate the steep parts altogether and extend the sloping parts to link the treads directly. I think something has to be done in the if...else construct, but I just can´t figure it out. Please HELP!

library(raster)
library(sp)
library(ncdf4)

L=300
W=300
dx=01
x0=0.
y0=0.
z0=0.
Nrow=ceiling(L/dx)
Ncol=ceiling(W/dx)
LB=25
WB=32
sx=11
sy=12
H=0.12
Y=2
NB=ceiling(L/(LB+sy+Y))
ymx=y0+Nrow*dx

s1=-0.04
s2=-0.0

M<-matrix(z0, nrow=Nrow, ncol=Ncol, byrow = TRUE)

jkink=floor(Nrow/NB)

for(i in 1:Ncol) {
x = x0 + dx*(i-0.5)
for(j1 in 1:Nrow) {
y = ymx-dx*(j1-0.5)
if(j1%%jkink<NB){
M[j1, i]= z0 + s1*(y-ymx)
zkink = M[j1, i]
ykink = y
}else{
M[j1, i] = zkink + s2*(y-ykink)
}
}
}

Ma <- raster(M, xmn = x0, xmx = (x0+Ncol)*dx, ymn = y0, ymx = y0+Nrow*dx)
plot(Ma)