Xonotic
spawning.qc
Go to the documentation of this file.
1 #include "spawning.qh"
2 
7 
9 #include <common/weapons/all.qh>
10 #include <server/items/items.qh>
11 #include <server/mutators/_mod.qh>
13 
14 .bool m_isloot;
15 .bool m_isexpiring;
18 
19 entity Item_FindDefinition(string class_name)
20 {
21  FOREACH(Items, it.m_canonical_spawnfunc == class_name,
22  {
23  return it;
24  });
25  FOREACH(Weapons, it.m_canonical_spawnfunc == class_name,
26  {
27  return it.m_pickup;
28  });
29  return NULL;
30 }
31 
33 {
34  FOREACH(Items, it.netname == item_name,
35  {
36  return it;
37  });
38  FOREACH(Weapons, it.netname == item_name,
39  {
40  return it.m_pickup;
41  });
42  return NULL;
43 }
44 
45 bool Item_IsAllowed(string class_name)
46 {
47  entity definition = Item_FindDefinition(class_name);
48  if (definition == NULL)
49  {
50  return false;
51  }
52  return Item_IsDefinitionAllowed(definition);
53 }
54 
56 {
57  return !MUTATOR_CALLHOOK(FilterItemDefinition, definition);
58 }
59 
60 entity Item_Create(string class_name, vector position, bool no_align)
61 {
62  entity item = spawn();
63  item.classname = class_name;
64  item.spawnfunc_checked = true;
65  setorigin(item, position);
66  item.noalign = no_align;
67  Item_Initialize(item, class_name);
68  if (wasfreed(item))
69  {
70  return NULL;
71  }
72  return item;
73 }
74 
75 void Item_Initialize(entity item, string class_name)
76 {
77  FOREACH(Weapons, it.m_canonical_spawnfunc == class_name,
78  {
79  weapon_defaultspawnfunc(item, it);
80  return;
81  });
82  FOREACH(Items, it.m_canonical_spawnfunc == class_name,
83  {
84  StartItem(item, it);
85  return;
86  });
87  LOG_FATALF("Item_Initialize: Invalid classname: %s", class_name);
88 }
89 
90 entity Item_CreateLoot(string class_name, vector position, vector vel,
91  float time_to_live)
92 {
93  entity item = spawn();
94  if (!Item_InitializeLoot(item, class_name, position, vel, time_to_live))
95  {
96  return NULL;
97  }
98  return item;
99 }
100 
101 bool Item_InitializeLoot(entity item, string class_name, vector position,
102  vector vel, float time_to_live)
103 {
104  item.classname = class_name;
105  Item_SetLoot(item, true);
106  item.noalign = true;
107  setorigin(item, position);
108  item.pickup_anyway = true;
109  item.spawnfunc_checked = true;
110  Item_Initialize(item, class_name);
111  if (wasfreed(item))
112  {
113  return false;
114  }
115  item.gravity = 1;
116  item.velocity = vel;
117  SUB_SetFade(item, time + time_to_live, 1);
118  return true;
119 }
120 
121 bool Item_IsLoot(entity item)
122 {
123  return item.m_isloot || item.classname == "droppedweapon";
124 }
125 
126 void Item_SetLoot(entity item, bool loot)
127 {
128  item.m_isloot = loot;
129 }
130 
132 {
133  return item.noalign || (item.spawnflags & 1);
134 }
135 
137 {
138  return item.m_isexpiring;
139 }
140 
141 void Item_SetExpiring(entity item, bool expiring)
142 {
143  item.m_isexpiring = expiring;
144 }
145 
146 // Compatibility spawn functions
147 
148 // in Quake this is green armor, in Xonotic maps it is an armor shard
149 SPAWNFUNC_ITEM_COND(item_armor1, cvar("sv_mapformat_is_quake3"), ITEM_ArmorSmall, ITEM_ArmorMedium)
150 
151 SPAWNFUNC_ITEM(item_armor25, ITEM_ArmorMega)
152 
153 SPAWNFUNC_ITEM(item_armor_large, ITEM_ArmorMega)
154 
155 SPAWNFUNC_ITEM(item_health1, ITEM_HealthSmall)
156 
157 SPAWNFUNC_ITEM(item_health25, ITEM_HealthMedium)
158 
159 SPAWNFUNC_ITEM(item_health_large, ITEM_HealthBig)
160 
161 SPAWNFUNC_ITEM(item_health100, ITEM_HealthMega)
bool Item_IsDefinitionAllowed(entity definition)
Checks whether the items with the specified definition are allowed to spawn.
Definition: spawning.qc:55
entity Item_DefinitionFromInternalName(string item_name)
Returns the item definition corresponding to the given internal name.
Definition: spawning.qc:32
SPAWNFUNC_ITEM(item_armor_green, ITEM_ArmorMedium) .float wait
void Item_Initialize(entity item, string class_name)
Initializes the item according to class name.
Definition: spawning.qc:75
bool Item_InitializeLoot(entity item, string class_name, vector position, vector vel, float time_to_live)
Initializes the loot item.
Definition: spawning.qc:101
entity Item_FindDefinition(string class_name)
Returns the item definition corresponding to the given class name.
Definition: spawning.qc:19
entity() spawn
bool m_isexpiring
Holds whether strength, shield or superweapon timers expire while this item is on the ground...
Definition: spawning.qc:17
bool m_isloot
Holds whether item is loot.
Definition: spawning.qc:14
#define NULL
Definition: post.qh:17
vector(float skel, float bonenum) _skel_get_boneabs_hidden
bool Item_ShouldKeepPosition(entity item)
Returns whether item should keep its position or be dropped to the ground.
Definition: spawning.qc:131
#define LOG_FATALF(...)
Definition: log.qh:59
entity Item_Create(string class_name, vector position, bool no_align)
Creates a new item.
Definition: spawning.qc:60
#define MUTATOR_CALLHOOK(id,...)
Definition: base.qh:140
setorigin(ent, v)
bool Item_IsLoot(entity item)
Returns whether the item is loot.
Definition: spawning.qc:121
void SUB_SetFade(entity ent, float vanish_time, float fading_time)
Definition: subs.qc:77
Header file that describes the functions related to game items.
float time
Definition: csprogsdefs.qc:16
void Item_SetExpiring(entity item, bool expiring)
Sets the item expiring status (i.e.
Definition: spawning.qc:141
bool Item_IsExpiring(entity item)
Returns whether the item is expiring (i.e.
Definition: spawning.qc:136
#define FOREACH(list, cond, body)
Definition: iter.qh:19
bool Item_IsAllowed(string class_name)
Checks whether the items with the specified class name are allowed to spawn.
Definition: spawning.qc:45
entity Item_CreateLoot(string class_name, vector position, vector vel, float time_to_live)
Creates a loot item.
Definition: spawning.qc:90
void Item_SetLoot(entity item, bool loot)
Sets the item loot status.
Definition: spawning.qc:126