git wants me to commit, so I will: begin using the improved kill delay code
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / items.qc
1 // WEAPON PLUGIN SYSTEM
2 entity weapon_info[WEP_MAXCOUNT];
3 entity dummy_weapon_info;
4
5 void register_weapon(float id, float(float) func, float ammotype, float i, float weapontype, float pickupbasevalue, string modelname, string shortname, string wname)
6 {
7         entity e;
8         weapon_info[id - 1] = e = spawn();
9         e.classname = "weapon_info";
10         e.weapon = id;
11         e.weapons = power2of(id - WEP_FIRST);
12         e.netname = shortname;
13         e.message = wname;
14         e.items = ammotype;
15         e.weapon_func = func;
16         e.mdl = modelname;
17         e.model = strzone(strcat("models/weapons/g_", modelname, ".md3"));
18         e.spawnflags = weapontype;
19         e.model2 = strzone(strcat("wpn-", e.mdl));
20         e.impulse = i;
21         e.bot_pickupbasevalue = pickupbasevalue;
22 }
23 float w_null(float dummy)
24 {
25         return 0;
26 }
27 void register_weapons_done()
28 {
29         dummy_weapon_info = spawn();
30         dummy_weapon_info.classname = "weapon_info";
31         dummy_weapon_info.weapon = 0; // you can recognize dummies by this
32         dummy_weapon_info.weapons = 0; // you can recognize dummies by this too
33         dummy_weapon_info.netname = "";
34         dummy_weapon_info.message = "@!#%'n Tuba";
35         dummy_weapon_info.items = 0;
36         dummy_weapon_info.weapon_func = w_null;
37         dummy_weapon_info.mdl = "";
38         dummy_weapon_info.model = "";
39         dummy_weapon_info.spawnflags = 0;
40         dummy_weapon_info.model2 = "";
41         dummy_weapon_info.impulse = -1;
42         dummy_weapon_info.bot_pickupbasevalue = 0;
43
44         float i;
45         weaponorder_byid = "";
46         for(i = WEP_MAXCOUNT; i >= 1; --i)
47                 if(weapon_info[i-1])
48                         weaponorder_byid = strcat(weaponorder_byid, " ", ftos(i));
49         weaponorder_byid = strzone(substring(weaponorder_byid, 1, strlen(weaponorder_byid) - 1));
50 }
51 entity get_weaponinfo(float id)
52 {
53         entity w;
54         if(id < WEP_FIRST || id > WEP_LAST)
55                 return dummy_weapon_info;
56         w = weapon_info[id - 1];
57         if(w)
58                 return w;
59         return dummy_weapon_info;
60 }
61 string W_FixWeaponOrder(string order, float complete)
62 {
63         return fixPriorityList(order, WEP_FIRST, WEP_LAST, 230 - WEP_FIRST, complete);
64 }
65 string W_NameWeaponOrder_MapFunc(string s)
66 {
67         entity wi;
68         if(s == "0" || stof(s))
69         {
70                 wi = get_weaponinfo(stof(s));
71                 if(wi != dummy_weapon_info)
72                         return wi.netname;
73         }
74         return s;
75 }
76 string W_NameWeaponOrder(string order)
77 {
78         return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
79 }
80 string W_NumberWeaponOrder_MapFunc(string s)
81 {
82         float i;
83         if(s == "0" || stof(s))
84                 return s;
85         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
86                 if(s == get_weaponinfo(i).netname)
87                         return ftos(i);
88         return s;
89 }
90 string W_NumberWeaponOrder(string order)
91 {
92         return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
93 }
94
95 float W_FixWeaponOrder_BuildImpulseList_buf[WEP_MAXCOUNT];
96 string W_FixWeaponOrder_BuildImpulseList_order;
97 void W_FixWeaponOrder_BuildImpulseList_swap(float i, float j, entity pass)
98 {
99         float h;
100         h = W_FixWeaponOrder_BuildImpulseList_buf[i];
101         W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
102         W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
103 }
104 float W_FixWeaponOrder_BuildImpulseList_cmp(float i, float j, entity pass)
105 {
106         entity e1, e2;
107         float d;
108         e1 = get_weaponinfo(W_FixWeaponOrder_BuildImpulseList_buf[i]);
109         e2 = get_weaponinfo(W_FixWeaponOrder_BuildImpulseList_buf[j]);
110         d = mod(e1.impulse + 9, 10) - mod(e2.impulse + 9, 10);
111         if(d != 0)
112                 return -d; // high impulse first!
113         return
114                 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "), sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[i]), 0)
115                 -
116                 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "), sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[j]), 0)
117                 ; // low char index first!
118 }
119 string W_FixWeaponOrder_BuildImpulseList(string o)
120 {
121         float i;
122         W_FixWeaponOrder_BuildImpulseList_order = o;
123         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
124                 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
125         heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp, world);
126         o = "";
127         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
128                 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
129         W_FixWeaponOrder_BuildImpulseList_order = string_null;
130         return substring(o, 1, -1);
131 }
132
133 string W_FixWeaponOrder_AllowIncomplete(string order)
134 {
135         return W_FixWeaponOrder(order, 0);
136 }
137
138 string W_FixWeaponOrder_ForceComplete(string order)
139 {
140         if(order == "")
141                 order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
142         return W_FixWeaponOrder(order, 1);
143 }
144