| 1938 | }; |
| 1939 | |
| 1940 | int dfhack_timeout(lua_State *L) |
| 1941 | { |
| 1942 | using df::global::world; |
| 1943 | using df::global::enabler; |
| 1944 | |
| 1945 | // Parse arguments |
| 1946 | lua_Number time = luaL_checknumber(L, 1); |
| 1947 | int mode = luaL_checkoption(L, 2, NULL, timeout_modes); |
| 1948 | luaL_checktype(L, 3, LUA_TFUNCTION); |
| 1949 | lua_settop(L, 3); |
| 1950 | |
| 1951 | if (mode > 0 && !Core::getInstance().isWorldLoaded()) |
| 1952 | { |
| 1953 | lua_pushnil(L); |
| 1954 | return 1; |
| 1955 | } |
| 1956 | |
| 1957 | // Compute timeout value |
| 1958 | switch (mode) |
| 1959 | { |
| 1960 | case 2: |
| 1961 | time *= 1200; |
| 1962 | break; |
| 1963 | case 3: |
| 1964 | time *= 33600; |
| 1965 | break; |
| 1966 | case 4: |
| 1967 | time *= 403200; |
| 1968 | break; |
| 1969 | default:; |
| 1970 | } |
| 1971 | |
| 1972 | int delta = time; |
| 1973 | |
| 1974 | if (delta <= 0) |
| 1975 | luaL_error(L, "Invalid timeout: %d", delta); |
| 1976 | |
| 1977 | // Queue the timeout |
| 1978 | int id = next_timeout_id++; |
| 1979 | if (mode) |
| 1980 | tick_timers.insert(std::pair<int,int>(world->frame_counter+delta, id)); |
| 1981 | else |
| 1982 | frame_timers.insert(std::pair<int,int>(frame_idx+delta, id)); |
| 1983 | |
| 1984 | lua_rawgetp(L, LUA_REGISTRYINDEX, &DFHACK_TIMEOUTS_TOKEN); |
| 1985 | lua_swap(L); |
| 1986 | lua_rawseti(L, -2, id); |
| 1987 | |
| 1988 | lua_pushinteger(L, id); |
| 1989 | return 1; |
| 1990 | } |
| 1991 | |
| 1992 | int dfhack_timeout_active(lua_State *L) |
| 1993 | { |
nothing calls this directly
no test coverage detected