Index: data/mods/public/maps/random/rmgen/misc.js
===================================================================
--- data/mods/public/maps/random/rmgen/misc.js	(revision 11950)
+++ data/mods/public/maps/random/rmgen/misc.js	(working copy)
@@ -194,7 +194,7 @@
 			placePolygonalWall(fx, fz, 15, ['entry'], 'tower', civ, playerid, angle, 7);
 		else
 		{
-			placeGenericFortress(fx, fz, 20/*radius*/, playerid, ['tower', 'wall', 'tower', 'entry', 'tower', 'wall'], civ, PI/6/*angle offset (0 to PI/4)*/)
+			placeGenericFortress(fx, fz, 20/*radius*/, playerid);
 		}
 	}
 }
Index: data/mods/public/maps/random/rmgen/wall_builder.js
===================================================================
--- data/mods/public/maps/random/rmgen/wall_builder.js	(revision 11950)
+++ data/mods/public/maps/random/rmgen/wall_builder.js	(working copy)
@@ -3,6 +3,7 @@
 ////////////////////////////////////////////////////////////////////
 
 // To do:
+// Check if placeGenericFortress always works without gaps for all races
 // Rename wall elements to fit he entity names so that entity = 'structures/' + 'civ + '_' + wallElement.type in the common case
 // Add roman army camp to style palisades and add upgraded default palisade fortress types matching civ default fortresses
 // Add further wall elements cornerHalfIn, cornerHalfOut and adjust default fortress types to better fit in the octagonal territory of a civil center
@@ -92,58 +93,55 @@
 var wallStyles = {};
 
 // Generic civ dependent wall style definition. 'rome_siege' needs some tweek...
