#!/bin/ksh

USAGE="$0 mapname width height [style]"

GATEHEIGHT=128

if (( $# != 3 )) && (( $# != 4 ))
then
	print -u2 "Usage: $USAGE"
	print -u2 "style is for example 'metal' (this is the default)"
	exit 2
fi

mapname=$1
integer width=$2
integer height=$3
style=${4:-metal}

# The Y coordinate of the top of the gate (center it along the vertical extent of the map)
(( gate_start_y = height / 2 - GATEHEIGHT / 2 ))
# The Y coordinate of the bottom of the gate
(( gate_end_y = gate_start_y + GATEHEIGHT ))

# Map header
print "name $mapname"
print "# Remember to increment revision every time you make a change!"
print "revision 1"
print "width $width"
print "height $height"
print "# You can customize game parameters here.  See the lmserver man page for a list of game parameters."
print "# Examples:"
print "# max_players 16"
print "# freeze_time 7500"
print "# friendly_fire off"
print "# The next blank line is important - it signifies that the header section has ended."
print

# Background
cat <<-EOF
# Background
BACKGROUND	0,0	${style}_bgtile	tile=${width},${height}
BACKGROUND	-64,${gate_start_y}	${style}_bgtile	tile=64,${GATEHEIGHT}
BACKGROUND	${width},${gate_start_y}	${style}_bgtile	tile=64,${GATEHEIGHT}

EOF

# Walls
cat <<-EOF
# Walls
BACKGROUND	0,${height}	${style}_wall_s32	tile=${width},32
BACKGROUND	0,-32	${style}_wall_n32	tile=${width},32
BACKGROUND	${width},0	${style}_wall_e32	tile=32,${gate_start_y}
BACKGROUND	${width},${gate_start_y}	${style}_gate_e
BACKGROUND	${width},${gate_end_y}	${style}_wall_e32	tile=32,${gate_start_y}
BACKGROUND	-32,0	${style}_wall_w32	tile=32,${gate_start_y}
BACKGROUND	-64,${gate_start_y}	${style}_gate_w
BACKGROUND	-32,${gate_end_y}	${style}_wall_w32	tile=32,${gate_start_y}

# Wall Corners
BACKGROUND	${width},-32	${style}_wall_ne32
BACKGROUND	-32,-32	${style}_wall_nw32
BACKGROUND	-32,${height}	${style}_wall_sw32
BACKGROUND	${width},${height}	${style}_wall_se32
EOF

# Gates
cat <<-EOF
# Gates and Gate Repulsion Fields
GATE	-15,$((gate_start_y + 8))	blue_gate	team=blue
GATE	$((width + 7)),$((gate_start_y + 8))	red_gate	team=red
FORCE	0,$((height / 2))	-	circle:512	frozen	sucks-off-obstacles	gravity=-200
FORCE	${width},$((height / 2))	-	circle:512	frozen	sucks-off-obstacles	gravity=-200

EOF

# Sample Objects
cat <<-EOF
# Spawnpoints at Gates
SPAWN	0,$((height / 2))	BLUE
SPAWN	${width},$((height / 2))	RED

# SAMPLE obstacles (make your own)
# Look in data/sprites to see what sprites are available
# This is a 64x64 obstacle:
#OBSTACLE	64,256	${style}_obstacle64	rect:64,64
# This is a 128x128 obstacle:
#OBSTACLE	128,256	${style}_obstacle128	rect:128,128
EOF

