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