-var scaleByCiv = {'athen' : 1.5, 'cart' : 1.8, 'celt' : 1.5, 'hele' : 1.5, 'iber' : 1.5, 'mace' : 1.5, 'pers' : 1.5, 'rome' : 1.5, 'spart' : 1.5, 'rome_siege' : 1.5};
-for (var style in scaleByCiv)
+var wallScaleByType = {'athen' : 1.5, 'cart' : 1.8, 'celt' : 1.5, 'hele' : 1.5, 'iber' : 1.5, 'mace' : 1.5, 'pers' : 1.5, 'rome' : 1.5, 'spart' : 1.5, 'rome_siege' : 1.5};
+for (var style in wallScaleByType)
 {
 	var civ = style;
 	if (style == 'rome_siege')
 		civ = 'rome';
 	wallStyles[style] = {};
 	// Default wall elements
-	wallStyles[style]['tower'] = new WallElement('tower', 'structures/' + style + '_wall_tower', PI, scaleByCiv[style]);
-	wallStyles[style]['endLeft'] = new WallElement('endLeft', 'structures/' + style + '_wall_tower', PI, scaleByCiv[style]); // Same as tower. To be compatible with palisades...
-	wallStyles[style]['endRight'] = new WallElement('endRight', 'structures/' + style + '_wall_tower', PI, scaleByCiv[style]); // Same as tower. To be compatible with palisades...
-	wallStyles[style]['cornerIn'] = new WallElement('cornerIn', 'structures/' + style + '_wall_tower', 5*PI/4, 0, 0.35*scaleByCiv[style], PI/2); // 2^0.5 / 4 ~= 0.35 ~= 1/3
-	wallStyles[style]['cornerOut'] = new WallElement('cornerOut', 'structures/' + style + '_wall_tower', 3*PI/4, 0.71*scaleByCiv[style], 0, -PI/2); // // 2^0.5 / 2 ~= 0.71 ~= 2/3
-	wallStyles[style]['wallShort'] = new WallElement('wallShort', 'structures/' + style + '_wall_short', 0*PI, 2*scaleByCiv[style]);
-	wallStyles[style]['wall'] = new WallElement('wall', 'structures/' + style + '_wall_medium', 0*PI, 4*scaleByCiv[style]);
-	wallStyles[style]['wallLong'] = new WallElement('wallLong', 'structures/' + style + '_wall_long', 0*PI, 6*scaleByCiv[style]);
+	wallStyles[style]['tower'] = new WallElement('tower', 'structures/' + style + '_wall_tower', PI, wallScaleByType[style]);
+	wallStyles[style]['endLeft'] = new WallElement('endLeft', 'structures/' + style + '_wall_tower', PI, wallScaleByType[style]); // Same as tower. To be compatible with palisades...
+	wallStyles[style]['endRight'] = new WallElement('endRight', 'structures/' + style + '_wall_tower', PI, wallScaleByType[style]); // Same as tower. To be compatible with palisades...
+	wallStyles[style]['cornerIn'] = new WallElement('cornerIn', 'structures/' + style + '_wall_tower', 5*PI/4, 0, 0.35*wallScaleByType[style], PI/2); // 2^0.5 / 4 ~= 0.35 ~= 1/3
+	wallStyles[style]['cornerOut'] = new WallElement('cornerOut', 'structures/' + style + '_wall_tower', 3*PI/4, 0.71*wallScaleByType[style], 0, -PI/2); // // 2^0.5 / 2 ~= 0.71 ~= 2/3
+	wallStyles[style]['wallShort'] = new WallElement('wallShort', 'structures/' + style + '_wall_short', 0*PI, 2*wallScaleByType[style]);
+	wallStyles[style]['wall'] = new WallElement('wall', 'structures/' + style + '_wall_medium', 0*PI, 4*wallScaleByType[style]);
+	wallStyles[style]['wallMedium'] = new WallElement('wall', 'structures/' + style + '_wall_medium', 0*PI, 4*wallScaleByType[style]);
+	wallStyles[style]['wallLong'] = new WallElement('wallLong', 'structures/' + style + '_wall_long', 0*PI, 6*wallScaleByType[style]);
 	// Gate and entrance wall elements
 	if (style == 'cart')
-		var gateWidth = 3.5*scaleByCiv[style];
+		var gateWidth = 3.5*wallScaleByType[style];
 	else if (style == 'celt')
-		var gateWidth = 4*scaleByCiv[style];
+		var gateWidth = 4*wallScaleByType[style];
 	// else if (style == 'iber')
-		// var gateWidth = 5.5*scaleByCiv[style];
+		// var gateWidth = 5.5*wallScaleByType[style];
 	else
-		var gateWidth = 6*scaleByCiv[style];
+		var gateWidth = 6*wallScaleByType[style];
 	wallStyles[style]['gate'] = new WallElement('gate', 'structures/' + style + '_wall_gate', 0*PI, gateWidth);
 	wallStyles[style]['entry'] = new WallElement('entry', undefined, 0*PI, gateWidth);
-	if (civ == 'iber') // Adjust iberians to have no upkeep at entries with a tower for convinience ATM, may be changed
-		wallStyles[style]['entryTower'] = new WallElement('entryTower', 'structures/' + civ + '_wall_tower', PI, gateWidth, -4*scaleByCiv[style]);
-	else
-		wallStyles[style]['entryTower'] = new WallElement('entryTower', 'structures/' + civ + '_defense_tower', PI, gateWidth, -4*scaleByCiv[style]);
-	wallStyles[style]['entryFort'] = new WallElement('entryFort', 'structures/' + civ + '_fortress', 0*PI, 8*scaleByCiv[style], 6*scaleByCiv[style]);
+	wallStyles[style]['entryTower'] = new WallElement('entryTower', 'structures/' + civ + '_defense_tower', PI, gateWidth, -4*wallScaleByType[style]);
+	wallStyles[style]['entryFort'] = new WallElement('entryFort', 'structures/' + civ + '_fortress', 0*PI, 8*wallScaleByType[style], 6*wallScaleByType[style]);
 	// Defensive wall elements with 0 width outside the wall
-	wallStyles[style]['outpost'] = new WallElement('outpost', 'structures/' + civ + '_outpost', PI, 0, -4*scaleByCiv[style]);
-	wallStyles[style]['defenseTower'] = new WallElement('defenseTower', 'structures/' + civ + '_defenseTower', PI, 0, -4*scaleByCiv[style]);
+	wallStyles[style]['outpost'] = new WallElement('outpost', 'structures/' + civ + '_outpost', PI, 0, -4*wallScaleByType[style]);
+	wallStyles[style]['defenseTower'] = new WallElement('defenseTower', 'structures/' + civ + '_defenseTower', PI, 0, -4*wallScaleByType[style]);
 	// Base buildings wall elements with 0 width inside the wall
-	wallStyles[style]['barracks'] = new WallElement('barracks', 'structures/' + civ + '_barracks', PI, 0, 4.5*scaleByCiv[style]);
-	wallStyles[style]['civilCentre'] = new WallElement('civilCentre', 'structures/' + civ + '_civil_centre', PI, 0, 4.5*scaleByCiv[style]);
-	wallStyles[style]['farmstead'] = new WallElement('farmstead', 'structures/' + civ + '_farmstead', PI, 0, 4.5*scaleByCiv[style]);
-	wallStyles[style]['field'] = new WallElement('field', 'structures/' + civ + '_field', PI, 0, 4.5*scaleByCiv[style]);
-	wallStyles[style]['fortress'] = new WallElement('fortress', 'structures/' + civ + '_fortress', PI, 0, 4.5*scaleByCiv[style]);
-	wallStyles[style]['house'] = new WallElement('house', 'structures/' + civ + '_house', PI, 0, 4.5*scaleByCiv[style]);
-	wallStyles[style]['market'] = new WallElement('market', 'structures/' + civ + '_market', PI, 0, 4.5*scaleByCiv[style]);
-	wallStyles[style]['mill'] = new WallElement('mill', 'structures/' + civ + '_mill', PI, 0, 4.5*scaleByCiv[style]);
-	wallStyles[style]['temple'] = new WallElement('temple', 'structures/' + civ + '_temple', PI, 0, 4.5*scaleByCiv[style]);
+	wallStyles[style]['barracks'] = new WallElement('barracks', 'structures/' + civ + '_barracks', PI, 0, 4.5*wallScaleByType[style]);
+	wallStyles[style]['civilCentre'] = new WallElement('civilCentre', 'structures/' + civ + '_civil_centre', PI, 0, 4.5*wallScaleByType[style]);
+	wallStyles[style]['farmstead'] = new WallElement('farmstead', 'structures/' + civ + '_farmstead', PI, 0, 4.5*wallScaleByType[style]);
+	wallStyles[style]['field'] = new WallElement('field', 'structures/' + civ + '_field', PI, 0, 4.5*wallScaleByType[style]);
+	wallStyles[style]['fortress'] = new WallElement('fortress', 'structures/' + civ + '_fortress', PI, 0, 4.5*wallScaleByType[style]);
+	wallStyles[style]['house'] = new WallElement('house', 'structures/' + civ + '_house', PI, 0, 4.5*wallScaleByType[style]);
+	wallStyles[style]['market'] = new WallElement('market', 'structures/' + civ + '_market', PI, 0, 4.5*wallScaleByType[style]);
+	wallStyles[style]['mill'] = new WallElement('mill', 'structures/' + civ + '_mill', PI, 0, 4.5*wallScaleByType[style]);
+	wallStyles[style]['temple'] = new WallElement('temple', 'structures/' + civ + '_temple', PI, 0, 4.5*wallScaleByType[style]);
 	// Generic space/gap wall elements
-	wallStyles[style]['space1'] = new WallElement('space1', undefined, 0*PI, scaleByCiv[style]);
-	wallStyles[style]['space2'] = new WallElement('space2', undefined, 0*PI, 2*scaleByCiv[style]);
-	wallStyles[style]['space3'] = new WallElement('space3', undefined, 0*PI, 3*scaleByCiv[style]);
-	wallStyles[style]['space4'] = new WallElement('space4', undefined, 0*PI, 4*scaleByCiv[style]);
+	wallStyles[style]['space1'] = new WallElement('space1', undefined, 0*PI, wallScaleByType[style]);
+	wallStyles[style]['space2'] = new WallElement('space2', undefined, 0*PI, 2*wallScaleByType[style]);
+	wallStyles[style]['space3'] = new WallElement('space3', undefined, 0*PI, 3*wallScaleByType[style]);
+	wallStyles[style]['space4'] = new WallElement('space4', undefined, 0*PI, 4*wallScaleByType[style]);
 }
