]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapons.qc
More work on switching over to using entity field instead of item field
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapons.qc
1 #ifndef MENUQC
2 #include "calculations.qc"
3 #endif
4 #include "all.qh"
5
6 // WEAPON PLUGIN SYSTEM
7 entity weapon_info[WEP_MAXCOUNT];
8 entity dummy_weapon_info;
9
10 #if WEP_MAXCOUNT > 72
11 # error Kein Weltraum links auf dem Gerät
12 #endif
13
14 WepSet WepSet_FromWeapon(float a) {
15         a -= WEP_FIRST;
16 #if WEP_MAXCOUNT > 24
17         if(a >= 24) {
18                 a -= 24;
19 #if WEP_MAXCOUNT > 48
20                 if(a >= 24) {
21                         a -= 24;
22                         return '0 0 1' * power2of(a);
23                 }
24 #endif
25                 return '0 1 0' * power2of(a);
26         }
27 #endif
28         return '1 0 0' * power2of(a);
29 }
30 #ifdef SVQC
31 void WepSet_AddStat()
32 {
33         addstat(STAT_WEAPONS, AS_INT, weapons_x);
34 #if WEP_MAXCOUNT > 24
35         addstat(STAT_WEAPONS2, AS_INT, weapons_y);
36 #if WEP_MAXCOUNT > 48
37         addstat(STAT_WEAPONS3, AS_INT, weapons_z);
38 #endif
39 #endif
40 }
41 void WriteWepSet(float dst, WepSet w)
42 {
43 #if WEP_MAXCOUNT > 48
44         WriteInt72_t(dst, w);
45 #elif WEP_MAXCOUNT > 24
46         WriteInt48_t(dst, w);
47 #else
48         WriteInt24_t(dst, w_x);
49 #endif
50 }
51 #endif
52 #ifdef CSQC
53 WepSet WepSet_GetFromStat()
54 {
55         WepSet w = '0 0 0';
56         w_x = getstati(STAT_WEAPONS);
57 #if WEP_MAXCOUNT > 24
58         w_y = getstati(STAT_WEAPONS2);
59 #if WEP_MAXCOUNT > 48
60         w_z = getstati(STAT_WEAPONS3);
61 #endif
62 #endif
63         return w;
64 }
65 WepSet ReadWepSet()
66 {
67 #if WEP_MAXCOUNT > 48
68         return ReadInt72_t();
69 #elif WEP_MAXCOUNT > 24
70         return ReadInt48_t();
71 #else
72         return ReadInt24_t() * '1 0 0';
73 #endif
74 }
75 #endif
76
77 void register_weapon(float id, WepSet bit, float(float) func, .float ammotype, float i, float weapontype, float pickupbasevalue, string modelname, string shortname, string wname)
78 {
79         entity e;
80         weapon_info[id - 1] = e = spawn();
81         e.classname = "weapon_info";
82         e.weapon = id;
83         e.weapons = bit;
84         e.netname = shortname;
85         e.message = wname;
86         //e.items = ammotype;
87         e.weapon_func = func;
88         e.mdl = modelname;
89         e.model = strzone(strcat("models/weapons/g_", modelname, ".md3"));
90         e.spawnflags = weapontype;
91         e.model2 = strzone(strcat("wpn-", e.mdl));
92         e.impulse = i;
93         e.bot_pickupbasevalue = pickupbasevalue;
94         e.current_ammo = ammotype;
95
96         #ifndef MENUQC
97         func(WR_INIT);
98         #endif
99 }
100 float w_null(float dummy)
101 {
102         return 0;
103 }
104 void register_weapons_done()
105 {
106         dummy_weapon_info = spawn();
107         dummy_weapon_info.classname = "weapon_info";
108         dummy_weapon_info.weapon = 0; // you can recognize dummies by this
109         dummy_weapon_info.weapons = '0 0 0';
110         dummy_weapon_info.netname = "";
111         dummy_weapon_info.message = "AOL CD Thrower";
112         //dummy_weapon_info.items = 0;
113         dummy_weapon_info.weapon_func = w_null;
114         dummy_weapon_info.mdl = "";
115         dummy_weapon_info.model = "";
116         dummy_weapon_info.spawnflags = 0;
117         dummy_weapon_info.model2 = "";
118         dummy_weapon_info.impulse = -1;
119         dummy_weapon_info.bot_pickupbasevalue = 0;
120         dummy_weapon_info.current_ammo = ammo_broken;
121
122         float i;
123         weaponorder_byid = "";
124         for(i = WEP_MAXCOUNT; i >= 1; --i)
125                 if(weapon_info[i-1])
126                         weaponorder_byid = strcat(weaponorder_byid, " ", ftos(i));
127         weaponorder_byid = strzone(substring(weaponorder_byid, 1, strlen(weaponorder_byid) - 1));
128 }
129 entity get_weaponinfo(float id)
130 {
131         entity w;
132         if(id < WEP_FIRST || id > WEP_LAST)
133                 return dummy_weapon_info;
134         w = weapon_info[id - 1];
135         if(w)
136                 return w;
137         return dummy_weapon_info;
138 }
139 string W_FixWeaponOrder(string order, float complete)
140 {
141         return fixPriorityList(order, WEP_FIRST, WEP_LAST, 230 - WEP_FIRST, complete);
142 }
143 string W_NameWeaponOrder_MapFunc(string s)
144 {
145         entity wi;
146         if(s == "0" || stof(s))
147         {
148                 wi = get_weaponinfo(stof(s));
149                 if(wi != dummy_weapon_info)
150                         return wi.netname;
151         }
152         return s;
153 }
154 string W_NameWeaponOrder(string order)
155 {
156         return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
157 }
158 string W_NumberWeaponOrder_MapFunc(string s)
159 {
160         float i;
161         if(s == "0" || stof(s))
162                 return s;
163         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
164                 if(s == get_weaponinfo(i).netname)
165                         return ftos(i);
166         return s;
167 }
168 string W_NumberWeaponOrder(string order)
169 {
170         return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
171 }
172
173 float W_FixWeaponOrder_BuildImpulseList_buf[WEP_MAXCOUNT];
174 string W_FixWeaponOrder_BuildImpulseList_order;
175 void W_FixWeaponOrder_BuildImpulseList_swap(float i, float j, entity pass)
176 {
177         float h;
178         h = W_FixWeaponOrder_BuildImpulseList_buf[i];
179         W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
180         W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
181 }
182 float W_FixWeaponOrder_BuildImpulseList_cmp(float i, float j, entity pass)
183 {
184         entity e1, e2;
185         float d;
186         e1 = get_weaponinfo(W_FixWeaponOrder_BuildImpulseList_buf[i]);
187         e2 = get_weaponinfo(W_FixWeaponOrder_BuildImpulseList_buf[j]);
188         d = mod(e1.impulse + 9, 10) - mod(e2.impulse + 9, 10);
189         if(d != 0)
190                 return -d; // high impulse first!
191         return
192                 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "), sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[i]), 0)
193                 -
194                 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "), sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[j]), 0)
195                 ; // low char index first!
196 }
197 string W_FixWeaponOrder_BuildImpulseList(string o)
198 {
199         float i;
200         W_FixWeaponOrder_BuildImpulseList_order = o;
201         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
202                 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
203         heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp, world);
204         o = "";
205         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
206                 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
207         W_FixWeaponOrder_BuildImpulseList_order = string_null;
208         return substring(o, 1, -1);
209 }
210
211 string W_FixWeaponOrder_AllowIncomplete(string order)
212 {
213         return W_FixWeaponOrder(order, 0);
214 }
215
216 string W_FixWeaponOrder_ForceComplete(string order)
217 {
218         if(order == "")
219                 order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
220         return W_FixWeaponOrder(order, 1);
221 }
222
223 void W_RandomWeapons(entity e, float n)
224 {
225         float i, j;
226         WepSet remaining;
227         WepSet result;
228         remaining = e.weapons;
229         result = '0 0 0';
230         for(i = 0; i < n; ++i)
231         {
232                 RandomSelection_Init();
233                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
234                         if(remaining & WepSet_FromWeapon(j))
235                                 RandomSelection_Add(world, j, string_null, 1, 1);
236                 result |= WepSet_FromWeapon(RandomSelection_chosen_float);
237                 remaining &= ~WepSet_FromWeapon(RandomSelection_chosen_float);
238         }
239         e.weapons = result;
240 }
241
242 string W_Name(float weaponid) // WEAPONTODO: make into a macro
243 {
244         return (get_weaponinfo(weaponid)).message;
245 }