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