-
 // Add wall fortresses for all generic styles
 wallStyles['athen']['wallFort'] = new WallElement('wallFort', 'structures/athen_fortress', 2*PI/2 /* PI/2 */, 5.1 /* 5.6 */, 1.9 /* 1.9 */);
 wallStyles['cart']['wallFort'] = new WallElement('wallFort', 'structures/cart_fortress', PI, 5.1, 1.6);
@@ -158,17 +156,21 @@
 wallStyles['rome_siege']['wallFort'] = new WallElement('wallFort', 'structures/rome_army_camp', PI, 7.2, 2);
 wallStyles['rome_siege']['entryFort'] = new WallElement('entryFort', 'structures/rome_army_camp', PI, 12, 7);
 wallStyles['rome_siege']['house'] = new WallElement('house', 'structures/rome_tent', PI, 0, 4);
+// Add pseudo "theb" wall style (No actual entities available)
+wallStyles["theb"] = deepcopy(wallStyles["hele"]);
 
 // Add special wall styles not well to implement generic (and to show how custom styles can be added)
 
 // Add special wall style 'palisades'
+wallScaleByType['palisades'] = 0.55;
 wallStyles['palisades'] = {};
 wallStyles['palisades']['wall'] = new WallElement('wall', 'other/palisades_rocks_medium', 0*PI, 2.3);
