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