2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
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.
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.
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
20 #include "DTreePlanter.h"
32 #include "ScriptParser.h"
38 #include "funchandlers.h"
40 SignalHandlerResult DTreePlanter::mouseDown(const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers)
42 if(button != c_buttonLeft)
44 return SIGNAL_CONTINUE_EMISSION;
46 VIEWTYPE vt = GlobalRadiant().XYWindow_getViewType();
54 return SIGNAL_CONTINUE_EMISSION;
59 pt = vector3_snapped(GlobalRadiant().XYWindow_windowToWorld(position), GlobalRadiant().getGridSize());
61 if(FindDropPoint(vector3_to_array(pt), vector3_to_array(vhit))) {
67 sprintf(buffer, "%i %i %i", (int)vhit[0], (int)vhit[1], (int)vhit[2]);
68 e.AddEPair("origin", buffer);
72 const scene::Path* pLastEntity = NULL;
73 const scene::Path* pThisEntity = NULL;
76 for(int i = 0; i < 256; i++) {
77 sprintf(buffer, m_linkName, i);
78 pThisEntity = FindEntityFromTargetname( buffer );
82 pLastEntity = pThisEntity;
87 sprintf(buffer, m_linkName, 0);
89 sprintf(buffer, m_linkName, entpos + 1);
92 e.AddEPair( "targetname", buffer );
96 e2.LoadFromEntity(pLastEntity->top(), true);
97 e2.AddEPair("target", buffer);
98 e2.RemoveFromRadiant();
99 e2.BuildInRadiant(false);
105 int angleYaw = (rand() % (m_maxYaw - m_minYaw + 1)) + m_minYaw;
106 int anglePitch = (rand() % (m_maxPitch - m_minPitch + 1)) + m_minPitch;
108 sprintf(buffer, "%i %i 0", anglePitch, angleYaw);
109 e.AddEPair("angles", buffer);
113 int treetype = rand() % m_numModels;
114 e.AddEPair("model", m_trees[treetype].name);
118 float scale = (((rand()%1000)*0.001f) * (m_maxScale - m_minScale)) + m_minScale;
120 sprintf(buffer, "%f", scale );
121 e.AddEPair("modelscale", buffer);
124 e.BuildInRadiant( false );
131 return SIGNAL_STOP_EMISSION;
134 bool DTreePlanter::FindDropPoint(vec3_t in, vec3_t out) {
138 vec3_t vUp = { 0, 0, 1 };
139 vec3_t vForward = { 0, 1, 0 };
140 vec3_t vLeft = { 1, 0, 0 };
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]);
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]);
162 int cnt = m_world.GetIDMax();
163 for(int i = 0; i < cnt; i++) {
164 DBrush* pBrush = m_world.GetBrushForID( i );
166 if(pBrush->IntersectsWith( &p1, &p2, temp )) {
169 VectorSubtract(in, temp, diff);
170 tempdist = VectorLength( diff );
171 if(!found || (tempdist < dist)) {
173 VectorCopy( temp, out );
182 class TreePlanterDropEntityIfSelected
185 DTreePlanter& planter;
187 TreePlanterDropEntityIfSelected(DTreePlanter& planter) : planter(planter)
190 void operator()(scene::Instance& instance) const
192 if(!instance.isSelected())
196 ent.LoadFromEntity(instance.path().top());
198 DEPair* pEpair = ent.FindEPairByKey("origin");
204 sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2]);
206 planter.FindDropPoint( vec, out );
209 sprintf( buffer, "%f %f %f", out[0], out[1], out[2] );
210 ent.AddEPair( "origin", buffer );
211 ent.RemoveFromRadiant();
212 ent.BuildInRadiant(false);
216 void DTreePlanter::DropEntsToGround( void ) {
217 Scene_forEachEntity(TreePlanterDropEntityIfSelected(*this));
220 void DTreePlanter::MakeChain( void ) {
224 for(i = 0; i < m_linkNum; i++) {
225 DEntity e("info_train_spline_main");
227 sprintf( buffer, "%s_pt%i", m_linkName, i );
228 e.AddEPair( "targetname", buffer );
230 sprintf( buffer, "0 %i 0", i * 64 );
231 e.AddEPair( "origin", buffer );
233 if(i != m_linkNum-1) {
234 sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
235 e.AddEPair( "target", buffer );
237 sprintf( buffer, "%s_ctl%i", m_linkName, i );
238 e.AddEPair( "control", buffer );
241 e.BuildInRadiant( false );
244 for(i = 0; i < m_linkNum-1; i++) {
245 DEntity e("info_train_spline_control");
247 sprintf( buffer, "%s_ctl%i", m_linkName, i );
248 e.AddEPair( "targetname", buffer );
250 sprintf( buffer, "0 %i 0", (i * 64) + 32);
251 e.AddEPair( "origin", buffer );
253 e.BuildInRadiant( false );
257 void DTreePlanter::SelectChain( void ) {
260 for(int i = 0; i < m_linkNum; i++) {
261 DEntity e("info_train_spline_main");
263 sprintf( buffer, "%s_pt%i", m_linkName, i );
264 e.AddEPair( "targetname", buffer );
266 sprintf( buffer, "0 %i 0", i * 64 );
267 e.AddEPair( "origin", buffer );
269 if(i != m_linkNum-1) {
270 sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
271 e.AddEPair( "target", buffer );
273 sprintf( buffer, "%s_ctl%i", m_linkName, i );
274 e.AddEPair( "control", buffer );
277 e.BuildInRadiant( false );
280 for(int i = 0; i < m_linkNum-1; i++) {
281 DEntity e("info_train_spline_control");
283 sprintf( buffer, "%s_ctl%i", m_linkName, i );
284 e.AddEPair( "targetname", buffer );
286 sprintf( buffer, "0 %i 0", (i * 64) + 32);
287 e.AddEPair( "origin", buffer );
289 e.BuildInRadiant( false );