+wallStyles['palisades']['wallMedium'] = new WallElement('wall', 'other/palisades_rocks_medium', 0*PI, 2.3);
 wallStyles['palisades']['wallLong'] = new WallElement('wall', 'other/palisades_rocks_long', 0*PI, 3.5);
 wallStyles['palisades']['wallShort'] = new WallElement('wall', 'other/palisades_rocks_short', 0*PI, 1.2);
 wallStyles['palisades']['tower'] = new WallElement('tower', 'other/palisades_rocks_tower', -PI/2, 0.7);
 wallStyles['palisades']['wallFort'] = new WallElement('wallFort', 'other/palisades_rocks_fort', PI, 1.7);
-wallStyles['palisades']['gate'] = new WallElement('gate', 'other/palisades_rocks_gate', 0*PI, 3.6);
+wallStyles['palisades']['gate'] = new WallElement('gate', 'other/palisades_rocks_gate', PI, 3.6);
 wallStyles['palisades']['entry'] = new WallElement('entry', undefined, wallStyles['palisades']['gate'].angle, wallStyles['palisades']['gate'].width);
 wallStyles['palisades']['entryTower'] = new WallElement('entryTower', 'other/palisades_rocks_watchtower', 0*PI, wallStyles['palisades']['gate'].width, -3);
 wallStyles['palisades']['entryFort'] = new WallElement('entryFort', 'other/palisades_rocks_fort', PI, 6, 3);
@@ -181,11 +183,27 @@
 wallStyles['palisades']['endLeft'] = new WallElement('endLeft', 'other/palisades_rocks_end', PI/2, 0.2);
 
 // Add special wall style 'road'
