Index: src/genworld.cpp
===================================================================
--- src/genworld.cpp	(revision 14173)
+++ src/genworld.cpp	(working copy)
@@ -34,6 +34,7 @@
 void GenerateUnmovables();
 bool GenerateTowns();
 void GenerateTrees();
+void GenerateRivers();
 
 void StartupEconomy();
 void StartupPlayers();
@@ -113,8 +114,9 @@
 			GenerateLandscape(_gw.mode);
 			GenerateClearTile();
 
-			/* only generate towns, tree and industries in newgame mode. */
+			/* only generate towns, tree, rivers and industries in newgame mode. */
 			if (_game_mode != GM_EDITOR) {
+				GenerateRivers();
 				GenerateTowns();
 				GenerateIndustries();
 				GenerateUnmovables();
Index: src/rivergen.cpp
===================================================================
--- src/rivergen.cpp	(revision 0)
+++ src/rivergen.cpp	(revision 0)
@@ -0,0 +1,272 @@
+/* $Id: rivergen.cpp planetmaker $ */
+
+/** @file water_cmd.cpp Handling of water tiles. */
+
+#include "stdafx.h"
+#include "openttd.h"
+#include "bridge_map.h"
+#include "bridge.h"
+#include "cmd_helper.h"
+#include "station_map.h"
+#include "tile_cmd.h"
+#include "landscape.h"
+#include "viewport_func.h"
+#include "command_func.h"
+#include "town.h"
+#include "news_func.h"
+#include "depot_base.h"
+#include "depot_func.h"
+#include "vehicle_gui.h"
+#include "train.h"
+#include "roadveh.h"
+#include "water.h"
+#include "water_map.h"
+#include "industry_map.h"
+#include "newgrf.h"
+#include "newgrf_canal.h"
+#include "transparency.h"
+#include "strings_func.h"
+#include "functions.h"
+#include "window_func.h"
+#include "vehicle_func.h"
+#include "sound_func.h"
+#include "variables.h"
+#include "player_func.h"
+#include "settings_type.h"
+#include "clear_map.h"
+#include "tree_map.h"
+#include "station_base.h"
+#include "airport.h"
+#include "newgrf_cargo.h"
+#include "effectvehicle_func.h"
+#include "oldpool_func.h"
+#include "tunnelbridge_map.h"
+#include "table/sprites.h"
+#include "table/strings.h"
+
+/** Height map - allocated array of heights (MapSizeX() + 1) x (MapSizeY() + 1) */
+/* It's currently a duplicate from tgp.cpp
+ * TODO: don't declare it here, again! Reuse old
+ * declaration instead
+ */
+/*struct HeightMap
+{
+	height_t *h;         //< array of heights
+	uint     dim_x;      //< height map size_x MapSizeX() + 1
+	uint     total_size; //< height map total size
+	uint     size_x;     //< MapSizeX()
+	uint     size_y;     //< MapSizeY()
+*/	
+	/**
+	 * Height map accessor
+	 * @param x X position
+	 * @param y Y position
+	 * @return height as fixed point number
+	 */
+/*	inline height_t &height(uint x, uint y) {
+		return h[x + y * dim_x];
+	}
+};*/
+
+/** Global height map instance */
+//static HeightMap _height_map = {NULL, 0, 0, 0, 0};
+
+void GetHeightMap(TileIndex tile, uint16 radius)
+{
+}
+
+bool RiverTerraform(TileIndex old_tile, TileIndex tile, DiagDirection dir)
+{
+	/*
+	 * Procedure terraforms a tile and returns true, if
+	 * the terraform was successful. In that case we could continue the
+	 * river there
+	 */
+//	TileIndex target_tile =  TileAddByDiagDir(tile,dir);
+	if (!IsValidTile(tile)) { return false; }
+	CommandCost ret_val = DoCommand(old_tile, (uint32) tile, 0, 0, CMD_LEVEL_LAND);
+	if (CmdFailed(ret_val)) { return false; }
+//	bool ret_val = DoCommandP(target_tile, tile, 0, 0, CMD_LEVEL_LAND);
+//	return ret_val;
+	return true;
+}
+
+DiagDirection GetAntiSeaDirection(TileIndex tile)
+{	
+	for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
+		TileIndex diag_tile = TileAddByDiagDir(tile, d);
+		if (!IsValidTile(diag_tile)) { continue; }
+		if ((IsTileType(diag_tile, MP_WATER)) && (IsSea(diag_tile))) {
+			return ReverseDiagDir(d);
+		}
+	}
+	return INVALID_DIAGDIR;
+}
+
+DiagDirection GeneralDirectionSea(TileIndex tile, DiagDirection dir)
+{
+	TileIndex sea_tile;
+	int16 x = 0;
+	int16 y = 0;
+	int16 best_x = 0;
+	int16 best_y = 0;
+	uint16 best_distance = 0xffff;
+	for (x = -5; x < 6; x++) {
+		for (y = -5; y < 6; y++) {
+			sea_tile = TileAddWrap(tile, x, y);
+			if (! IsValidTile(sea_tile)) { continue; }
+			if ((IsTileType(sea_tile, MP_WATER)) && (IsSea(sea_tile))) {
+				if ((x * x + y * y) < best_distance) {
+					best_x = x;
+					best_y = y;
+					best_distance = x * x + y * y;
+				}
+			}
+		}
+	}
+	if (best_distance == 0xffff) { return dir; }
+	if (abs(best_x) > abs(best_y)) { return best_x > 0 ? DIAGDIR_SW : DIAGDIR_NE; }
+	return best_y > 0 ? DIAGDIR_SE : DIAGDIR_NW;	
+}
+
+TileIndex GetRandomWell()
+{
+	TileIndex tile = INVALID_TILE;
+	Slope slopet;
+	for (int i = 0; i < 1000; i++) {
+		tile = RandomTile();
+		if (!IsValidTile(tile)) { continue; }
+		slopet = GetTileSlope(tile,NULL);
+		/// We cannot build a river, if the slope is wrong
+		if (! ( (slopet == SLOPE_NW) || (slopet == SLOPE_NE) ||
+			(slopet == SLOPE_SW) || (slopet == SLOPE_SE) ||
+			(slopet == SLOPE_FLAT))) {
+			continue;
+		}
+		///Don't start river adjacent to sea
+/*		if (GetAntiSeaDirection(tile) != INVALID_DIAGDIR) {
+			continue;
+		}*/
+		return tile;
+	}
+	return INVALID_TILE;
+}
+
+DiagDirection GetRandomDiagDir(DiagDirection old_dir)
+{
+	switch (RandomRange(4)) {
+		case 0: 
+			return DIAGDIR_SE;
+		case 1:
+			return DIAGDIR_SW;
+		case 2:
+			return DIAGDIR_NE;
+		case 3:
+			default:
+			return DIAGDIR_NW;
+	}		 
+	return old_dir;
+}
+
+bool SuitableRiverTile(TileIndex old_tile, TileIndex tile, DiagDirection dir)
+{
+	Slope slopet = GetTileSlope(tile,NULL);
+	if (slopet == SLOPE_FLAT) { return true; }
+	
+	TileIndex next_tile = TileAddByDiagDir(tile,dir);
+	if (!IsValidTile(next_tile)) { return false; }
+	if (((slopet == SLOPE_NW) && (dir == DIAGDIR_SE) && (SuitableRiverTile(tile,next_tile,dir))) || 
+		((slopet == SLOPE_NE) && (dir == DIAGDIR_SW) && (SuitableRiverTile(tile,next_tile,dir))) ||
+		((slopet == SLOPE_SW) && (dir == DIAGDIR_NE) && (SuitableRiverTile(tile,next_tile,dir))) || 
+		((slopet == SLOPE_SE) && (dir == DIAGDIR_NW) && (SuitableRiverTile(tile,next_tile,dir)))) {
+		return true;
+	}
+/*	if (RiverTerraform(old_tile, tile, dir)) {
+		return true;
+	}*/
+	return false;
+}
+
+bool WalkRiver(TileIndex tile, DiagDirection dir, uint16 step)
+{
+	if (step > 5000) { return true; }
+	DiagDirection new_dir = dir;
+	if (GetTileSlope(tile,NULL) == SLOPE_FLAT) {
+		/* randomly change the flow direction for this tile
+		 * don't change general direction, though. */
+		if (Chance16(100,1000)) { new_dir = GetRandomDiagDir(dir); }
+		/* also give a general change in direction a certain chance */
+		if (Chance16(50,1000)) { dir = GetRandomDiagDir(dir); } 
+		dir = GeneralDirectionSea(tile, new_dir);
+	}
+	TileIndex new_tile = TileAddByDiagDir(tile,new_dir);
+	/* Invalid tile means we're too close to the edge anyway... */
+	if (IsValidTile(new_tile)) { 
+		/* stop river building, if we hit any water */
+		if (IsTileType(new_tile, MP_WATER)) {
+			if (IsSea(new_tile)) {
+				return true;
+			}
+			if (IsRiver(new_tile)) {
+				if (Chance16(50,100)) { return false; }
+				return true;
+			}
+		}
+		if (SuitableRiverTile(tile,new_tile,new_dir) && 
+			(TileHeight(new_tile) <= TileHeight(tile))) {
+			if (WalkRiver(new_tile,dir,step+1)) {
+				MakeRiver(new_tile,Random());
+				return true;
+			} 
+		} else {
+			if ((RiverTerraform(tile, new_tile, dir)) && 
+				( TileHeight(new_tile) <= TileHeight(tile))) {
+				if (WalkRiver(new_tile,dir,step+1)) {
+					MakeRiver(new_tile,Random());
+					return true;
+				}
+			} else {
+				return false;
+			}
+//			if (GetTileSlope(tile,NULL) != SLOPE_FLAT) { return false; }
+		}
+	}	
+	/* If we cannot walk on, we try the usual approach
+	 * we try all possible directions */
+	for (new_dir = DIAGDIR_BEGIN; new_dir < DIAGDIR_END; new_dir++) {
+		new_tile = TileAddByDiagDir(tile, new_dir);
+		if (!IsValidTile(new_tile)) { continue; }
+		if (IsTileType(new_tile, MP_WATER)) { continue; }
+		if (SuitableRiverTile(tile,new_tile,new_dir) && 
+			(TileHeight(new_tile) <= TileHeight(tile))) {
+			if (WalkRiver(new_tile,dir,step+1)) {
+				MakeRiver(new_tile,Random());
+				return true;
+			}
+		}
+		
+	}
+	return false;
+}
+
+void GenerateRivers()
+{
+	int number_rivers = ScaleByMapSize(10);
+//	AllocHeightMap();
+	DiagDirection d = DIAGDIR_BEGIN;
+	for (int i = 0; i < number_rivers; i++) {
+		/*
+		 * Find a suitable coastal tile and flow the river upwards
+		 */
+		TileIndex tile_riverwell = GetRandomWell();
+		/// Should the search for a suitable tile fail, we skip the loop run
+		if (tile_riverwell == INVALID_TILE) { continue; }
+		MakeRiver(tile_riverwell,Random());
+//		d = GetAntiSeaDirection(tile_riverwell);
+		/// get a random starting direction
+		d = GetRandomDiagDir(d);
+		/// Now just walk the river. We may end any time.
+		WalkRiver(tile_riverwell,d,0);
+	}
+//	FreeHeightMap();
+}
\ No newline at end of file
Index: source.list
===================================================================
--- source.list	(revision 14173)
+++ source.list	(working copy)
@@ -63,6 +63,7 @@
 queue.cpp
 rail.cpp
 rev.cpp
+rivergen.cpp
 road.cpp
 saveload.cpp
 screenshot.cpp
