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