]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/rubble.qc
snaaaaake
[divverent/nexuiz.git] / data / qcsrc / client / rubble.qc
1 .float creationtime;
2
3 // LordHavoc: rewrote this file, it was really bad code
4
5 void RubbleLimit(string cname, float limit, void() deleteproc)
6 {
7         entity e;
8         entity oldest;
9         entity oldself;
10         float c;
11         float oldesttime;
12
13         oldself = self;
14
15         // remove rubble of the same type if it's at the limit
16         // remove multiple rubble if the limit has been decreased
17         while(1)
18         {
19                 e = findchain(classname,cname);
20                 if (e == world)
21                         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)
42                         break;
43
44                 // delete this oldest one and search again
45                 self = oldest;
46                 deleteproc();
47                 self = oldself;
48         }
49 }
50
51 entity RubbleNew(string cname)
52 {
53         entity e;
54         // spawn a new entity and return it
55         e = spawn();
56         e.classname = cname;
57         e.creationtime = time;
58         return e;
59 }