ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/object.C
(Generate patch)

Comparing deliantra/server/common/object.C (file contents):
Revision 1.323 by root, Sun Apr 11 23:56:09 2010 UTC vs.
Revision 1.330 by root, Sun Apr 18 14:01:33 2010 UTC

36static const uint64 UUID_GAP = 1<<19; 36static const uint64 UUID_GAP = 1<<19;
37uint32_t mapspace::smellcount = 10000; 37uint32_t mapspace::smellcount = 10000;
38 38
39objectvec objects; 39objectvec objects;
40activevec actives; 40activevec actives;
41
42freelist_item *object::freelist;
43uint32_t object::object_count;
44uint32_t object::free_count;
45uint32_t object::create_count;
46uint32_t object::destroy_count;
41 47
42//+GPL 48//+GPL
43 49
44short freearr_x[SIZEOFFREE] = { 50short freearr_x[SIZEOFFREE] = {
45 0, 51 0,
477 483
478 sum = weight_adjust_for (this, sum); 484 sum = weight_adjust_for (this, sum);
479 485
480 if (sum != carrying) 486 if (sum != carrying)
481 { 487 {
488 if (carrying != sum)//D
489 LOG (llevDebug, "updating weight got %ld, expected %ld (%s)\n",
490 (long long)sum, (long long)carrying, debug_desc ());
491
482 carrying = sum; 492 carrying = sum;
483 493
484 if (object *pl = visible_to ()) 494 if (object *pl = visible_to ())
485 if (pl != this) // player is handled lazily 495 if (pl != this) // player is handled lazily
486 esrv_update_item (UPD_WEIGHT, pl, this); 496 esrv_update_item (UPD_WEIGHT, pl, this);
809 unlink (); 819 unlink ();
810 820
811 free_key_values (this); 821 free_key_values (this);
812} 822}
813 823
814static int object_count;
815
816void object::link () 824void object::link ()
817{ 825{
818 assert (!index);//D 826 assert (!index);//D
819 uuid = UUID::gen (); 827 uuid = UUID::gen ();
820 count = ++object_count;
821 828
822 refcnt_inc (); 829 refcnt_inc ();
823 objects.insert (this); 830 objects.insert (this);
831
832 ++create_count;
833
824} 834}
825 835
826void object::unlink () 836void object::unlink ()
827{ 837{
828 if (!index) 838 if (!index)
829 return; 839 return;
840
841 ++destroy_count;
830 842
831 objects.erase (this); 843 objects.erase (this);
832 refcnt_dec (); 844 refcnt_dec ();
833} 845}
834 846
960 // then destroy 972 // then destroy
961 op->destroy (); 973 op->destroy ();
962 } 974 }
963} 975}
964 976
977void
978object::freelist_free (int count)
979{
980 while (count-- && freelist)
981 {
982 freelist_item *next = freelist->next;
983 // count is being "destroyed"
984
985 sfree ((char *)freelist, sizeof (object));
986
987 freelist = next;
988 --free_count;
989 }
990}
991
992object *
965object *object::create () 993object::create ()
966{ 994{
967 object *op = new object; 995 object *op;
996
997 if (freelist)
998 {
999 freelist_item li = *freelist;
1000 memset (freelist, 0, sizeof (object));
1001
1002 op = new (freelist) object;
1003 op->count = li.count;
1004
1005 freelist = li.next;
1006 --free_count;
1007 }
1008 else
1009 {
1010 void *ni = salloc0<char> (sizeof (object));
1011
1012 op = new(ni) object;
1013
1014 op->count = ++object_count;
1015 }
1016
968 op->link (); 1017 op->link ();
1018
969 return op; 1019 return op;
970} 1020}
971 1021
1022void
1023object::do_delete ()
1024{
1025 uint32_t count = this->count;
1026
1027 this->~object ();
1028
1029 freelist_item *li = (freelist_item *)this;
1030 li->next = freelist;
1031 li->count = count;
1032
1033 freelist = li;
1034 ++free_count;
1035}
1036
972static struct freed_map : maptile 1037static struct freed_map : maptile
973{ 1038{
974 freed_map () 1039 freed_map ()
1040 : maptile (3, 3)
975 { 1041 {
976 path = "<freed objects map>"; 1042 path = "<freed objects map>";
977 name = "/internal/freed_objects_map"; 1043 name = "/internal/freed_objects_map";
978 width = 3;
979 height = 3;
980 no_drop = 1; 1044 no_drop = 1;
981 no_reset = 1; 1045 no_reset = 1;
982 1046
983 alloc ();
984 in_memory = MAP_ACTIVE; 1047 in_memory = MAP_ACTIVE;
985 } 1048 }
986 1049
987 ~freed_map () 1050 ~freed_map ()
988 { 1051 {
2155{ 2218{
2156 return (ob1->x - ob2->x) * (ob1->x - ob2->x) + (ob1->y - ob2->y) * (ob1->y - ob2->y); 2219 return (ob1->x - ob2->x) * (ob1->x - ob2->x) + (ob1->y - ob2->y) * (ob1->y - ob2->y);
2157} 2220}
2158 2221
2159/* 2222/*
2160 * find_dir_2(delta-x,delta-y) will return a direction in which 2223 * find_dir_2(delta-x,delta-y) will return a direction value
2161 * an object which has subtracted the x and y coordinates of another 2224 * for running into direct [dx, dy].
2162 * object, needs to travel toward it. 2225 * (the opposite of crossfire's find_dir_2!)
2163 */ 2226 */
2164int 2227int
2165find_dir_2 (int x, int y) 2228find_dir_2 (int x, int y)
2166{ 2229{
2230#if 1 // new algorithm
2231 // this works by putting x, y into 16 sectors, which
2232 // are not equal sized, but are a better approximation
2233 // then the old algorithm, and then using a mapping
2234 // table to map it into a direction value.
2235
2236 static const uint8 dir[16] = {
2237 4, 5, 4, 3,
2238 2, 1, 2, 3,
2239 6, 5, 6, 7,
2240 8, 1, 8, 7,
2241 };
2242 int sector = 0;
2243
2244 // this is a bit ugly, but more likely to result in branchless code
2245 sector |= x < 0 ? 8 : 0;
2246 x = x < 0 ? -x : x; // abs
2247
2248 sector |= y < 0 ? 4 : 0;
2249 y = y < 0 ? -y : y; // abs
2250
2251 if (x > y)
2252 {
2253 sector |= 2;
2254
2255 if (x > y * 2)
2256 sector |= 1;
2257 }
2258 else
2259 {
2260 if (y > x * 2)
2261 sector |= 1;
2262 else if (!y)
2263 return 0; // x == 0 here
2264 }
2265
2266 return dir [sector];
2267#else // old algorithm
2167 int q; 2268 int q;
2168 2269
2169 if (y) 2270 if (y)
2170 q = x * 100 / y; 2271 q = 128 * x / y;
2171 else if (x) 2272 else if (x)
2172 q = -300 * x; 2273 q = -512 * x; // to make it > 309
2173 else 2274 else
2174 return 0; 2275 return 0;
2175 2276
2176 if (y > 0) 2277 if (y > 0)
2177 { 2278 {
2178 if (q < -242) 2279 if (q < -309) return 7;
2280 if (q < -52) return 6;
2281 if (q < 52) return 5;
2282 if (q < 309) return 4;
2283
2179 return 3; 2284 return 3;
2180 if (q < -41) 2285 }
2181 return 2; 2286 else
2182 if (q < 41) 2287 {
2183 return 1; 2288 if (q < -309) return 3;
2184 if (q < 242) 2289 if (q < -52) return 2;
2185 return 8; 2290 if (q < 52) return 1;
2291 if (q < 309) return 8;
2292
2186 return 7; 2293 return 7;
2187 } 2294 }
2188 2295#endif
2189 if (q < -242)
2190 return 7;
2191 if (q < -41)
2192 return 6;
2193 if (q < 41)
2194 return 5;
2195 if (q < 242)
2196 return 4;
2197
2198 return 3;
2199} 2296}
2200 2297
2201/* 2298/*
2202 * dirdiff(dir1, dir2) returns how many 45-degrees differences there is 2299 * dirdiff(dir1, dir2) returns how many 45-degrees differences there is
2203 * between two directions (which are expected to be absolute (see absdir()) 2300 * between two directions (which are expected to be absolute (see absdir())
2204 */ 2301 */
2205int 2302int
2206dirdiff (int dir1, int dir2) 2303dirdiff (int dir1, int dir2)
2207{ 2304{
2208 int d;
2209
2210 d = abs (dir1 - dir2); 2305 int d = abs (dir1 - dir2);
2211 if (d > 4)
2212 d = 8 - d;
2213 2306
2214 return d; 2307 return d > 4 ? 8 - d : d;
2215} 2308}
2216 2309
2217/* peterm: 2310/* peterm:
2218 * do LOS stuff for ball lightning. Go after the closest VISIBLE monster. 2311 * do LOS stuff for ball lightning. Go after the closest VISIBLE monster.
2219 * Basically, this is a table of directions, and what directions 2312 * Basically, this is a table of directions, and what directions
2677 } 2770 }
2678 else 2771 else
2679 move_type = mt; 2772 move_type = mt;
2680} 2773}
2681 2774
2775/* object should be a player.
2776 * we return the object the player has marked with the 'mark' command
2777 * below. If no match is found (or object has changed), we return
2778 * NULL. We leave it up to the calling function to print messages if
2779 * nothing is found.
2780 */
2781object *
2782object::mark () const
2783{
2784 if (contr && contr->mark && contr->mark->env == this)
2785 return contr->mark;
2786 else
2787 return 0;
2788}
2789

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines