]> icculus.org git repositories - divverent/nexuiz.git/blob - scmenu/source/custom/creategame.qm
-Added maplist support
[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 #define MAPLIST "maplist.cfg"
85
86 void() Nex_WriteCycleList =
87 {
88         local float lHandle;
89         local float lCount;
90         local float lIndex;
91         local entity lCycle;
92
93         lHandle = fopen( MAPLIST, FILE_WRITE );
94         if( lHandle < 0 ) {
95                 print( "menu: Failed to write ", MAPLIST, "!\n" );
96                 return;
97         }
98
99         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
100         lCount = Util_GetAltStringCount( lCycle.valueList );
101         for( lIndex = 0 ; lIndex < lCount ; lIndex++ ) {
102                 fputs( lHandle, String_Normal( Util_GetAltStringItem( lCycle.valueList, lIndex ) ) );
103                 fputs( lHandle, "\n" );
104         }
105
106         fclose( lHandle );
107 };
108
109 void() Nex_ReadCycleList =
110 {
111         local float lHandle;
112         local string lLine;
113         local entity lMapInfo;
114         local entity lCycle;
115
116         lHandle = fopen( MAPLIST, FILE_READ );
117         if( lHandle < 0 ) {
118                 return;
119         }
120
121         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
122
123         lLine = String_Create();
124         while( 1 ) {
125                 lLine = String_Set( lLine, fgets( lHandle ) );
126
127                 if( lLine == "" )
128                         break;
129
130                 lMapInfo = Nex_MapDB_GetByPath( lLine );
131                 if( lMapInfo == null_entity ) {
132                         print( MAPLIST, ": Map '", lLine, "' not found in database - ignored\n" );
133                         continue;
134                 }
135
136                 lCycle.descList = Util_AltStringPushBack( lCycle.descList, lMapInfo.normal );
137                 lCycle.valueList = Util_AltStringPushBack( lCycle.valueList, lMapInfo.link );
138         }
139         String_Free( lLine );
140
141         fclose( lHandle );
142
143         DataLink_Nex_MapList_UpdateRange( lCycle );
144         Nex_MapSelector_Synchronize();
145 };
146
147 // TODO: Make MaxPlayers real Data items (no container with correct set behavior)
148 void() Nex_Action_Map_Start =
149 {
150         local entity lItem;
151         local float  lNumBots, lSkill;
152         // we need to set the following var
153         lItem = Menu_GetItem( "::Data::Server::NumBots" );
154         lNumBots = stof( lItem.value );
155         lItem = Menu_GetItem( "::Data::Server::BotSkill" );
156         lSkill = stof( lItem.value );
157
158         cvar_set( "saved1", "1" );
159         cvar_set( "scratch1", ftos( pow( 2, lNumBots ) - 1 ) );
160         cvar_set( "scratch2", ftos( lSkill * (pow(4, min(lNumBots, 8)) - 1) / 3) );
161         cvar_set( "scratch3", ftos( lSkill * (pow(4, min(lNumBots - 8, 8)) - 1) / 3) );
162         // Set maxplayers
163         lItem = Menu_GetItem( "::Data::Server::MaxPlayers" );
164         cmd( "maxplayers \"" );
165         cmd( lItem.value );
166         cmd( "\"\n" );
167
168         lItem = Menu_GetItem( "::Data::Server::Map::Cycle" );
169         cmd( "map \"" );
170         cmd( String_Normal( Util_GetAltStringItem( lItem.valueList, 0 ) ) );
171         cmd( "\"\n");
172
173         Nex_WriteCycleList();
174
175         // force the menu to hide
176         m_hide();
177 };
178
179 // cycle list edit features
180 void() Nex_Action_Cycle_Add =
181 {
182         local entity lList;
183         local entity lCycle;
184         local string lPath;
185         local string lName;
186
187         lList = Menu_GetItem( "::Data::Server::Map::List" );
188         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
189
190         lName = DataLink_Nex_MapList_GetCurrentName( lList );
191         lPath = DataLink_Nex_MapList_GetCurrentPath( lList );
192
193         DataLink_Nex_MapList_InsertEntryAfter( lCycle, lName, lPath );
194
195         String_Free( lName );
196         String_Free( lPath );
197
198         lCycle._realValue = lCycle._realValue + 1;
199         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
200
201         Nex_MapSelector_Synchronize();
202 };
203
204 void() Nex_Action_Cycle_Remove =
205 {
206         local entity lCycle;
207
208         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
209         DataLink_Nex_MapList_DeleteEntry( lCycle );
210
211         Nex_MapSelector_Synchronize();
212 };
213
214 void() Nex_Action_Cycle_MoveUp =
215 {
216         local entity lCycle;
217
218         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
219         DataLink_Nex_MapList_MoveEntryUp( lCycle );
220
221         lCycle._realValue = lCycle._realValue - 1;
222         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
223
224         Nex_MapSelector_Synchronize();
225 };
226
227 void() Nex_Action_Cycle_MoveDown =
228 {
229         local entity lCycle;
230
231         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
232         DataLink_Nex_MapList_MoveEntryDown( lCycle );
233
234         lCycle._realValue = lCycle._realValue + 1;
235         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
236
237         Nex_MapSelector_Synchronize();
238 };
239
240 void() Nex_Action_Cycle_Clear =
241 {
242         local entity lCycle;
243
244         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
245         DataLink_Nex_MapList_Clear( lCycle );
246
247         Nex_MapSelector_Synchronize();
248 };