]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/rubble.qh
Replace most cases of findchain* with the FOREACH_ENTITY_* macros
[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         // remove rubble of the same type if it's at the limit
12         // remove multiple rubble if the limit has been decreased
13         while (1)
14         {
15                 // walk the list and count the entities, find the oldest
16                 // initialize our search with the first entity
17                 int c = 0;
18                 entity oldest = NULL;
19                 float oldesttime = 0;
20                 // compare to all other matching entities
21                 FOREACH_ENTITY_CLASS(cname, true,
22                 {
23                         ++c;
24                         if(!oldest || oldesttime > it.creationtime)
25                         {
26                                 oldest = it;
27                                 oldesttime = it.creationtime;
28                         }
29                 });
30
31                 // stop if there are less than the limit already
32                 if (c <= limit) break;
33
34                 // delete this oldest one and search again
35                 deleteproc(oldest);
36         }
37 }
38
39 entity RubbleNew(string cname)
40 {
41         // spawn a new entity and return it
42         entity e = spawn();
43         e.classname = cname;
44         e.creationtime = time;
45         return e;
46 }
47
48 #endif
49
50 #endif