Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
throwing.qc
Go to the documentation of this file.
1#include "throwing.qh"
2
4#include <common/mapinfo.qh>
9#include <common/state.qh>
10#include <common/util.qh>
12#include <common/wepent.qh>
13#include <server/damage.qh>
14#include <server/items/items.qh>
19#include <server/world.qh>
20
21// returns amount of ammo used, or -1 for failure, or 0 for no ammo count
22float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo, .entity weaponentity)
23{
24 Weapon info = REGISTRY_GET(Weapons, wpn);
25 Resource ammotype = info.ammo_type;
26
27 entity wep = spawn();
28 ITEM_SET_LOOT(wep, true);
29 setorigin(wep, org);
30 wep.velocity = velo;
31 wep.owner = wep.enemy = own;
32 wep.flags |= FL_TOSSED;
33 wep.colormap = own.colormap;
34 // wep.glowmod will be set in weapon_defaultspawnfunc
36
37 W_DropEvent(wr_drop,own,wpn,wep,weaponentity);
38
40 {
41 ITEM_SET_EXPIRING(wep, true);
42 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
43 {
44 wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
45 }
46 else
47 {
48 int superweapons = 1;
49 FOREACH(Weapons, it != WEP_Null, {
50 WepSet set = it.m_wepset;
51 if((WEPSET_SUPERWEAPONS & STAT(WEAPONS, own)) & set) ++superweapons;
52 });
53 if(superweapons <= 1)
54 {
55 wep.superweapons_finished = StatusEffects_gettime(STATUSEFFECT_Superweapon, own);
56 StatusEffects_remove(STATUSEFFECT_Superweapon, own, STATUSEFFECT_REMOVE_CLEAR);
57 }
58 else
59 {
60 float timeleft = StatusEffects_gettime(STATUSEFFECT_Superweapon, own) - time;
61 float weptimeleft = timeleft / superweapons;
62 wep.superweapons_finished = time + weptimeleft;
63 if(own.statuseffects)
64 {
65 // TODO: this doesn't explicitly enable the effect, use apply here?
66 own.statuseffects.statuseffect_time[STATUSEFFECT_Superweapon.m_id] -= weptimeleft;
68 }
69 }
70 }
71 }
72
73 weapon_defaultspawnfunc(wep, info);
75 return -1;
76
77 wep.pickup_anyway = true; // these are ALWAYS pickable
78
79 //wa = W_AmmoItemCode(wpn);
80 if(ammotype == RES_NONE)
81 {
82 return 0;
83 }
84 else
85 {
86 if(doreduce && g_weapon_stay == 2)
87 {
88 // if our weapon is loaded, give its load back to the player
89 int i = own.(weaponentity).m_weapon.m_id;
90 if(own.(weaponentity).(weapon_load[i]) > 0)
91 {
92 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
93 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
94 }
95 SetResource(wep, ammotype, 0);
96 }
97 else if(doreduce)
98 {
99 // if our weapon is loaded, give its load back to the player
100 int i = own.(weaponentity).m_weapon.m_id;
101 if(own.(weaponentity).(weapon_load[i]) > 0)
102 {
103 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
104 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
105 }
106
107 float ownderammo = GetResource(own, ammotype);
108 float thisammo = min(ownderammo, GetResource(wep, ammotype));
109 SetResource(wep, ammotype, thisammo);
110 SetResource(own, ammotype, ownderammo - thisammo);
111
112 return thisammo;
113 }
114 return 0;
115 }
116}
117
119{
120 if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
121 return false;
123 return false;
124 if (g_weaponarena)
125 return false;
126 if (w == WEP_Null.m_id)
127 return false;
128
129 return (REGISTRY_GET(Weapons, w)).weaponthrowable;
130}
131
132// toss current weapon
133void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
134{
135 Weapon w = this.(weaponentity).m_weapon;
136 if (w == WEP_Null)
137 return; // just in case
138 if (time < game_starttime)
139 return;
140 if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
141 return;
143 return;
144 if(this.(weaponentity).state != WS_READY)
145 return;
146 if(!W_IsWeaponThrowable(this, w.m_id))
147 return;
148
149 WepSet set = WepSet_FromWeapon(w);
150 if(!(STAT(WEAPONS, this) & set)) return;
151 STAT(WEAPONS, this) &= ~set;
152
153 W_SwitchWeapon_Force(this, w_getbestweapon(this, weaponentity), weaponentity);
154 float a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo, weaponentity);
155
156 if(a < 0) return;
157 Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, w.m_id, a);
158}
159
160void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
161{
162 //entity wep = this.(weaponentity).m_weapon;
163
164 if(STAT(WEAPONS, this) & WepSet_FromWeapon(wep))
165 if(W_IsWeaponThrowable(this, wep.m_id))
166 W_ThrowNewWeapon(this, wep.m_id, false, org, randomvec() * 125 + '0 0 200', weaponentity);
167}
void navigation_dynamicgoal_init(entity this, bool initially_static)
Definition navigation.qc:77
#define MUTATOR_CALLHOOK(id,...)
Definition base.qh:143
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
void SetResource(entity e, Resource res_type, float amount)
Sets the current amount of resource the given entity will have.
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
fields which are explicitly/manually set are marked with "M", fields set automatically are marked wit...
Definition weapon.qh:44
Resource ammo_type
M: ammotype : main ammo type.
Definition weapon.qh:51
int m_id
Definition weapon.qh:45
const int IT_UNLIMITED_SUPERWEAPONS
Definition item.qh:24
float game_starttime
Definition stats.qh:82
const int FL_TOSSED
Definition constants.qh:86
float time
#define spawn
void W_SwitchWeapon_Force(Player this, Weapon w,.entity weaponentity)
Definition selection.qc:246
int state
Weapons
Definition guide.qh:113
Header file that describes the functions related to game items.
#define ITEM_SET_EXPIRING(item, expiring)
Sets the item expiring status (i.e.
Definition spawning.qh:65
#define ITEM_SET_LOOT(item, loot)
Sets the item loot status.
Definition spawning.qh:45
bool startitem_failed
Definition spawning.qh:7
#define FOREACH(list, cond, body)
Definition iter.qh:19
#define STAT(...)
Definition stats.qh:82
vector randomvec(void)
float min(float f,...)
@ STATUSEFFECT_REMOVE_CLEAR
Effect is being forcibly removed without calling any additional mechanics.
Definition all.qh:30
void Send_Notification(NOTIF broadcast, entity client, MSG net_type, Notification net_name,...count)
Definition all.qc:1573
#define REGISTRY_GET(id, i)
Definition registry.qh:43
#define w_getbestweapon(ent, wepent)
Definition selection.qh:23
vector
Definition self.qh:92
vector org
Definition self.qh:92
float autocvar_g_balance_superweapons_time
Definition items.qh:6
int autocvar_g_pickup_items
Definition items.qh:10
void StatusEffects_remove(StatusEffect this, entity actor, int removal_type)
float StatusEffects_gettime(StatusEffect this, entity actor)
void StatusEffects_update(entity e)
void GiveResource(entity receiver, Resource res_type, float amount)
Gives an entity some resource.
Header file that describes the resource system.
void W_ThrowWeapon(entity this,.entity weaponentity, vector velo, vector delta, float doreduce)
Definition throwing.qc:133
void SpawnThrownWeapon(entity this, vector org, Weapon wep,.entity weaponentity)
Definition throwing.qc:160
bool W_IsWeaponThrowable(entity this, int w)
Definition throwing.qc:118
float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo,.entity weaponentity)
Definition throwing.qc:22
bool autocvar_g_weapon_throwable
Definition throwing.qh:6
WepSet WEPSET_SUPERWEAPONS
Definition all.qh:329
#define WepSet_FromWeapon(it)
Definition all.qh:46
const int WS_READY
idle frame
Definition weapon.qh:41
void weapon_defaultspawnfunc(entity this, Weapon e)
Definition spawning.qc:30
vector WepSet
Definition weapon.qh:14
void W_DropEvent(.void(Weapon, entity actor,.entity) event, entity player, int weapon_type, entity weapon_item,.entity weaponentity)
float weapon_load[REGISTRY_MAX(Weapons)]
Weapon m_weapon
Definition wepent.qh:26
float g_weaponarena
Definition world.qh:75
float g_weapon_stay
Definition world.qh:109