]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/sv_minigames.qc
Replace all direct assignments to self with setself(e)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / sv_minigames.qc
1 #include "minigames.qh"
2
3 void player_clear_minigame(entity player)
4 {
5         player.active_minigame = world;
6         player.minigame_players = world;
7         if ( IS_PLAYER(player) )
8                 player.movetype = MOVETYPE_WALK;
9         else
10                 player.movetype = MOVETYPE_FLY_WORLDONLY;
11         player.team_forced = 0;
12 }
13
14 void minigame_rmplayer(entity minigame_session, entity player)
15 {
16         entity e;
17         entity p = minigame_session.minigame_players;
18         
19         if ( p.minigame_players == player )
20         {
21                 if ( p.list_next == world )
22                 {
23                         end_minigame(minigame_session);
24                         return;
25                 }
26                 minigame_session.minigame_event(minigame_session,"part",player);
27                 GameLogEcho(strcat(":minigame:part:",minigame_session.netname,":",
28                         ftos(num_for_edict(player)),":",player.netname));
29                 minigame_session.minigame_players = p.list_next;
30                 remove ( p );
31                 player_clear_minigame(player);
32         }
33         else
34         {
35                 for ( e = p.list_next; e != world; e = e.list_next )
36                 {
37                         if ( e.minigame_players == player )
38                         {
39                                 minigame_session.minigame_event(minigame_session,"part",player);
40                                 GameLogEcho(strcat(":minigame:part:",minigame_session.netname,":",
41                                         ftos(num_for_edict(player)),":",player.netname));
42                                 p.list_next = e.list_next;
43                                 remove(e);
44                                 player_clear_minigame(player);
45                                 return;
46                         }
47                         p = e;
48                 }
49         }
50 }
51
52
53 #define FIELD(Flags, Type,Name) if ( sf & (Flags) ) Write##Type(MSG_ENTITY, self.Name);
54 #define WriteVector(to,Name) WriteCoord(to,Name##_x); WriteCoord(to,Name##_y); WriteCoord(to,Name##_z)
55 #define WriteVector2D(to,Name) WriteCoord(to,Name##_x); WriteCoord(to,Name##_y)
56 #define MSLE(Name,Fields) \
57         else if ( self.classname == #Name ) { \
58                 if ( sf & MINIG_SF_CREATE ) WriteString(MSG_ENTITY,self.owner.netname); \
59                 Fields }
60
61 // Send an entity to a client
62 // only use on minigame entities or entities with a minigame owner
63 bool minigame_SendEntity(entity to, int sf)
64 {SELFPARAM();
65         WriteByte(MSG_ENTITY, ENT_CLIENT_MINIGAME);
66         WriteByte(MSG_ENTITY, sf);
67         
68         if ( sf & MINIG_SF_CREATE )
69         {
70                 WriteShort(MSG_ENTITY,msle_id(self.classname));
71                 WriteString(MSG_ENTITY,self.netname);
72         }
73         
74         entity minigame_ent = self.owner;
75         
76         if ( self.classname == "minigame" )
77         {
78                 minigame_ent = self;
79                 
80                 if ( sf & MINIG_SF_CREATE )
81                         WriteString(MSG_ENTITY,self.descriptor.netname);
82                 
83                 if ( sf & MINIG_SF_UPDATE )
84                         WriteLong(MSG_ENTITY,self.minigame_flags);
85         }
86         else if ( self.classname == "minigame_player" )
87         {
88                 if ( sf & MINIG_SF_CREATE )
89                 {
90                         WriteString(MSG_ENTITY,self.owner.netname);
91                         WriteLong(MSG_ENTITY,num_for_edict(self.minigame_players));
92                 }
93                 if ( sf & MINIG_SF_UPDATE )
94                         WriteByte(MSG_ENTITY,self.team);
95         }
96         MINIGAME_SIMPLELINKED_ENTITIES
97         
98         minigame_ent.minigame_event(minigame_ent,"network_send",self,sf);
99         
100         return true;
101         
102 }
103 #undef FIELD
104 #undef MSLE
105
106 // Force resend all minigame entities
107 void minigame_resend(entity minigame)
108 {
109         minigame.SendFlags = MINIG_SF_ALL;
110         entity e = world;
111         while (( e = findentity(e,owner,minigame) ))
112         {
113                 e.SendFlags = MINIG_SF_ALL;
114         }
115 }
116
117 bool minigame_CheckSend()
118 {SELFPARAM();
119         entity e;
120         for ( e = self.owner.minigame_players; e != world; e = e.list_next )
121                 if ( e.minigame_players == other )
122                         return true;
123         return false;
124 }
125
126 int minigame_addplayer(entity minigame_session, entity player)
127 {SELFPARAM();
128         if ( player.active_minigame )
129         {
130                 if ( player.active_minigame == minigame_session )
131                         return 0;
132                 minigame_rmplayer(player.active_minigame,player);
133         }
134         
135         int mgteam = minigame_session.minigame_event(minigame_session,"join",player);
136         
137         if ( mgteam )
138         {
139                 entity player_pointer = spawn();
140                 player_pointer.classname = "minigame_player";
141                 player_pointer.owner = minigame_session;
142                 player_pointer.minigame_players = player;
143                 player_pointer.team = mgteam;
144                 player_pointer.list_next = minigame_session.minigame_players;
145                 minigame_session.minigame_players = player_pointer;
146                 player.active_minigame = minigame_session;
147                 player.minigame_players = player_pointer;
148                 player_pointer.customizeentityforclient = minigame_CheckSend;
149                 Net_LinkEntity(player_pointer, false, 0, minigame_SendEntity);
150
151                 if ( !IS_OBSERVER(player) && autocvar_sv_minigames_observer )
152                 {
153                         SELFCALL(player, PutObserverInServer());
154                         SELFCALL_DONE();
155                 }
156                 if ( autocvar_sv_minigames_observer == 2 )
157                         player.team_forced = -1;
158                 
159                 minigame_resend(minigame_session);
160         }
161         GameLogEcho(strcat(":minigame:join",(mgteam?"":"fail"),":",minigame_session.netname,":",
162                 ftos(num_for_edict(player)),":",player.netname));
163         
164         return mgteam;
165 }
166
167 entity start_minigame(entity player, string minigame )
168 {
169         if ( !autocvar_sv_minigames || !IS_REAL_CLIENT(player) )
170                 return world;
171         
172         entity e = minigame_get_descriptor(minigame);
173         if ( e ) 
174         {
175                 entity minig = spawn();
176                 minig.classname = "minigame";
177                 minig.netname = strzone(strcat(e.netname,"_",ftos(num_for_edict(minig))));
178                 minig.descriptor = e;
179                 minig.minigame_event = e.minigame_event;
180                 minig.minigame_event(minig,"start");
181                 GameLogEcho(strcat(":minigame:start:",minig.netname));
182                 if ( ! minigame_addplayer(minig,player) )
183                 {
184                         LOG_TRACE("Minigame ",minig.netname," rejected the first player join!\n");
185                         end_minigame(minig);
186                         return world;
187                 }
188                 Net_LinkEntity(minig, false, 0, minigame_SendEntity);
189                 
190                 if ( !minigame_sessions )
191                         minigame_sessions = minig;
192                 else
193                 {
194                         minigame_sessions.owner = minig;
195                         minig.list_next = minigame_sessions;
196                         minigame_sessions = minig;
197                 }
198                 return minig;
199         }
200                 
201         return world;
202 }
203
204 entity join_minigame(entity player, string game_id )
205 {
206         if ( !autocvar_sv_minigames || !IS_REAL_CLIENT(player) )
207                 return world;
208         
209         entity minig;
210         for ( minig = minigame_sessions; minig != world; minig = minig.list_next )
211         {
212                 if ( minig.netname == game_id )
213                 if ( minigame_addplayer(minig,player) )
214                         return minig;
215         }
216         
217         return world;
218 }
219
220 void part_minigame(entity player )
221 {
222         entity minig = player.active_minigame;
223         
224         if ( minig && minig.classname == "minigame" )
225                 minigame_rmplayer(minig,player);
226 }
227
228 void end_minigame(entity minigame_session)
229 {
230         if ( minigame_session.owner )
231                 minigame_session.owner.list_next = minigame_session.list_next;
232         else
233                 minigame_sessions = minigame_session.list_next;
234         
235         minigame_session.minigame_event(minigame_session,"end");
236         GameLogEcho(strcat(":minigame:end:",minigame_session.netname));
237         
238         
239         entity e = world;
240         while( (e = findentity(e, owner, minigame_session)) )
241                 if ( e.minigame_autoclean )
242                 {
243                         LOG_TRACE("SV Auto-cleaned: ",ftos(num_for_edict(e)), " (",e.classname,")\n");
244                         remove(e);
245                 }
246         
247         entity p;
248         for ( e = minigame_session.minigame_players; e != world; e = p )
249         {
250                 p = e.list_next;
251                 player_clear_minigame(e.minigame_players);
252                 remove(e);
253         }
254         
255         strunzone(minigame_session.netname);
256         remove(minigame_session);
257 }
258
259 void end_minigames()
260 {
261         while ( minigame_sessions )
262         {
263                 end_minigame(minigame_sessions);
264         }
265 }
266
267 void initialize_minigames()
268 {
269         entity last_minig = world;
270         entity minig;
271         #define MINIGAME(name,nicename) \
272                 minig = spawn(); \
273                 minig.classname = "minigame_descriptor"; \
274                 minig.netname = #name; \
275                 minig.message = nicename; \
276                 minig.minigame_event = name##_server_event; \
277                 if ( !last_minig ) minigame_descriptors = minig; \
278                 else last_minig.list_next = minig; \
279                 last_minig = minig;
280                 
281         REGISTERED_MINIGAMES
282         
283         #undef MINIGAME
284 }
285
286 string invite_minigame(entity inviter, entity player)
287 {
288         if ( !inviter || !inviter.active_minigame )
289                 return "Invalid minigame";
290         if ( !VerifyClientEntity(player, true, false) )
291                 return "Invalid player";
292         if ( inviter == player )
293                 return "You can't invite yourself";
294         if ( player.active_minigame == inviter.active_minigame )
295                 return strcat(player.netname," is already playing");
296         
297         Send_Notification(NOTIF_ONE, player, MSG_INFO, INFO_MINIGAME_INVITE, 
298                 inviter.active_minigame.netname, inviter.netname );
299         
300         GameLogEcho(strcat(":minigame:invite:",inviter.active_minigame.netname,":",
301                 ftos(num_for_edict(player)),":",player.netname));
302         
303         return "";
304 }
305
306 entity minigame_find_player(entity client)
307 {
308         if ( ! client.active_minigame )
309                 return world;
310         entity e;
311         for ( e = client.active_minigame.minigame_players; e; e = e.list_next )
312                 if ( e.minigame_players == client )
313                         return e;
314         return world;
315 }
316
317 bool MinigameImpulse(int imp)
318 {SELFPARAM();
319         entity e = minigame_find_player(self);
320         if ( imp && self.active_minigame && e )
321         {
322                 return self.active_minigame.minigame_event(self.active_minigame,"impulse",e,imp);
323         }
324         return false;
325 }
326
327
328
329 void ClientCommand_minigame(int request, int argc, string command)
330 {SELFPARAM();
331         if ( !autocvar_sv_minigames )
332         {
333                 sprint(self,"Minigames are not enabled!\n");
334                 return;
335         }
336         
337         if (request == CMD_REQUEST_COMMAND )
338         {
339                 string minig_cmd = argv(1);
340                 if ( minig_cmd == "create" && argc > 2 )
341                 {
342                         entity minig = start_minigame(self, argv(2));
343                         if ( minig )
344                                 sprint(self,"Created minigame session: ",minig.netname,"\n");
345                         else
346                                 sprint(self,"Cannot start minigame session!\n");
347                         return;
348                 }
349                 else if ( minig_cmd == "join" && argc > 2 )
350                 {
351                         entity minig = join_minigame(self, argv(2));
352                         if ( minig )
353                                 sprint(self,"Joined: ",minig.netname,"\n");
354                         else
355                         {
356                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_JOIN_PREVENT_MINIGAME);
357                                 sprint(self,"Cannot join given minigame session!\n");
358                         }
359                         return;
360                 }
361                 else if ( minig_cmd == "list" )
362                 {
363                         entity e;
364                         for ( e = minigame_descriptors; e != world; e = e.list_next )
365                                 sprint(self,e.netname," (",e.message,") ","\n");
366                         return;
367                 }
368                 else if ( minig_cmd == "list-sessions" )
369                 {
370                         entity e;
371                         for ( e = minigame_sessions; e != world; e = e.list_next )
372                                 sprint(self,e.netname,"\n");
373                         return;
374                 }
375                 else if ( minig_cmd == "end" || minig_cmd == "part" )
376                 {
377                         if ( self.active_minigame )
378                         {
379                                 part_minigame(self);
380                                 sprint(self,"Left minigame session\n");
381                         }
382                         else
383                                 sprint(self,"You aren't playing any minigame...\n");
384                         return;
385                 }
386                 else if ( minig_cmd == "invite" && argc > 2 )
387                 {
388                         if ( self.active_minigame )
389                         {
390                                 entity client = GetIndexedEntity(argc, 2);
391                                 string error = invite_minigame(self,client);
392                                 if ( error == "" )
393                                 {
394                                         sprint(self,"You have invited ",client.netname,
395                                                 " to join your game of ", self.active_minigame.descriptor.message, "\n");
396                                 }
397                                 else
398                                         sprint(self,"Could not invite: ", error, ".\n");
399                         }
400                         else
401                                 sprint(self,"You aren't playing any minigame...\n");
402                         return;
403                 }
404                 else if ( self.active_minigame )
405                 {
406                         entity e = minigame_find_player(self);
407                         string subcommand = substring(command,argv_end_index(0),-1);
408                         int arg_c = tokenize_console(subcommand);
409                         if ( self.active_minigame.minigame_event(self.active_minigame,"cmd",e,arg_c,subcommand) )
410                                 return;
411                                 
412                 }
413                 else sprint(self,strcat("Wrong command:^1 ",command,"\n"));
414         }
415         
416         sprint(self, "\nUsage:^3 cmd minigame create <minigame>\n");
417         sprint(self, "  Start a new minigame session\n");
418         sprint(self, "Usage:^3 cmd minigame join <session>\n");
419         sprint(self, "  Join an exising minigame session\n");
420         sprint(self, "Usage:^3 cmd minigame list\n");
421         sprint(self, "  List available minigames\n");
422         sprint(self, "Usage:^3 cmd minigame list-sessions\n");
423         sprint(self, "  List available minigames sessions\n");
424         sprint(self, "Usage:^3 cmd minigame part|end\n");
425         sprint(self, "  Leave the current minigame\n");
426         sprint(self, "Usage:^3 cmd minigame invite <player>\n");
427         sprint(self, "  Invite the given player to join you in a minigame\n");
428 }