]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/minigames.qc
Merge branch 'master' of ssh://gitlab.com/xonotic/xonotic-data.pk3dir into Melanosuch...
[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 // set send flags only when on server
72 // (for example in game logic which can be used both in client and server
73 void minigame_server_sendflags(entity ent, int mgflags)
74 {
75         #ifdef SVQC
76                 ent.SendFlags |= mgflags;
77         #endif
78 }
79
80 // Spawn linked entity on the server or local entity on the client
81 // This entity will be removed automatically when the minigame ends
82 entity msle_spawn(entity minigame_session, string class_name)
83 {
84         entity e = spawn();
85         e.classname = class_name;
86         e.owner = minigame_session;
87         e.minigame_autoclean = 1;
88         #ifdef SVQC
89                 e.customizeentityforclient = minigame_CheckSend;
90                 Net_LinkEntity(e, false, 0, minigame_SendEntity);
91         #endif
92         return e;
93 }
94
95 const int msle_base_id = 2;
96 int msle_id(string class_name)
97 {
98         if ( class_name == "minigame" ) return 1;
99         if ( class_name == "minigame_player" ) return 2;
100         int i = msle_base_id;
101 #define MSLE(Name, Fields) i++; if ( class_name == #Name ) return i;
102         MINIGAME_SIMPLELINKED_ENTITIES
103 #undef MSLE
104         return 0;
105 }
106
107 string msle_classname(int id)
108 {
109         if ( id == 1 ) return "minigame";
110         if ( id == 2 ) return "minigame_player";
111         int i = msle_base_id;
112 #define MSLE(Name, Fields) i++; if ( id == i ) return #Name;
113         MINIGAME_SIMPLELINKED_ENTITIES
114 #undef MSLE
115         return "";
116 }
117
118 int minigame_count_players(entity minigame)
119 {
120         int pl_num = 0;
121         entity e;
122 #ifdef SVQC
123         for(e = minigame.minigame_players; e; e = e.list_next)
124 #elif defined(CSQC)
125         e = world;
126         while( (e = findentity(e,owner,minigame)) )
127                 if ( e.classname == "minigame_player" )
128 #endif
129                 pl_num++;
130         return pl_num;
131 }