DTreePlanter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. BobToolz plugin for GtkRadiant
  3. Copyright (C) 2001 Gordon Biggans
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "StdAfx.h"
  17. #include "gtkr_list.h"
  18. #include "str.h"
  19. #include "DPoint.h"
  20. #include "DPlane.h"
  21. #include "DBrush.h"
  22. #include "DEPair.h"
  23. #include "DPatch.h"
  24. #include "DEntity.h"
  25. #include "ScriptParser.h"
  26. #include "misc.h"
  27. #include "DTreePlanter.h"
  28. #include "funchandlers.h"
  29. bool DTreePlanter::OnMouseMove(guint32 nFlags, gdouble x, gdouble y) {
  30. return false;
  31. }
  32. bool DTreePlanter::OnLButtonDown(guint32 nFlags, gdouble x, gdouble y) {
  33. VIEWTYPE vt = m_XYWrapper->GetViewType();
  34. switch(vt) {
  35. case XY:
  36. break;
  37. case YZ:
  38. case XZ:
  39. default:
  40. return false;
  41. }
  42. vec3_t pt, vhit;
  43. m_XYWrapper->SnapToGrid( static_cast<int>(x), static_cast<int>(y), pt );
  44. if(FindDropPoint(pt, vhit)) {
  45. vhit[2] += m_offset;
  46. char buffer[128];
  47. DEntity e(m_entType);
  48. sprintf(buffer, "%i %i %i", (int)vhit[0], (int)vhit[1], (int)vhit[2]);
  49. e.AddEPair("origin", buffer);
  50. if(m_autoLink) {
  51. #if 0
  52. entity_t* pLastEntity = NULL;
  53. entity_t* pThisEntity = NULL;
  54. int entNum = -1, lastEntNum = -1, entpos;
  55. for(int i = 0; i < 256; i++) {
  56. sprintf(buffer, m_linkName, i);
  57. pThisEntity = FindEntityFromTargetname( buffer, &entNum );
  58. if(pThisEntity) {
  59. entpos = i;
  60. lastEntNum = entNum;
  61. pLastEntity = pThisEntity;
  62. }
  63. }
  64. if(!pLastEntity) {
  65. sprintf(buffer, m_linkName, 0);
  66. } else {
  67. sprintf(buffer, m_linkName, entpos + 1);
  68. }
  69. e.AddEPair( "targetname", buffer );
  70. if(pLastEntity) {
  71. DEntity e2;
  72. e2.LoadFromEntity(lastEntNum, TRUE);
  73. e2.AddEPair("target", buffer);
  74. e2.RemoveFromRadiant();
  75. e2.BuildInRadiant(FALSE);
  76. }
  77. #endif
  78. }
  79. if(m_setAngles) {
  80. int angleYaw = (rand() % (m_maxYaw - m_minYaw + 1)) + m_minYaw;
  81. int anglePitch = (rand() % (m_maxPitch - m_minPitch + 1)) + m_minPitch;
  82. sprintf(buffer, "%i %i 0", anglePitch, angleYaw);
  83. e.AddEPair("angles", buffer);
  84. }
  85. if(m_numModels) {
  86. int treetype = rand() % m_numModels;
  87. e.AddEPair("model", m_trees[treetype].name);
  88. }
  89. if(m_useScale) {
  90. float scale = (((rand()%1000)*0.001f) * (m_maxScale - m_minScale)) + m_minScale;
  91. sprintf(buffer, "%f", scale );
  92. e.AddEPair("modelscale", buffer);
  93. }
  94. e.BuildInRadiant( FALSE );
  95. }
  96. if(m_autoLink) {
  97. DoTrainPathPlot();
  98. }
  99. return true;
  100. }
  101. bool DTreePlanter::OnLButtonUp(guint32 nFlags, gdouble x, gdouble y) {
  102. return false;
  103. }
  104. bool DTreePlanter::OnRButtonDown(guint32 nFlags, gdouble x, gdouble y) {
  105. return false;
  106. }
  107. bool DTreePlanter::OnRButtonUp(guint32 nFlags, gdouble x, gdouble y) {
  108. return false;
  109. }
  110. bool DTreePlanter::OnMButtonDown(guint32 nFlags, gdouble x, gdouble y) {
  111. return false;
  112. }
  113. bool DTreePlanter::OnMButtonUp(guint32 nFlags, gdouble x, gdouble y) {
  114. return false;
  115. }
  116. bool DTreePlanter::FindDropPoint(vec3_t in, vec3_t out) {
  117. DPlane p1;
  118. DPlane p2;
  119. vec3_t vUp = { 0, 0, 1 };
  120. vec3_t vForward = { 0, 1, 0 };
  121. vec3_t vLeft = { 1, 0, 0 };
  122. in[2] = 65535;
  123. VectorCopy(in, p1.points[0]);
  124. VectorCopy(in, p1.points[1]);
  125. VectorCopy(in, p1.points[2]);
  126. VectorMA(p1.points[1], 20, vUp, p1.points[1]);
  127. VectorMA(p1.points[1], 20, vLeft, p1.points[2]);
  128. VectorCopy(in, p2.points[0]);
  129. VectorCopy(in, p2.points[1]);
  130. VectorCopy(in, p2.points[2]);
  131. VectorMA(p1.points[1], 20, vUp, p2.points[1]);
  132. VectorMA(p1.points[1], 20, vForward, p2.points[2]);
  133. p1.Rebuild();
  134. p2.Rebuild();
  135. bool found = false;
  136. vec3_t temp;
  137. vec_t dist;
  138. int cnt = m_world.GetIDMax();
  139. for(int i = 0; i < cnt; i++) {
  140. DBrush* pBrush = m_world.GetBrushForID( i );
  141. if(pBrush->IntersectsWith( &p1, &p2, temp )) {
  142. vec3_t diff;
  143. vec_t tempdist;
  144. VectorSubtract(in, temp, diff);
  145. tempdist = VectorLength( diff );
  146. if(!found || (tempdist < dist)) {
  147. dist = tempdist;
  148. VectorCopy( temp, out );
  149. found = true;
  150. }
  151. }
  152. }
  153. return found;
  154. }
  155. void DTreePlanter::DropEntsToGround( void ) {
  156. #if 0
  157. // tell Radiant we want to access the selected brushes
  158. g_FuncTable.m_pfnAllocateSelectedBrushHandles();
  159. DEntity ent;
  160. int cnt = g_FuncTable.m_pfnSelectedBrushCount();
  161. for(int i = 0; i < cnt; i++) {
  162. brush_t *brush = (brush_t*)g_FuncTable.m_pfnGetSelectedBrushHandle(i);
  163. ent.LoadFromEntity(brush->owner, TRUE);
  164. DEPair* pEpair = ent.FindEPairByKey("origin");
  165. if(!pEpair) {
  166. continue;
  167. }
  168. vec3_t vec, out;
  169. sscanf( pEpair->value.GetBuffer(), "%f %f %f", &vec[0], &vec[1], &vec[2]);
  170. FindDropPoint( vec, out );
  171. char buffer[256];
  172. sprintf( buffer, "%f %f %f", out[0], out[1], out[2] );
  173. ent.AddEPair( "origin", buffer );
  174. ent.RemoveFromRadiant();
  175. ent.BuildInRadiant(FALSE);
  176. }
  177. g_FuncTable.m_pfnReleaseSelectedBrushHandles();
  178. #endif
  179. }
  180. void DTreePlanter::MakeChain( void ) {
  181. char buffer[256];
  182. int i;
  183. for(i = 0; i < m_linkNum; i++) {
  184. DEntity e("info_train_spline_main");
  185. sprintf( buffer, "%s_pt%i", m_linkName, i );
  186. e.AddEPair( "targetname", buffer );
  187. sprintf( buffer, "0 %i 0", i * 64 );
  188. e.AddEPair( "origin", buffer );
  189. if(i != m_linkNum-1) {
  190. sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
  191. e.AddEPair( "target", buffer );
  192. sprintf( buffer, "%s_ctl%i", m_linkName, i );
  193. e.AddEPair( "control", buffer );
  194. }
  195. e.BuildInRadiant( FALSE );
  196. }
  197. for(i = 0; i < m_linkNum-1; i++) {
  198. DEntity e("info_train_spline_control");
  199. sprintf( buffer, "%s_ctl%i", m_linkName, i );
  200. e.AddEPair( "targetname", buffer );
  201. sprintf( buffer, "0 %i 0", (i * 64) + 32);
  202. e.AddEPair( "origin", buffer );
  203. e.BuildInRadiant( FALSE );
  204. }
  205. }
  206. void DTreePlanter::SelectChain( void ) {
  207. /* char buffer[256];
  208. for(int i = 0; i < m_linkNum; i++) {
  209. DEntity e("info_train_spline_main");
  210. sprintf( buffer, "%s_pt%i", m_linkName, i );
  211. e.AddEPair( "targetname", buffer );
  212. sprintf( buffer, "0 %i 0", i * 64 );
  213. e.AddEPair( "origin", buffer );
  214. if(i != m_linkNum-1) {
  215. sprintf( buffer, "%s_pt%i", m_linkName, i+1 );
  216. e.AddEPair( "target", buffer );
  217. sprintf( buffer, "%s_ctl%i", m_linkName, i );
  218. e.AddEPair( "control", buffer );
  219. }
  220. e.BuildInRadiant( FALSE );
  221. }
  222. for(int i = 0; i < m_linkNum-1; i++) {
  223. DEntity e("info_train_spline_control");
  224. sprintf( buffer, "%s_ctl%i", m_linkName, i );
  225. e.AddEPair( "targetname", buffer );
  226. sprintf( buffer, "0 %i 0", (i * 64) + 32);
  227. e.AddEPair( "origin", buffer );
  228. e.BuildInRadiant( FALSE );
  229. }*/
  230. }