Xonotic
minigames.qc
Go to the documentation of this file.
1 #include "minigames.qh"
2 
3 REGISTER_NET_LINKED(ENT_CLIENT_MINIGAME)
4 
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;
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;
117 #undef MSLE
118  return "";
119 }
120 
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
int minigame_tile_number(string id)
Definition: minigames.qc:21
void minigame_server_sendflags(entity ent, int mgflags)
Definition: minigames.qc:78
#define REGISTER_NET_LINKED(id)
Definition: net.qh:67
int int number
Definition: impulse.qc:89
#define str2chr
Definition: dpextensions.qh:45
entity msle_spawn(entity minigame_session, entity e)
Definition: minigames.qc:87
int minigame_next_team(int curr_team, int n_teams)
Definition: minigames.qc:65
entity() spawn
int msle_id(string class_name)
Definition: minigames.qc:99
const int msle_base_id
Definition: minigames.qc:98
entity owner
Definition: main.qh:73
string minigame_tile_name(vector pos, int rows, int columns)
Definition: minigames.qc:54
#define MINIGAME_SIMPLELINKED_ENTITIES
How to create a minigame
Definition: all.qh:100
int minigame_prev_team(int curr_team, int n_teams)
Definition: minigames.qc:71
bool minigame_CheckSend(entity this, entity client)
vector minigame_tile_pos(string id, int rows, int columns)
Definition: minigames.qc:27
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
entity minigame_get_descriptor(string id)
Definition: minigames.qc:5
#define NULL
Definition: post.qh:17
int minigame_tile_letter(string id)
Definition: minigames.qc:12
string minigame_tile_buildname(int letter, int number)
Definition: minigames.qc:34
vector(float skel, float bonenum) _skel_get_boneabs_hidden
string minigame_relative_tile(string start_id, int dx, int dy, int rows, int columns)
Definition: minigames.qc:40
int minigame_count_players(entity minigame)
Definition: minigames.qc:121
#define vec2(...)
Definition: vector.qh:90
bool minigame_SendEntity(entity this, entity to, int sf)
Definition: sv_minigames.qc:64
#define chr2str
Definition: dpextensions.qh:48
#define FOREACH(list, cond, body)
Definition: iter.qh:19
string msle_classname(int id)
Definition: minigames.qc:110