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
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 = colormapPaletteColor(wep.owner.clientcolors & 0x0F, true);
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 wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
44 else
45 {
46 int superweapons = 1;
47 FOREACH(Weapons, it != WEP_Null,
48 {
49 WepSet set = it.m_wepset;
50 if ((WEPSET_SUPERWEAPONS & STAT(WEAPONS, own)) & set)
51 ++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 wep.itemdef = info.m_pickup;
74 StartItem(wep);
75 if (wasfreed(wep))
76 return -1;
77
78 wep.pickup_anyway = true; // these are ALWAYS pickable
79
80 //wa = W_AmmoItemCode(wpn);
81 if (ammotype == RES_NONE)
82 return 0;
83 else
84 {
85 SetResource(wep, ammotype, WEP_CVAR(info, pickup_ammo));
86
87 if (doreduce && g_weapon_stay == 2)
88 {
89 // if our weapon is loaded, give its load back to the player
90 int i = own.(weaponentity).m_weapon.m_id;
91 if (own.(weaponentity).(weapon_load[i]) > 0)
92 {
93 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
94 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
95 }
96 SetResource(wep, ammotype, 0);
97 }
98 else if (doreduce)
99 {
100 // if our weapon is loaded, give its load back to the player
101 int i = own.(weaponentity).m_weapon.m_id;
102 if (own.(weaponentity).(weapon_load[i]) > 0)
103 {
104 GiveResource(own, ammotype, own.(weaponentity).(weapon_load[i]));
105 own.(weaponentity).(weapon_load[i]) = -1; // schedule the weapon for reloading
106 }
107
108 float ownderammo = GetResource(own, ammotype);
109 float thisammo = min(ownderammo, GetResource(wep, ammotype));
110 SetResource(wep, ammotype, thisammo);
111 SetResource(own, ammotype, ownderammo - thisammo);
112
113 return thisammo;
114 }
115 return 0;
116 }
117}
118
120{
121 if (MUTATOR_CALLHOOK(ForbidDropCurrentWeapon, this, w))
122 return false;
124 return false;
125 if (w == WEP_Null.m_id)
126 return false;
127
128 return (REGISTRY_GET(Weapons, w)).weaponthrowable;
129}
130
132void W_ThrowWeapon(entity this, .entity weaponentity, vector velo, vector delta, float doreduce)
133{
134 Weapon w = this.(weaponentity).m_weapon;
135 if (w == WEP_Null)
136 return; // just in case
137 if (time < game_starttime)
138 return;
139 if (MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon, this, this.(weaponentity)))
140 return;
141 if (!autocvar_g_weapon_throwable || this.(weaponentity).state != WS_READY || !W_IsWeaponThrowable(this, w.m_id))
142 return;
143
144 WepSet set = WepSet_FromWeapon(w);
145 if (!(STAT(WEAPONS, this) & set))
146 return;
147 STAT(WEAPONS, this) &= ~set;
148
149 W_SwitchWeapon_Force(this, w_getbestweapon(this, weaponentity), weaponentity);
150 float a = W_ThrowNewWeapon(this, w.m_id, doreduce, this.origin + delta, velo, weaponentity);
151
152 if (a < 0)
153 return;
154 Send_Notification(NOTIF_ONE, this, MSG_MULTI, ITEM_WEAPON_DROP, w.m_id, a);
155}
156
157void SpawnThrownWeapon(entity this, vector org, Weapon wep, .entity weaponentity)
158{
159 //entity wep = this.(weaponentity).m_weapon;
160
161 if ((STAT(WEAPONS, this) & WepSet_FromWeapon(wep))
162 && W_IsWeaponThrowable(this, wep.m_id))
163 W_ThrowNewWeapon(this, wep.m_id, false, org, randomvec() * 125 + '0 0 200', weaponentity);
164}
void navigation_dynamicgoal_init(entity this, bool initially_static)
Definition navigation.qc:77
#define MUTATOR_CALLHOOK(name,...)
Definition base.qh:186
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:36
Resource ammo_type
M: ammotype : main ammo type.
Definition weapon.qh:50
int m_id
Definition weapon.qh:37
#define colormapPaletteColor(c, isPants)
Definition color.qh:5
const int IT_UNLIMITED_SUPERWEAPONS
Definition item.qh:24
float game_starttime
Definition stats.qh:82
const int FL_TOSSED
Definition constants.qh:78
float time
#define spawn
void W_SwitchWeapon_Force(Player this, Weapon w,.entity weaponentity)
Definition selection.qc:237
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:63
#define ITEM_SET_LOOT(item, loot)
Sets the item loot status.
Definition spawning.qh:43
#define FOREACH(list, cond, body)
Definition iter.qh:19
#define STAT(...)
Definition stats.qh:94
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:1500
#define REGISTRY_GET(id, i)
Definition registry.qh:62
#define w_getbestweapon(ent, wepent)
Definition selection.qh:23
vector
Definition self.qh:96
vector org
Definition self.qh:96
void StartItem(entity this)
Definition items.qc:1008
float autocvar_g_balance_superweapons_time
Definition items.qh:7
int autocvar_g_pickup_items
Definition items.qh:11
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)
Toss current weapon.
Definition throwing.qc:132
void SpawnThrownWeapon(entity this, vector org, Weapon wep,.entity weaponentity)
Definition throwing.qc:157
bool W_IsWeaponThrowable(entity this, int w)
Definition throwing.qc:119
float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo,.entity weaponentity)
Returns amount of ammo used, or -1 for failure, or 0 for no ammo count.
Definition throwing.qc:22
bool autocvar_g_weapon_throwable
Definition throwing.qh:6
#define WEP_CVAR(wep, name)
Definition all.qh:339
WepSet WEPSET_SUPERWEAPONS
Definition all.qh:347
#define WepSet_FromWeapon(it)
Definition all.qh:48
@ WEPARENA_OFF
Definition weapon.qh:272
const int WS_READY
idle frame
Definition weapon.qh:32
vector WepSet
Definition weapon.qh:13
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_weapon_stay
Definition world.qh:108
int g_weaponarena_type
Definition world.qh:77