1 #include "minigames.qh"
3 void player_clear_minigame(entity player)
5 player.active_minigame = world;
6 player.minigame_players = world;
7 if ( IS_PLAYER(player) )
8 player.movetype = MOVETYPE_WALK;
10 player.movetype = MOVETYPE_FLY_WORLDONLY;
11 player.team_forced = 0;
14 void minigame_rmplayer(entity minigame_session, entity player)
17 entity p = minigame_session.minigame_players;
19 if ( p.minigame_players == player )
21 if ( p.list_next == world )
23 end_minigame(minigame_session);
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;
31 player_clear_minigame(player);
35 for ( e = p.list_next; e != world; e = e.list_next )
37 if ( e.minigame_players == player )
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;
44 player_clear_minigame(player);
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); \
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)
65 WriteByte(MSG_ENTITY, ENT_CLIENT_MINIGAME);
66 WriteByte(MSG_ENTITY, sf);
68 if ( sf & MINIG_SF_CREATE )
70 WriteShort(MSG_ENTITY,msle_id(self.classname));
71 WriteString(MSG_ENTITY,self.netname);
74 entity minigame_ent = self.owner;
76 if ( self.classname == "minigame" )
80 if ( sf & MINIG_SF_CREATE )
81 WriteString(MSG_ENTITY,self.descriptor.netname);
83 if ( sf & MINIG_SF_UPDATE )
84 WriteLong(MSG_ENTITY,self.minigame_flags);
86 else if ( self.classname == "minigame_player" )
88 if ( sf & MINIG_SF_CREATE )
90 WriteString(MSG_ENTITY,self.owner.netname);
91 WriteLong(MSG_ENTITY,num_for_edict(self.minigame_players));
93 if ( sf & MINIG_SF_UPDATE )
94 WriteByte(MSG_ENTITY,self.team);
96 MINIGAME_SIMPLELINKED_ENTITIES
98 minigame_ent.minigame_event(minigame_ent,"network_send",self,sf);
106 // Force resend all minigame entities
107 void minigame_resend(entity minigame)
109 minigame.SendFlags = MINIG_SF_ALL;
111 while (( e = findentity(e,owner,minigame) ))
113 e.SendFlags = MINIG_SF_ALL;
117 bool minigame_CheckSend()
120 for ( e = self.owner.minigame_players; e != world; e = e.list_next )
121 if ( e.minigame_players == other )
126 int minigame_addplayer(entity minigame_session, entity player)
128 if ( player.active_minigame )
130 if ( player.active_minigame == minigame_session )
132 minigame_rmplayer(player.active_minigame,player);
135 int mgteam = minigame_session.minigame_event(minigame_session,"join",player);
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);
151 if ( !IS_OBSERVER(player) && autocvar_sv_minigames_observer )
153 WITH(entity, self, player, PutObserverInServer());
155 if ( autocvar_sv_minigames_observer == 2 )
156 player.team_forced = -1;
158 minigame_resend(minigame_session);
160 GameLogEcho(strcat(":minigame:join",(mgteam?"":"fail"),":",minigame_session.netname,":",
161 ftos(num_for_edict(player)),":",player.netname));
166 void minigame_frame()
170 minig.minigame_event(minig,"frame");
171 minig.nextthink = time;
174 entity start_minigame(entity player, string minigame )
176 if ( !autocvar_sv_minigames || !IS_REAL_CLIENT(player) )
179 entity e = minigame_get_descriptor(minigame);
182 entity minig = spawn();
183 minig.classname = "minigame";
184 minig.netname = strzone(strcat(e.netname,"_",ftos(num_for_edict(minig))));
185 minig.descriptor = e;
186 minig.think = minigame_frame;
187 minig.nextthink = time;
188 minig.minigame_event = e.minigame_event;
189 minig.minigame_event(minig,"start");
190 GameLogEcho(strcat(":minigame:start:",minig.netname));
191 if ( ! minigame_addplayer(minig,player) )
193 LOG_TRACE("Minigame ",minig.netname," rejected the first player join!\n");
197 Net_LinkEntity(minig, false, 0, minigame_SendEntity);
199 if ( !minigame_sessions )
200 minigame_sessions = minig;
203 minigame_sessions.owner = minig;
204 minig.list_next = minigame_sessions;
205 minigame_sessions = minig;
213 entity join_minigame(entity player, string game_id )
215 if ( !autocvar_sv_minigames || !IS_REAL_CLIENT(player) )
219 for ( minig = minigame_sessions; minig != world; minig = minig.list_next )
221 if ( minig.netname == game_id )
222 if ( minigame_addplayer(minig,player) )
229 void part_minigame(entity player )
231 entity minig = player.active_minigame;
233 if ( minig && minig.classname == "minigame" )
234 minigame_rmplayer(minig,player);
237 void end_minigame(entity minigame_session)
239 if ( minigame_session.owner )
240 minigame_session.owner.list_next = minigame_session.list_next;
242 minigame_sessions = minigame_session.list_next;
244 minigame_session.minigame_event(minigame_session,"end");
245 GameLogEcho(strcat(":minigame:end:",minigame_session.netname));
249 while( (e = findentity(e, owner, minigame_session)) )
250 if ( e.minigame_autoclean )
252 LOG_TRACE("SV Auto-cleaned: ",ftos(num_for_edict(e)), " (",e.classname,")\n");
257 for ( e = minigame_session.minigame_players; e != world; e = p )
260 player_clear_minigame(e.minigame_players);
264 strunzone(minigame_session.netname);
265 remove(minigame_session);
270 while ( minigame_sessions )
272 end_minigame(minigame_sessions);
276 string invite_minigame(entity inviter, entity player)
278 if ( !inviter || !inviter.active_minigame )
279 return "Invalid minigame";
280 if ( !VerifyClientEntity(player, true, false) )
281 return "Invalid player";
282 if ( inviter == player )
283 return "You can't invite yourself";
284 if ( player.active_minigame == inviter.active_minigame )
285 return strcat(player.netname," is already playing");
287 Send_Notification(NOTIF_ONE, player, MSG_INFO, INFO_MINIGAME_INVITE,
288 inviter.active_minigame.netname, inviter.netname );
290 GameLogEcho(strcat(":minigame:invite:",inviter.active_minigame.netname,":",
291 ftos(num_for_edict(player)),":",player.netname));
296 entity minigame_find_player(entity client)
298 if ( ! client.active_minigame )
301 for ( e = client.active_minigame.minigame_players; e; e = e.list_next )
302 if ( e.minigame_players == client )
307 bool MinigameImpulse(int imp)
309 entity e = minigame_find_player(self);
310 if ( imp && self.active_minigame && e )
312 return self.active_minigame.minigame_event(self.active_minigame,"impulse",e,imp);
319 void ClientCommand_minigame(int request, int argc, string command)
321 if ( !autocvar_sv_minigames )
323 sprint(self,"Minigames are not enabled!\n");
327 if (request == CMD_REQUEST_COMMAND )
329 string minig_cmd = argv(1);
330 if ( minig_cmd == "create" && argc > 2 )
332 entity minig = start_minigame(self, argv(2));
334 sprint(self,"Created minigame session: ",minig.netname,"\n");
336 sprint(self,"Cannot start minigame session!\n");
339 else if ( minig_cmd == "join" && argc > 2 )
341 entity minig = join_minigame(self, argv(2));
343 sprint(self,"Joined: ",minig.netname,"\n");
346 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_JOIN_PREVENT_MINIGAME);
347 sprint(self,"Cannot join given minigame session!\n");
351 else if ( minig_cmd == "list" )
353 FOREACH(MINIGAMES, true, LAMBDA(
355 sprint(self,it.netname," (",it.message,") ","\n");
359 else if ( minig_cmd == "list-sessions" )
362 for ( e = minigame_sessions; e != world; e = e.list_next )
363 sprint(self,e.netname,"\n");
366 else if ( minig_cmd == "end" || minig_cmd == "part" )
368 if ( self.active_minigame )
371 sprint(self,"Left minigame session\n");
374 sprint(self,"You aren't playing any minigame...\n");
377 else if ( minig_cmd == "invite" && argc > 2 )
379 if ( self.active_minigame )
381 entity client = GetIndexedEntity(argc, 2);
382 string error = invite_minigame(self,client);
385 sprint(self,"You have invited ",client.netname,
386 " to join your game of ", self.active_minigame.descriptor.message, "\n");
389 sprint(self,"Could not invite: ", error, ".\n");
392 sprint(self,"You aren't playing any minigame...\n");
395 else if ( self.active_minigame )
397 entity e = minigame_find_player(self);
398 string subcommand = substring(command,argv_end_index(0),-1);
399 int arg_c = tokenize_console(subcommand);
400 if ( self.active_minigame.minigame_event(self.active_minigame,"cmd",e,arg_c,subcommand) )
404 else sprint(self,strcat("Wrong command:^1 ",command,"\n"));
407 sprint(self, "\nUsage:^3 cmd minigame create <minigame>\n");
408 sprint(self, " Start a new minigame session\n");
409 sprint(self, "Usage:^3 cmd minigame join <session>\n");
410 sprint(self, " Join an exising minigame session\n");
411 sprint(self, "Usage:^3 cmd minigame list\n");
412 sprint(self, " List available minigames\n");
413 sprint(self, "Usage:^3 cmd minigame list-sessions\n");
414 sprint(self, " List available minigames sessions\n");
415 sprint(self, "Usage:^3 cmd minigame part|end\n");
416 sprint(self, " Leave the current minigame\n");
417 sprint(self, "Usage:^3 cmd minigame invite <player>\n");
418 sprint(self, " Invite the given player to join you in a minigame\n");