]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapons.qc
Use weaponinfo to get weapon sprite info
[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, vector clr, 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.wpcolor = clr;
89         e.mdl = modelname;
90         e.model = strzone(strcat("models/weapons/g_", modelname, ".md3"));
91         e.spawnflags = weapontype;
92         e.model2 = strzone(strcat("wpn-", ftos(id)));
93         e.impulse = i;
94         e.bot_pickupbasevalue = pickupbasevalue;
95         e.current_ammo = ammotype;
96
97         #ifndef MENUQC
98         func(WR_INIT);
99         #endif
100 }
101 float w_null(float dummy)
102 {
103         return 0;
104 }
105 void register_weapons_done()
106 {
107         dummy_weapon_info = spawn();
108         dummy_weapon_info.classname = "weapon_info";
109         dummy_weapon_info.weapon = 0; // you can recognize dummies by this
110         dummy_weapon_info.weapons = '0 0 0';
111         dummy_weapon_info.netname = "";
112         dummy_weapon_info.message = "AOL CD Thrower";
113         //dummy_weapon_info.items = 0;
114         dummy_weapon_info.weapon_func = w_null;
115         dummy_weapon_info.mdl = "";
116         dummy_weapon_info.model = "";
117         dummy_weapon_info.spawnflags = 0;
118         dummy_weapon_info.model2 = "";
119         dummy_weapon_info.impulse = -1;
120         dummy_weapon_info.bot_pickupbasevalue = 0;
121         dummy_weapon_info.current_ammo = ammo_none;
122
123         float i;
124         weaponorder_byid = "";
125         for(i = WEP_MAXCOUNT; i >= 1; --i)
126                 if(weapon_info[i-1])
127                         weaponorder_byid = strcat(weaponorder_byid, " ", ftos(i));
128         weaponorder_byid = strzone(substring(weaponorder_byid, 1, strlen(weaponorder_byid) - 1));
129 }
130 entity get_weaponinfo(float id)
131 {
132         entity w;
133         if(id < WEP_FIRST || id > WEP_LAST)
134                 return dummy_weapon_info;
135         w = weapon_info[id - 1];
136         if(w)
137                 return w;
138         return dummy_weapon_info;
139 }
140 string W_FixWeaponOrder(string order, float complete)
141 {
142         return fixPriorityList(order, WEP_FIRST, WEP_LAST, 230 - WEP_FIRST, complete);
143 }
144 string W_NameWeaponOrder_MapFunc(string s)
145 {
146         entity wi;
147         if(s == "0" || stof(s))
148         {
149                 wi = get_weaponinfo(stof(s));
150                 if(wi != dummy_weapon_info)
151                         return wi.netname;
152         }
153         return s;
154 }
155 string W_NameWeaponOrder(string order)
156 {
157         return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
158 }
159 string W_NumberWeaponOrder_MapFunc(string s)
160 {
161         float i;
162         if(s == "0" || stof(s))
163                 return s;
164         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
165                 if(s == get_weaponinfo(i).netname)
166                         return ftos(i);
167         return s;
168 }
169 string W_NumberWeaponOrder(string order)
170 {
171         return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
172 }
173
174 float W_FixWeaponOrder_BuildImpulseList_buf[WEP_MAXCOUNT];
175 string W_FixWeaponOrder_BuildImpulseList_order;
176 void W_FixWeaponOrder_BuildImpulseList_swap(float i, float j, entity pass)
177 {
178         float h;
179         h = W_FixWeaponOrder_BuildImpulseList_buf[i];
180         W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
181         W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
182 }
183 float W_FixWeaponOrder_BuildImpulseList_cmp(float i, float j, entity pass)
184 {
185         entity e1, e2;
186         float d;
187         e1 = get_weaponinfo(W_FixWeaponOrder_BuildImpulseList_buf[i]);
188         e2 = get_weaponinfo(W_FixWeaponOrder_BuildImpulseList_buf[j]);
189         d = mod(e1.impulse + 9, 10) - mod(e2.impulse + 9, 10);
190         if(d != 0)
191                 return -d; // high impulse first!
192         return
193                 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "), sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[i]), 0)
194                 -
195                 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "), sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[j]), 0)
196                 ; // low char index first!
197 }
198 string W_FixWeaponOrder_BuildImpulseList(string o)
199 {
200         float i;
201         W_FixWeaponOrder_BuildImpulseList_order = o;
202         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
203                 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
204         heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp, world);
205         o = "";
206         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
207                 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
208         W_FixWeaponOrder_BuildImpulseList_order = string_null;
209         return substring(o, 1, -1);
210 }
211
212 string W_FixWeaponOrder_AllowIncomplete(string order)
213 {
214         return W_FixWeaponOrder(order, 0);
215 }
216
217 string W_FixWeaponOrder_ForceComplete(string order)
218 {
219         if(order == "")
220                 order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
221         return W_FixWeaponOrder(order, 1);
222 }
223
224 void W_RandomWeapons(entity e, float n)
225 {
226         float i, j;
227         WepSet remaining;
228         WepSet result;
229         remaining = e.weapons;
230         result = '0 0 0';
231         for(i = 0; i < n; ++i)
232         {
233                 RandomSelection_Init();
234                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
235                         if(remaining & WepSet_FromWeapon(j))
236                                 RandomSelection_Add(world, j, string_null, 1, 1);
237                 result |= WepSet_FromWeapon(RandomSelection_chosen_float);
238                 remaining &= ~WepSet_FromWeapon(RandomSelection_chosen_float);
239         }
240         e.weapons = result;
241 }
242
243 string W_Name(float weaponid) // WEAPONTODO: make into a macro
244 {
245         return (get_weaponinfo(weaponid)).message;
246 }