]> icculus.org git repositories - divverent/nexuiz.git/blob - scmenu/source/custom/creategame.qm
-Added a management window to creategame, which allows you to save and load
[divverent/nexuiz.git] / scmenu / source / custom / creategame.qm
1 // Property of Alientrap
2 // custom/creategame.qm
3
4 // uses target and _target
5 // requires neighbors: Picture, Name, Description
6 void() Nex_MapSelector_UpdateMap =
7 {
8         local entity lInfo;
9         local entity lItem;
10         local entity lSelector;
11         local float lIndex;
12
13         lSelector = Menu_GetItem( "::Data::Server::Map::Selector" );
14         Raise_DataEvent( lSelector, ITEM_DATALINK_GET );
15         lIndex = lSelector._realValue;
16         lInfo = Nex_MapDB_GetByIndex( lIndex );
17
18         // If there is a new selected map, the info panel is updated accordingly
19         if( lIndex != self.minValue ) {
20                 self.minValue = lIndex;
21
22                 lItem = Menu_GetItem( "Picture" );
23                 String_EntitySet( lItem, picture, lInfo.picture );
24
25                 lItem = Menu_GetItem( "Name" );
26                 String_EntitySet( lItem, text, lInfo.normal );
27
28                 lItem = Menu_GetItem( "Description" );
29                 String_EntitySet( lItem, text, lInfo.text );
30         }
31
32         lItem = Menu_GetItem( "::Data::Server::Map::Cycle" );
33         DataLink_Nex_MapList_SetFirst( lItem, lInfo.normal, lInfo.link );
34 };
35
36 void() Nex_MapSelector_Synchronize =
37 {
38         local entity lCycle;
39         local entity lSelector;
40
41         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
42         lSelector = Menu_GetItem( "::Data::Server::Map::Selector" );
43
44         if( lCycle.stepValue == 0.0 ) {
45                 Raise_DataEvent( lCycle, ITEM_DATA_RESET );
46         } else {
47                 local string lPath;
48                 local entity lMapInfo;
49
50                 lPath = Util_GetAltStringItem( lCycle.valueList, 0 );
51                 lMapInfo = Nex_MapDB_GetByPath( lPath );
52                 String_Free( lPath );
53
54                 if( lMapInfo == null_entity ) {
55                         Raise_DataEvent( lCycle, ITEM_DATA_RESET );
56                 } else {
57                         lSelector._realValue = Nex_MapDB_GetIndex( lMapInfo );
58                         Raise_DataEvent( lSelector, ITEM_DATALINK_SET );
59                 }
60         }
61 };
62
63 // more concrete functions
64 void() Nex_Action_MapSelector_Next =
65 {
66         local entity lItem;
67
68         lItem = Menu_GetItem( "::Data::Server::Map::Selector" );
69         Raise_DataEvent( lItem, ITEM_DATALINK_GET );
70         lItem._realValue = lItem._realValue + 1;
71         Raise_DataEvent( lItem, ITEM_DATALINK_SET );
72 };
73
74 void() Nex_Action_MapSelector_Prev =
75 {
76         local entity lItem;
77
78         lItem = Menu_GetItem( "::Data::Server::Map::Selector" );
79         Raise_DataEvent( lItem, ITEM_DATALINK_GET );
80         lItem._realValue = lItem._realValue - 1;
81         Raise_DataEvent( lItem, ITEM_DATALINK_SET );
82 };
83
84 // TODO: Make MaxPlayers real Data items (no container with correct set behavior)
85 void() Nex_Action_Map_Start =
86 {
87         local entity lItem;
88         local float  lNumBots, lSkill;
89         // we need to set the following var
90         lItem = Menu_GetItem( "::Data::Server::NumBots" );
91         lNumBots = stof( lItem.value );
92         lItem = Menu_GetItem( "::Data::Server::BotSkill" );
93         lSkill = stof( lItem.value );
94
95         cvar_set( "saved1", "1" );
96         cvar_set( "scratch1", ftos( pow( 2, lNumBots ) - 1 ) );
97         cvar_set( "scratch2", ftos( lSkill * (pow(4, min(lNumBots, 8)) - 1) / 3) );
98         cvar_set( "scratch3", ftos( lSkill * (pow(4, min(lNumBots - 8, 8)) - 1) / 3) );
99         // Set maxplayers
100         lItem = Menu_GetItem( "::Data::Server::MaxPlayers" );
101         cmd( "maxplayers \"" );
102         cmd( lItem.value );
103         cmd( "\"\n" );
104
105         lItem = Menu_GetItem( "::Data::Server::Map::Cycle" );
106         cmd( "map \"" );
107         cmd( String_Normal( Util_GetAltStringItem( lItem.valueList, 0 ) ) );
108         cmd( "\"\n");
109
110         Raise_DataEvent( lItem, ITEM_DATA_SEND );
111
112         // force the menu to hide
113         m_hide();
114 };
115
116 // cycle list edit features
117 void() Nex_Action_Cycle_Add =
118 {
119         local entity lList;
120         local entity lCycle;
121         local string lPath;
122         local string lName;
123
124         lList = Menu_GetItem( "::Data::Server::Map::List" );
125         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
126
127         lName = DataLink_Nex_MapList_GetCurrentName( lList );
128         lPath = DataLink_Nex_MapList_GetCurrentPath( lList );
129
130         if( lCycle.stepValue == 1.0 ) {
131                 DataLink_Nex_MapList_InsertEntryAfter( lCycle, lName, lPath );
132         } else {
133                 DataLink_Nex_MapList_SetFirst( lCycle, lName, lPath );
134         }
135
136         String_Free( lName );
137         String_Free( lPath );
138
139         lCycle._realValue = lCycle._realValue + 1;
140         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
141
142         Nex_MapSelector_Synchronize();
143 };
144
145 void() Nex_Action_Cycle_Remove =
146 {
147         local entity lCycle;
148
149         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
150         DataLink_Nex_MapList_DeleteEntry( lCycle );
151
152         Nex_MapSelector_Synchronize();
153 };
154
155 void() Nex_Action_Cycle_MoveUp =
156 {
157         local entity lCycle;
158
159         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
160         DataLink_Nex_MapList_MoveEntryUp( lCycle );
161
162         lCycle._realValue = lCycle._realValue - 1;
163         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
164
165         Nex_MapSelector_Synchronize();
166 };
167
168 void() Nex_Action_Cycle_MoveDown =
169 {
170         local entity lCycle;
171
172         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
173         DataLink_Nex_MapList_MoveEntryDown( lCycle );
174
175         lCycle._realValue = lCycle._realValue + 1;
176         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
177
178         Nex_MapSelector_Synchronize();
179 };
180
181 void() Nex_Action_Cycle_Clear =
182 {
183         local entity lCycle;
184
185         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
186         DataLink_Nex_MapList_Clear( lCycle );
187
188         Nex_MapSelector_Synchronize();
189 };
190
191 // mangement
192
193 entity( float pType ) Nex_Management_GetFileList =
194 {
195         switch( pType ) {
196                 case 0:
197                         return Menu_GetItem( "::Data::Management::FileList::Weapon" );
198                 case 1:
199                         return Menu_GetItem( "::Data::Management::FileList::Game" );
200                 case 2:
201                         return Menu_GetItem( "::Data::Management::FileList::MapList" );
202         }
203         crash();
204         return null_entity;
205 };
206
207 entity( float pType ) Nex_Management_GetContainer =
208 {
209         switch( pType ) {
210                 case 0:
211                         return Menu_GetItem( "::Data::Weapon" );
212                 case 1:
213                         return Menu_GetItem( "::Data::Game" );
214                 case 2:
215                         return Menu_GetItem( "::Data::Server::Map::Cycle" );
216         }
217         crash();
218         return null_entity;
219 };
220
221 float() Nex_Management_GetType =
222 {
223         local entity lTypeData;
224
225         lTypeData = Menu_GetItem( "::Data::Management::ConfigType" );
226         return stof( lTypeData.value );
227 };
228
229 void( float pType ) Nex_Management_SetType =
230 {
231         local entity lItem;
232         local entity lList;
233         local entity lTypeData;
234
235         lTypeData = Menu_GetItem( "::Data::Management::ConfigType" );
236         String_EntitySet( lTypeData, value, ftos( pType ) );
237         Raise_DataEvent( lTypeData, ITEM_DATALINK_SET );
238
239         // update the filename
240         /*lItem = Menu_GetItem( "::Data::Management::Filename::Text" );
241         String_EntitySet( lItem, value, "" );
242         Raise_DataEvent( lItem, ITEM_DATALINK_SET );*/
243
244         lItem = Menu_GetItem( "FileList" );
245         lList = Nex_Management_GetFileList( pType );
246         String_EntitySet( lItem, target, lList.name );
247 };
248
249 void() Nex_Action_CopyToFilename =
250 {
251         local entity lItem;
252
253         if( !self._target ) {
254                 return;
255         }
256
257         Raise_DataEvent( self._target, ITEM_DATALINK_GET );
258
259         lItem = Menu_GetItem( "::Data::Management::Filename::Text" );
260         String_EntitySet( lItem, value, self._target.value );
261         Raise_DataEvent( lItem, ITEM_DATALINK_SET );
262 };
263
264 void() Nex_Update_Management_SyncData =
265 {
266         if( self._realValue < Timer_Time && self._realValue > 0.0 ) {
267                 Raise_DataEvent( Nex_Management_GetContainer( Nex_Management_GetType() ), ITEM_DATA_SYNC );
268                 self._realValue = 0.0;
269         }
270 };
271
272 void() Nex_Action_Management_Refresh =
273 {
274         local entity lItem;
275
276         lItem = Menu_GetItem( "::Data::Management::FileList" );
277         Raise_DataEvent( lItem, ITEM_DATA_SYNC );
278 };
279
280 void() Nex_Action_Management_LoadConfig =
281 {
282         local entity lFileList;
283         local entity lFilenameData;
284         local entity lSyncItem;
285
286         lFileList = Nex_Management_GetFileList( Nex_Management_GetType() );
287         lFilenameData = Menu_GetItem( "::Data::Management::Filename" );
288
289         cmd( strcat( "exec \"", lFileList.selected, "/", lFilenameData.value, ".", lFileList.normal, "\"\n" ) );
290
291         lSyncItem = Menu_GetItem( "CreateGame::Panel::Management::SyncCategory" );
292         lSyncItem._realValue = Timer_Time;
293 };
294
295 void() Nex_Action_Management_Load =
296 {
297         local entity lItem;
298         local string lQuestion;
299
300         lItem = Menu_GetItem( "::Data::Management::Filename" );
301         if( lItem.value == "" ) {
302                 Nex_MessageBox( "Loading", "You must specify a filename!", "Ok", "", Util_NullFunction, Util_NullFunction );
303                 return;
304         }
305
306         lQuestion = String_Zone( strcat( "Do you want to load '", lItem.value, "'?" ) );
307         Nex_MessageBox( "Loading", lQuestion, "Yes", "No", Nex_Action_Management_LoadConfig, Util_NullFunction );
308         String_Free( lQuestion );
309 };
310
311 void() Nex_Action_Management_SaveConfig =
312 {
313         local float lType;
314         local entity lFileList;
315         local entity lFilename;
316         local entity lContainer;
317         local float lHandle;
318
319         lType = Nex_Management_GetType();
320         lFileList = Nex_Management_GetFileList( lType );
321         lContainer = Nex_Management_GetContainer( lType );
322         lFilename = Menu_GetItem( "::Data::Management::Filename" );
323         if( lFilename.value == "" ) {
324                 Nex_MessageBox( "Loading", "You must specify a filename!", "Ok", "", Util_NullFunction, Util_NullFunction );
325                 return;
326         }
327         lHandle = fopen( strcat( lFileList.selected, "/", lFilename.value, ".", lFileList.normal ), FILE_WRITE );
328
329         if( lHandle < 0 ) {
330                 return;
331         }
332
333         Data_ExecString_BeginUse();
334         Raise_DataEvent( lContainer, ITEM_DATA_SAVE_EXECSTRING );
335
336         fputs( lHandle, Data_ExecString );
337
338         Data_ExecString_EndUse();
339
340         fclose( lHandle );
341
342         Nex_Action_Management_Refresh();
343 };
344
345 void() Nex_Action_Management_Save =
346 {
347         local entity lFileList;
348         local entity lFilename;
349         local float lHandle;
350
351         lFileList = Nex_Management_GetFileList( Nex_Management_GetType() );
352         lFilename = Menu_GetItem( "::Data::Management::Filename" );
353
354         lHandle = fopen( strcat( lFileList.selected, "/", lFilename.value, ".", lFileList.normal ), FILE_READ );
355
356         if( lHandle == ERR_CANNOTOPEN ) {
357                 Nex_Action_Management_SaveConfig();
358                 fclose( lHandle );
359         } else if( lHandle > 0 ) {
360                 local string lQuestion;
361
362                 lQuestion = String_Zone( strcat( "Do you want to overwrite '", lFilename.value, "'?" ) );
363                 Nex_MessageBox( "Saving", lQuestion, "Yes", "No", Nex_Action_Management_SaveConfig, Util_NullFunction );
364                 String_Free( lQuestion );
365         }
366 };
367
368 void() Nex_Action_Management_TypeWeapon =
369 {
370         Nex_Management_SetType( 0 );
371 };
372
373 void() Nex_Action_Management_TypeGame =
374 {
375         Nex_Management_SetType( 1 );
376 };
377
378 void() Nex_Action_Management_TypeMapList =
379 {
380         Nex_Management_SetType( 2 );
381 };