]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DTreePlanter.cpp
Merge branch 'master' into divVerent/farplanedist-sky-fix
[xonotic/netradiant.git] / contrib / bobtoolz / DTreePlanter.cpp
1 /*
2    BobToolz plugin for GtkRadiant
3    Copyright (C) 2001 Gordon Biggans
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "DTreePlanter.h"
21
22 #include <list>
23 #include "str.h"
24
25 #include "DPoint.h"
26 #include "DPlane.h"
27 #include "DBrush.h"
28 #include "DEPair.h"
29 #include "DPatch.h"
30 #include "DEntity.h"
31
32 #include "ScriptParser.h"
33 #include "misc.h"
34 #include "scenelib.h"
35
36
37
38 #include "funchandlers.h"
39
40 SignalHandlerResult DTreePlanter::mouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
41         if ( button != c_buttonLeft ) {
42                 return SIGNAL_CONTINUE_EMISSION;
43         }
44         VIEWTYPE vt = GlobalRadiant().XYWindow_getViewType();
45
46         switch ( vt ) {
47         case XY:
48                 break;
49         case YZ:
50         case XZ:
51         default:
52                 return SIGNAL_CONTINUE_EMISSION;
53         }
54
55         Vector3 pt, vhit;
56
57         pt = vector3_snapped( GlobalRadiant().XYWindow_windowToWorld( position ), GlobalRadiant().getGridSize() );
58
59         if ( FindDropPoint( vector3_to_array( pt ), vector3_to_array( vhit ) ) ) {
60                 vhit[2] += m_offset;
61
62                 char buffer[128];
63                 DEntity e( m_entType );
64
65                 sprintf( buffer, "%i %i %i", (int)vhit[0], (int)vhit[1], (int)vhit[2] );
66                 e.AddEPair( "origin", buffer );
67
68                 if ( m_autoLink ) {
69
70                         const scene::Path* pLastEntity = NULL;
71                         const scene::Path* pThisEntity = NULL;
72
73                         int entpos;
74                         for ( int i = 0; i < 256; i++ ) {
75                                 sprintf( buffer, m_linkName, i );
76                                 pThisEntity = FindEntityFromTargetname( buffer );
77
78                                 if ( pThisEntity ) {
79                                         entpos = i;
80                                         pLastEntity = pThisEntity;
81                                 }
82                         }
83
84                         if ( !pLastEntity ) {
85                                 sprintf( buffer, m_linkName, 0 );
86                         }
87                         else {
88                                 sprintf( buffer, m_linkName, entpos + 1 );
89                         }
90
91                         e.AddEPair( "targetname", buffer );
92
93                         if ( pLastEntity ) {
94                                 DEntity e2;
95                                 e2.LoadFromEntity( pLastEntity->top(), true );
96                                 e2.AddEPair( "target", buffer );
97                                 e2.RemoveFromRadiant();
98                                 e2.BuildInRadiant( false );
99                         }
100
101                 }
102
103                 if ( m_setAngles ) {
104                         int angleYaw = ( rand() % ( m_maxYaw - m_minYaw + 1 ) ) + m_minYaw;
105                         int anglePitch = ( rand() % ( m_maxPitch - m_minPitch + 1 ) ) + m_minPitch;
106
107                         sprintf( buffer, "%i %i 0", anglePitch, angleYaw );
108                         e.AddEPair( "angles", buffer );
109                 }
110
111                 if ( m_numModels ) {
112                         int treetype = rand() % m_numModels;
113                         e.AddEPair( "model", m_trees[treetype].name );
114                 }
115
116                 if ( m_useScale ) {
117                         float scale = ( ( ( rand() % 1000 ) * 0.001f ) * ( m_maxScale - m_minScale ) ) + m_minScale;
118
119                         sprintf( buffer, "%f", scale );
120                         e.AddEPair( "modelscale", buffer );
121                 }
122
123                 e.BuildInRadiant( false );
124         }
125
126         if ( m_autoLink ) {
127                 DoTrainPathPlot();
128         }
129
130         return SIGNAL_STOP_EMISSION;
131 }
132
133 bool DTreePlanter::FindDropPoint( vec3_t in, vec3_t out ) {
134         DPlane p1;
135         DPlane p2;
136
137         vec3_t vUp =        { 0, 0, 1 };
138         vec3_t vForward =   { 0, 1, 0 };
139         vec3_t vLeft =      { 1, 0, 0 };
140
141         in[2] = 65535;
142
143         VectorCopy( in, p1.points[0] );
144         VectorCopy( in, p1.points[1] );
145         VectorCopy( in, p1.points[2] );
146         VectorMA( p1.points[1], 20, vUp,         p1.points[1] );
147         VectorMA( p1.points[1], 20, vLeft,       p1.points[2] );
148
149         VectorCopy( in, p2.points[0] );
150         VectorCopy( in, p2.points[1] );
151         VectorCopy( in, p2.points[2] );
152         VectorMA( p1.points[1], 20, vUp,         p2.points[1] );
153         VectorMA( p1.points[1], 20, vForward,    p2.points[2] );
154
155         p1.Rebuild();
156         p2.Rebuild();
157
158         bool found = false;
159         vec3_t temp;
160         vec_t dist;
161         int cnt = m_world.GetIDMax();
162         for ( int i = 0; i < cnt; i++ ) {
163                 DBrush* pBrush = m_world.GetBrushForID( i );
164
165                 if ( pBrush->IntersectsWith( &p1, &p2, temp ) ) {
166                         vec3_t diff;
167                         vec_t tempdist;
168                         VectorSubtract( in, temp, diff );
169                         tempdist = VectorLength( diff );
170                         if ( !found || ( tempdist < dist ) ) {
171                                 dist = tempdist;
172                                 VectorCopy( temp, out );
173                                 found  = true;
174                         }
175                 }
176         }
177
178         return found;
179 }
180
181 class TreePlanterDropEntityIfSelected
182 {
183 mutable DEntity ent;
184 DTreePlanter& planter;
185 public:
186 TreePlanterDropEntityIfSelected( DTreePlanter& planter ) : planter( planter ){
187 }
188 void operator()( scene::Instance& instance ) const {
189         if ( !instance.isSelected() ) {
190                 return;
191         }
192         ent.LoadFromEntity( instance.path().top() );
193
194         DEPair* pEpair = ent.FindEPairByKey( "origin" );
195         if ( !pEpair ) {
196                 return;
197         }
198
199         vec3_t vec, out;
200         sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2] );
201
202         planter.FindDropPoint( vec, out );
203
204         char buffer[256];
205         sprintf( buffer, "%f %f %f", out[0], out[1], out[2] );
206         ent.AddEPair( "origin", buffer );
207         ent.RemoveFromRadiant();
208         ent.BuildInRadiant( false );
209 }
210 };
211
212 void DTreePlanter::DropEntsToGround( void ) {
213         Scene_forEachEntity( TreePlanterDropEntityIfSelected( *this ) );
214 }
215
216 void DTreePlanter::MakeChain( int linkNum, const char* linkName ) {
217         char buffer[256];
218         int i;
219         for ( i = 0; i < linkNum; i++ ) {
220                 DEntity e( "info_train_spline_main" );
221
222                 sprintf( buffer, "%s_pt%i", linkName, i );
223                 e.AddEPair( "targetname", buffer );
224
225                 sprintf( buffer, "0 %i 0", i * 64 );
226                 e.AddEPair( "origin", buffer );
227
228                 if ( i != m_linkNum - 1 ) {
229                         sprintf( buffer, "%s_pt%i", linkName, i + 1 );
230                         e.AddEPair( "target", buffer );
231
232                         sprintf( buffer, "%s_ctl%i", linkName, i );
233                         e.AddEPair( "control", buffer );
234                 }
235                 e.BuildInRadiant( false );
236         }
237
238         for ( i = 0; i < linkNum - 1; i++ ) {
239                 DEntity e( "info_train_spline_control" );
240
241                 sprintf( buffer, "%s_ctl%i", linkName, i );
242                 e.AddEPair( "targetname", buffer );
243
244                 sprintf( buffer, "0 %i 0", ( i * 64 ) + 32 );
245                 e.AddEPair( "origin", buffer );
246
247                 e.BuildInRadiant( false );
248         }
249 }
250
251 void DTreePlanter::SelectChain( void ) {
252 /*      char buffer[256];
253
254     for(int i = 0; i < m_linkNum; i++) {
255         DEntity e("info_train_spline_main");
256
257         sprintf( buffer, "%s_pt%i", m_linkName, i );
258         e.AddEPair( "targetname", buffer );
259
260         sprintf( buffer, "0 %i 0", i * 64 );
261         e.AddEPair( "origin", buffer );
262
263         if(i != m_linkNum-1) {
264             sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
265             e.AddEPair( "target", buffer );
266
267             sprintf( buffer, "%s_ctl%i", m_linkName, i );
268             e.AddEPair( "control", buffer );
269         }
270
271         e.BuildInRadiant( false );
272     }
273
274     for(int i = 0; i < m_linkNum-1; i++) {
275         DEntity e("info_train_spline_control");
276
277         sprintf( buffer, "%s_ctl%i", m_linkName, i );
278         e.AddEPair( "targetname", buffer );
279
280         sprintf( buffer, "0 %i 0", (i * 64) + 32);
281         e.AddEPair( "origin", buffer );
282
283         e.BuildInRadiant( false );
284     }*/
285 }