| 1 | /* |
| 2 | * PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 | * Development, and is not to be reproduced, transmitted, or disclosed |
| 4 | * in any way without written permission. |
| 5 | * |
| 6 | * Produced by: Jeff Lait |
| 7 | * |
| 8 | * POWDER Development |
| 9 | * |
| 10 | * NAME: mobref.cpp ( POWDER Library, C++ ) |
| 11 | * |
| 12 | * COMMENTS: |
| 13 | * Implementation of MOBREFs. |
| 14 | * |
| 15 | * MOBREFs are the preferred way to refer to specific MOBs rather than |
| 16 | * using MOB *. When a MOB is deleted, its MOBREF will start |
| 17 | * returning null (rather than the MOB *, which will point to deleted |
| 18 | * data.) Furthermore, MOBREFs are safe to save & load. |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include "mobref.h" |
| 23 | #include "creature.h" |
| 24 | #include "assert.h" |
| 25 | #include "sramstream.h" |
| 26 | #include "msg.h" |
| 27 | |
| 28 | struct MOBREF_REF |
| 29 | { |
| 30 | MOB *ptr; |
| 31 | u16 count; |
| 32 | |
| 33 | // Note this isn't masked by free ref! Thus, we can use it |
| 34 | // to share data between the two. |
| 35 | u16 isfree; |
| 36 | }; |
| 37 | |
| 38 | struct MOBREF_FREEREF |
| 39 | { |
| 40 | u16 prev; |
| 41 | u16 next; |
| 42 | }; |
| 43 | |
| 44 | union MOBREF_DATA |
| 45 | { |
| 46 | MOBREF_REF ref; |
| 47 | MOBREF_FREEREF list; |
| 48 | }; |
| 49 | |
| 50 | // We have 20 arrays of 256 entries - maximum number of refs is 5,000. |
| 51 | #define MAX_REF_TABLES 20 |
| 52 | |
| 53 | MOBREF_DATA *glb_refdata[MAX_REF_TABLES]; |
| 54 | u16 glb_freelist = 0; |
| 55 | u16 glb_numrefs = 0; |
| 56 | |
| 57 | void |
| 58 | mobref_init() |
| 59 | { |
| 60 | memset(glb_refdata, 0, sizeof(MOBREF_DATA *) * MAX_REF_TABLES); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | mobref_clearallrefs() |
| 65 | { |
| 66 | int i; |
| 67 | |
| 68 | for (i = 0; i < MAX_REF_TABLES; i++) |
| 69 | { |
| 70 | delete [] glb_refdata[i]; |
| 71 | glb_refdata[i] = 0; |
| 72 | } |
| 73 | glb_freelist = 0; |
| 74 | glb_numrefs = 0; |
| 75 | } |
| 76 | |
| 77 | int |
| 78 | mobref_getnumrefs() |
| 79 | { |
| 80 | return glb_numrefs; |
| 81 | } |
| 82 | |
| 83 | int |
| 84 | mobref_getnumtables() |
| 85 | { |
| 86 | int i; |
| 87 | int numtable = 0; |
| 88 | |
| 89 | for (i = 0; i < MAX_REF_TABLES; i++) |
| 90 | { |
| 91 | if (glb_refdata[i]) |
| 92 | numtable++; |
| 93 | } |
| 94 | return numtable; |
| 95 | } |
| 96 | |
| 97 | // Very basic reference handlers: |
| 98 | |
| 99 | // Finds the data without creating. Result may be a free ref. |
| 100 | MOBREF_DATA * |
| 101 | mobref_findraw(u16 id) |
| 102 | { |
| 103 | int table, off; |
| 104 | |
| 105 | if (!id) |
| 106 | return 0; |
| 107 | |
| 108 | id--; |
| 109 | table = id >> 8; |
| 110 | off = id & 255; |
| 111 | |
| 112 | if (!glb_refdata[table]) |
| 113 | return 0; |
| 114 | |
| 115 | return &glb_refdata[table][off]; |
| 116 | } |
| 117 | |
| 118 | // Allocates the given table & updates the free list. |
| 119 | void |
| 120 | mobref_alloctable(int table) |
| 121 | { |
| 122 | if (glb_refdata[table]) |
| 123 | return; |
| 124 | |
| 125 | glb_refdata[table] = new MOBREF_DATA [256]; |
| 126 | |
| 127 | int i, tableidx; |
| 128 | |
| 129 | // Address of first entry in table: |
| 130 | tableidx = table << 8; |
| 131 | tableidx++; |
| 132 | for (i = 0; i < 256; i++) |
| 133 | { |
| 134 | glb_refdata[table][i].ref.isfree = 1; |
| 135 | glb_refdata[table][i].list.prev = tableidx + i - 1; |
| 136 | glb_refdata[table][i].list.next = tableidx + i + 1; |
| 137 | } |
| 138 | // The very last guy points to current free list, the first guy |
| 139 | // becomes the free list. |
| 140 | glb_refdata[table][255].list.next = glb_freelist; |
| 141 | glb_refdata[table][0].list.prev = 0; |
| 142 | |
| 143 | // The previous of the current free list must point to our very last guy. |
| 144 | // This should be a no-op because we should be calling this because |
| 145 | // we ran out of freelist. |
| 146 | // However, on load we have to create tables as they are touched. |
| 147 | // If we load two isolated mobs on different tables, we'd create a table |
| 148 | // while the free list is non-empty. This will cause our double-linked |
| 149 | // list to become broken. |
| 150 | // Unfortunately, the only damage this seems able to cause is to |
| 151 | // cause us to falsely move the glbfreelist forward, thereby |
| 152 | // forcing us to allocate more mob tables than we actually need. |
| 153 | MOBREF_DATA *head; |
| 154 | |
| 155 | head = mobref_findraw(glb_freelist); |
| 156 | if (head) |
| 157 | head->list.prev = tableidx + 255; |
| 158 | |
| 159 | glb_freelist = tableidx; |
| 160 | } |
| 161 | |
| 162 | // Creates & allocs the refdata for the given id. |
| 163 | MOBREF_DATA * |
| 164 | mobref_findorcreate(u16 id) |
| 165 | { |
| 166 | int table, off; |
| 167 | MOBREF_DATA *data; |
| 168 | |
| 169 | // one-correct the id. |
| 170 | id--; |
| 171 | |
| 172 | table = id >> 8; |
| 173 | off = id & 255; |
| 174 | |
| 175 | UT_ASSERT(table < MAX_REF_TABLES); |
| 176 | |
| 177 | if (!glb_refdata[table]) |
| 178 | { |
| 179 | mobref_alloctable(table); |
| 180 | } |
| 181 | data = &glb_refdata[table][off]; |
| 182 | |
| 183 | // Check if already freed... |
| 184 | if (data->ref.isfree) |
| 185 | { |
| 186 | // Yuck, gotta free out this reference. |
| 187 | // Get adjoining references... |
| 188 | MOBREF_DATA *prev, *next; |
| 189 | |
| 190 | prev = mobref_findraw(data->list.prev); |
| 191 | next = mobref_findraw(data->list.next); |
| 192 | |
| 193 | if (prev) |
| 194 | { |
| 195 | prev->list.next = data->list.next; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | glb_freelist = data->list.next; |
| 200 | } |
| 201 | if (next) |
| 202 | { |
| 203 | next->list.prev = data->list.prev; |
| 204 | } |
| 205 | |
| 206 | // The following turns data into a normal ref. |
| 207 | data->ref.count = 0; |
| 208 | data->ref.ptr = 0; |
| 209 | data->ref.isfree = 0; |
| 210 | |
| 211 | // printf("Create ref given ID %d\n", id); |
| 212 | |
| 213 | glb_numrefs++; |
| 214 | } |
| 215 | |
| 216 | return data; |
| 217 | } |
| 218 | |
| 219 | // Allocs a new refdata, giving you the index. |
| 220 | u16 |
| 221 | mobref_allocid() |
| 222 | { |
| 223 | if (!glb_freelist) |
| 224 | { |
| 225 | // Find & allocate a new table, this will update this. |
| 226 | int i; |
| 227 | for (i = 0; i < MAX_REF_TABLES; i++) |
| 228 | { |
| 229 | if (!glb_refdata[i]) |
| 230 | { |
| 231 | mobref_alloctable(i); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | // This fails implies we are screwed! |
| 236 | UT_ASSERT(i < MAX_REF_TABLES); |
| 237 | } |
| 238 | |
| 239 | if (!glb_freelist) |
| 240 | return 0; |
| 241 | |
| 242 | int newid; |
| 243 | MOBREF_DATA *newdata, *next; |
| 244 | |
| 245 | newid = glb_freelist; |
| 246 | #if 0 |
| 247 | { |
| 248 | BUF buf; |
| 249 | buf.sprintf("new id: %d. ", newid); |
| 250 | msg_report(buf); |
| 251 | } |
| 252 | #endif |
| 253 | newdata = mobref_findraw(newid); |
| 254 | next = mobref_findraw(newdata->list.next); |
| 255 | if (next) |
| 256 | next->list.prev = 0; |
| 257 | glb_freelist = newdata->list.next; |
| 258 | #if 0 |
| 259 | { |
| 260 | BUF buf; |
| 261 | buf.sprintf("new free: %d. ", glb_freelist); |
| 262 | msg_report(buf); |
| 263 | } |
| 264 | #endif |
| 265 | newdata->ref.isfree = 0; |
| 266 | newdata->ref.ptr = 0; |
| 267 | newdata->ref.count = 0; |
| 268 | |
| 269 | // printf("Create ref %d\n", newid); |
| 270 | glb_numrefs++; |
| 271 | |
| 272 | return newid; |
| 273 | } |
| 274 | |
| 275 | // Add a reference to id. increferences must balance decreferences. |
| 276 | // If no one has inced this reference before, it is allocated & the |
| 277 | // pointer is set to 0. This way load() works seamlessly (eventually |
| 278 | // the guilty party will do a transferMOB) |
| 279 | // It is valid to increment a null id. |
| 280 | void |
| 281 | mobref_increference(u16 id) |
| 282 | { |
| 283 | if (!id) |
| 284 | return; |
| 285 | |
| 286 | MOBREF_DATA *data; |
| 287 | |
| 288 | data = mobref_findorcreate(id); |
| 289 | data->ref.count++; |
| 290 | } |
| 291 | |
| 292 | // Removes a reference, freeing if it hits zero. |
| 293 | // Valid to call with null ids - is a noop then. |
| 294 | void |
| 295 | mobref_decreference(u16 id) |
| 296 | { |
| 297 | if (!id) |
| 298 | return; |
| 299 | |
| 300 | MOBREF_DATA *data; |
| 301 | |
| 302 | data = mobref_findraw(id); |
| 303 | |
| 304 | // If this id is too old, it may point to garbage, sanity check: |
| 305 | if (!data) |
| 306 | { |
| 307 | UT_ASSERT(!"Invlaid dec reference!"); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | if (data->ref.isfree) |
| 312 | { |
| 313 | // WTF? Already freed! |
| 314 | UT_ASSERT(!"Ref already freed!"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | // If the count is 0 or 1, we will delete. |
| 319 | // Zero is in case some one allocs and decs before incing... |
| 320 | if (data->ref.count <= 1) |
| 321 | { |
| 322 | data->ref.count = 0; |
| 323 | data->ref.isfree = 1; |
| 324 | |
| 325 | MOBREF_DATA *head; |
| 326 | |
| 327 | head = mobref_findraw(glb_freelist); |
| 328 | |
| 329 | // Setup the next/prev pointers & put at front of list. |
| 330 | data->list.next = glb_freelist; |
| 331 | data->list.prev = 0; |
| 332 | |
| 333 | if (head) |
| 334 | head->list.prev = id; |
| 335 | |
| 336 | glb_freelist = id; |
| 337 | |
| 338 | // printf("Id %d done\n", id); |
| 339 | |
| 340 | glb_numrefs--; |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | // Straightforward... |
| 345 | data->ref.count--; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // Takes the given id & sets the mob to point to it. |
| 350 | bool |
| 351 | mobref_setptr(u16 id, MOB *mob) |
| 352 | { |
| 353 | if (!id) |
| 354 | return true; |
| 355 | |
| 356 | MOBREF_DATA *data; |
| 357 | |
| 358 | data = mobref_findraw(id); |
| 359 | if (!data) |
| 360 | { |
| 361 | UT_ASSERT(!"Stale ref, no data, in setptr!"); |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | if (data->ref.isfree) |
| 366 | { |
| 367 | UT_ASSERT(!"Stale ref, freed, in setptr!"); |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | data->ref.ptr = mob; |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | MOB * |
| 376 | mobref_lookupptr(u16 id) |
| 377 | { |
| 378 | if (!id) |
| 379 | return 0; |
| 380 | |
| 381 | MOBREF_DATA *data; |
| 382 | |
| 383 | data = mobref_findraw(id); |
| 384 | if (!data || data->ref.isfree) |
| 385 | { |
| 386 | UT_ASSERT(!"Stale reference in lut!"); |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | return data->ref.ptr; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | // |
| 395 | // MOBREF definitions |
| 396 | // |
| 397 | |
| 398 | MOBREF::MOBREF() |
| 399 | { |
| 400 | // Clear our id. |
| 401 | // No need to ref count nulls. |
| 402 | myId = 0; |
| 403 | } |
| 404 | |
| 405 | MOBREF::MOBREF(const MOBREF &ref) |
| 406 | { |
| 407 | // Another person interested in it (ourselves) |
| 408 | mobref_increference(ref.myId); |
| 409 | myId = ref.myId; |
| 410 | } |
| 411 | |
| 412 | MOBREF::~MOBREF() |
| 413 | { |
| 414 | // Nulls ignored by decreference. |
| 415 | mobref_decreference(myId); |
| 416 | |
| 417 | // Zero out our ID in case someone double-deletes a mobref. |
| 418 | // Of course, that is a bug for other reasons and won't survive |
| 419 | // a debug build, but at least it minimizes the damage on the GBA. |
| 420 | myId = 0; |
| 421 | } |
| 422 | |
| 423 | MOBREF & |
| 424 | MOBREF::operator=(const MOBREF &ref) |
| 425 | { |
| 426 | mobref_increference(ref.myId); |
| 427 | mobref_decreference(myId); |
| 428 | myId = ref.myId; |
| 429 | return *this; |
| 430 | } |
| 431 | |
| 432 | void |
| 433 | MOBREF::load(SRAMSTREAM &is) |
| 434 | { |
| 435 | int val; |
| 436 | |
| 437 | mobref_decreference(myId); |
| 438 | is.uread(val, 16); |
| 439 | myId = val; |
| 440 | mobref_increference(myId); |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | MOBREF::save(SRAMSTREAM &os) const |
| 445 | { |
| 446 | os.write(myId, 16); |
| 447 | } |
| 448 | |
| 449 | void |
| 450 | MOBREF::createAndSetMOB(MOB *mob) |
| 451 | { |
| 452 | // Clear our old ID. |
| 453 | mobref_decreference(myId); |
| 454 | |
| 455 | // If mob is null, we want a null reference. |
| 456 | if (!mob) |
| 457 | { |
| 458 | myId = 0; |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | // Find a new one... |
| 463 | myId = mobref_allocid(); |
| 464 | mobref_increference(myId); |
| 465 | if (!mobref_setptr(myId, mob)) |
| 466 | { |
| 467 | UT_ASSERT(!"Set failed in create"); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | bool |
| 472 | MOBREF::transferMOB(MOB *mob) |
| 473 | { |
| 474 | // If we are null, this is a problem if mob is not null! |
| 475 | if (!myId && mob) |
| 476 | { |
| 477 | UT_ASSERT(!"Invalid transfer!"); |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | if (!mobref_setptr(myId, mob)) |
| 482 | { |
| 483 | UT_ASSERT(!"Set ptr failed in transfer"); |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | // If new mob is null, we instantly clear ourselves. |
| 488 | if (!mob) |
| 489 | { |
| 490 | mobref_decreference(myId); |
| 491 | myId = 0; |
| 492 | } |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | void |
| 498 | MOBREF::setMob(const MOB *mob) |
| 499 | { |
| 500 | if (!mob) |
| 501 | { |
| 502 | mobref_decreference(myId); |
| 503 | myId = 0; |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | *this = mob->getMobRef(); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | MOB * |
| 512 | MOBREF::getMob() const |
| 513 | { |
| 514 | if (!myId) |
| 515 | return 0; |
| 516 | |
| 517 | MOB *mob; |
| 518 | |
| 519 | mob = mobref_lookupptr(myId); |
| 520 | if (!mob) |
| 521 | { |
| 522 | // Clear out our reference, it's stale. |
| 523 | mobref_decreference(myId); |
| 524 | myId = 0; |
| 525 | } |
| 526 | return mob; |
| 527 | } |