1 
2 /*******************************************************************************
3     Grid_Random_Terrain.cpp
4     Copyright (C) Victor Olaya
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA
19 *******************************************************************************/
20 
21 ///////////////////////////////////////////////////////////
22 //														 //
23 //														 //
24 //														 //
25 ///////////////////////////////////////////////////////////
26 
27 //---------------------------------------------------------
28 #include "Grid_Random_Terrain.h"
29 
30 
31 ///////////////////////////////////////////////////////////
32 //														 //
33 //														 //
34 //														 //
35 ///////////////////////////////////////////////////////////
36 
37 //---------------------------------------------------------
CGrid_Random_Terrain(void)38 CGrid_Random_Terrain::CGrid_Random_Terrain(void)
39 {
40 	Set_Name		(_TL("Random Terrain"));
41 
42 	Set_Author		("V.Olaya (c) 2004");
43 
44 	Set_Description	(_TW(
45 		"(c) 2004 by Victor Olaya. Random Terrain Generation"
46 	));
47 
48 	Parameters.Add_Int(
49 		"", "RADIUS"    , _TL("Radius (cells)"),
50 		_TL(""),
51 		25, 1, true
52 	);
53 
54 	Parameters.Add_Int(
55 		"", "ITERATIONS", _TL("Iterations"),
56 		_TL(""),
57 		100, 1, true
58 	);
59 
60 	//-----------------------------------------------------
61 	m_Grid_Target.Create(&Parameters, true, "", "TARGET_");
62 }
63 
64 
65 ///////////////////////////////////////////////////////////
66 //														 //
67 ///////////////////////////////////////////////////////////
68 
69 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)70 int CGrid_Random_Terrain::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
71 {
72 	m_Grid_Target.On_Parameter_Changed(pParameters, pParameter);
73 
74 	return( CSG_Tool::On_Parameter_Changed(pParameters, pParameter) );
75 }
76 
77 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)78 int CGrid_Random_Terrain::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
79 {
80 	m_Grid_Target.On_Parameters_Enable(pParameters, pParameter);
81 
82 	return( CSG_Tool::On_Parameters_Enable(pParameters, pParameter) );
83 }
84 
85 
86 ///////////////////////////////////////////////////////////
87 //														 //
88 ///////////////////////////////////////////////////////////
89 
90 //---------------------------------------------------------
On_Execute(void)91 bool CGrid_Random_Terrain::On_Execute(void)
92 {
93 	if( (m_pGrid = m_Grid_Target.Get_Grid()) == NULL )
94 	{
95 		Error_Set(_TL("invalid target grid"));
96 
97 		return( false );
98 	}
99 
100 	m_pGrid->Set_Name(_TL("Random Terrain"));
101 
102 	m_pGrid->Assign(0.);
103 
104 	//-----------------------------------------------------
105 	m_Kernel.Set_Radius(m_Radius = Parameters("RADIUS")->asInt());
106 
107 	int Iterations	= Parameters("ITERATIONS")->asInt();
108 
109 	for(int i=0; i<Iterations && Set_Progress(i, Iterations); i++)
110 	{
111 		Add_Bump();
112 	}
113 
114 	//-----------------------------------------------------
115 	m_Kernel.Destroy();
116 
117 	return( true );
118 }
119 
120 
121 ///////////////////////////////////////////////////////////
122 //														 //
123 ///////////////////////////////////////////////////////////
124 
125 //---------------------------------------------------------
Add_Bump(void)126 void CGrid_Random_Terrain::Add_Bump(void)
127 {
128 	int	x	= CSG_Random::Get_Uniform(-m_Radius, m_Radius + m_pGrid->Get_NX());
129 	int	y	= CSG_Random::Get_Uniform(-m_Radius, m_Radius + m_pGrid->Get_NY());
130 
131 	for(int i=0; i<m_Kernel.Get_Count(); i++)
132 	{
133 		int	ix	= m_Kernel.Get_X(i, x);
134 		int	iy	= m_Kernel.Get_Y(i, y);
135 
136 		if( m_pGrid->is_InGrid(ix, iy) )
137 		{
138 			m_pGrid->Add_Value(ix, iy, (m_Radius*m_Radius) - SG_Get_Square(m_Kernel.Get_Distance(i)));
139 		}
140 	}
141 }
142 
143 
144 ///////////////////////////////////////////////////////////
145 //														 //
146 //														 //
147 //														 //
148 ///////////////////////////////////////////////////////////
149 
150 //---------------------------------------------------------
151