more eol-style
[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                         } else {
72                                 sprintf(buffer, m_linkName, entpos + 1);
73                         }
74
75                         e.AddEPair( "targetname", buffer );
76
77                         if(pLastEntity) {
78                                 DEntity e2;
79                                 e2.LoadFromEntity(lastEntNum, TRUE);
80                                 e2.AddEPair("target", buffer);
81                                 e2.RemoveFromRadiant();
82                                 e2.BuildInRadiant(FALSE);
83                         }
84                 }
85
86                 if(m_setAngles) {
87                         int angleYaw = (rand() % (m_maxYaw - m_minYaw + 1)) + m_minYaw;
88                         int anglePitch = (rand() % (m_maxPitch - m_minPitch + 1)) + m_minPitch;
89
90                         sprintf(buffer, "%i %i 0", anglePitch, angleYaw);
91                         e.AddEPair("angles", buffer);
92                 }
93
94                 if(m_numModels) {
95                         int treetype = rand() % m_numModels;
96                         e.AddEPair("model", m_trees[treetype].name);
97                 }
98
99                 if(m_useScale) {
100                         float scale = (((rand()%1000)*0.001f) * (m_maxScale - m_minScale)) + m_minScale;
101
102                         sprintf(buffer, "%f", scale );
103                         e.AddEPair("modelscale", buffer);
104                 }
105
106                 e.BuildInRadiant( FALSE );
107         }
108
109         if(m_autoLink) {
110                 DoTrainPathPlot();
111         }
112
113         return true;
114 }
115
116 bool DTreePlanter::OnLButtonUp(guint32 nFlags, gdouble x, gdouble y) {
117         return false;
118 }
119
120 bool DTreePlanter::OnRButtonDown(guint32 nFlags, gdouble x, gdouble y) {
121         return false;
122 }
123
124 bool DTreePlanter::OnRButtonUp(guint32 nFlags, gdouble x, gdouble y) {
125         return false;
126 }
127
128 bool DTreePlanter::OnMButtonDown(guint32 nFlags, gdouble x, gdouble y) {
129         return false;
130 }
131
132 bool DTreePlanter::OnMButtonUp(guint32 nFlags, gdouble x, gdouble y) {
133         return false;
134 }
135
136 bool DTreePlanter::FindDropPoint(vec3_t in, vec3_t out) {
137         DPlane p1;
138         DPlane p2;
139
140         vec3_t vUp =            { 0, 0, 1 };
141         vec3_t vForward =       { 0, 1, 0 };
142         vec3_t vLeft =          { 1, 0, 0 };
143
144         in[2] = 65535;
145
146         VectorCopy(in, p1.points[0]);
147         VectorCopy(in, p1.points[1]);
148         VectorCopy(in, p1.points[2]);
149         VectorMA(p1.points[1], 20, vUp,                 p1.points[1]);
150         VectorMA(p1.points[1], 20, vLeft,               p1.points[2]);
151
152         VectorCopy(in, p2.points[0]);
153         VectorCopy(in, p2.points[1]);
154         VectorCopy(in, p2.points[2]);
155         VectorMA(p1.points[1], 20, vUp,                 p2.points[1]);
156         VectorMA(p1.points[1], 20, vForward,    p2.points[2]);
157
158         p1.Rebuild();
159         p2.Rebuild();
160
161         bool found = false;
162         vec3_t temp;
163         vec_t dist;
164         int cnt = m_world.GetIDMax();
165         for(int i = 0; i < cnt; i++) {
166                 DBrush* pBrush = m_world.GetBrushForID( i );
167                 
168                 if(pBrush->IntersectsWith( &p1, &p2, temp )) {
169                         vec3_t diff;
170                         vec_t tempdist;
171                         VectorSubtract(in, temp, diff);
172                         tempdist = VectorLength( diff );
173                         if(!found || (tempdist < dist)) {
174                                 dist = tempdist;
175                                 VectorCopy( temp, out );
176                                 found  = true;
177                         }
178                 }
179         }
180
181         return found;
182 }
183
184 void DTreePlanter::DropEntsToGround( void ) {
185         // tell Radiant we want to access the selected brushes
186         g_FuncTable.m_pfnAllocateSelectedBrushHandles();
187                         
188         DEntity ent;
189
190         int cnt = g_FuncTable.m_pfnSelectedBrushCount();
191         for(int i = 0; i < cnt; i++) {
192                 brush_t *brush = (brush_t*)g_FuncTable.m_pfnGetSelectedBrushHandle(i);
193
194                 ent.LoadFromEntity(brush->owner, TRUE);
195
196                 DEPair* pEpair = ent.FindEPairByKey("origin");
197                 if(!pEpair) {
198                         continue;
199                 }
200
201                 vec3_t vec, out;
202                 sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2]);
203
204                 FindDropPoint( vec, out );
205
206                 char buffer[256];
207                 sprintf( buffer, "%f %f %f", out[0], out[1], out[2] );
208                 ent.AddEPair( "origin", buffer );
209                 ent.RemoveFromRadiant();
210                 ent.BuildInRadiant(FALSE);
211         }
212
213         g_FuncTable.m_pfnReleaseSelectedBrushHandles();
214 }
215
216 void DTreePlanter::MakeChain( void ) {
217         char buffer[256];
218         int i;
219
220         for(i = 0; i < m_linkNum; i++) {
221                 DEntity e("info_train_spline_main");
222
223                 sprintf( buffer, "%s_pt%i", m_linkName, i );
224                 e.AddEPair( "targetname", buffer );
225
226                 sprintf( buffer, "0 %i 0", i * 64 );
227                 e.AddEPair( "origin", buffer );
228
229                 if(i != m_linkNum-1) {
230                         sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
231                         e.AddEPair( "target", buffer );
232
233                         sprintf( buffer, "%s_ctl%i", m_linkName, i );
234                         e.AddEPair( "control", buffer );
235                 }
236
237                 e.BuildInRadiant( FALSE );
238         }
239
240         for(i = 0; i < m_linkNum-1; i++) {
241                 DEntity e("info_train_spline_control");
242
243                 sprintf( buffer, "%s_ctl%i", m_linkName, i );
244                 e.AddEPair( "targetname", buffer );
245
246                 sprintf( buffer, "0 %i 0", (i * 64) + 32);
247                 e.AddEPair( "origin", buffer );
248
249                 e.BuildInRadiant( FALSE );
250         }
251 }
252
253 void DTreePlanter::SelectChain( void ) {
254 /*      char buffer[256];
255         
256         for(int i = 0; i < m_linkNum; i++) {
257                 DEntity e("info_train_spline_main");
258
259                 sprintf( buffer, "%s_pt%i", m_linkName, i );
260                 e.AddEPair( "targetname", buffer );
261
262                 sprintf( buffer, "0 %i 0", i * 64 );
263                 e.AddEPair( "origin", buffer );
264
265                 if(i != m_linkNum-1) {
266                         sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
267                         e.AddEPair( "target", buffer );
268
269                         sprintf( buffer, "%s_ctl%i", m_linkName, i );
270                         e.AddEPair( "control", buffer );
271                 }
272
273                 e.BuildInRadiant( FALSE );
274         }
275
276         for(int i = 0; i < m_linkNum-1; i++) {
277                 DEntity e("info_train_spline_control");
278
279                 sprintf( buffer, "%s_ctl%i", m_linkName, i );
280                 e.AddEPair( "targetname", buffer );
281
282                 sprintf( buffer, "0 %i 0", (i * 64) + 32);
283                 e.AddEPair( "origin", buffer );
284
285                 e.BuildInRadiant( FALSE );
286         }*/
287 }