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