#include #include #include #include #include static const char *HELP = "usage: newgame [options...]\n\n" "options:\n" "\t-w --width specify width for the world (default: 1024)\n" "\t-h --height specify height for the world (default: 1024)\n" "\t-s --size specify width and height for the world\n" "\t-p --path specify path to save the game in (default: path)\n" "\t-f --force create the new game even if the directory exists, this will delete data\n" "\t --no-force opposite of -f (default)\n" "\t-c --compress compress the world after creating it (recommended)\n" "\t --no-compress don't compress the world after creating it (default)\n" "\t --help display this message\n" ; int main(int argc, char *argv[]) { char *path = "data"; int width = 1024, height = 1024; bool compress = false; bool force = false; for (int i = 1 ; i < argc ; i++) { char *arg = argv[i]; # define _checkshort(SHORT) (strncmp(arg, "-" SHORT, strlen("-" SHORT)) == 0) # define _checklong(LONG) (strncmp(arg, "--" LONG, strlen("--" LONG)) == 0) # define _check(SHORT, LONG) (_checkshort(SHORT) || _checklong(LONG)) if (_check("w", "width")) width = atoi(argv[++i]); else if (_check("h", "height")) height = atoi(argv[++i]); else if (_check("s", "size")) width = height = atoi(argv[++i]); else if (_check("p", "path")) path = argv[++i]; else if (_check("c", "compress")) compress = true; else if (_checklong("no-compress")) compress = false; else if (_check("f", "force")) force = true; else if (_checklong("no-force")) force = false; else if (_checklong("help")) { kf_loginfo("%s", HELP); exit(0); } else { kf_logerr("invalid argument: %s", arg); exit(1); } # undef _checkshort # undef _checklong # undef _check } if (!force && DirectoryExists(path)) KF_THROW("path exists: %s", path); struct kf_world *world = NULL; kf_loginfo("creating world"); kf_timeit("create world", { world = kf_world_new(width, height, 2); }); /* path for our map.bin */ char worldpath[4096] = {0}; strcpy(worldpath, path); strcpy(&worldpath[0] + strlen(path), compress ? "/tmp/map.bin" : "/map.bin"); MakeDirectory(GetDirectoryPath(worldpath)); size_t len = kf_world_getsize(world); kf_loginfo("saving world to %s (%lu bytes uncompressed)", worldpath, len); kf_timeit("save world", kf_world_save(world, compress, worldpath)); free(world); }