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