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