]> icculus.org git repositories - divverent/nexuiz.git/blob - data/menuqc/custom/creategame/maps.qc
give menu source its own directory
[divverent/nexuiz.git] / data / menuqc / 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 string _Nex_MapList_FilteredFullPath;
48 string _Nex_MapList_FilteredName;
49 string _Nex_MapList_Filter;
50 entity _Nex_MapList_Root;
51
52 /*
53 ===================
54 Item_Nex_MapDB_EnumFiles
55 ===================
56 */
57 // map definition format
58 // name
59 // rest: description text
60 void() Item_Nex_MapDB_EnumFiles =
61 {
62         local float lSearchHandle;
63         local float lSearchSize;
64         local float lSearchCounter;
65         local entity lMap;
66
67         lSearchHandle = search_begin( "maps/*.mapcfg", true, true );
68         if( lSearchHandle < 0 )
69                 return;
70         for( lSearchSize = search_getsize( lSearchHandle ), lSearchCounter = 0;
71         lSearchCounter < lSearchSize; ++lSearchCounter ) {
72                 local float     lHandle;
73
74                 local string    lFilename;
75                 local string    lStripped;
76                 local string    lPic;
77                 local string    lName;
78                 local string    lDescription;
79                 local string    lTitle;
80
81                 lFilename = search_getfilename( lSearchHandle, lSearchCounter );
82                 lStripped = String_Zone( substring( lFilename, 0, strlen( lFilename ) - 7 ) );
83                 lName = String_Zone( substring( lStripped, 5, 100000 ) );
84
85                 lHandle = fopen( strcat( lStripped, ".txt" ), FILE_READ );
86                 if( lHandle < 0 ) {
87                         lTitle = String_Zone( lName );
88                         lDescription = String_Zone( strcat( "--NO INFORMATION AVAILABLE--\n", lFilename ) );
89                 } else {
90                         // uncomment this line and comment the other if you have the old map info files
91                         //lTitle = String_Zone( lName );
92                         lTitle = String_Zone( fgets( lHandle ) );
93                         lPic = fgets( lHandle ); // also get picture
94                         if(lPic)
95                         {
96                                 lStripped = String_Zone( lPic );
97                         }
98
99                         lDescription = String_Create();
100                         do {
101                                 local string lLine;
102
103                                 lLine = fgets( lHandle );
104                                 lDescription = String_Append( lDescription, strcat( lLine, "\n" ) );
105                         } while( validstring( lLine ) );
106
107                         fclose( lHandle );
108                 }
109
110                 // save this in a new Map_Info
111                 lMap = Menu_CreateItem( "Item_Nex_Map_Info", ftos( lSearchCounter ), self.name );
112
113                 lMap.link = lName;
114                 // check the picture
115                 if( Gfx_Precache( lStripped ) ) {
116                         lMap.picture = lStripped;
117                 } else {
118                         lMap.picture = "gfx/m_nomap";
119                 }
120                 lMap.normal = lTitle;
121                 lMap.text = lDescription;
122
123                 _Nex_MapList_FullPath = Util_AltStringPushBack( _Nex_MapList_FullPath, lName );
124                 _Nex_MapList_Name = Util_AltStringPushBack( _Nex_MapList_Name, lTitle );
125
126                 Menu_LinkItem( lMap );
127
128                 String_Free( lTitle );
129                 String_Free( lName );
130                 String_Free( lStripped );
131                 String_Free( lDescription );
132         }
133
134         search_end( lSearchHandle );
135
136         _Nex_MapList_FilteredFullPath = String_Set( _Nex_MapList_FilteredFullPath, _Nex_MapList_FullPath );
137         _Nex_MapList_FilteredName = String_Set( _Nex_MapList_FilteredName, _Nex_MapList_Name );
138
139         Menu_LinkChildren( self );
140
141         self.minValue = 1;
142         self.stepValue = 1;
143         self.maxValue = fabs( lMap.orderPos );
144 };
145
146 /*
147 ===================
148 Item_Nex_MapDB_Destroy
149 ===================
150 */
151 void() Item_Nex_MapDB_Destroy =
152 {
153         String_Free( _Nex_MapList_FullPath );
154         String_Free( _Nex_MapList_Name );
155         String_Free( _Nex_MapList_FilteredFullPath );
156         String_Free( _Nex_MapList_FilteredName );
157         String_Free( _Nex_MapList_Filter );
158
159         _Nex_MapList_Root = null_entity;
160 };
161
162 /*
163 ===================
164 Item_Nex_MapDB_Spawn
165 ===================
166 */
167 void() Item_Nex_MapDB_Spawn =
168 {
169         if( _Nex_MapList_Root )
170                 error( "There is already another Item_Nex_MapDB object!" );
171
172         _Nex_MapList_FullPath = String_Create();
173         _Nex_MapList_Name = String_Create();
174         _Nex_MapList_FilteredFullPath = String_Create();
175         _Nex_MapList_FilteredName = String_Create();
176         _Nex_MapList_Filter = String_Create();
177         _Nex_MapList_Root = self;
178
179         Item_Nex_MapDB_EnumFiles();
180
181         self._destroy = Item_Nex_MapDB_Destroy;
182 };
183
184 /*
185 ===================
186 Nex_MapDB_GetByName
187 ===================
188 */
189 entity( string pPath ) Nex_MapDB_GetByPath =
190 {
191         local entity lNode;
192
193         if( !_Nex_MapList_Root )
194                 error( "No Item_Nex_MapDB found!" );
195
196         for( lNode = _Nex_MapList_Root._child ; lNode ; lNode = lNode._next ) {
197                 if( lNode.link == pPath ) {
198                         return lNode;
199                 }
200         }
201
202         return null_entity;
203 };
204
205 /*
206 ===================
207 Nex_MapDB_GetFilteredByIndex
208 ===================
209 */
210 entity( float pIndex ) Nex_MapDB_GetFilteredByIndex =
211 {
212         local entity lNode;
213         local float lFilteredIndex;
214
215         if( !_Nex_MapList_Root )
216                 error( "No Item_Nex_MapDB found!" );
217
218         pIndex = fabs( pIndex );
219         lFilteredIndex = 0;
220         for( lNode = _Nex_MapList_Root._child ; lNode ; lNode = lNode._next ) {
221                 if( substring( lNode.link, 0, strlen( _Nex_MapList_Filter ) ) == _Nex_MapList_Filter ) {
222                         ++lFilteredIndex;
223                 }
224                 if( pIndex == lFilteredIndex ) {
225                         return lNode;
226                 }
227         }
228
229         return null_entity;
230 };
231
232 /*
233 ===================
234 Nex_MapDB_GetByIndex
235 ===================
236 *//*
237 entity( float pIndex ) Nex_MapDB_GetByIndex =
238 {
239         local entity lNode;
240
241         if( !_Nex_MapList_Root )
242                 error( "No Item_Nex_MapDB found!" );
243
244         pIndex = fabs( pIndex );
245         for( lNode = _Nex_MapList_Root._child ; lNode ; lNode = lNode._next ) {
246                 if( fabs( lNode.orderPos ) == pIndex ) {
247                         return lNode;
248                 }
249         }
250
251         return null_entity;
252 };*/
253
254 /*
255 ===================
256 Nex_MapDB_FilterMaps
257 ===================
258 */
259 void( string pPrefix ) Nex_MapDB_FilterMaps =
260 {
261         local float lPrefixLength;
262         local float lCounter;
263         local float lSize;
264
265         _Nex_MapList_Filter = String_Set( _Nex_MapList_Filter, pPrefix );
266
267         _Nex_MapList_FilteredFullPath = String_Set( _Nex_MapList_FilteredFullPath, "" );
268         _Nex_MapList_FilteredName = String_Set( _Nex_MapList_FilteredName, "" );
269
270         lPrefixLength = strlen( pPrefix );
271         lSize = Util_GetAltStringCount( _Nex_MapList_FullPath );
272         for( lCounter = 0 ; lCounter < lSize ; ++lCounter ) {
273                 local string lPath;
274
275                 lPath = Util_GetAltStringItem( _Nex_MapList_FullPath, lCounter );
276                 if( substring( lPath, 0, lPrefixLength ) == pPrefix ) {
277                         local string lName;
278                         lName = Util_GetAltStringItem( _Nex_MapList_Name, lCounter );
279
280                         _Nex_MapList_FilteredFullPath = Util_AltStringPushBack( _Nex_MapList_FilteredFullPath, lPath );
281                         _Nex_MapList_FilteredName = Util_AltStringPushBack( _Nex_MapList_FilteredName, lName );
282
283                         String_Free( lName );
284                 }
285
286                 String_Free( lPath );
287         }
288 };
289
290 /*
291 ===================
292 Nex_MapDB_GetFilteredPathAltString
293 ===================
294 */
295 string() Nex_MapDB_GetFilteredPathAltString =
296 {
297         return _Nex_MapList_FilteredFullPath;
298 }
299
300 /*
301 ===================
302 Nex_MapDB_GetFilteredNameAltString
303 ===================
304 */
305 string() Nex_MapDB_GetFilteredNameAltString =
306 {
307         return _Nex_MapList_FilteredName;
308 };
309
310 /*
311 ===================
312 Nex_MapDB_GetPathAltString
313 ===================
314 */
315 string() Nex_MapDB_GetPathAltString =
316 {
317         return _Nex_MapList_FullPath;
318 };
319
320 /*
321 ===================
322 Nex_MapDB_GetNameAltString
323 ===================
324 */
325 string() Nex_MapDB_GetNameAltString =
326 {
327         return _Nex_MapList_Name;
328 };
329
330 /*
331 ===================
332 Nex_MapDB_GetFilteredIndex
333 ===================
334 */
335 float( entity pItem ) Nex_MapDB_GetFilteredIndex =
336 {
337         local float lIndex;
338         local float lCount;
339
340         lCount = Util_GetAltStringCount( _Nex_MapList_FilteredFullPath );
341         for( lIndex = 0 ; lIndex < lCount ; ++lIndex ) {
342                 local string lPath;
343                 lPath = String_Normal( Util_GetAltStringItem( _Nex_MapList_FilteredFullPath, lIndex ) );
344                 if( lPath == pItem.link ) {
345                         return lIndex + 1;
346                 }
347         }
348         return 1;
349 };
350
351 /*
352 ===================
353 Item_DataLink_Nex_MapList_Save
354 ===================
355 */
356 void() Item_DataLink_Nex_MapList_Save =
357 {
358         local float lCount;
359         local float lIndex;
360         local string lOutput;
361
362         lOutput = String_Create();
363
364         lCount = Util_GetAltStringCount( self.valueList );
365         for( lIndex = 0 ; lIndex < lCount ; lIndex++ ) {
366                 lOutput = String_Append( lOutput, strcat( "\'", String_Normal( Util_GetAltStringItem( self.valueList, lIndex ) ), "\'" ) );
367         }
368
369         cvar_set( self.cvarName, lOutput );
370         cvar_set( strcat( self.cvarName, "_index" ), "0" );
371
372         String_Free( lOutput );
373 };
374
375 /*
376 ===================
377 Item_DataLink_Nex_MapList_Load
378 ===================
379 */
380 void() Item_DataLink_Nex_MapList_Load =
381 {
382         local string lEntry;
383         local entity lMapInfo;
384         local float lCounter;
385         local float lCount;
386
387         DataLink_Nex_MapList_Clear( self );
388
389         lCount = tokenize( cvar_string( self.cvarName ) );
390
391         lEntry = String_Create();
392         for( lCounter = 0 ; lCounter < lCount ; lCounter++ ) {
393                 lEntry = String_Set( lEntry, argv( lCounter ) );
394
395                 lMapInfo = Nex_MapDB_GetByPath( lEntry );
396                 if( lMapInfo == null_entity ) {
397                         print( self.cvarName, ": Map '", lEntry, "' not found in database - ignored\n" );
398                         continue;
399                 }
400
401                 self.descList = Util_AltStringPushBack( self.descList, lMapInfo.normal );
402                 self.valueList = Util_AltStringPushBack( self.valueList, lMapInfo.link );
403         }
404         String_Free( lEntry );
405
406         DataLink_Nex_MapList_UpdateRange( self );
407         Nex_MapSelector_Synchronize();
408 };
409
410 /*
411 ===================
412 Item_DataLink_Nex_MapList_ExecString
413 ===================
414 */
415 void() Item_DataLink_Nex_MapList_ExecString =
416 {
417         local float lCount;
418         local float lIndex;
419
420         Data_ExecString = String_Append( Data_ExecString, strcat( "set \"", self.cvarName, "_index\" \"0\"\nset \"", self.cvarName, "\" \"" ) );
421
422         lCount = Util_GetAltStringCount( self.valueList );
423         for( lIndex = 0 ; lIndex < lCount ; lIndex++ ) {
424                 Data_ExecString = String_Append( Data_ExecString, strcat( "\'", String_Normal( Util_GetAltStringItem( self.valueList, lIndex ) ), "\'" ) );
425         }
426
427         Data_ExecString = String_Append( Data_ExecString, "\"\n" );
428 };
429
430 /*
431 ===================
432 Item_DataLink_Nex_MapList_DataEvent
433 ===================
434 */
435 void( float pEvent ) Item_DataLink_Nex_MapList_DataEvent =
436 {
437         switch( pEvent ) {
438         case ITEM_DATA_SYNC:
439                 if( self.cvarName != "" ) {
440                         Item_DataLink_Nex_MapList_Load();
441                 }
442                 break;
443         case ITEM_DATA_SEND:
444                 if( self.cvarName != "" ) {
445                         Item_DataLink_Nex_MapList_Save();
446                 }
447                 break;
448         case ITEM_DATA_RESET:
449                 self._realValue = 1.0;
450                 break;
451         case ITEM_DATA_TEST_START:
452                 break;
453         case ITEM_DATA_TEST_END:
454                 break;
455         case ITEM_DATA_SAVE_EXECSTRING:
456                 if( self.cvarName != "" ) {
457                         Item_DataLink_Nex_MapList_ExecString();
458                 }
459                 break;
460         case ITEM_DATALINK_SET:
461         case ITEM_DATALINK_GET:
462                 self._realValue = floor( self._realValue );
463                 if( self._realValue < 1.0 ) {
464                         self._realValue = 1.0;
465                 } else if( self._realValue > self.maxValue ) {
466                         self._realValue = self.maxValue;
467                 }
468
469                 break;
470         }
471 };
472
473 /*
474 ===================
475 IDLNML_Reinit
476 ===================
477 */
478 void() IDLNML_Reinit =
479 {
480         Raise_DataEvent( self, ITEM_DATA_SYNC );
481 };
482
483 /*
484 ===================
485 Item_DataLink_Nex_MapList_Destroy
486 ===================
487 */
488 void() Item_DataLink_Nex_MapList_Destroy =
489 {
490         String_EntityFree( self, valueList );
491         String_EntityFree( self, cvarName );
492
493         Item_DataLink_Switch_Destroy();
494 };
495
496 /*
497 ===================
498 Item_DataLink_Nex_MapList_Spawn
499 ===================
500 */
501 void() Item_DataLink_Nex_MapList_Spawn =
502 {
503         Item_DataLink_Switch_Init();
504         String_EntityZone( self, valueList );
505         String_EntityZone( self, cvarName );
506
507         if( self.cvarName != "" ) {
508                 registercvar( self.cvarName, "", CVAR_SAVE );
509                 registercvar( strcat( self.cvarName, "_index" ), "", CVAR_SAVE );
510         }
511
512         self.minValue = 1.0;
513         self.maxValue = 1.0;
514         self.stepValue = 0.0;
515
516         self._reinit = IDLNML_Reinit;
517         self._dataEvent = Item_DataLink_Nex_MapList_DataEvent;
518         self._destroy = Item_DataLink_Nex_MapList_Destroy;
519
520         CtCall_Init();
521 };
522
523 /*
524 ===================
525 Item_DataLink_Nex_MapList_InitWithMapList
526 ===================
527 */
528 void() Item_DataLink_Nex_MapList_InitWithMapList =
529 {
530         String_EntitySet( self, valueList, Nex_MapDB_GetFilteredPathAltString() );
531         String_EntitySet( self, descList, Nex_MapDB_GetFilteredNameAltString() );
532
533         self._realValue = 1.0;
534         DataLink_Nex_MapList_UpdateRange( self );
535 };
536
537 /*
538 ===================
539 DataLink_Nex_MapList_UpdateRange
540 ===================
541 */
542 void( entity pItem ) DataLink_Nex_MapList_UpdateRange =
543 {
544         pItem.minValue = 1.0;
545         pItem.maxValue = Util_GetAltStringCount( pItem.descList );
546
547         if( pItem.maxValue >= 1 ) {
548                 pItem.stepValue = 1.0;
549         } else {
550                 pItem.maxValue = 1.0;
551                 pItem.stepValue = 0.0;
552         }
553 };
554
555 /*
556 ===================
557 DataLink_Nex_MapList_InsertEntryAfter
558 ===================
559 */
560 void( entity pItem, string pName, string pPath ) DataLink_Nex_MapList_InsertEntryAfter =
561 {
562         local float lIndex;
563
564         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
565         // - 1, because minValue == 1
566         lIndex = pItem._realValue - 1;
567         pItem.descList = Util_InsAltStringItem( pItem.descList, lIndex, pName );
568         pItem.valueList = Util_InsAltStringItem( pItem.valueList, lIndex, pPath );
569
570         DataLink_Nex_MapList_UpdateRange( pItem );
571 };
572
573 /*
574 ===================
575 DataLink_Nex_MapList_DeleteEntry
576 ===================
577 */
578 void( entity pItem ) DataLink_Nex_MapList_DeleteEntry =
579 {
580         local float lIndex;
581
582         if( pItem.stepValue == 0.0 )
583                 return;
584
585         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
586         lIndex = pItem._realValue - 1;
587         pItem.descList = Util_DelAltStringItem( pItem.descList, lIndex );
588         pItem.valueList = Util_DelAltStringItem( pItem.valueList, lIndex );
589
590         DataLink_Nex_MapList_UpdateRange( pItem );
591 };
592
593 /*
594 ===================
595 DataLink_Nex_MapList_MoveEntryUp
596 ===================
597 */
598 void( entity pItem ) DataLink_Nex_MapList_MoveEntryUp =
599 {
600         local string lName;
601         local string lPath;
602         local float lIndexNew;
603         local float lIndexOld;
604
605         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
606         if( pItem._realValue == 1 )
607                 return;
608
609         // map 1..n to 0..n-1
610         lIndexOld = pItem._realValue - 1;
611
612         // we want to insert it up by one, ie. insert it after up by two
613         lIndexNew = lIndexOld - 2;
614
615         lName = Util_GetAltStringItem( pItem.descList, lIndexOld );
616         lPath = Util_GetAltStringItem( pItem.valueList, lIndexOld );
617
618         pItem.descList = Util_InsAltStringItem( pItem.descList, lIndexNew, String_Normal( lName ) );
619         pItem.valueList = Util_InsAltStringItem( pItem.valueList, lIndexNew, String_Normal( lPath ) );
620
621         // adjust lIndexOld for the entry that has been inserted before it
622         lIndexOld = lIndexOld + 1;
623
624         pItem.descList = Util_DelAltStringItem( pItem.descList, lIndexOld );
625         pItem.valueList = Util_DelAltStringItem( pItem.valueList, lIndexOld );
626
627         DataLink_Nex_MapList_UpdateRange( pItem );
628 };
629
630 /*
631 ===================
632 DataLink_Nex_MapList_MoveEntryDown
633 ===================
634 */
635 void( entity pItem ) DataLink_Nex_MapList_MoveEntryDown =
636 {
637         local string lName;
638         local string lPath;
639         local float lIndexNew;
640         local float lIndexOld;
641
642         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
643         if( pItem._realValue == pItem.maxValue )
644                 return;
645
646         // map 1..n to 0..n-1
647         lIndexOld = pItem._realValue - 1;
648
649         // we want to insert it down by one
650         lIndexNew = lIndexOld + 1;
651
652         lName = Util_GetAltStringItem( pItem.descList, lIndexOld );
653         lPath = Util_GetAltStringItem( pItem.valueList, lIndexOld );
654
655         pItem.descList = Util_InsAltStringItem( pItem.descList, lIndexNew, String_Normal( lName ) );
656         pItem.valueList = Util_InsAltStringItem( pItem.valueList, lIndexNew, String_Normal( lPath ) );
657
658         pItem.descList = Util_DelAltStringItem( pItem.descList, lIndexOld );
659         pItem.valueList = Util_DelAltStringItem( pItem.valueList, lIndexOld );
660
661         DataLink_Nex_MapList_UpdateRange( pItem );
662 };
663
664 /*
665 ===================
666 DataLink_Nex_MapList_SetFirst
667 ===================
668 */
669 void( entity pItem, string pName, string pPath ) DataLink_Nex_MapList_SetFirst =
670 {
671         if( pItem.stepValue == 0.0 ) {
672                 pItem.descList = Util_AltStringPush( pItem.descList, pName );
673                 pItem.valueList = Util_AltStringPush( pItem.valueList, pPath );
674         } else {
675                 pItem.descList = Util_SetAltStringItem( pItem.descList, 0, pName );
676                 pItem.valueList = Util_SetAltStringItem( pItem.valueList, 0, pPath );
677         }
678
679         DataLink_Nex_MapList_UpdateRange( pItem );
680 };
681
682 /*
683 ===================
684 DataLink_Nex_MapList_Clear
685 ===================
686 */
687 void( entity pItem ) DataLink_Nex_MapList_Clear =
688 {
689         String_EntitySet( pItem, descList, "" );
690         String_EntitySet( pItem, valueList, "" );
691
692         DataLink_Nex_MapList_UpdateRange( pItem );
693 };
694
695 /*
696 ===================
697 DataLink_Nex_MapList_GetCurrentName
698 ===================
699 */
700 string( entity pItem ) DataLink_Nex_MapList_GetCurrentName =
701 {
702         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
703         return Util_GetAltStringItem( pItem.descList, pItem._realValue - 1 );
704 };
705
706 /*
707 ===================
708 DataLink_Nex_MapList_GetCurrentPath
709 ===================
710 */
711 string( entity pItem ) DataLink_Nex_MapList_GetCurrentPath =
712 {
713         Raise_DataEvent( pItem, ITEM_DATALINK_GET );
714         return Util_GetAltStringItem( pItem.valueList, pItem._realValue - 1 );
715 };