+// NOTE: This is not a wall style in the common sense. Use with care!
+wallStyles['road'] = {};
+wallStyles['road']['short'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_short.xml", PI/2, 4.5);
+wallStyles['road']['long'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_long.xml", PI/2, 9.5);
+wallStyles['road']['cornerLeft'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_corner.xml", -PI/2, 4.5-2*1.25, 1.25, PI/2); // Correct width by -2*indent to fit xStraicht/corner
+wallStyles['road']['cornerRight'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_corner.xml", 0*PI, 4.5-2*1.25, -1.25, -PI/2); // Correct width by -2*indent to fit xStraicht/corner
+wallStyles['road']['curveLeft'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_curve_small.xml", -PI/2, 4.5+2*0.2, -0.2, PI/2); // Correct width by -2*indent to fit xStraicht/corner
+wallStyles['road']['curveRight'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_curve_small.xml", 0*PI, 4.5+2*0.2, 0.2, -PI/2); // Correct width by -2*indent to fit xStraicht/corner
+wallStyles['road']['start'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_end.xml", PI/2, 2);
+wallStyles['road']['end'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_end.xml", -PI/2, 2);
+wallStyles['road']['xStraight'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_intersect_x.xml", 0*PI, 4.5);
+wallStyles['road']['xLeft'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_intersect_x.xml", 0*PI, 4.5, 0, PI/2);
+wallStyles['road']['xRight'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_intersect_x.xml", 0*PI, 4.5, 0, -PI/2);
+wallStyles['road']['tLeft'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_intersect_T.xml", PI, 4.5, 1.25);
+wallStyles['road']['tRight'] = new WallElement('road', "actor|props/special/eyecandy/road_temperate_intersect_T.xml", 0*PI, 4.5, -1.25);
 
 // Add special wall element collection 'other'
 // NOTE: This is not a wall style in the common sense. Use with care!
 wallStyles['other'] = {};
 wallStyles['other']['fence'] = new WallElement('fence', 'other/fence_long', -PI/2, 3.1);
+wallStyles['other']['fence_medium'] = new WallElement('fence', 'other/fence_long', -PI/2, 3.1);
 wallStyles['other']['fence_short'] = new WallElement('fence_short', 'other/fence_short', -PI/2, 1.5);
 wallStyles['other']['fence_stone'] = new WallElement('fence_stone', 'other/fence_stone', -PI/2, 2.5);
 wallStyles['other']['palisade'] = new WallElement('palisade', 'other/palisades_rocks_short', 0, 1.2);
@@ -248,6 +266,23 @@
 	'cornerOut', 'wall', 'outpost', 'wall', 'cornerIn', 'wall', 'outpost', 'wall', 'endRight'];
 fortressTypes['giant'].wall = wallPart.concat(wallPart, wallPart, wallPart);
 
+// Add some "fortress types" for roads (will only work with style "road")
+// ["start", "short", "xRight", "xLeft", "cornerLeft", "xStraight", "long", "xLeft", "xRight", "cornerRight", "tRight", "tLeft", "xRight", "xLeft", "curveLeft", "xStraight", "curveRight", "end"];
+var wall = ["short", "curveLeft", "short", "curveLeft", "short", "curveLeft", "short", "curveLeft"];
+fortressTypes['road01'] = new Fortress('road01', wall);
+var wall = ["short", "cornerLeft", "short", "cornerLeft", "short", "cornerLeft", "short", "cornerLeft"];
+fortressTypes['road02'] = new Fortress('road02', wall);
+var wall = ["xStraight", "curveLeft", "xStraight", "curveLeft", "xStraight", "curveLeft", "xStraight", "curveLeft"];
+fortressTypes['road03'] = new Fortress('road03', wall);
+var wall = ["start", "curveLeft", "tRight", "cornerLeft", "tRight", "curveRight", "short", "xRight", "curveLeft", "xRight", "short", "cornerLeft", "tRight", "short",
+	"curveLeft", "short", "tRight", "cornerLeft", "short", "xRight", "curveLeft", "xRight", "short", "curveRight", "tRight", "cornerLeft", "tRight", "curveLeft", "end"];
+fortressTypes['road04'] = new Fortress('road04', wall);
+var wall = ["start", "tLeft", "short", "xRight",
+	"curveLeft", "xRight", "tRight", "cornerLeft", "tRight",
+	"curveLeft", "short", "tRight", "cornerLeft", "xRight",
+	"cornerLeft", "xRight", "short", "tRight", "curveLeft", "end"];
+fortressTypes['road05'] = new Fortress('road05', wall);
+
 // Add (some) iberian civ bonus fortress (The civ can still be set, just an uncommon default fortress)
 var wall = ['gate', 'tower', 'wall', 'cornerIn', 'wallLong', 'tower',
 	'gate', 'tower', 'wallLong', 'cornerIn', 'wall', 'tower',
@@ -294,8 +329,8 @@
 // An alignement can be used to get the center of a 'wall' (more likely used for closed walls like fortresses) with the getWallCenter function
 function getWallAlignment(startX, startY, wall, style, orientation)
 {
-	orientation = (orientation !== undefined) ? orientation : 0*PI;
-	var alignment = []
+	orientation = (orientation || 0*PI);
+	var alignment = [];
 	var wallX = startX;
 	var wallY = startY;
 	for (var i = 0; i < wall.length; i++)
@@ -325,7 +360,7 @@
 				wallX += element.indent * cos(orientation);
 				wallY += element.indent * sin(orientation);
 			}
-			// Set the next coordinates of the next element in the wall (meaning without indentation adjustment)
+			// Set the next coordinates of the next element in the wall without indentation adjustment
 			wallX -= distance * sin(orientation);
 			wallY += distance * cos(orientation);
 		}
@@ -421,6 +456,7 @@
 // style: Optional. An wall style string (like defined in the 'wallStyles' dictionary). Default is 'palisades'
 // playerId: Optional. The walls owners player ID (like in the function 'placeObject' so 0 is gaia, 1 is the first player), Default is 0 (gaia)
 // endWithFirst: Optional. A boolean value. If true the 1st wall element in the wallPart array will finalize the wall. Default is true
+// TODO: Maybe add angle offset for more generic looking?
 function placeLinearWall(startX, startY, targetX, targetY, wallPart, style, playerId, endWithFirst)
 {
 	// Setup optional arguments to the default
@@ -488,7 +524,7 @@
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 // The wall is not necessarily closed depending on the optional argument maxAngle (better name?)
 // NOTE: Don't use wall elements with bending like corners!
-// Todo: add eccentricity
+// TODO: Add eccentricity and maxBendOff functionality (untill now an unused argument)
 function placeCircularWall(centerX, centerY, radius, wallPart, style, playerId, orientation, maxAngle, endWithFirst, maxBendOff)
 {
 	// Setup optional arguments to the default
@@ -581,7 +617,7 @@
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 // NOTE: Don't use wall elements with bending like corners!
 // TODO: Check some arguments
-// TODO: Add eccentricity and irregularity (how far the corners can differ from a regulare convex poygon)
+// TODO: Add eccentricity and perhaps make it call placeIrregularPolygonalWall with irregularity = 0
 function placePolygonalWall(centerX, centerY, radius, wallPart, cornerWallElement, style, playerId, orientation, numCorners, skipFirstWall)
 {
 	// Setup optional arguments to the default
@@ -620,11 +656,10 @@
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 // Place an irregular polygonal wall of some wall parts to choose from arround centerX/centerY with the given radius
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// NOTE: Under construction!!!
 // NOTE: wallPartList is put to the end because it's hardest to set
 // NOTE: Don't use wall elements with bending like corners!
 // TODO: Check some arguments
-// TODO: Add eccentricity and irregularity (how far the corners can differ from a regulare convex poygon)
+// TODO: Add eccentricity
 function placeIrregularPolygonalWall(centerX, centerY, radius, cornerWallElement, style, playerId, orientation, numCorners, irregularity, skipFirstWall, wallPartsAssortment)
 {
 	// Generating a generic wall part assortment with each wall part including 1 gate enlengthend by walls and towers
@@ -721,35 +756,119 @@
 	}
 }
 
-// Just a test function, USE WITH CARE!
+////////////////////////////////////////////////////////////////////////////////////////////////////////
+//
+//  Place generic fortress with towers at the edges connected with long walls and gates
+//
+//  This is the default Iberian civ bonus starting wall
+//
+//  style          Which wall style should be used, default is the given players civ, palisades for gaia
+//  irregularity   How irregular the wall will be, 0 -> circle, 1 -> very spiky, default is 0.5
+//  gateOccurence  Integer number, every n walls will be gates instead, default is 3
+//  maxTrys        How often the function tries to find a better fitting shape at max, default is 100
+//
+////////////////////////////////////////////////////////////////////////////////////////////////////////
+function placeGenericFortress(centerX, centerY, radius, playerId, style, irregularity, gateOccurence, maxTrys)
+{
+	// Setup optional arguments
+	radius = (radius || 20);
+	playerId = (playerId || 0);
+	if (playerId == 0)
+		style = (style || "palisades");
+	else
+		style = (style || g_MapSettings.PlayerData[playerId-1].Civ);
+	irregularity = (irregularity || 0.5);
+	gateOccurence = (gateOccurence || 3);
+	maxTrys = (maxTrys || 100);
+	// Setup some vars
+	var startAngle = randFloat(0, 2*PI);
+	var actualOffX = radius*cos(startAngle);
+	var actualOffY = radius*sin(startAngle);
+	var actualAngle = startAngle;
+	var pointDistance = wallStyles[style]["wallLong"].width + wallStyles[style]["tower"].width;
+	// Searching for a well fitting point derivation
+	var tries = 0;
+	var bestPointDerivation = undefined;
+	var minOverlap = 1000;
+	var overlap = undefined;
+	while (tries < maxTrys && minOverlap > wallStyles[style]["tower"].width / 10)
+	{
+		var pointDerivation = [];
+		var distanceToTarget = 1000;
+		var targetReached = false;
+		while (!targetReached)
+		{
+			var indent = randFloat(-irregularity*pointDistance, irregularity*pointDistance);
+			var tmpAngle = getAngle(actualOffX, actualOffY,
+				(radius + indent)*cos(actualAngle + (pointDistance / radius)),
+				(radius + indent)*sin(actualAngle + (pointDistance / radius)));
+			actualOffX += pointDistance*cos(tmpAngle);
+			actualOffY += pointDistance*sin(tmpAngle);
+			actualAngle = getAngle(0, 0, actualOffX, actualOffY);
+			pointDerivation.push([actualOffX, actualOffY]);
+			distanceToTarget = getDistance(actualOffX, actualOffY, pointDerivation[0][0], pointDerivation[0][1]);
+			if ( pointDerivation.length > 3 && distanceToTarget < pointDistance) // Could be done better...
+			{
+				targetReached = true;
+				overlap = pointDistance - getDistance(pointDerivation[pointDerivation.length - 1][0], pointDerivation[pointDerivation.length - 1][1], pointDerivation[0][0], pointDerivation[0][1]);
+				if (overlap < minOverlap)
+				{
+					minOverlap = overlap;
+					bestPointDerivation = pointDerivation;
+				}
+			}
+		}
+		tries++;
+	}
+	log("placeGenericFortress: Reduced overlap to " + minOverlap + " after " + tries + " tries");
+	// Place wall
+	for (var pointIndex = 0; pointIndex < bestPointDerivation.length; pointIndex++)
+	{
+		var startX = centerX + bestPointDerivation[pointIndex][0];
+		var startY = centerY + bestPointDerivation[pointIndex][1];
+		var targetX = centerX + bestPointDerivation[(pointIndex + 1) % bestPointDerivation.length][0];
+		var targetY = centerY + bestPointDerivation[(pointIndex + 1) % bestPointDerivation.length][1];
+		var angle = getAngle(startX, startY, targetX, targetY);
+		var wallElement = "wallLong";
+		if ((pointIndex + 1) % gateOccurence == 0)
+			wallElement = "entry";
+		if (wallStyles[style][wallElement].entity)
+		{
+			placeObject(startX + (getDistance(startX, startY, targetX, targetY)/2)*cos(angle), // placeX
+				startY + (getDistance(startX, startY, targetX, targetY)/2)*sin(angle), // placeY
+				wallStyles[style][wallElement].entity, playerId, angle - PI/2 + wallStyles[style][wallElement].angle);
+		}
+		// Place tower
+		var startX = centerX + bestPointDerivation[(pointIndex + bestPointDerivation.length - 1) % bestPointDerivation.length][0];
+		var startY = centerY + bestPointDerivation[(pointIndex + bestPointDerivation.length - 1) % bestPointDerivation.length][1];
+		var angle = getAngle(startX, startY, targetX, targetY);
+		placeObject(centerX + bestPointDerivation[pointIndex][0], centerY + bestPointDerivation[pointIndex][1], wallStyles[style]["tower"].entity, playerId, angle - PI/2 + wallStyles[style]["tower"].angle);
+	}
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Place an generic 'organic' looking wall of repeatant wall elements given by wallPart roughly arround centerX/centerY with roughly the given radius
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 // radius: A float seting the aproximate radius of the fortress
-// maxBendOff: Optional, The maximum random bending offset of the wall elements NOTE: If maxBendOff > PI/2 the wall might never close!!!
-// wallPart: Optional, An array of wall element string, example: ["tower", "wall"] NOTE: Don't use wall elements with bending!!!
-function placeGenericFortress(centerX, centerY, radius, playerId, wallPart, style, maxBendOff)
+// maxBendOff: Optional, The maximum random bending offset of the wall elements
+// NOTE: If maxBendOff > PI/3 the wall might not circumvent the center, if > PI/2 the wall might wind up in an infinite loop!!!
+// wallPart: Optional, An array of wall element string, example: ["tower", "wall"]
+// NOTE: Don't use wall elements with bending like corners!
+function placeOrganicFortress(centerX, centerY, radius, playerId, wallPart, style, maxBendOff)
 {
-	if (wallPart == undefined)
+	// Setup optional arguments to the default
+	if (wallPart === undefined)
 		wallPart = ['tower', 'wall', 'tower', 'entry', 'tower', 'wall'];
-	if (maxBendOff > PI/2 || maxBendOff < 0)
-		warn("setGenericFortress maxBendOff sould satisfy 0 < maxBendOff < PI/2 (~1.5) but it is: " + maxBendOff);
 	if (maxBendOff === undefined)
 		maxBendOff = 0;
-	var wallLength = 0;
-	for (var eleIndex = 0; eleIndex < wallPart.length; eleIndex++)
-	{
-		wallLength += wallStyles[style][wallPart[eleIndex]].width
-	}
-	if (wallLength > 2*PI*radius || wallLength <= 0)
-		warn("setGenericFortress: sum of the width of wall's elements should satisfy 0 < total length < 2*PI*radius but: radius = " + radius + ", wallPart = " + wallPart + " with total length of " + wallLength);
-	
-	var minEleWidth = 1000000; // Assuming the smallest element is about as wide as deep.
-	for (var eleIndex = 0; eleIndex < wallPart.length; eleIndex++)
-	{
-		eleWidth = wallStyles[style][wallPart[eleIndex]].width;
-		if (eleWidth < minEleWidth)
-			minEleWidth = eleWidth;
-	}
-	var widthSafty = minEleWidth/4; // Can be done better...
-	
+	// Check arguments for sane values
+	if (maxBendOff > PI/3 || maxBendOff < 0)
+		warn("placeOrganicFortress maxBendOff sould satisfy 0 < maxBendOff < PI/3 (~1) to ensure stability but it is: " + maxBendOff);
+	// Check wall part length against shortest theoretical total wall length (circle perimeter)
+	var wallPartLength = getWallLength(wallPart, style);
+	if (wallPartLength > PI*radius || wallPartLength <= 0)
+		warn("placeOrganicFortress: Given wall part is longer than half the circle perimeter of given radius: Half circle perimeter ~= " + round(PI*radius) +", wallPart total length ~= " + round(wallPartLength));
+	// Setting up some variables
 	var x = radius;
 	var y = 0;
 	var angle = 0;
@@ -757,11 +876,12 @@
 	var targetY = 0;
 	var targetReached = false;
 	var eleIndex = 0;
+	// Start the evil while loope ^^
 	while (targetReached == false)
 	{
 		var wallElement = wallStyles[style][wallPart[eleIndex % wallPart.length]];
-		var eleWidth = wallElement.width - widthSafty;
-		// Stabalized bendOff
+		var eleWidth = wallElement.width; // Reducing this a bit to avoid gaps might be good
+		// Stabalized bendOff. TODO: Ensure positive eleAngleWidth!!!
 		var actualRadius = getDistance(centerX, centerY, centerX + x, centerY + y);
 		var bendOff = randFloat(-maxBendOff*radius/actualRadius, maxBendOff*actualRadius/radius);
 		// Element width in radians
@@ -774,11 +894,11 @@
 		x = placeX - eleWidth/2 * sin(eleAngle);
 		y = placeY + eleWidth/2 * cos(eleAngle);
 		angle += eleAngleWidth;
-		if (eleIndex % wallPart.length == 0 && eleIndex > wallPart.length && (getDistance(x, y, targetX, targetY) < wallLength || angle > 2*PI))
+		if (eleIndex % wallPart.length == 0 && eleIndex > wallPart.length && (getDistance(x, y, targetX, targetY) < wallPartLength || angle > 2*PI))
 			targetReached = true;
 		eleIndex++;
-		// if (eleIndex == 10)
-			// break;
 	}
-	placeLinearWall(centerX + x, centerY + y, centerX + targetX, centerY + targetY, ['wall', 'tower'], style, playerId, true);
+	// Close wall with simple linear wall
+	if (getDistance(centerX + x, centerY + y, centerX + targetX, centerY + targetY) > wallStyles[style]['wallLong'].width)
+		placeLinearWall(centerX + x, centerY + y, centerX + targetX, centerY + targetY, ['wall', 'tower'], style, playerId, true);
 }
