final fix for ufoai
[xonotic/netradiant.git] / contrib / ufoaiplug / ufoai.cpp
1 /*
2 This file is part of GtkRadiant.
3
4 GtkRadiant is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 GtkRadiant is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GtkRadiant; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "ufoai.h"
20 #include "ufoai_level.h"
21 #include "ufoai_gtk.h"
22 #include "ufoai_filters.h"
23
24 #include "debugging/debugging.h"
25
26 #include "iplugin.h"
27
28 #include "version.h"
29
30 #include "string/string.h"
31 #include "modulesystem/singletonmodule.h"
32
33 #include <gtk/gtk.h>
34
35 #define PLUGIN_VERSION "0.1"
36
37 #include "ifilter.h"
38 #include "ibrush.h"
39 #include "iundo.h"       // declaration of undo system
40 #include "ientity.h"     // declaration of entity system
41 #include "iscenegraph.h" // declaration of datastructure of the map
42 #include "scenelib.h"    // declaration of datastructure of the map
43 #include "qerplugin.h"   // declaration to use other interfaces as a plugin
44 #include "ieclass.h"
45
46 class UFOAIPluginDependencies :
47   public GlobalRadiantModuleRef,    // basic class for all other module refs
48   public GlobalUndoModuleRef,       // used to say radiant that something has changed and to undo that
49   public GlobalSceneGraphModuleRef, // necessary to handle data in the mapfile (change, retrieve data)
50   public GlobalEntityModuleRef,     // to access and modify the entities
51   public GlobalEntityClassManagerModuleRef
52 {
53 public:
54         UFOAIPluginDependencies(void) :
55                 GlobalEntityModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entities")),
56                 GlobalEntityClassManagerModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entityclass"))
57         {
58         }
59 };
60
61 namespace UFOAI
62 {
63         GtkWindow* g_mainwnd;
64
65         const char* init(void* hApp, void* pMainWidget)
66         {
67                 g_mainwnd = GTK_WINDOW(pMainWidget);
68                 return "Initializing GTKRadiant UFOAI plugin";
69         }
70         const char* getName()
71         {
72                 return "UFO:AI";
73         }
74         const char* getCommandList()
75         {
76                 /*GlobalRadiant().getGameName()*/
77                 return "About;-;Worldspawn reset (day);Worldspawn reset (night);Worldspawn (day);Worldspawn (night);Perform check;-;Level 1;Level 2;Level 3;Level 4;Level 5;Level 6;Level 7;Level 8;-;StepOn;ActorClip";
78         }
79         const char* getCommandTitleList()
80         {
81                 return "";
82         }
83         void dispatch(const char* command, float* vMin, float* vMax, bool bSingleBrush)
84         {
85                 char *message = NULL;
86                 if(string_equal(command, "About"))
87                 {
88                         GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_mainwnd),
89                                 "UFO:AI Plugin (http://www.ufoai.net)\nBuild: " __DATE__ "\nRadiant version: " RADIANT_VERSION "\nPlugin version: " PLUGIN_VERSION "\n", "About",
90                                 eMB_OK, eMB_ICONDEFAULT);
91                 }
92                 else if(string_equal(command, "Level 1"))
93                 {
94                         filter_level(CONTENTS_LEVEL1);
95                 }
96                 else if(string_equal(command, "Level 2"))
97                 {
98                         filter_level(CONTENTS_LEVEL2);
99                 }
100                 else if(string_equal(command, "Level 3"))
101                 {
102                         filter_level(CONTENTS_LEVEL3);
103                 }
104                 else if(string_equal(command, "Worldspawn (day)"))
105                 {
106                         assign_default_values_to_worldspawn(false, true, &message);
107                 }
108                 else if(string_equal(command, "Worldspawn (night)"))
109                 {
110                         assign_default_values_to_worldspawn(false, false, &message);
111                 }
112                 else if(string_equal(command, "Worldspawn reset (day)"))
113                 {
114                         assign_default_values_to_worldspawn(true, true, &message);
115                 }
116                 else if(string_equal(command, "Worldspawn reset (night)"))
117                 {
118                         assign_default_values_to_worldspawn(true, false, &message);
119                 }
120                 else if(string_equal(command, "Perform check"))
121                 {
122                         check_map_values(&message);
123                 }
124                 else if(string_equal(command, "Level 4"))
125                 {
126                         filter_level(CONTENTS_LEVEL4);
127                 }
128                 else if(string_equal(command, "Level 5"))
129                 {
130                         filter_level(CONTENTS_LEVEL5);
131                 }
132                 else if(string_equal(command, "Level 6"))
133                 {
134                         filter_level(CONTENTS_LEVEL6);
135                 }
136                 else if(string_equal(command, "Level 7"))
137                 {
138                         filter_level(CONTENTS_LEVEL7);
139                 }
140                 else if(string_equal(command, "Level 8"))
141                 {
142                         filter_level(CONTENTS_LEVEL8);
143                 }
144                 else if(string_equal(command, "StepOn"))
145                 {
146                         filter_stepon();
147                 }
148                 else if(string_equal(command, "ActorClip"))
149                 {
150                         filter_actorclip();
151                 }
152
153                 if (message != NULL)
154                 {
155                         GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_mainwnd),
156                                 message, "Note",
157                                 eMB_OK, eMB_ICONDEFAULT);
158                 }
159                 SceneChangeNotify();
160         }
161 } // namespace
162
163
164 class UFOAIModule : public TypeSystemRef
165 {
166         _QERPluginTable m_plugin;
167 public:
168         typedef _QERPluginTable Type;
169         STRING_CONSTANT(Name, "UFO:AI");
170
171         UFOAIModule()
172         {
173                 m_plugin.m_pfnQERPlug_Init = &UFOAI::init;
174                 m_plugin.m_pfnQERPlug_GetName = &UFOAI::getName;
175                 m_plugin.m_pfnQERPlug_GetCommandList = &UFOAI::getCommandList;
176                 m_plugin.m_pfnQERPlug_GetCommandTitleList = &UFOAI::getCommandTitleList;
177                 m_plugin.m_pfnQERPlug_Dispatch = &UFOAI::dispatch;
178         }
179         _QERPluginTable* getTable()
180         {
181                 return &m_plugin;
182         }
183 };
184
185 typedef SingletonModule<UFOAIModule, UFOAIPluginDependencies> SingletonUFOAIModule;
186
187 SingletonUFOAIModule g_UFOAIModule;
188
189
190 class UFOAIToolbarDependencies : public ModuleRef<_QERPluginTable>
191 {
192 public:
193         UFOAIToolbarDependencies() : ModuleRef<_QERPluginTable>("UFO:AI")
194         {
195         }
196 };
197
198 class UFOAIToolbarModule : public TypeSystemRef
199 {
200         _QERPlugToolbarTable m_table;
201 public:
202         typedef _QERPlugToolbarTable Type;
203         STRING_CONSTANT(Name, "UFO:AI");
204
205         UFOAIToolbarModule()
206         {
207                 if (!strcmp(GlobalRadiant().getGameDescriptionKeyValue("name"), "UFO:Alien Invasion")) {
208                         m_table.m_pfnToolbarButtonCount = ToolbarButtonCount;
209                         m_table.m_pfnGetToolbarButton = GetToolbarButton;
210                 }
211                 else
212                 {
213                         m_table.m_pfnToolbarButtonCount = ToolbarNoButtons;
214                         m_table.m_pfnGetToolbarButton = GetToolbarNoButton;
215                 }
216         }
217         _QERPlugToolbarTable* getTable()
218         {
219                 return &m_table;
220         }
221 };
222
223 typedef SingletonModule<UFOAIToolbarModule, UFOAIToolbarDependencies> SingletonUFOAIToolbarModule;
224
225 SingletonUFOAIToolbarModule g_UFOAIToolbarModule;
226
227
228 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer& server)
229 {
230         initialiseModule(server);
231
232         g_UFOAIModule.selfRegister();
233         g_UFOAIToolbarModule.selfRegister();
234 }