]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/rubble.qc
3363f8971ec6e5d6dd44dd89f2b53a1fd5d1551a
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / rubble.qc
1 #if defined(CSQC)
2         #include "../dpdefs/csprogsdefs.qh"
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5 #endif
6
7 .float creationtime;
8
9 // LordHavoc: rewrote this file, it was really bad code
10
11 void RubbleLimit(string cname, float limit, void() deleteproc)
12 {
13         entity e;
14         entity oldest;
15         entity oldself;
16         float c;
17         float oldesttime;
18
19         oldself = self;
20
21         // remove rubble of the same type if it's at the limit
22         // remove multiple rubble if the limit has been decreased
23         while(1)
24         {
25                 e = findchain(classname,cname);
26                 if (e == world)
27                         break;
28                 // walk the list and count the entities, find the oldest
29                 // initialize our search with the first entity
30                 c = 1;
31                 oldest = e;
32                 oldesttime = e.creationtime;
33                 e = e.chain;
34                 // compare to all other matching entities
35                 while (e)
36                 {
37                         c = c + 1;
38                         if (oldesttime > e.creationtime)
39                         {
40                                 oldesttime = e.creationtime;
41                                 oldest = e;
42                         }
43                         e = e.chain;
44                 }
45
46                 // stop if there are less than the limit already
47                 if (c <= limit)
48                         break;
49
50                 // delete this oldest one and search again
51                 self = oldest;
52                 deleteproc();
53                 self = oldself;
54         }
55 }
56
57 entity RubbleNew(string cname)
58 {
59         entity e;
60         // spawn a new entity and return it
61         e = spawn();
62         e.classname = cname;
63         e.creationtime = time;
64         return e;
65 }