]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/rubble.qh
Merge branch 'master' into terencehill/min_spec_time
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / rubble.qh
1 #pragma once
2
3 #ifdef CSQC
4
5 entityclass(Rubble);
6 classfield(Rubble).float creationtime;
7
8 IntrusiveList g_rubble;
9 STATIC_INIT(g_rubble) { g_rubble = IL_NEW(); }
10
11 void RubbleLimit(string cname, int limit, void(entity) deleteproc)
12 {
13         // remove rubble of the same type if it's at the limit
14         // remove multiple rubble if the limit has been decreased
15         while (1)
16         {
17                 // walk the list and count the entities, find the oldest
18                 // initialize our search with the first entity
19                 int c = 0;
20                 entity oldest = NULL;
21                 float oldesttime = 0;
22                 // compare to all other matching entities
23                 IL_EACH(g_rubble, it.classname == cname,
24                 {
25                         ++c;
26                         if(!oldest || oldesttime > it.creationtime)
27                         {
28                                 oldest = it;
29                                 oldesttime = it.creationtime;
30                         }
31                 });
32
33                 // stop if there are less than the limit already
34                 if (c <= limit) break;
35
36                 // delete this oldest one and search again
37                 deleteproc(oldest);
38         }
39 }
40
41 entity RubbleNew(string cname)
42 {
43         // spawn a new entity and return it
44         entity e = spawn();
45         e.classname = cname;
46         e.creationtime = time;
47         IL_PUSH(g_rubble, e);
48         return e;
49 }
50
51 #endif