]> icculus.org git repositories - divverent/nexuiz.git/blob - scmenu/source/custom/creategame/maps.qc
-Renamed str_cvar to cvar_string.
[divverent/nexuiz.git] / scmenu / source / custom / creategame / maps.qc
1 // Property of Alientrap/AK
2 // custom/creategame/maps.qc
3
4 /*
5 ===================
6 Item_Nex_Map_Info
7 ===================
8 */
9
10 /*
11 ===================
12 Item_Nex_Map_Info_Destroy
13 ===================
14 */
15 void() Item_Nex_Map_Info_Destroy =
16 {
17         String_EntityFree( self, link );
18         String_EntityFree( self, picture );
19         String_EntityFree( self, text );
20         String_EntityFree( self, normal );
21 };
22
23 /*
24 ===================
25 Item_Nex_Map_Info_Spawn
26 ===================
27 */
28 void() Item_Nex_Map_Info_Spawn =
29 {
30         String_EntityZone( self, link );
31         String_EntityZone( self, picture );
32         String_EntityZone( self, text );
33         String_EntityZone( self, normal );
34
35         Gfx_Precache( self.picture );
36
37         self._destroy = Item_Nex_Map_Info_Destroy;
38 };
39
40 /*
41 ===================
42 Item_Nex_MapDB
43 ===================
44 */
45 string _Nex_MapList_FullPath;
46 string _Nex_MapList_Name;
47 entity _Nex_MapList_Root;
48
49 /*
50 ===================
51 Item_Nex_MapDB_EnumFiles
52 ===================
53 */
54 // map definition format
55 // name
56 // rest: description text
57 void() Item_Nex_MapDB_EnumFiles =
58 {
59         local float     lSearchHandle;
60         local float     lSearchSize;
61         local float     lSearchCounter;
62         local entity    lMap;
63
64         lSearchHandle = search_begin( "maps/*.bsp", true, true );
65         if( lSearchHandle < 0 )
66                 return;
67         for( lSearchSize = search_getsize( lSearchHandle ), lSearchCounter = 0;
68         lSearchCounter < lSearchSize; ++lSearchCounter ) {
69                 local float     lHandle;
70
71                 local string    lFilename;
72                 local string    lStripped;
73                 local string    lName;
74                 local string    lDescription;
75                 local string    lTitle;
76
77                 lFilename = search_getfilename( lSearchHandle, lSearchCounter );
78                 lStripped = String_Zone( substring( lFilename, 0, strlen( lFilename ) - 4 ) );
79                 lName = String_Zone( substring( lStripped, 5, 100000 ) );
80
81                 lHandle = fopen( strcat( lStripped, ".txt" ), FILE_READ );
82                 if( lHandle < 0 ) {
83                         lTitle = String_Zone( lName );
84                         lDescription = String_Zone( strcat( "--NO INFORMATION AVAILABLE--\n", lFilename ) );
85                 } else {
86                         // uncomment this line and comment the other if you have the old map info files
87                         //lTitle = String_Zone( lName );
88                         lTitle = String_Zone( fgets( lHandle ) );
89
90                         lDescription = String_Create();
91                         do {
92                                 local string lLine;
93
94                                 lLine = fgets( lHandle );
95                                 lDescription = String_Append( lDescription, strcat( lLine, "\n" ) );
96                         } while( validstring( lLine ) );
97
98                         fclose( lHandle );
99                 }
100
101                 // save this in a new Map_Info
102                 lMap = Menu_CreateItem( "Item_Nex_Map_Info", ftos( lSearchCounter ), self.name );
103
104                 lMap.link = lName;
105                 // check the picture
106                 if( Gfx_Precache( lStripped ) ) {
107                         lMap.picture = lStripped;
108                 } else {
109                         lMap.picture = "gfx/m_nomap";
110                 }
111                 lMap.normal = lTitle;
112                 lMap.text = lDescription;
113
114                 _Nex_MapList_FullPath = Util_AltStringPushBack( _Nex_MapList_FullPath, lName );
115                 _Nex_MapList_Name = Util_AltStringPushBack( _Nex_MapList_Name, lTitle );
116
117                 Menu_LinkItem( lMap );
118
119                 String_Free( lTitle );
120                 String_Free( lName );
121                 String_Free( lStripped );
122                 String_Free( lDescription );
123         }
124
125         search_end( lSearchHandle );
126
127         Menu_LinkChildren( self );
128
129         self.minValue = 1;
130         self.stepValue = 1;
131         self.maxValue = fabs( lMap.orderPos );
132 };
133
134 /*
135 ===================
136 Item_Nex_MapDB_Destroy
137 ===================
138 */
139 void() Item_Nex_MapDB_Destroy =
140 {
141         String_Free( _Nex_MapList_FullPath );
142         String_Free( _Nex_MapList_Name );
143
144         _Nex_MapList_Root = null_entity;
145 };
146
147 /*
148 ===================
149 Item_Nex_MapDB_Spawn
150 ===================
151 */
152 void() Item_Nex_MapDB_Spawn =
153 {
154         if( _Nex_MapList_Root )
155                 error( "There is already another Item_Nex_MapDB object!" );
156
157         _Nex_MapList_FullPath = String_Create();
158         _Nex_MapList_Name = String_Create();
159         _Nex_MapList_Root = self;
160
161         Item_Nex_MapDB_EnumFiles();
162
163         self._destroy = Item_Nex_MapDB_Destroy;
164 };
165
166 /*
167 ===================
168 Nex_MapDB_GetByName
169 ===================
170 */
171 entity( string pPath ) Nex_MapDB_GetByPath =
172 {
173         local entity lNode;
174
175         if( !_Nex_MapList_Root )
176                 error( "No Item_Nex_MapDB found!" );
177
178         for( lNode = _Nex_MapList_Root._child ; lNode ; lNode = lNode._next ) {
179                 if( lNode.link == pPath ) {
180                         return lNode;
181                 }
182         }
183
184         return null_entity;
185 };
186
187 /*
188 ===================
189 Nex_MapDB_GetByIndex
190 ===================
191 */
192 entity( float pIndex ) Nex_MapDB_GetByIndex =
193 {
194         local entity lNode;
195
196         if( !_Nex_MapList_Root )
197                 error( "No Item_Nex_MapDB found!" );
198
199         pIndex = fabs( pIndex );
200         for( lNode = _Nex_MapList_Root._child ; lNode ; lNode = lNode._next ) {
201                 if( fabs( lNode.orderPos ) == pIndex ) {
202                         return lNode;
203                 }
204         }
205
206         return null_entity;
207 };
208
209 /*
210 ===================
211 Nex_MapDB_GetPathAltString
212 ===================
213 */
214 string() Nex_MapDB_GetPathAltString =
215 {
216         return _Nex_MapList_FullPath;
217 };
218
219 /*
220 ===================
221 Nex_MapDB_GetNameAltString
222 ===================
223 */
224 string() Nex_MapDB_GetNameAltString =
225 {
226         return _Nex_MapList_Name;
227 };
228
229 /*
230 ===================
231 Nex_MapDB_GetIndex
232 ===================
233 */
234 float( entity pItem ) Nex_MapDB_GetIndex =
235 {
236         return fabs( pItem.orderPos );
237 };
238
239 void() Item_DataLink_Nex_MapList_Save =
240 {
241         local float lCount;
242         local float lIndex;
243         local string lOutput;
244
245         lOutput = String_Create();
246
247         lCount = Util_GetAltStringCount( self.valueList );
248         for( lIndex = 0 ; lIndex < lCount ; lIndex++ ) {
249                 lOutput = String_Append( lOutput, strcat( "\'", String_Normal( Util_GetAltStringItem( self.valueList, lIndex ) ), "\'" ) );
250         }
251
252         cvar_set( self.cvarName, lOutput );
253         cvar_set( strcat( self.cvarName, "_index" ), "0" );
254
255         String_Free( lOutput );
256 };
257
258 void() Item_DataLink_Nex_MapList_Load =
259 {
260         local string lEntry;
261         local entity lMapInfo;
262         local float lCounter;
263         local float lCount;
264
265         DataLink_Nex_MapList_Clear( self );
266
267         lCount = tokenize( cvar_string( self.cvarName ) );
268
269         lEntry = String_Create();
270         for( lCounter = 0 ; lCounter < lCount ; lCounter++ ) {
271                 lEntry = String_Set( lEntry, argv( lCounter ) );
272
273                 lMapInfo = Nex_MapDB_GetByPath( lEntry );
274                 if( lMapInfo == null_entity ) {
275                         print( self.cvarName, ": Map '", lEntry, "' not found in database - ignored\n" );
276                         continue;
277                 }
278
279                 self.descList = Util_AltStringPushBack( self.descList, lMapInfo.normal );
280                 self.valueList = Util_AltStringPushBack( self.valueList, lMapInfo.link );
281         }
282         String_Free( lEntry );
283
284         DataLink_Nex_MapList_UpdateRange( self );
285         Nex_MapSelector_Synchronize();
286 };
287
288 void() Item_DataLink_Nex_MapList_ExecString =
289 {
290         local float lCount;
291         local float lIndex;
292
293         Data_ExecString = String_Append( Data_ExecString, strcat( "set \"", self.cvarName, "_index\" \"0\"\nset \"", self.cvarName, "\" \"" ) );
294
295         lCount = Util_GetAltStringCount( self.valueList );
296         for( lIndex = 0 ; lIndex < lCount ; lIndex++ ) {
297                 Data_ExecString = String_Append( Data_ExecString, strcat( "\'", String_Normal( Util_GetAltStringItem( self.valueList, lIndex ) ), "\'" ) );
298         }
299
300         Data_ExecString = String_Append( Data_ExecString, "\"\n" );
301 };
302
303 /*
304 ===================
305 Item_DataLink_Nex_MapList_DataEvent
306 ===================
307 */
308 void( float pEvent ) Item_DataLink_Nex_MapList_DataEvent =
309 {
310         switch( pEvent ) {
311         case ITEM_DATA_SYNC:
312                 if( self.cvarName != "" ) {
313                         Item_DataLink_Nex_MapList_Load();
314                 }
315                 break;
316         case ITEM_DATA_SEND:
317                 if( self.cvarName != "" ) {
318                         Item_DataLink_Nex_MapList_Save();
319                 }
320                 break;
321         case ITEM_DATA_RESET:
322                 self._realValue = 1.0;
323                 break;
324         case ITEM_DATA_TEST_START:
325                 break;
326         case ITEM_DATA_TEST_END:
327                 break;
328         case ITEM_DATA_SAVE_EXECSTRING:
329                 if( self.cvarName != "" ) {
330                         Item_DataLink_Nex_MapList_ExecString();
331                 }
332                 break;
333         case ITEM_DATALINK_SET:
334         case ITEM_DATALINK_GET:
335                 self._realValue = floor( self._realValue );
336                 if( self._realValue < 1.0 ) {
337                         self._realValue = 1.0;
338                 } else if( self._realValue > self.maxValue ) {
339                         self._realValue = self.maxValue;
340                 }
341
342                 break;
343         }
344 };
345
346 void() IDLNML_Reinit =
347 {
348         Raise_DataEvent( self, ITEM_DATA_SYNC );
349 };
350
351 /*
352 ===================
353 Item_DataLink_Nex_MapList_Destroy
354 ===================
355 */
356 void() Item_DataLink_Nex_MapList_Destroy =
357 {
358         String_EntityFree( self, valueList );
359         String_EntityFree( self, cvarName );
360
361         Item_DataLink_Switch_Destroy();
362 };
363
364 /*
365 ===================
366 Item_DataLink_Nex_MapList_Spawn
367 ===================
368 */
369 void() Item_DataLink_Nex_MapList_Spawn =
370 {
371         Item_DataLink_Switch_Init();
372         String_EntityZone( self, valueList );
373         String_EntityZone( self, cvarName );
374
375         if( self.cvarName != "" ) {
376                 registercvar( self.cvarName, "", CVAR_SAVE );
377                 registercvar( strcat( self.cvarName, "_index" ), "", CVAR_SAVE );
378         }
379
380         self.minValue = 1.0;
381         self.maxValue = 1.0;
382         self.stepValue = 0.0;
383
384         self._reinit = IDLNML_Reinit;
385         self._dataEvent = Item_DataLink_Nex_MapList_DataEvent;
386         self._destroy = Item_DataLink_Nex_MapList_Destroy;
387
388         CtCall_Init();
389 };
390
391 /*
392 ===================
393 Item_DataLink_Nex_MapList_InitWithMapList
394 ===================
395 */
396 void() Item_DataLink_Nex_MapList_InitWithMapList =
397 {
398         String_EntitySet( self, valueList, Nex_MapDB_GetPathAltString() );
399         String_EntitySet( self, descList, Nex_MapDB_GetNameAltString() );
400
401         DataLink_Nex_MapList_UpdateRange( self );
402 };
403
404 /*
405 ===================
406 DataLink_Nex_MapList_UpdateRange
407 ===================
408 */
409 void( entity pItem ) DataLink_Nex_MapList_UpdateRange =
410 {
411         pItem.minValue = 1.0;
412         pItem.maxValue = Util_GetAltStringCount( pItem.descList );
413
414         if( pItem.maxValue >= 1 ) {
415                 pItem.stepValue = 1.0;
416         } else {
417                 pItem.maxValue = 1.0;
418                 pItem.stepValue = 0.0;
419         }
420 };
421
422 /*
423 ===================
424 DataLink_Nex_MapList_InsertEntryAfter
425 ===================
426 */
427 void( entity pItem, string pName, string pPath ) DataLink_Nex_MapList_InsertEntryAfter =
428 {
429         local float lIndex;
430
431         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
432         // - 1, because minValue == 1
433         lIndex = pItem._realValue - 1;
434         pItem.descList = Util_InsAltStringItem( pItem.descList, lIndex, pName );
435         pItem.valueList = Util_InsAltStringItem( pItem.valueList, lIndex, pPath );
436
437         DataLink_Nex_MapList_UpdateRange( pItem );
438 };
439
440 /*
441 ===================
442 DataLink_Nex_MapList_DeleteEntry
443 ===================
444 */
445 void( entity pItem ) DataLink_Nex_MapList_DeleteEntry =
446 {
447         local float lIndex;
448
449         if( pItem.stepValue == 0.0 )
450                 return;
451
452         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
453         lIndex = pItem._realValue - 1;
454         pItem.descList = Util_DelAltStringItem( pItem.descList, lIndex );
455         pItem.valueList = Util_DelAltStringItem( pItem.valueList, lIndex );
456
457         DataLink_Nex_MapList_UpdateRange( pItem );
458 };
459
460 /*
461 ===================
462 DataLink_Nex_MapList_MoveEntryUp
463 ===================
464 */
465 void( entity pItem ) DataLink_Nex_MapList_MoveEntryUp =
466 {
467         local string lName;
468         local string lPath;
469         local float lIndexNew;
470         local float lIndexOld;
471
472         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
473         if( pItem._realValue == 1 )
474                 return;
475
476         // map 1..n to 0..n-1
477         lIndexOld = pItem._realValue - 1;
478
479         // we want to insert it up by one, ie. insert it after up by two
480         lIndexNew = lIndexOld - 2;
481
482         lName = Util_GetAltStringItem( pItem.descList, lIndexOld );
483         lPath = Util_GetAltStringItem( pItem.valueList, lIndexOld );
484
485         pItem.descList = Util_InsAltStringItem( pItem.descList, lIndexNew, String_Normal( lName ) );
486         pItem.valueList = Util_InsAltStringItem( pItem.valueList, lIndexNew, String_Normal( lPath ) );
487
488         // adjust lIndexOld for the entry that has been inserted before it
489         lIndexOld = lIndexOld + 1;
490
491         pItem.descList = Util_DelAltStringItem( pItem.descList, lIndexOld );
492         pItem.valueList = Util_DelAltStringItem( pItem.valueList, lIndexOld );
493
494         DataLink_Nex_MapList_UpdateRange( pItem );
495 };
496
497 /*
498 ===================
499 DataLink_Nex_MapList_MoveEntryDown
500 ===================
501 */
502 void( entity pItem ) DataLink_Nex_MapList_MoveEntryDown =
503 {
504         local string lName;
505         local string lPath;
506         local float lIndexNew;
507         local float lIndexOld;
508
509         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
510         if( pItem._realValue == pItem.maxValue )
511                 return;
512
513         // map 1..n to 0..n-1
514         lIndexOld = pItem._realValue - 1;
515
516         // we want to insert it down by one
517         lIndexNew = lIndexOld + 1;
518
519         lName = Util_GetAltStringItem( pItem.descList, lIndexOld );
520         lPath = Util_GetAltStringItem( pItem.valueList, lIndexOld );
521
522         pItem.descList = Util_InsAltStringItem( pItem.descList, lIndexNew, String_Normal( lName ) );
523         pItem.valueList = Util_InsAltStringItem( pItem.valueList, lIndexNew, String_Normal( lPath ) );
524
525         pItem.descList = Util_DelAltStringItem( pItem.descList, lIndexOld );
526         pItem.valueList = Util_DelAltStringItem( pItem.valueList, lIndexOld );
527
528         DataLink_Nex_MapList_UpdateRange( pItem );
529 };
530
531 /*
532 ===================
533 DataLink_Nex_MapList_SetFirst
534 ===================
535 */
536 void( entity pItem, string pName, string pPath ) DataLink_Nex_MapList_SetFirst =
537 {
538         if( pItem.stepValue == 0.0 ) {
539                 pItem.descList = Util_AltStringPush( pItem.descList, pName );
540                 pItem.valueList = Util_AltStringPush( pItem.valueList, pPath );
541         } else {
542                 pItem.descList = Util_SetAltStringItem( pItem.descList, 0, pName );
543                 pItem.valueList = Util_SetAltStringItem( pItem.valueList, 0, pPath );
544         }
545
546         DataLink_Nex_MapList_UpdateRange( pItem );
547 };
548
549 /*
550 ===================
551 DataLink_Nex_MapList_Clear
552 ===================
553 */
554 void( entity pItem ) DataLink_Nex_MapList_Clear =
555 {
556         String_EntitySet( pItem, descList, "" );
557         String_EntitySet( pItem, valueList, "" );
558
559         DataLink_Nex_MapList_UpdateRange( pItem );
560 };
561
562 /*
563 ===================
564 DataLink_Nex_MapList_GetCurrentName
565 ===================
566 */
567 string( entity pItem ) DataLink_Nex_MapList_GetCurrentName =
568 {
569         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
570         return Util_GetAltStringItem( pItem.descList, pItem._realValue - 1 );
571 };
572
573 /*
574 ===================
575 DataLink_Nex_MapList_GetCurrentPath
576 ===================
577 */
578 string( entity pItem ) DataLink_Nex_MapList_GetCurrentPath =
579 {
580         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
581         return Util_GetAltStringItem( pItem.valueList, pItem._realValue - 1 );
582 };