----------------------------------------------------------------------------- initialize a new object. adds to the list for the given room returns the object number
| 1680 | // initialize a new object. adds to the list for the given room |
| 1681 | // returns the object number |
| 1682 | int ObjCreate(uint8_t type, uint16_t id, int roomnum, vector *pos, const matrix *orient, int parent_handle) { |
| 1683 | int objnum; |
| 1684 | object *obj; |
| 1685 | int handle; |
| 1686 | |
| 1687 | ASSERT(type != OBJ_NONE); |
| 1688 | |
| 1689 | if (ROOMNUM_OUTSIDE(roomnum)) { |
| 1690 | ASSERT(CELLNUM(roomnum) <= TERRAIN_WIDTH * TERRAIN_DEPTH); |
| 1691 | ASSERT(CELLNUM(roomnum) >= 0); |
| 1692 | |
| 1693 | roomnum = GetTerrainRoomFromPos(pos); |
| 1694 | |
| 1695 | if (roomnum == -1) |
| 1696 | return -1; |
| 1697 | } |
| 1698 | |
| 1699 | // Get next free object |
| 1700 | objnum = ObjAllocate(); |
| 1701 | if (objnum == -1) // no free objects |
| 1702 | return -1; |
| 1703 | obj = &Objects[objnum]; |
| 1704 | |
| 1705 | // Make sure the object is ok |
| 1706 | ASSERT(obj->type == OBJ_NONE); // make sure unused |
| 1707 | ASSERT(obj->roomnum == -1); // make sure unlinked |
| 1708 | |
| 1709 | // Compute the new handle |
| 1710 | handle = obj->handle + HANDLE_COUNT_INCREMENT; |
| 1711 | |
| 1712 | // Check for handle wrap |
| 1713 | if ((handle & HANDLE_COUNT_MASK) == HANDLE_COUNT_MASK) // Going to wrap! |
| 1714 | Int3(); // Show this to Matt, or email him if he's not here |
| 1715 | |
| 1716 | // Initialize the object |
| 1717 | if (!ObjInit(obj, type, id, handle, pos, Gametime, parent_handle)) { // Couldn't init! |
| 1718 | obj->type = OBJ_NONE; // mark as unused |
| 1719 | ObjFree(objnum); // de-allocate object |
| 1720 | return -1; |
| 1721 | } |
| 1722 | |
| 1723 | #ifdef _DEBUG |
| 1724 | if (print_object_info) { |
| 1725 | mprintf(0, "Created object %d of type %d\n", objnum, obj->type); |
| 1726 | } |
| 1727 | #endif |
| 1728 | |
| 1729 | // Set the object's orietation |
| 1730 | // THIS MUST BE DONE AFTER ObjInit (as ObjInit could load in a polymodel and set the anim and wall offsets) |
| 1731 | ObjSetOrient(obj, orient ? orient : &Identity_matrix); |
| 1732 | |
| 1733 | // Link object into room or terrain cell |
| 1734 | ObjLink(objnum, roomnum); |
| 1735 | |
| 1736 | // Type specific should have set up the size, so now we can compute the bounding box. |
| 1737 | ObjSetAABB(obj); |
| 1738 | |
| 1739 | // Let the demo system know about this object |
no test coverage detected