X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fnetradiant.git;a=blobdiff_plain;f=contrib%2Fbobtoolz%2FDTreePlanter.cpp;h=9207f1ce2472a786a7c9b15e7d26c98849bdb8db;hp=985987dfa667ad25469063294883bdafb726f5a1;hb=02a51890a3d97a0e937fbb11071cf7c41cc00aa9;hpb=5265d3cc1517566910718738ee6fa48e2466d3ea diff --git a/contrib/bobtoolz/DTreePlanter.cpp b/contrib/bobtoolz/DTreePlanter.cpp index 985987df..9207f1ce 100644 --- a/contrib/bobtoolz/DTreePlanter.cpp +++ b/contrib/bobtoolz/DTreePlanter.cpp @@ -1,287 +1,291 @@ /* -BobToolz plugin for GtkRadiant -Copyright (C) 2001 Gordon Biggans + BobToolz plugin for GtkRadiant + Copyright (C) 2001 Gordon Biggans -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ -#include "StdAfx.h" #include "DTreePlanter.h" -#include "funchandlers.h" -bool DTreePlanter::OnMouseMove(guint32 nFlags, gdouble x, gdouble y) { - return false; -} +#include +#include "str.h" -bool DTreePlanter::OnLButtonDown(guint32 nFlags, gdouble x, gdouble y) { - VIEWTYPE vt = m_XYWrapper->GetViewType(); +#include "DPoint.h" +#include "DPlane.h" +#include "DBrush.h" +#include "DEPair.h" +#include "DPatch.h" +#include "DEntity.h" - switch(vt) { - case XY: - break; - case YZ: - case XZ: - default: - return false; - } +#include "ScriptParser.h" +#include "misc.h" +#include "scenelib.h" - vec3_t pt, vhit; - m_XYWrapper->SnapToGrid( static_cast< int >( x ), static_cast< int >( y ), pt ); +#include "funchandlers.h" - if(FindDropPoint(pt, vhit)) { - vhit[2] += m_offset; +SignalHandlerResult +DTreePlanter::mouseDown(const WindowVector &position, ButtonIdentifier button, ModifierFlags modifiers) +{ + if (button != c_buttonLeft) { + return SIGNAL_CONTINUE_EMISSION; + } + VIEWTYPE vt = GlobalRadiant().XYWindow_getViewType(); - char buffer[128]; - DEntity e(m_entType); + switch (vt) { + case XY: + break; + case YZ: + case XZ: + default: + return SIGNAL_CONTINUE_EMISSION; + } - sprintf(buffer, "%i %i %i", (int)vhit[0], (int)vhit[1], (int)vhit[2]); - e.AddEPair("origin", buffer); + Vector3 pt, vhit; - if(m_autoLink) { - entity_t* pLastEntity = NULL; - entity_t* pThisEntity = NULL; + pt = vector3_snapped(GlobalRadiant().XYWindow_windowToWorld(position), GlobalRadiant().getGridSize()); - int entNum = -1, lastEntNum = -1, entpos; - for(int i = 0; i < 256; i++) { - sprintf(buffer, m_linkName, i); - pThisEntity = FindEntityFromTargetname( buffer, &entNum ); + if (FindDropPoint(vector3_to_array(pt), vector3_to_array(vhit))) { + vhit[2] += m_offset; - if(pThisEntity) { - entpos = i; - lastEntNum = entNum; - pLastEntity = pThisEntity; - } - } + char buffer[128]; + DEntity e(m_entType); - if(!pLastEntity) { - sprintf(buffer, m_linkName, 0); - } else { - sprintf(buffer, m_linkName, entpos + 1); - } + sprintf(buffer, "%i %i %i", (int) vhit[0], (int) vhit[1], (int) vhit[2]); + e.AddEPair("origin", buffer); - e.AddEPair( "targetname", buffer ); + if (m_autoLink) { - if(pLastEntity) { - DEntity e2; - e2.LoadFromEntity(lastEntNum, TRUE); - e2.AddEPair("target", buffer); - e2.RemoveFromRadiant(); - e2.BuildInRadiant(FALSE); - } - } + const scene::Path *pLastEntity = NULL; + const scene::Path *pThisEntity = NULL; - if(m_setAngles) { - int angleYaw = (rand() % (m_maxYaw - m_minYaw + 1)) + m_minYaw; - int anglePitch = (rand() % (m_maxPitch - m_minPitch + 1)) + m_minPitch; + int entpos; + for (int i = 0; i < 256; i++) { + sprintf(buffer, m_linkName, i); + pThisEntity = FindEntityFromTargetname(buffer); - sprintf(buffer, "%i %i 0", anglePitch, angleYaw); - e.AddEPair("angles", buffer); - } + if (pThisEntity) { + entpos = i; + pLastEntity = pThisEntity; + } + } - if(m_numModels) { - int treetype = rand() % m_numModels; - e.AddEPair("model", m_trees[treetype].name); - } + if (!pLastEntity) { + sprintf(buffer, m_linkName, 0); + } else { + sprintf(buffer, m_linkName, entpos + 1); + } - if(m_useScale) { - float scale = (((rand()%1000)*0.001f) * (m_maxScale - m_minScale)) + m_minScale; + e.AddEPair("targetname", buffer); - sprintf(buffer, "%f", scale ); - e.AddEPair("modelscale", buffer); - } + if (pLastEntity) { + DEntity e2; + e2.LoadFromEntity(pLastEntity->top(), true); + e2.AddEPair("target", buffer); + e2.RemoveFromRadiant(); + e2.BuildInRadiant(false); + } - e.BuildInRadiant( FALSE ); - } + } - if(m_autoLink) { - DoTrainPathPlot(); - } + if (m_setAngles) { + int angleYaw = (rand() % (m_maxYaw - m_minYaw + 1)) + m_minYaw; + int anglePitch = (rand() % (m_maxPitch - m_minPitch + 1)) + m_minPitch; - return true; -} + sprintf(buffer, "%i %i 0", anglePitch, angleYaw); + e.AddEPair("angles", buffer); + } -bool DTreePlanter::OnLButtonUp(guint32 nFlags, gdouble x, gdouble y) { - return false; -} + if (m_numModels) { + int treetype = rand() % m_numModels; + e.AddEPair("model", m_trees[treetype].name); + } -bool DTreePlanter::OnRButtonDown(guint32 nFlags, gdouble x, gdouble y) { - return false; -} + if (m_useScale) { + float scale = (((rand() % 1000) * 0.001f) * (m_maxScale - m_minScale)) + m_minScale; -bool DTreePlanter::OnRButtonUp(guint32 nFlags, gdouble x, gdouble y) { - return false; -} + sprintf(buffer, "%f", scale); + e.AddEPair("modelscale", buffer); + } -bool DTreePlanter::OnMButtonDown(guint32 nFlags, gdouble x, gdouble y) { - return false; -} + e.BuildInRadiant(false); + } -bool DTreePlanter::OnMButtonUp(guint32 nFlags, gdouble x, gdouble y) { - return false; -} + if (m_autoLink) { + DoTrainPathPlot(); + } -bool DTreePlanter::FindDropPoint(vec3_t in, vec3_t out) { - DPlane p1; - DPlane p2; - - vec3_t vUp = { 0, 0, 1 }; - vec3_t vForward = { 0, 1, 0 }; - vec3_t vLeft = { 1, 0, 0 }; - - in[2] = 65535; - - VectorCopy(in, p1.points[0]); - VectorCopy(in, p1.points[1]); - VectorCopy(in, p1.points[2]); - VectorMA(p1.points[1], 20, vUp, p1.points[1]); - VectorMA(p1.points[1], 20, vLeft, p1.points[2]); - - VectorCopy(in, p2.points[0]); - VectorCopy(in, p2.points[1]); - VectorCopy(in, p2.points[2]); - VectorMA(p1.points[1], 20, vUp, p2.points[1]); - VectorMA(p1.points[1], 20, vForward, p2.points[2]); - - p1.Rebuild(); - p2.Rebuild(); - - bool found = false; - vec3_t temp; - vec_t dist; - int cnt = m_world.GetIDMax(); - for(int i = 0; i < cnt; i++) { - DBrush* pBrush = m_world.GetBrushForID( i ); - - if(pBrush->IntersectsWith( &p1, &p2, temp )) { - vec3_t diff; - vec_t tempdist; - VectorSubtract(in, temp, diff); - tempdist = VectorLength( diff ); - if(!found || (tempdist < dist)) { - dist = tempdist; - VectorCopy( temp, out ); - found = true; - } - } - } - - return found; + return SIGNAL_STOP_EMISSION; } -void DTreePlanter::DropEntsToGround( void ) { - // tell Radiant we want to access the selected brushes - g_FuncTable.m_pfnAllocateSelectedBrushHandles(); - - DEntity ent; - - int cnt = g_FuncTable.m_pfnSelectedBrushCount(); - for(int i = 0; i < cnt; i++) { - brush_t *brush = (brush_t*)g_FuncTable.m_pfnGetSelectedBrushHandle(i); - - ent.LoadFromEntity(brush->owner, TRUE); - - DEPair* pEpair = ent.FindEPairByKey("origin"); - if(!pEpair) { - continue; - } - - vec3_t vec, out; - sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2]); - - FindDropPoint( vec, out ); - - char buffer[256]; - sprintf( buffer, "%f %f %f", out[0], out[1], out[2] ); - ent.AddEPair( "origin", buffer ); - ent.RemoveFromRadiant(); - ent.BuildInRadiant(FALSE); - } - - g_FuncTable.m_pfnReleaseSelectedBrushHandles(); +bool DTreePlanter::FindDropPoint(vec3_t in, vec3_t out) +{ + DPlane p1; + DPlane p2; + + vec3_t vUp = {0, 0, 1}; + vec3_t vForward = {0, 1, 0}; + vec3_t vLeft = {1, 0, 0}; + + in[2] = 65535; + + VectorCopy(in, p1.points[0]); + VectorCopy(in, p1.points[1]); + VectorCopy(in, p1.points[2]); + VectorMA(p1.points[1], 20, vUp, p1.points[1]); + VectorMA(p1.points[1], 20, vLeft, p1.points[2]); + + VectorCopy(in, p2.points[0]); + VectorCopy(in, p2.points[1]); + VectorCopy(in, p2.points[2]); + VectorMA(p1.points[1], 20, vUp, p2.points[1]); + VectorMA(p1.points[1], 20, vForward, p2.points[2]); + + p1.Rebuild(); + p2.Rebuild(); + + bool found = false; + vec3_t temp; + vec_t dist; + int cnt = m_world.GetIDMax(); + for (int i = 0; i < cnt; i++) { + DBrush *pBrush = m_world.GetBrushForID(i); + + if (pBrush->IntersectsWith(&p1, &p2, temp)) { + vec3_t diff; + vec_t tempdist; + VectorSubtract(in, temp, diff); + tempdist = VectorLength(diff); + if (!found || (tempdist < dist)) { + dist = tempdist; + VectorCopy(temp, out); + found = true; + } + } + } + + return found; } -void DTreePlanter::MakeChain( void ) { - char buffer[256]; - int i; - - for(i = 0; i < m_linkNum; i++) { - DEntity e("info_train_spline_main"); +class TreePlanterDropEntityIfSelected { + mutable DEntity ent; + DTreePlanter &planter; +public: + TreePlanterDropEntityIfSelected(DTreePlanter &planter) : planter(planter) + { + } + + void operator()(scene::Instance &instance) const + { + if (!instance.isSelected()) { + return; + } + ent.LoadFromEntity(instance.path().top()); + + DEPair *pEpair = ent.FindEPairByKey("origin"); + if (!pEpair) { + return; + } + + vec3_t vec, out; + sscanf(pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2]); + + planter.FindDropPoint(vec, out); + + char buffer[256]; + sprintf(buffer, "%f %f %f", out[0], out[1], out[2]); + ent.AddEPair("origin", buffer); + ent.RemoveFromRadiant(); + ent.BuildInRadiant(false); + } +}; + +void DTreePlanter::DropEntsToGround(void) +{ + Scene_forEachEntity(TreePlanterDropEntityIfSelected(*this)); +} - sprintf( buffer, "%s_pt%i", m_linkName, i ); - e.AddEPair( "targetname", buffer ); +void DTreePlanter::MakeChain(int linkNum, const char *linkName) +{ + char buffer[256]; + int i; + for (i = 0; i < linkNum; i++) { + DEntity e("info_train_spline_main"); - sprintf( buffer, "0 %i 0", i * 64 ); - e.AddEPair( "origin", buffer ); + sprintf(buffer, "%s_pt%i", linkName, i); + e.AddEPair("targetname", buffer); - if(i != m_linkNum-1) { - sprintf( buffer, "%s_pt%i", m_linkName, i+1 ); - e.AddEPair( "target", buffer ); + sprintf(buffer, "0 %i 0", i * 64); + e.AddEPair("origin", buffer); - sprintf( buffer, "%s_ctl%i", m_linkName, i ); - e.AddEPair( "control", buffer ); - } + if (i != m_linkNum - 1) { + sprintf(buffer, "%s_pt%i", linkName, i + 1); + e.AddEPair("target", buffer); - e.BuildInRadiant( FALSE ); - } + sprintf(buffer, "%s_ctl%i", linkName, i); + e.AddEPair("control", buffer); + } + e.BuildInRadiant(false); + } - for(i = 0; i < m_linkNum-1; i++) { - DEntity e("info_train_spline_control"); + for (i = 0; i < linkNum - 1; i++) { + DEntity e("info_train_spline_control"); - sprintf( buffer, "%s_ctl%i", m_linkName, i ); - e.AddEPair( "targetname", buffer ); + sprintf(buffer, "%s_ctl%i", linkName, i); + e.AddEPair("targetname", buffer); - sprintf( buffer, "0 %i 0", (i * 64) + 32); - e.AddEPair( "origin", buffer ); + sprintf(buffer, "0 %i 0", (i * 64) + 32); + e.AddEPair("origin", buffer); - e.BuildInRadiant( FALSE ); - } + e.BuildInRadiant(false); + } } -void DTreePlanter::SelectChain( void ) { +void DTreePlanter::SelectChain(void) +{ /* char buffer[256]; - - for(int i = 0; i < m_linkNum; i++) { - DEntity e("info_train_spline_main"); - sprintf( buffer, "%s_pt%i", m_linkName, i ); - e.AddEPair( "targetname", buffer ); + for(int i = 0; i < m_linkNum; i++) { + DEntity e("info_train_spline_main"); + + sprintf( buffer, "%s_pt%i", m_linkName, i ); + e.AddEPair( "targetname", buffer ); - sprintf( buffer, "0 %i 0", i * 64 ); - e.AddEPair( "origin", buffer ); + sprintf( buffer, "0 %i 0", i * 64 ); + e.AddEPair( "origin", buffer ); - if(i != m_linkNum-1) { - sprintf( buffer, "%s_pt%i", m_linkName, i+1 ); - e.AddEPair( "target", buffer ); + if(i != m_linkNum-1) { + sprintf( buffer, "%s_pt%i", m_linkName, i+1 ); + e.AddEPair( "target", buffer ); - sprintf( buffer, "%s_ctl%i", m_linkName, i ); - e.AddEPair( "control", buffer ); - } + sprintf( buffer, "%s_ctl%i", m_linkName, i ); + e.AddEPair( "control", buffer ); + } - e.BuildInRadiant( FALSE ); - } + e.BuildInRadiant( false ); + } - for(int i = 0; i < m_linkNum-1; i++) { - DEntity e("info_train_spline_control"); + for(int i = 0; i < m_linkNum-1; i++) { + DEntity e("info_train_spline_control"); - sprintf( buffer, "%s_ctl%i", m_linkName, i ); - e.AddEPair( "targetname", buffer ); + sprintf( buffer, "%s_ctl%i", m_linkName, i ); + e.AddEPair( "targetname", buffer ); - sprintf( buffer, "0 %i 0", (i * 64) + 32); - e.AddEPair( "origin", buffer ); + sprintf( buffer, "0 %i 0", (i * 64) + 32); + e.AddEPair( "origin", buffer ); - e.BuildInRadiant( FALSE ); - }*/ + e.BuildInRadiant( false ); + }*/ }