]> icculus.org git repositories - divverent/nexuiz.git/blob - scmenu/source/custom/creategame.qm
-MapList should now be refreshed every time the menu is reopened.
[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         DataLink_Nex_MapList_Clear( lCycle );
123
124         lLine = String_Create();
125         while( 1 ) {
126                 lLine = String_Set( lLine, fgets( lHandle ) );
127
128                 if( lLine == "" )
129                         break;
130
131                 lMapInfo = Nex_MapDB_GetByPath( lLine );
132                 if( lMapInfo == null_entity ) {
133                         print( MAPLIST, ": Map '", lLine, "' not found in database - ignored\n" );
134                         continue;
135                 }
136
137                 lCycle.descList = Util_AltStringPushBack( lCycle.descList, lMapInfo.normal );
138                 lCycle.valueList = Util_AltStringPushBack( lCycle.valueList, lMapInfo.link );
139         }
140         String_Free( lLine );
141
142         fclose( lHandle );
143
144         DataLink_Nex_MapList_UpdateRange( lCycle );
145         Nex_MapSelector_Synchronize();
146 };
147
148 // TODO: Make MaxPlayers real Data items (no container with correct set behavior)
149 void() Nex_Action_Map_Start =
150 {
151         local entity lItem;
152         local float  lNumBots, lSkill;
153         // we need to set the following var
154         lItem = Menu_GetItem( "::Data::Server::NumBots" );
155         lNumBots = stof( lItem.value );
156         lItem = Menu_GetItem( "::Data::Server::BotSkill" );
157         lSkill = stof( lItem.value );
158
159         cvar_set( "saved1", "1" );
160         cvar_set( "scratch1", ftos( pow( 2, lNumBots ) - 1 ) );
161         cvar_set( "scratch2", ftos( lSkill * (pow(4, min(lNumBots, 8)) - 1) / 3) );
162         cvar_set( "scratch3", ftos( lSkill * (pow(4, min(lNumBots - 8, 8)) - 1) / 3) );
163         // Set maxplayers
164         lItem = Menu_GetItem( "::Data::Server::MaxPlayers" );
165         cmd( "maxplayers \"" );
166         cmd( lItem.value );
167         cmd( "\"\n" );
168
169         lItem = Menu_GetItem( "::Data::Server::Map::Cycle" );
170         cmd( "map \"" );
171         cmd( String_Normal( Util_GetAltStringItem( lItem.valueList, 0 ) ) );
172         cmd( "\"\n");
173
174         Nex_WriteCycleList();
175
176         // force the menu to hide
177         m_hide();
178 };
179
180 // cycle list edit features
181 void() Nex_Action_Cycle_Add =
182 {
183         local entity lList;
184         local entity lCycle;
185         local string lPath;
186         local string lName;
187
188         lList = Menu_GetItem( "::Data::Server::Map::List" );
189         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
190
191         lName = DataLink_Nex_MapList_GetCurrentName( lList );
192         lPath = DataLink_Nex_MapList_GetCurrentPath( lList );
193
194         if( lCycle.stepValue == 1.0 ) {
195                 DataLink_Nex_MapList_InsertEntryAfter( lCycle, lName, lPath );
196         } else {
197                 DataLink_Nex_MapList_SetFirst( lCycle, lName, lPath );
198         }
199
200         String_Free( lName );
201         String_Free( lPath );
202
203         lCycle._realValue = lCycle._realValue + 1;
204         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
205
206         Nex_MapSelector_Synchronize();
207 };
208
209 void() Nex_Action_Cycle_Remove =
210 {
211         local entity lCycle;
212
213         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
214         DataLink_Nex_MapList_DeleteEntry( lCycle );
215
216         Nex_MapSelector_Synchronize();
217 };
218
219 void() Nex_Action_Cycle_MoveUp =
220 {
221         local entity lCycle;
222
223         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
224         DataLink_Nex_MapList_MoveEntryUp( lCycle );
225
226         lCycle._realValue = lCycle._realValue - 1;
227         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
228
229         Nex_MapSelector_Synchronize();
230 };
231
232 void() Nex_Action_Cycle_MoveDown =
233 {
234         local entity lCycle;
235
236         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
237         DataLink_Nex_MapList_MoveEntryDown( lCycle );
238
239         lCycle._realValue = lCycle._realValue + 1;
240         Raise_DataEvent( lCycle, ITEM_DATALINK_SET );
241
242         Nex_MapSelector_Synchronize();
243 };
244
245 void() Nex_Action_Cycle_Clear =
246 {
247         local entity lCycle;
248
249         lCycle = Menu_GetItem( "::Data::Server::Map::Cycle" );
250         DataLink_Nex_MapList_Clear( lCycle );
251
252         Nex_MapSelector_Synchronize();
253 };