]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DTreePlanter.h
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / contrib / bobtoolz / DTreePlanter.h
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 #ifndef __DTREE_H__
21 #define __DTREE_H__
22
23 #include "qerplugin.h"
24 #include "signal/isignal.h"
25 #include "string/string.h"
26
27 #include "DEntity.h"
28 #include "ScriptParser.h"
29 #include "mathlib.h"
30 #include "misc.h"
31
32 const int MAX_QPATH = 64;
33
34 typedef struct treeModel_s {
35     char name[MAX_QPATH];
36 } treeModel_t;
37
38 const int MAX_TP_MODELS = 256;
39
40 class DTreePlanter {
41     MouseEventHandlerId m_mouseDown;
42     SignalHandlerId m_destroyed;
43 public:
44     SignalHandlerResult mouseDown(const WindowVector &position, ButtonIdentifier button, ModifierFlags modifiers);
45
46     typedef Member<DTreePlanter, SignalHandlerResult(const WindowVector &, ButtonIdentifier,
47                                                      ModifierFlags), &DTreePlanter::mouseDown> MouseDownCaller;
48
49     void destroyed()
50     {
51         m_mouseDown = MouseEventHandlerId();
52         m_destroyed = SignalHandlerId();
53     }
54
55     typedef Member<DTreePlanter, void(), &DTreePlanter::destroyed> DestroyedCaller;
56
57     DTreePlanter()
58     {
59         m_numModels = 0;
60         m_offset = 0;
61         m_maxPitch = 0;
62         m_minPitch = 0;
63         m_maxYaw = 0;
64         m_minYaw = 0;
65         m_setAngles = false;
66         m_useScale = false;
67         m_autoLink = false;
68         m_linkNum = 0;
69
70         m_world.LoadSelectedBrushes();
71
72         char buffer[256];
73         GetFilename(buffer, "bt/tp_ent.txt");
74
75         FILE *file = fopen(buffer, "rb");
76         if (file) {
77             fseek(file, 0, SEEK_END);
78             int len = ftell(file);
79             fseek(file, 0, SEEK_SET);
80
81             if (len) {
82                 char *buf = new char[len + 1];
83                 buf[len] = '\0';
84                 // parser will do the cleanup, dont delete.
85
86                 fread(buf, len, 1, file);
87
88                 CScriptParser parser;
89                 parser.SetScript(buf);
90
91                 ReadConfig(&parser);
92             }
93
94             fclose(file);
95         }
96
97         m_mouseDown = GlobalRadiant().XYWindowMouseDown_connect(makeSignalHandler3(MouseDownCaller(), *this));
98         m_destroyed = GlobalRadiant().XYWindowDestroyed_connect(makeSignalHandler(DestroyedCaller(), *this));
99     }
100
101     virtual ~DTreePlanter()
102     {
103         if (!m_mouseDown.isNull()) {
104             GlobalRadiant().XYWindowMouseDown_disconnect(m_mouseDown);
105         }
106         if (!m_destroyed.isNull()) {
107             GlobalRadiant().XYWindowDestroyed_disconnect(m_destroyed);
108         }
109     }
110
111 #define MT(t)   string_equal_nocase( pToken, t )
112 #define GT      pToken = pScriptParser->GetToken( true )
113 #define CT      if ( !*pToken ) { return; }
114
115     void ReadConfig(CScriptParser *pScriptParser)
116     {
117         const char *GT;
118         CT;
119
120         do {
121             GT;
122             if (*pToken == '}') {
123                 break;
124             }
125
126             if (MT("model")) {
127                 if (m_numModels >= MAX_TP_MODELS) {
128                     return;
129                 }
130
131                 GT;
132                 CT;
133
134                 strncpy(m_trees[m_numModels++].name, pToken, MAX_QPATH);
135             } else if (MT("link")) {
136                 GT;
137                 CT;
138
139                 strncpy(m_linkName, pToken, MAX_QPATH);
140
141                 m_autoLink = true;
142             } else if (MT("entity")) {
143                 GT;
144                 CT;
145
146                 strncpy(m_entType, pToken, MAX_QPATH);
147             } else if (MT("offset")) {
148                 GT;
149                 CT;
150
151                 m_offset = atoi(pToken);
152             } else if (MT("pitch")) {
153                 GT;
154                 CT;
155
156                 m_minPitch = atoi(pToken);
157
158                 GT;
159                 CT;
160
161                 m_maxPitch = atoi(pToken);
162
163                 m_setAngles = true;
164             } else if (MT("yaw")) {
165                 GT;
166                 CT;
167
168                 m_minYaw = atoi(pToken);
169
170                 GT;
171                 CT;
172
173                 m_maxYaw = atoi(pToken);
174
175                 m_setAngles = true;
176             } else if (MT("scale")) {
177                 GT;
178                 CT;
179
180                 m_minScale = static_cast<float>( atof(pToken));
181
182                 GT;
183                 CT;
184
185                 m_maxScale = static_cast<float>( atof(pToken));
186
187                 m_useScale = true;
188             } else if (MT("numlinks")) {
189                 GT;
190                 CT;
191
192                 m_linkNum = atoi(pToken);
193             }
194         } while (true);
195     }
196
197     bool FindDropPoint(vec3_t in, vec3_t out);
198
199     void DropEntsToGround(void);
200
201     void MakeChain(int linkNum, const char *linkName);
202
203     void SelectChain(void);
204
205 private:
206     DEntity m_world;
207
208     treeModel_t m_trees[MAX_TP_MODELS];
209
210     int m_numModels;
211     int m_offset;
212     int m_maxPitch;
213     int m_minPitch;
214     int m_maxYaw;
215     int m_minYaw;
216
217     char m_entType[MAX_QPATH];
218     char m_linkName[MAX_QPATH];
219     int m_linkNum;
220
221     float m_minScale;
222     float m_maxScale;
223
224     bool m_useScale;
225     bool m_setAngles;
226     bool m_autoLink;
227 };
228
229 #endif