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