]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DTreePlanter.cpp
54850ceb4f76966106e2041a64da841f9b2d2cda
[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
35
36
37 #include "funchandlers.h"
38
39 SignalHandlerResult DTreePlanter::mouseDown(const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers)
40 {
41   if(button != c_buttonLeft)
42   {
43     return SIGNAL_CONTINUE_EMISSION;
44   }
45         VIEWTYPE vt = GlobalRadiant().XYWindow_getViewType();
46
47         switch(vt) {
48                 case XY:
49                         break;
50                 case YZ:
51                 case XZ:
52                 default:
53                         return SIGNAL_CONTINUE_EMISSION;
54         }
55
56         Vector3 pt, vhit;
57
58   pt = vector3_snapped(GlobalRadiant().XYWindow_windowToWorld(position));
59
60         if(FindDropPoint(vector3_to_array(pt), vector3_to_array(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 SIGNAL_STOP_EMISSION;
132 }
133
134 bool DTreePlanter::FindDropPoint(vec3_t in, vec3_t out) {
135         DPlane p1;
136         DPlane p2;
137
138         vec3_t vUp =            { 0, 0, 1 };
139         vec3_t vForward =       { 0, 1, 0 };
140         vec3_t vLeft =          { 1, 0, 0 };
141
142         in[2] = 65535;
143
144         VectorCopy(in, p1.points[0]);
145         VectorCopy(in, p1.points[1]);
146         VectorCopy(in, p1.points[2]);
147         VectorMA(p1.points[1], 20, vUp,                 p1.points[1]);
148         VectorMA(p1.points[1], 20, vLeft,               p1.points[2]);
149
150         VectorCopy(in, p2.points[0]);
151         VectorCopy(in, p2.points[1]);
152         VectorCopy(in, p2.points[2]);
153         VectorMA(p1.points[1], 20, vUp,                 p2.points[1]);
154         VectorMA(p1.points[1], 20, vForward,    p2.points[2]);
155
156         p1.Rebuild();
157         p2.Rebuild();
158
159         bool found = false;
160         vec3_t temp;
161         vec_t dist;
162         int cnt = m_world.GetIDMax();
163         for(int i = 0; i < cnt; i++) {
164                 DBrush* pBrush = m_world.GetBrushForID( i );
165                 
166                 if(pBrush->IntersectsWith( &p1, &p2, temp )) {
167                         vec3_t diff;
168                         vec_t tempdist;
169                         VectorSubtract(in, temp, diff);
170                         tempdist = VectorLength( diff );
171                         if(!found || (tempdist < dist)) {
172                                 dist = tempdist;
173                                 VectorCopy( temp, out );
174                                 found  = true;
175                         }
176                 }
177         }
178
179         return found;
180 }
181
182 void DTreePlanter::DropEntsToGround( void ) {
183 #if 0
184         // tell Radiant we want to access the selected brushes
185         g_FuncTable.m_pfnAllocateSelectedBrushHandles();
186                         
187         DEntity ent;
188
189         int cnt = g_FuncTable.m_pfnSelectedBrushCount();
190         for(int i = 0; i < cnt; i++) {
191                 brush_t *brush = (brush_t*)g_FuncTable.m_pfnGetSelectedBrushHandle(i);
192
193                 ent.LoadFromEntity(brush->owner, TRUE);
194
195                 DEPair* pEpair = ent.FindEPairByKey("origin");
196                 if(!pEpair) {
197                         continue;
198                 }
199
200                 vec3_t vec, out;
201                 sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2]);
202
203                 FindDropPoint( vec, out );
204
205                 char buffer[256];
206                 sprintf( buffer, "%f %f %f", out[0], out[1], out[2] );
207                 ent.AddEPair( "origin", buffer );
208                 ent.RemoveFromRadiant();
209                 ent.BuildInRadiant(false);
210         }
211
212         g_FuncTable.m_pfnReleaseSelectedBrushHandles();
213 #endif
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 }