]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DTreePlanter.cpp
uncrustify! now the code is only ugly on the *inside*
[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 "StdAfx.h"
21 #include "DTreePlanter.h"
22 #include "funchandlers.h"
23
24 bool DTreePlanter::OnMouseMove( guint32 nFlags, gdouble x, gdouble y ) {
25         return false;
26 }
27
28 bool DTreePlanter::OnLButtonDown( guint32 nFlags, gdouble x, gdouble y ) {
29         VIEWTYPE vt = m_XYWrapper->GetViewType();
30
31         switch ( vt ) {
32         case XY:
33                 break;
34         case YZ:
35         case XZ:
36         default:
37                 return false;
38         }
39
40         vec3_t pt, vhit;
41
42         m_XYWrapper->SnapToGrid( static_cast< int >( x ), static_cast< int >( y ), pt );
43
44         if ( FindDropPoint( pt, vhit ) ) {
45                 vhit[2] += m_offset;
46
47                 char buffer[128];
48                 DEntity e( m_entType );
49
50                 sprintf( buffer, "%i %i %i", (int)vhit[0], (int)vhit[1], (int)vhit[2] );
51                 e.AddEPair( "origin", buffer );
52
53                 if ( m_autoLink ) {
54                         entity_t* pLastEntity = NULL;
55                         entity_t* pThisEntity = NULL;
56
57                         int entNum = -1, lastEntNum = -1, entpos;
58                         for ( int i = 0; i < 256; i++ ) {
59                                 sprintf( buffer, m_linkName, i );
60                                 pThisEntity = FindEntityFromTargetname( buffer, &entNum );
61
62                                 if ( pThisEntity ) {
63                                         entpos = i;
64                                         lastEntNum = entNum;
65                                         pLastEntity = pThisEntity;
66                                 }
67                         }
68
69                         if ( !pLastEntity ) {
70                                 sprintf( buffer, m_linkName, 0 );
71                         }
72                         else {
73                                 sprintf( buffer, m_linkName, entpos + 1 );
74                         }
75
76                         e.AddEPair( "targetname", buffer );
77
78                         if ( pLastEntity ) {
79                                 DEntity e2;
80                                 e2.LoadFromEntity( lastEntNum, TRUE );
81                                 e2.AddEPair( "target", buffer );
82                                 e2.RemoveFromRadiant();
83                                 e2.BuildInRadiant( FALSE );
84                         }
85                 }
86
87                 if ( m_setAngles ) {
88                         int angleYaw = ( rand() % ( m_maxYaw - m_minYaw + 1 ) ) + m_minYaw;
89                         int anglePitch = ( rand() % ( m_maxPitch - m_minPitch + 1 ) ) + m_minPitch;
90
91                         sprintf( buffer, "%i %i 0", anglePitch, angleYaw );
92                         e.AddEPair( "angles", buffer );
93                 }
94
95                 if ( m_numModels ) {
96                         int treetype = rand() % m_numModels;
97                         e.AddEPair( "model", m_trees[treetype].name );
98                 }
99
100                 if ( m_useScale ) {
101                         float scale = ( ( ( rand() % 1000 ) * 0.001f ) * ( m_maxScale - m_minScale ) ) + m_minScale;
102
103                         sprintf( buffer, "%f", scale );
104                         e.AddEPair( "modelscale", buffer );
105                 }
106
107                 e.BuildInRadiant( FALSE );
108         }
109
110         if ( m_autoLink ) {
111                 DoTrainPathPlot();
112         }
113
114         return true;
115 }
116
117 bool DTreePlanter::OnLButtonUp( guint32 nFlags, gdouble x, gdouble y ) {
118         return false;
119 }
120
121 bool DTreePlanter::OnRButtonDown( guint32 nFlags, gdouble x, gdouble y ) {
122         return false;
123 }
124
125 bool DTreePlanter::OnRButtonUp( guint32 nFlags, gdouble x, gdouble y ) {
126         return false;
127 }
128
129 bool DTreePlanter::OnMButtonDown( guint32 nFlags, gdouble x, gdouble y ) {
130         return false;
131 }
132
133 bool DTreePlanter::OnMButtonUp( guint32 nFlags, gdouble x, gdouble y ) {
134         return false;
135 }
136
137 bool DTreePlanter::FindDropPoint( vec3_t in, vec3_t out ) {
138         DPlane p1;
139         DPlane p2;
140
141         vec3_t vUp =        { 0, 0, 1 };
142         vec3_t vForward =   { 0, 1, 0 };
143         vec3_t vLeft =      { 1, 0, 0 };
144
145         in[2] = 65535;
146
147         VectorCopy( in, p1.points[0] );
148         VectorCopy( in, p1.points[1] );
149         VectorCopy( in, p1.points[2] );
150         VectorMA( p1.points[1], 20, vUp,         p1.points[1] );
151         VectorMA( p1.points[1], 20, vLeft,       p1.points[2] );
152
153         VectorCopy( in, p2.points[0] );
154         VectorCopy( in, p2.points[1] );
155         VectorCopy( in, p2.points[2] );
156         VectorMA( p1.points[1], 20, vUp,         p2.points[1] );
157         VectorMA( p1.points[1], 20, vForward,    p2.points[2] );
158
159         p1.Rebuild();
160         p2.Rebuild();
161
162         bool found = false;
163         vec3_t temp;
164         vec_t dist;
165         int cnt = m_world.GetIDMax();
166         for ( int i = 0; i < cnt; i++ ) {
167                 DBrush* pBrush = m_world.GetBrushForID( i );
168
169                 if ( pBrush->IntersectsWith( &p1, &p2, temp ) ) {
170                         vec3_t diff;
171                         vec_t tempdist;
172                         VectorSubtract( in, temp, diff );
173                         tempdist = VectorLength( diff );
174                         if ( !found || ( tempdist < dist ) ) {
175                                 dist = tempdist;
176                                 VectorCopy( temp, out );
177                                 found  = true;
178                         }
179                 }
180         }
181
182         return found;
183 }
184
185 void DTreePlanter::DropEntsToGround( void ) {
186         // tell Radiant we want to access the selected brushes
187         g_FuncTable.m_pfnAllocateSelectedBrushHandles();
188
189         DEntity ent;
190
191         int cnt = g_FuncTable.m_pfnSelectedBrushCount();
192         for ( int i = 0; i < cnt; i++ ) {
193                 brush_t *brush = (brush_t*)g_FuncTable.m_pfnGetSelectedBrushHandle( i );
194
195                 ent.LoadFromEntity( brush->owner, TRUE );
196
197                 DEPair* pEpair = ent.FindEPairByKey( "origin" );
198                 if ( !pEpair ) {
199                         continue;
200                 }
201
202                 vec3_t vec, out;
203                 sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2] );
204
205                 FindDropPoint( vec, out );
206
207                 char buffer[256];
208                 sprintf( buffer, "%f %f %f", out[0], out[1], out[2] );
209                 ent.AddEPair( "origin", buffer );
210                 ent.RemoveFromRadiant();
211                 ent.BuildInRadiant( FALSE );
212         }
213
214         g_FuncTable.m_pfnReleaseSelectedBrushHandles();
215 }
216
217 void DTreePlanter::MakeChain( void ) {
218         char buffer[256];
219         int i;
220
221         for ( i = 0; i < m_linkNum; i++ ) {
222                 DEntity e( "info_train_spline_main" );
223
224                 sprintf( buffer, "%s_pt%i", m_linkName, i );
225                 e.AddEPair( "targetname", buffer );
226
227                 sprintf( buffer, "0 %i 0", i * 64 );
228                 e.AddEPair( "origin", buffer );
229
230                 if ( i != m_linkNum - 1 ) {
231                         sprintf( buffer, "%s_pt%i", m_linkName, i + 1 );
232                         e.AddEPair( "target", buffer );
233
234                         sprintf( buffer, "%s_ctl%i", m_linkName, i );
235                         e.AddEPair( "control", buffer );
236                 }
237
238                 e.BuildInRadiant( FALSE );
239         }
240
241         for ( i = 0; i < m_linkNum - 1; i++ ) {
242                 DEntity e( "info_train_spline_control" );
243
244                 sprintf( buffer, "%s_ctl%i", m_linkName, i );
245                 e.AddEPair( "targetname", buffer );
246
247                 sprintf( buffer, "0 %i 0", ( i * 64 ) + 32 );
248                 e.AddEPair( "origin", buffer );
249
250                 e.BuildInRadiant( FALSE );
251         }
252 }
253
254 void DTreePlanter::SelectChain( void ) {
255 /*      char buffer[256];
256
257     for(int i = 0; i < m_linkNum; i++) {
258         DEntity e("info_train_spline_main");
259
260         sprintf( buffer, "%s_pt%i", m_linkName, i );
261         e.AddEPair( "targetname", buffer );
262
263         sprintf( buffer, "0 %i 0", i * 64 );
264         e.AddEPair( "origin", buffer );
265
266         if(i != m_linkNum-1) {
267             sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
268             e.AddEPair( "target", buffer );
269
270             sprintf( buffer, "%s_ctl%i", m_linkName, i );
271             e.AddEPair( "control", buffer );
272         }
273
274         e.BuildInRadiant( FALSE );
275     }
276
277     for(int i = 0; i < m_linkNum-1; i++) {
278         DEntity e("info_train_spline_control");
279
280         sprintf( buffer, "%s_ctl%i", m_linkName, i );
281         e.AddEPair( "targetname", buffer );
282
283         sprintf( buffer, "0 %i 0", (i * 64) + 32);
284         e.AddEPair( "origin", buffer );
285
286         e.BuildInRadiant( FALSE );
287     }*/
288 }