… | |
… | |
36 | static const uint64 UUID_GAP = 1<<19; |
36 | static const uint64 UUID_GAP = 1<<19; |
37 | uint32_t mapspace::smellcount = 10000; |
37 | uint32_t mapspace::smellcount = 10000; |
38 | |
38 | |
39 | objectvec objects; |
39 | objectvec objects; |
40 | activevec actives; |
40 | activevec actives; |
|
|
41 | |
|
|
42 | freelist_item *object::freelist; |
|
|
43 | uint32_t object::object_count; |
|
|
44 | uint32_t object::free_count; |
|
|
45 | uint32_t object::create_count; |
|
|
46 | uint32_t object::destroy_count; |
41 | |
47 | |
42 | //+GPL |
48 | //+GPL |
43 | |
49 | |
44 | short freearr_x[SIZEOFFREE] = { |
50 | short 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 | |
814 | static int object_count; |
|
|
815 | |
|
|
816 | void object::link () |
824 | void 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 | |
826 | void object::unlink () |
836 | void 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 | |
|
|
977 | void |
|
|
978 | object::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 | |
|
|
992 | object * |
965 | object *object::create () |
993 | object::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 | |
|
|
1022 | void |
|
|
1023 | object::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 | |
972 | static struct freed_map : maptile |
1037 | static 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 | */ |
2164 | int |
2227 | int |
2165 | find_dir_2 (int x, int y) |
2228 | find_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 | // basically, it maps these comparisons to each bit |
|
|
2236 | // bit #3: x < 0 |
|
|
2237 | // bit #2: y < 0 |
|
|
2238 | // bit #1: x > y |
|
|
2239 | // bit #0: x > 2y |
|
|
2240 | |
|
|
2241 | static const uint8 dir[16] = { |
|
|
2242 | 4, 5, 4, 3, |
|
|
2243 | 2, 1, 2, 3, |
|
|
2244 | 6, 5, 6, 7, |
|
|
2245 | 8, 1, 8, 7, |
|
|
2246 | }; |
|
|
2247 | int sector = 0; |
|
|
2248 | |
|
|
2249 | // this is a bit ugly, but more likely to result in branchless code |
|
|
2250 | sector |= x < 0 ? 8 : 0; |
|
|
2251 | x = x < 0 ? -x : x; // abs |
|
|
2252 | |
|
|
2253 | sector |= y < 0 ? 4 : 0; |
|
|
2254 | y = y < 0 ? -y : y; // abs |
|
|
2255 | |
|
|
2256 | if (x > y) |
|
|
2257 | { |
|
|
2258 | sector |= 2; |
|
|
2259 | |
|
|
2260 | if (x > y * 2) |
|
|
2261 | sector |= 1; |
|
|
2262 | } |
|
|
2263 | else |
|
|
2264 | { |
|
|
2265 | if (y > x * 2) |
|
|
2266 | sector |= 1; |
|
|
2267 | else if (!y) |
|
|
2268 | return 0; // x == 0 here |
|
|
2269 | } |
|
|
2270 | |
|
|
2271 | return dir [sector]; |
|
|
2272 | #else // old algorithm |
2167 | int q; |
2273 | int q; |
2168 | |
2274 | |
2169 | if (y) |
2275 | if (y) |
2170 | q = x * 100 / y; |
2276 | q = 128 * x / y; |
2171 | else if (x) |
2277 | else if (x) |
2172 | q = -300 * x; |
2278 | q = -512 * x; // to make it > 309 |
2173 | else |
2279 | else |
2174 | return 0; |
2280 | return 0; |
2175 | |
2281 | |
2176 | if (y > 0) |
2282 | if (y > 0) |
2177 | { |
2283 | { |
2178 | if (q < -242) |
2284 | if (q < -309) return 7; |
|
|
2285 | if (q < -52) return 6; |
|
|
2286 | if (q < 52) return 5; |
|
|
2287 | if (q < 309) return 4; |
|
|
2288 | |
2179 | return 3; |
2289 | return 3; |
2180 | if (q < -41) |
2290 | } |
2181 | return 2; |
2291 | else |
2182 | if (q < 41) |
2292 | { |
2183 | return 1; |
2293 | if (q < -309) return 3; |
2184 | if (q < 242) |
2294 | if (q < -52) return 2; |
2185 | return 8; |
2295 | if (q < 52) return 1; |
|
|
2296 | if (q < 309) return 8; |
|
|
2297 | |
2186 | return 7; |
2298 | return 7; |
2187 | } |
2299 | } |
2188 | |
2300 | #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 | } |
2301 | } |
2200 | |
2302 | |
2201 | /* |
2303 | /* |
2202 | * dirdiff(dir1, dir2) returns how many 45-degrees differences there is |
2304 | * dirdiff(dir1, dir2) returns how many 45-degrees differences there is |
2203 | * between two directions (which are expected to be absolute (see absdir()) |
2305 | * between two directions (which are expected to be absolute (see absdir()) |
2204 | */ |
2306 | */ |
2205 | int |
2307 | int |
2206 | dirdiff (int dir1, int dir2) |
2308 | dirdiff (int dir1, int dir2) |
2207 | { |
2309 | { |
2208 | int d; |
|
|
2209 | |
|
|
2210 | d = abs (dir1 - dir2); |
2310 | int d = abs (dir1 - dir2); |
2211 | if (d > 4) |
|
|
2212 | d = 8 - d; |
|
|
2213 | |
2311 | |
2214 | return d; |
2312 | return d > 4 ? 8 - d : d; |
2215 | } |
2313 | } |
2216 | |
2314 | |
2217 | /* peterm: |
2315 | /* peterm: |
2218 | * do LOS stuff for ball lightning. Go after the closest VISIBLE monster. |
2316 | * do LOS stuff for ball lightning. Go after the closest VISIBLE monster. |
2219 | * Basically, this is a table of directions, and what directions |
2317 | * Basically, this is a table of directions, and what directions |
… | |
… | |
2677 | } |
2775 | } |
2678 | else |
2776 | else |
2679 | move_type = mt; |
2777 | move_type = mt; |
2680 | } |
2778 | } |
2681 | |
2779 | |
|
|
2780 | /* object should be a player. |
|
|
2781 | * we return the object the player has marked with the 'mark' command |
|
|
2782 | * below. If no match is found (or object has changed), we return |
|
|
2783 | * NULL. We leave it up to the calling function to print messages if |
|
|
2784 | * nothing is found. |
|
|
2785 | */ |
|
|
2786 | object * |
|
|
2787 | object::mark () const |
|
|
2788 | { |
|
|
2789 | if (contr && contr->mark && contr->mark->env == this) |
|
|
2790 | return contr->mark; |
|
|
2791 | else |
|
|
2792 | return 0; |
|
|
2793 | } |
|
|
2794 | |