]> icculus.org git repositories - divverent/nexuiz.git/blob - data/source/custom/player/avatar.qc
give menu source its own directory
[divverent/nexuiz.git] / data / source / custom / player / avatar.qc
1 // Property of Alientrap/AK
2 // custom/player/avatar.qm
3
4 /*
5 ===================
6 Item_Nex_Avatar_Info
7 ===================
8 */
9
10 void() Item_Nex_Avatar_Info_Destroy =
11 {
12         String_EntityFree( self, target );
13         String_EntityFree( self, link );
14         String_EntityFree( self, picture );
15         String_EntityFree( self, text );
16         String_EntityFree( self, normal );
17 };
18
19 void() Item_Nex_Avatar_Info_Spawn =
20 {
21         String_EntityZone( self, target );
22         String_EntityZone( self, link );
23         String_EntityZone( self, picture );
24         String_EntityZone( self, text );
25         String_EntityZone( self, normal );
26
27         Gfx_Precache( self.picture );
28
29         self._destroy = Item_Nex_Avatar_Info_Destroy;
30 };
31
32 /*
33 ===================
34 Item_Data_Nex_Avatar
35 ===================
36 */
37
38
39 // Nex_Action_Player_BuildAvatarList
40 // model definition format
41 // name
42 // picture\n
43 // skin filename\n
44 // model filename\n
45 // rest: description text
46 void() _IDNA_BuildList =
47 {
48         local float     lSearchHandle;
49         local float     lSearchSize;
50         local float     lSearchCounter;
51         local entity    lAvatar;
52
53         Menu_EmptyWindow( self );
54
55         // legacy mode
56         // TODO:
57         // FIXME: only accept *.mdef later
58         lSearchHandle = search_begin( "models/player/*.txt", true, true );
59         if( lSearchHandle < 0 )
60                 return;
61         for( lSearchSize = search_getsize( lSearchHandle ), lSearchCounter = 0;
62         lSearchCounter < lSearchSize; ++lSearchCounter ) {
63                 local string    lFilename;
64                 local float     lHandle;
65
66                 local string    lName;
67                 local string    lPicture;
68                 local string    lSkin;
69                 local string    lModel;
70                 local string    lDescription;
71
72                 lFilename = search_getfilename( lSearchHandle, lSearchCounter );
73                 lHandle = fopen( lFilename, FILE_READ );
74                 if( lHandle < 0 ) {
75                         print( "Menu: Couldn't open model definition file '", lFilename, "'\n" );
76                         continue;
77                 }
78
79                 lName = String_Zone( fgets( lHandle ) );
80                 lPicture = String_Zone(  fgets( lHandle ) );
81                 lSkin = String_Zone( fgets( lHandle ) );
82                 lModel = String_Zone( fgets( lHandle ) );
83                 if( !lName || !lPicture || !lSkin || !lModel ) {
84                         String_Free( lName );
85                         String_Free( lPicture );
86                         String_Free( lSkin );
87                         String_Free( lModel );
88                         print( "Menu: Couldn't parse model definition file '",
89                          search_getfilename( lSearchHandle, lSearchCounter ), "'\n" );
90                         fclose( lHandle );
91                         continue;
92                 }
93                 lDescription = String_Create();
94                 do {
95                         local string lLine;
96
97                         lLine = fgets( lHandle );
98                         lDescription = String_Append( lDescription, strcat( lLine, "\n" ) );
99                 } while( validstring( lLine ) );
100                 if( lDescription ) { // add this avatar_info
101                         lAvatar = Menu_CreateItem( "Item_Nex_Avatar_Info", ftos( lSearchCounter ), self.name );
102
103                         lAvatar.target = lModel;
104                         lAvatar.link = lSkin;
105                         lAvatar.picture = lPicture;
106                         lAvatar.normal = lName;
107                         lAvatar.text = lDescription;
108
109                         Menu_LinkItem( lAvatar );
110                 }
111                 String_Free( lName );
112                 String_Free( lPicture );
113                 String_Free( lSkin );
114                 String_Free( lModel );
115                 String_Free( lDescription );
116                 fclose( lHandle );
117         }
118
119         search_end( lSearchHandle );
120
121         Menu_LinkChildren( self );
122
123         self.minValue = 1;
124         self.stepValue = 1;
125         self.maxValue = fabs( lAvatar.orderPos );
126 };
127
128 void() _IDNA_Sync =
129 {
130         local string lModel, lSkin;
131         local entity lMatch;
132
133         lModel = String_Zone( cvar_string( "_cl_playermodel" ) );
134         lSkin = String_Zone( cvar_string( "_cl_playerskin" ) );
135
136         for( lMatch = self._child ; lMatch._next ; lMatch = lMatch._next )
137                 if( lMatch.target == lModel && lMatch.link == lSkin )
138                         break;
139
140         if( lMatch ) {
141                 self._link = lMatch;
142                 self._realValue = fabs( lMatch.orderPos );
143                 String_EntitySet( self, value, ftos( self._realValue ) );
144                 String_EntitySet( self, _syncValue, self.value );
145         }
146
147         String_Free( lModel );
148         String_Free( lSkin );
149 };
150
151 void() _IDNA_UpdateLink =
152 {
153         local float lCurrent;
154         local float lTarget;
155         local entity lMatch;
156
157         lCurrent = fabs( self._link.orderPos );
158         lTarget = self._realValue;
159         if( lCurrent < lTarget )
160                 for( lMatch = self._link; lMatch._next && fabs( lMatch.orderPos ) != lTarget; lMatch = lMatch._next );
161         else
162                 for( lMatch = self._link; lMatch._prev && fabs( lMatch.orderPos ) != lTarget; lMatch = lMatch._prev );
163         // lMatch always is valid if there are any children
164         self._link = lMatch;
165
166         self._realValue = fabs( self._link.orderPos );
167         String_EntitySet( self, value, ftos( self._realValue ) );
168 };
169
170 void() _IDNA_RawSet =
171 {
172         _IDNA_UpdateLink();
173
174         cmd( strcat( "playermodel \"", self._link.target, "\";" ) );
175         cmd( strcat( "playerskin \"", self._link.link, "\"\n" ) );
176 };
177
178 void() _IDNA_Send =
179 {
180         _IDNA_RawSet();
181
182         String_EntitySet( self, _syncValue, self.value );
183 };
184
185 void() _IDNA_Test_Start =
186 {
187         _IDNA_RawSet();
188 };
189
190 void() _IDNA_Test_End =
191 {
192         String_EntitySet( self, value, self._syncValue );
193         _IDNA_RawSet();
194 };
195
196 void() _IDNA_Reset =
197 {
198         String_EntitySet( self, value, self.defValue );
199         _IDNA_Send();
200 };
201
202 void( float pEvent ) Item_Data_Nex_Avatar_DataEvent =
203 {
204         switch( pEvent ) {
205         case ITEM_DATA_SYNC:
206                 _IDNA_Sync();
207                 break;
208         case ITEM_DATA_SEND:
209                 _IDNA_Send();
210                 break;
211         case ITEM_DATA_RESET:
212                 _IDNA_Reset();
213                 break;
214         case ITEM_DATA_TEST_START:
215                 _IDNA_Test_Start();
216                 break;
217         case ITEM_DATA_TEST_END:
218                 _IDNA_Test_End();
219                 break;
220         case ITEM_DATALINK_SET:
221                 _IDNA_UpdateLink();
222                 break;
223         }
224 };
225
226 void() Item_Data_Nex_Avatar_Spawn =
227 {
228         Item_Data_Init();
229
230         self.flag = self.flag | FLAG_HIDDEN;
231
232         self._reinit = _IDNA_Sync;
233         self._dataEvent = Item_Data_Nex_Avatar_DataEvent;
234
235         _IDNA_BuildList();
236 };