]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/minigames.qc
Merge branch 'master' into Mario/bulldozer
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigames.qc
1 #include "minigames.qh"
2
3 REGISTER_NET_LINKED(ENT_CLIENT_MINIGAME)
4
5 entity minigame_get_descriptor(string id)
6 {
7         FOREACH(Minigames, true, LAMBDA(
8         {
9                 if(it.netname == id)
10                         return it;
11         }));
12         return world;
13 }
14
15 // Get letter index of a tile name
16 int minigame_tile_letter(string id)
17 {
18         return str2chr(substring(id,0,1),0)-'a';
19 }
20
21 // Get number index of a tile name
22 // Note: this is 0 based, useful for mathematical operations
23 // Note: Since the tile notation starts from the bottom left,
24 //      you may want to do number_of_rows - what_this_function_returns or something
25 int minigame_tile_number(string id)
26 {
27         return stof(substring(id,1,-1)) -1 ;
28 }
29
30 // Get relative position of the center of a given tile
31 vector minigame_tile_pos(string id, int rows, int columns)
32 {
33         return eX*(minigame_tile_letter(id)+0.5)/columns +
34                eY - eY*(minigame_tile_number(id)+0.5)/rows;
35 }
36
37 // Get a tile name from indices
38 string minigame_tile_buildname(int letter, int number)
39 {
40         return strcat(chr2str('a'+letter),ftos(number+1));
41 }
42
43 // Get the id of a tile relative to the given one
44 string minigame_relative_tile(string start_id, int dx, int dy, int rows, int columns)
45 {
46         int letter = minigame_tile_letter(start_id);
47         int number = minigame_tile_number(start_id);
48         letter = (letter+dx) % columns;
49         number = (number+dy) % rows;
50         if ( letter < 0 )
51                 letter = columns + letter;
52         if ( number < 0 )
53                 number = rows + number;
54         return minigame_tile_buildname(letter, number);
55 }
56
57 // Get tile name from a relative position (matches the tile covering a square area)
58 string minigame_tile_name(vector pos, int rows, int columns)
59 {
60         if ( pos_x < 0 || pos_x > 1 || pos_y < 0 || pos_y > 1 )
61                 return ""; // no tile
62
63         int letter = floor(pos_x * columns);
64         int number = floor((1-pos_y) * rows);
65         return minigame_tile_buildname(letter, number);
66 }
67
68 // Get the next team number (note: team numbers are between 1 and n_teams, inclusive)
69 int minigame_next_team(int curr_team, int n_teams)
70 {
71         return curr_team % n_teams + 1;
72 }
73
74 // Get the previous team number
75 int minigame_prev_team(int curr_team, int n_teams)
76 {
77         return curr_team % n_teams - 1;
78 }
79
80 // set send flags only when on server
81 // (for example in game logic which can be used both in client and server
82 void minigame_server_sendflags(entity ent, int mgflags)
83 {
84         #ifdef SVQC
85                 ent.SendFlags |= mgflags;
86         #endif
87 }
88
89 // Spawn linked entity on the server or local entity on the client
90 // This entity will be removed automatically when the minigame ends
91 entity msle_spawn(entity minigame_session, string class_name)
92 {
93         entity e = spawn();
94         e.classname = class_name;
95         e.owner = minigame_session;
96         e.minigame_autoclean = 1;
97         #ifdef SVQC
98                 e.customizeentityforclient = minigame_CheckSend;
99                 Net_LinkEntity(e, false, 0, minigame_SendEntity);
100         #endif
101         return e;
102 }
103
104 const int msle_base_id = 2;
105 int msle_id(string class_name)
106 {
107         if ( class_name == "minigame" ) return 1;
108         if ( class_name == "minigame_player" ) return 2;
109         int i = msle_base_id;
110 #define MSLE(Name, Fields) i++; if ( class_name == #Name ) return i;
111         MINIGAME_SIMPLELINKED_ENTITIES
112 #undef MSLE
113         return 0;
114 }
115
116 string msle_classname(int id)
117 {
118         if ( id == 1 ) return "minigame";
119         if ( id == 2 ) return "minigame_player";
120         int i = msle_base_id;
121 #define MSLE(Name, Fields) i++; if ( id == i ) return #Name;
122         MINIGAME_SIMPLELINKED_ENTITIES
123 #undef MSLE
124         return "";
125 }
126
127 int minigame_count_players(entity minigame)
128 {
129         int pl_num = 0;
130         entity e;
131 #ifdef SVQC
132         for(e = minigame.minigame_players; e; e = e.list_next)
133 #elif defined(CSQC)
134         e = world;
135         while( (e = findentity(e,owner,minigame)) )
136                 if ( e.classname == "minigame_player" )
137 #endif
138                 pl_num++;
139         return pl_num;
140 }