]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/contrib/bobtoolz/DTreePlanter.cpp
Rename the compiled fteqcc to fteqcc-win32 (as that's what it is)
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / 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 {
42   if(button != c_buttonLeft)
43   {
44     return SIGNAL_CONTINUE_EMISSION;
45   }
46         VIEWTYPE vt = GlobalRadiant().XYWindow_getViewType();
47
48         switch(vt) {
49                 case XY:
50                         break;
51                 case YZ:
52                 case XZ:
53                 default:
54                         return SIGNAL_CONTINUE_EMISSION;
55         }
56
57         Vector3 pt, vhit;
58
59   pt = vector3_snapped(GlobalRadiant().XYWindow_windowToWorld(position), GlobalRadiant().getGridSize());
60
61         if(FindDropPoint(vector3_to_array(pt), vector3_to_array(vhit))) {
62                 vhit[2] += m_offset;
63
64                 char buffer[128];
65                 DEntity e(m_entType);
66
67                 sprintf(buffer, "%i %i %i", (int)vhit[0], (int)vhit[1], (int)vhit[2]);
68                 e.AddEPair("origin", buffer);
69
70                 if(m_autoLink) {
71
72       const scene::Path* pLastEntity = NULL;
73                         const scene::Path* pThisEntity = NULL;
74
75                         int entpos;
76                         for(int i = 0; i < 256; i++) {
77                                 sprintf(buffer, m_linkName, i);
78         pThisEntity = FindEntityFromTargetname( buffer );
79
80                                 if(pThisEntity) {
81                                         entpos = i;
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(pLastEntity->top(), true);
97                                 e2.AddEPair("target", buffer);
98                                 e2.RemoveFromRadiant();
99                                 e2.BuildInRadiant(false);
100                         }
101
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 class TreePlanterDropEntityIfSelected
183 {
184   mutable DEntity ent;
185   DTreePlanter& planter;
186 public:
187   TreePlanterDropEntityIfSelected(DTreePlanter& planter) : planter(planter)
188   {
189   }
190   void operator()(scene::Instance& instance) const
191   {
192     if(!instance.isSelected())
193     {
194       return;
195     }
196                 ent.LoadFromEntity(instance.path().top());
197
198                 DEPair* pEpair = ent.FindEPairByKey("origin");
199                 if(!pEpair) {
200                         return;
201                 }
202
203                 vec3_t vec, out;
204                 sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2]);
205
206                 planter.FindDropPoint( vec, out );
207
208                 char buffer[256];
209                 sprintf( buffer, "%f %f %f", out[0], out[1], out[2] );
210                 ent.AddEPair( "origin", buffer );
211                 ent.RemoveFromRadiant();
212                 ent.BuildInRadiant(false);
213   }
214 };
215
216 void DTreePlanter::DropEntsToGround( void ) {
217   Scene_forEachEntity(TreePlanterDropEntityIfSelected(*this));
218 }
219
220 void DTreePlanter::MakeChain( int linkNum, const char* linkName ) {
221         char buffer[256];
222         int i;
223         for(i = 0; i < linkNum; i++) {
224                 DEntity e("info_train_spline_main");
225
226                 sprintf( buffer, "%s_pt%i", linkName, i );
227                 e.AddEPair( "targetname", buffer );
228
229                 sprintf( buffer, "0 %i 0", i * 64 );
230                 e.AddEPair( "origin", buffer );
231
232                 if(i != m_linkNum-1) {
233                         sprintf( buffer, "%s_pt%i", linkName, i+1 );
234                         e.AddEPair( "target", buffer );
235
236                         sprintf( buffer, "%s_ctl%i", linkName, i );
237                         e.AddEPair( "control", buffer );
238                 }
239                 e.BuildInRadiant( false );
240         }
241
242         for(i = 0; i < linkNum-1; i++) {
243                 DEntity e("info_train_spline_control");
244
245                 sprintf( buffer, "%s_ctl%i", linkName, i );
246                 e.AddEPair( "targetname", buffer );
247
248                 sprintf( buffer, "0 %i 0", (i * 64) + 32);
249                 e.AddEPair( "origin", buffer );
250
251                 e.BuildInRadiant( false );
252         }
253 }
254
255 void DTreePlanter::SelectChain( void ) {
256 /*      char buffer[256];
257         
258         for(int i = 0; i < m_linkNum; i++) {
259                 DEntity e("info_train_spline_main");
260
261                 sprintf( buffer, "%s_pt%i", m_linkName, i );
262                 e.AddEPair( "targetname", buffer );
263
264                 sprintf( buffer, "0 %i 0", i * 64 );
265                 e.AddEPair( "origin", buffer );
266
267                 if(i != m_linkNum-1) {
268                         sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
269                         e.AddEPair( "target", buffer );
270
271                         sprintf( buffer, "%s_ctl%i", m_linkName, i );
272                         e.AddEPair( "control", buffer );
273                 }
274
275                 e.BuildInRadiant( false );
276         }
277
278         for(int i = 0; i < m_linkNum-1; i++) {
279                 DEntity e("info_train_spline_control");
280
281                 sprintf( buffer, "%s_ctl%i", m_linkName, i );
282                 e.AddEPair( "targetname", buffer );
283
284                 sprintf( buffer, "0 %i 0", (i * 64) + 32);
285                 e.AddEPair( "origin", buffer );
286
287                 e.BuildInRadiant( false );
288         }*/
289 }