store or restore game state */ * Currently handles * turn counter, * hero inventory and hunger, * hero attributes and skills and conducts (all via 'struct u'), * object discoveries, * monster generation and vanquished statistics. * NOTE: wouldn't work after restore if game has been saved, so the * #save command ('S') is disabled during the tutorial. */ gamestate(); -- save state
| 1719 | /* gamestate(); -- save state */ |
| 1720 | /* gamestate(true); -- restore state */ |
| 1721 | staticfn int |
| 1722 | nhl_gamestate(lua_State *L) |
| 1723 | { |
| 1724 | long wornmask; |
| 1725 | struct obj *otmp; |
| 1726 | int argc = lua_gettop(L); |
| 1727 | boolean reststate = (argc > 0) ? lua_toboolean(L, -1) : FALSE; |
| 1728 | int otyp; |
| 1729 | |
| 1730 | debugpline4("gamestate: %d:%d (%c vs %c)", u.uz.dnum, u.uz.dlevel, |
| 1731 | reststate ? 'T' : 'F', gg.gmst_stored ? 't' : 'f'); |
| 1732 | |
| 1733 | if (reststate && gg.gmst_stored) { |
| 1734 | d_level cur_uz = u.uz, cur_uz0 = u.uz0; |
| 1735 | |
| 1736 | /* restore game state */ |
| 1737 | svm.moves = gg.gmst_moves; |
| 1738 | pline("Resetting time to move #%ld.", svm.moves); |
| 1739 | gg.gmst_moves = 0L; |
| 1740 | |
| 1741 | gl.lastinvnr = 51; |
| 1742 | while (gi.invent) |
| 1743 | useupall(gi.invent); |
| 1744 | while ((otmp = gg.gmst_invent) != NULL) { |
| 1745 | wornmask = otmp->owornmask; |
| 1746 | otmp->owornmask = 0L; |
| 1747 | extract_nobj(otmp, &gg.gmst_invent); |
| 1748 | addinv_nomerge(otmp); |
| 1749 | if (wornmask) |
| 1750 | setworn(otmp, wornmask); |
| 1751 | } |
| 1752 | assert(gg.gmst_ubak != NULL); |
| 1753 | (void) memcpy((genericptr_t) &u, gg.gmst_ubak, sizeof u); |
| 1754 | assert(gg.gmst_disco != NULL); |
| 1755 | (void) memcpy((genericptr_t) &svd.disco, gg.gmst_disco, |
| 1756 | sizeof svd.disco); |
| 1757 | assert(gg.gmst_mvitals != NULL); |
| 1758 | (void) memcpy((genericptr_t) &svm.mvitals, gg.gmst_mvitals, |
| 1759 | sizeof svm.mvitals); |
| 1760 | /* clear user-given object type names */ |
| 1761 | for (otyp = 0; otyp < NUM_OBJECTS; otyp++) |
| 1762 | if (objects[otyp].oc_uname) { |
| 1763 | free(objects[otyp].oc_uname); |
| 1764 | objects[otyp].oc_uname = (char *) 0; |
| 1765 | } |
| 1766 | /* some restored state would confuse the level change in progress */ |
| 1767 | u.uz = cur_uz, u.uz0 = cur_uz0; |
| 1768 | init_uhunger(); |
| 1769 | free_tutorial(); /* release gg.gmst_XYZ */ |
| 1770 | gg.gmst_stored = FALSE; |
| 1771 | (void) memcpy(svs.spl_book, gg.gmst_spl_book, sizeof svs.spl_book); |
| 1772 | } else if (!reststate && !gg.gmst_stored) { |
| 1773 | /* store game state */ |
| 1774 | gg.gmst_moves = svm.moves; |
| 1775 | while ((otmp = gi.invent) != NULL) { |
| 1776 | wornmask = otmp->owornmask; |
| 1777 | setnotworn(otmp); |
| 1778 | freeinv(otmp); |
nothing calls this directly
no test coverage detected