convert line ends
[canaan.git] / prj / cam / src / editor / objhistp.cpp
blobd3c5e60bad1c86dd403c57f4194448cb4d501132
1 /*
2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
4 */
6 // $Header: r:/t2repos/thief2/src/editor/objhistp.cpp,v 1.5 2000/02/19 13:11:08 toml Exp $
8 #include <appagg.h>
9 #include <aggmemb.h>
11 #include <hashpp.h>
12 #include <hashfunc.h>
13 #include <hshpptem.h>
14 #include <mprintf.h>
16 #include <traitman.h>
17 #include <iobjsys.h>
18 #include <objquery.h>
19 #include <simpwrap.h>
20 #include <objdef.h>
21 #include <editobj.h>
23 #include <objhistp.h>
24 #include <memall.h>
25 #include <dbmem.h> // must be last header!
27 #define STRLEN 64
29 typedef struct {
30 int count;
31 ObjID arch;
32 } ObjHistData;
34 typedef cHashTableFunctions<ObjID> ObjNameHashFunctions;
35 typedef cHashTable<ObjID, int, ObjNameHashFunctions> cObjHistCBTable;
37 #ifdef _MSC_VER
38 template cObjHistCBTable;
39 #endif
41 static cObjHistCBTable objs_used;
43 int ObjHistCompare(const void *p, const void *q)
45 ObjHistData *a = (ObjHistData *) p;
46 ObjHistData *b = (ObjHistData *) q;
48 if (a->count > b->count)
49 return -1;
50 else
51 return a->count < b->count;
54 static int BuildObjData(char ***objstrings, ObjHistData **histdata)
56 int entries;
57 IObjectQuery* pQuery;
58 IObjectSystem* pOS = AppGetObj(IObjectSystem);
59 ITraitManager* traitman = AppGetObj(ITraitManager);
60 int i,count;
61 int total = 0;
62 ObjID obj,arch;
63 cObjHistCBTable::cIter hashiter;
65 objs_used.Clear();
67 // okay, sift through every object in the world and collect stats
68 pQuery = pOS->Iter(kObjectConcrete);
69 while (!pQuery->Done())
71 obj = pQuery->Object();
73 // reject transients, like texture concretes
74 if (pOS->ObjIsTransient(obj))
76 pQuery->Next();
77 continue;
80 arch = traitman->GetArchetype(obj);
81 if ((obj != OBJ_NULL) && (arch != OBJ_NULL))
83 count = objs_used.Search(arch);
84 objs_used.Set(arch,count+1);
85 ++total;
87 pQuery->Next();
90 // take those stats and generate a list
91 entries = objs_used.nElems();
92 // extra entry for total
93 *objstrings = (char **)Malloc(sizeof(char *) * (entries+1));
94 for (i=0; i < entries+1; i++)
95 (*objstrings)[i] = (char *)Malloc(sizeof(char) * STRLEN);
97 *histdata = (ObjHistData *)Malloc(sizeof(ObjHistData) * entries);
99 hashiter = objs_used.Iter();
100 count = 0;
101 while (!hashiter.Done())
103 (*histdata)[count].arch = hashiter.Key();
104 (*histdata)[count].count = hashiter.Value();
105 count++;
106 hashiter.Next();
109 SafeRelease(pQuery);
110 SafeRelease(pOS);
111 return total;
115 // handy dandy object histogram palette
116 void popup_obj_histogram(void)
118 int count;
119 char **objstrings;
120 ObjHistData *histdata;
121 int entries;
122 int i,choice;
123 ObjID arch;
124 IObjectSystem* pOS = AppGetObj(IObjectSystem);
125 int total;
127 total = BuildObjData(&objstrings,&histdata);
129 entries = objs_used.nElems();
131 // sort the list
132 qsort(histdata, entries, sizeof(ObjHistData), ObjHistCompare);
134 sprintf(objstrings[0],"Total %d", total);
135 // form the stringlist
136 for (count=0; count < entries; count++)
138 sprintf(objstrings[count+1],"(%02d) %03d %s",count, histdata[count].count,pOS->GetName(histdata[count].arch));
140 // make the selection
141 choice = PickFromStringList("Object Histogram", (const char* const*)objstrings, entries+1);
143 // now set the default object
144 if (choice >= 0)
146 arch = histdata[choice].arch;
147 editObjSetDefaultArchetype(arch);
150 // some cleanup
151 for (i=0; i < entries; i++)
152 Free(objstrings[i]);
153 Free(objstrings);
154 Free(histdata);
156 SafeRelease(pOS);
159 // simple alphabetical list of placed objects
160 void popup_obj_alpha(void)
162 int count;
163 char **objstrings;
164 ObjHistData *histdata;
165 int entries;
166 int i,choice;
167 ObjID arch;
168 IObjectSystem* pOS = AppGetObj(IObjectSystem);
170 BuildObjData(&objstrings,&histdata);
171 entries = objs_used.nElems();
173 // form the stringlist
174 for (count=0; count < entries; count++)
176 sprintf(objstrings[count],"%s %03d",pOS->GetName(histdata[count].arch),histdata[count].count);
179 // make the selection
180 choice = PickFromStringList("Object Alpha List", (const char* const*)objstrings, entries);
182 // now set the default object
183 if (choice != -1)
185 arch = histdata[choice].arch;
186 editObjSetDefaultArchetype(arch);
189 // some cleanup
190 for (i=0; i < entries; i++)
191 Free(objstrings[i]);
192 Free(objstrings);
193 Free(histdata);
195 SafeRelease(pOS);