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
21 #include "DTreePlanter.h"
22 #include "funchandlers.h"
24 bool DTreePlanter::OnMouseMove( guint32 nFlags, gdouble x, gdouble y ) {
28 bool DTreePlanter::OnLButtonDown( guint32 nFlags, gdouble x, gdouble y ) {
29 VIEWTYPE vt = m_XYWrapper->GetViewType();
42 m_XYWrapper->SnapToGrid( static_cast< int >( x ), static_cast< int >( y ), pt );
44 if ( FindDropPoint( pt, vhit ) ) {
48 DEntity e( m_entType );
50 sprintf( buffer, "%i %i %i", (int)vhit[0], (int)vhit[1], (int)vhit[2] );
51 e.AddEPair( "origin", buffer );
54 entity_t* pLastEntity = NULL;
55 entity_t* pThisEntity = NULL;
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 );
65 pLastEntity = pThisEntity;
70 sprintf( buffer, m_linkName, 0 );
73 sprintf( buffer, m_linkName, entpos + 1 );
76 e.AddEPair( "targetname", buffer );
80 e2.LoadFromEntity( lastEntNum, TRUE );
81 e2.AddEPair( "target", buffer );
82 e2.RemoveFromRadiant();
83 e2.BuildInRadiant( FALSE );
88 int angleYaw = ( rand() % ( m_maxYaw - m_minYaw + 1 ) ) + m_minYaw;
89 int anglePitch = ( rand() % ( m_maxPitch - m_minPitch + 1 ) ) + m_minPitch;
91 sprintf( buffer, "%i %i 0", anglePitch, angleYaw );
92 e.AddEPair( "angles", buffer );
96 int treetype = rand() % m_numModels;
97 e.AddEPair( "model", m_trees[treetype].name );
101 float scale = ( ( ( rand() % 1000 ) * 0.001f ) * ( m_maxScale - m_minScale ) ) + m_minScale;
103 sprintf( buffer, "%f", scale );
104 e.AddEPair( "modelscale", buffer );
107 e.BuildInRadiant( FALSE );
117 bool DTreePlanter::OnLButtonUp( guint32 nFlags, gdouble x, gdouble y ) {
121 bool DTreePlanter::OnRButtonDown( guint32 nFlags, gdouble x, gdouble y ) {
125 bool DTreePlanter::OnRButtonUp( guint32 nFlags, gdouble x, gdouble y ) {
129 bool DTreePlanter::OnMButtonDown( guint32 nFlags, gdouble x, gdouble y ) {
133 bool DTreePlanter::OnMButtonUp( guint32 nFlags, gdouble x, gdouble y ) {
137 bool DTreePlanter::FindDropPoint( vec3_t in, vec3_t out ) {
141 vec3_t vUp = { 0, 0, 1 };
142 vec3_t vForward = { 0, 1, 0 };
143 vec3_t vLeft = { 1, 0, 0 };
147 VectorCopy( in, p1.points[0] );
148 VectorCopy( in, p1.points[1] );
149 VectorCopy( in, p1.points[2] );
150 VectorMA( p1.points[1], 20, vUp, p1.points[1] );
151 VectorMA( p1.points[1], 20, vLeft, p1.points[2] );
153 VectorCopy( in, p2.points[0] );
154 VectorCopy( in, p2.points[1] );
155 VectorCopy( in, p2.points[2] );
156 VectorMA( p1.points[1], 20, vUp, p2.points[1] );
157 VectorMA( p1.points[1], 20, vForward, p2.points[2] );
165 int cnt = m_world.GetIDMax();
166 for ( int i = 0; i < cnt; i++ ) {
167 DBrush* pBrush = m_world.GetBrushForID( i );
169 if ( pBrush->IntersectsWith( &p1, &p2, temp ) ) {
172 VectorSubtract( in, temp, diff );
173 tempdist = VectorLength( diff );
174 if ( !found || ( tempdist < dist ) ) {
176 VectorCopy( temp, out );
185 void DTreePlanter::DropEntsToGround( void ) {
186 // tell Radiant we want to access the selected brushes
187 g_FuncTable.m_pfnAllocateSelectedBrushHandles();
191 int cnt = g_FuncTable.m_pfnSelectedBrushCount();
192 for ( int i = 0; i < cnt; i++ ) {
193 brush_t *brush = (brush_t*)g_FuncTable.m_pfnGetSelectedBrushHandle( i );
195 ent.LoadFromEntity( brush->owner, TRUE );
197 DEPair* pEpair = ent.FindEPairByKey( "origin" );
203 sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2] );
205 FindDropPoint( vec, out );
208 sprintf( buffer, "%f %f %f", out[0], out[1], out[2] );
209 ent.AddEPair( "origin", buffer );
210 ent.RemoveFromRadiant();
211 ent.BuildInRadiant( FALSE );
214 g_FuncTable.m_pfnReleaseSelectedBrushHandles();
217 void DTreePlanter::MakeChain( void ) {
221 for ( i = 0; i < m_linkNum; i++ ) {
222 DEntity e( "info_train_spline_main" );
224 sprintf( buffer, "%s_pt%i", m_linkName, i );
225 e.AddEPair( "targetname", buffer );
227 sprintf( buffer, "0 %i 0", i * 64 );
228 e.AddEPair( "origin", buffer );
230 if ( i != m_linkNum - 1 ) {
231 sprintf( buffer, "%s_pt%i", m_linkName, i + 1 );
232 e.AddEPair( "target", buffer );
234 sprintf( buffer, "%s_ctl%i", m_linkName, i );
235 e.AddEPair( "control", buffer );
238 e.BuildInRadiant( FALSE );
241 for ( i = 0; i < m_linkNum - 1; i++ ) {
242 DEntity e( "info_train_spline_control" );
244 sprintf( buffer, "%s_ctl%i", m_linkName, i );
245 e.AddEPair( "targetname", buffer );
247 sprintf( buffer, "0 %i 0", ( i * 64 ) + 32 );
248 e.AddEPair( "origin", buffer );
250 e.BuildInRadiant( FALSE );
254 void DTreePlanter::SelectChain( void ) {
257 for(int i = 0; i < m_linkNum; i++) {
258 DEntity e("info_train_spline_main");
260 sprintf( buffer, "%s_pt%i", m_linkName, i );
261 e.AddEPair( "targetname", buffer );
263 sprintf( buffer, "0 %i 0", i * 64 );
264 e.AddEPair( "origin", buffer );
266 if(i != m_linkNum-1) {
267 sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
268 e.AddEPair( "target", buffer );
270 sprintf( buffer, "%s_ctl%i", m_linkName, i );
271 e.AddEPair( "control", buffer );
274 e.BuildInRadiant( FALSE );
277 for(int i = 0; i < m_linkNum-1; i++) {
278 DEntity e("info_train_spline_control");
280 sprintf( buffer, "%s_ctl%i", m_linkName, i );
281 e.AddEPair( "targetname", buffer );
283 sprintf( buffer, "0 %i 0", (i * 64) + 32);
284 e.AddEPair( "origin", buffer );
286 e.BuildInRadiant( FALSE );