Mercurial > hg > batmud > maputils
changeset 37:a1a1d64382de
Added new utilities
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 11 Dec 2006 07:47:42 +0000 |
parents | ec00fb30b147 |
children | 6c7c42a135fa |
files | coloransi.c mkloc.c |
diffstat | 2 files changed, 224 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/coloransi.c Mon Dec 11 07:47:42 2006 +0000 @@ -0,0 +1,69 @@ +/* + * Generate ANSI output of given outerworld ASCII map input + * Programmed by Matti 'ccr' Hämäläinen (Ggr Pupunen) + * (C) Copyright 2006 Tecnic Software productions (TNSP) + */ +#include "maputils.h" +#include <string.h> + +void putANSI(FILE *outFile, DCHAR *s) +{ + fputc(0x1b, outFile); + fprintf(outFile, "[%s", s); +} + +void putANSIC(FILE *outFile, DINT c, DINT k) +{ + fputc(0x1b, outFile); + fprintf(outFile, "[%d;%dm%c", + mapColors[c].ansi_attr, + mapColors[c].ansi, k); +} + + +void processNormal(FILE *inFile, FILE *outFile) +{ + DINT k, c, p; + + c = p = -1; + while ((k = fgetc(inFile)) != EOF) { + if (k == '\n') { + fprintf(outFile, "\n"); + c = -1; + } else if (k == 0xff) { + /* Location title mode */ + c = -1; + putANSI(outFile, "0;47;30m"); + + while ((k = fgetc(inFile)) != EOF) { + if (k == 0xfe) + break; + else + fprintf(outFile, "%c", k); + } + + } else { + c = mcGetColor(k); + if (c != p) { + if (p != -1) putANSI(outFile, "0m"); + if (k > 0) putANSIC(outFile, c, k); + } else + fprintf(outFile, "%c", k); + } + + p = c; + } +} + + +DINT main(DINT argc, DCHAR *argv[]) +{ + FILE *inFile, *outFile; + + inFile = stdin; + outFile = stdout; + + processNormal(inFile, outFile); + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mkloc.c Mon Dec 11 07:47:42 2006 +0000 @@ -0,0 +1,155 @@ +/* + * mkloc - Add special location information into ASCII map + * + * Programmed by Matti 'ccr' Hämäläinen (Ggr Pupunen) + * (C) Copyright 2006 Tecnic Software productions (TNSP) + */ +#include "maputils.h" +#include <string.h> +#include <strings.h> +#include "th_args.h" +#include "th_string.h" + +/* Variables + */ +DCHAR *progName = NULL; +DCHAR *srcFile = NULL, + *destFile = NULL, + *locFile = NULL; + +DBOOL optGetLoc = FALSE; + +/* Arguments + */ +t_opt optList[] = { + { 0, '?', "help", "Show this help", 0 }, + { 2, 'v', "verbose", "Be more verbose", 0 }, + { 3, 'q', "quiet", "Be quiet", 0 }, + { 1, 'o', "output", "Output file (default stdout)", 1 }, + { 5, 'm', "map", "Input map file", 1 }, + { 6, 'l', "locinfo", "Input location info file", 1 }, + { 4, 'g', "getloc", "Get/generate location information", 0 }, +}; + +const DINT optListN = (sizeof(optList) / sizeof(t_opt)); + + +void argShowHelp() +{ + th_args_help(stdout, optList, optListN, progName, + "[options] <inputfile> [inputfile#2..]"); +} + + +void argHandleOpt(const DINT optN, DCHAR *optArg, DCHAR *currArg) +{ + switch (optN) { + case 0: + argShowHelp(); + exit(0); + break; + + case 2: + th_verbosityLevel++; + break; + + case 3: + th_verbosityLevel = -1; + break; + + case 1: + if (optArg) { + destFile = optArg; + THMSG(1, "Output file '%s'\n", destFile); + } else { + THERR("No output filename argument provided.\n"); + exit(1); + } + break; + + case 5: + if (optArg) { + srcFile = optArg; + THMSG(1, "Map file '%s'\n", destFile); + } else { + THERR("No map filename argument provided.\n"); + exit(1); + } + break; + + case 6: + if (optArg) { + destFile = optArg; + THMSG(1, "Location info file '%s'\n", destFile); + } else { + THERR("No location info filename argument provided.\n"); + exit(1); + } + break; + + case 4: + THMSG(1, "Location generation mode\n"); + optGetLoc = TRUE; + break; + + default: + THERR("Unknown argument '%s'.\n", currArg); + exit(3); + break; + } +} + + +int main(int argc, char *argv[]) +{ + mapblock_t *worldMap = NULL; + + progName = argv[0]; + th_init("mkloc", "Add locations to ASCII map", "0.1", NULL, NULL); + th_verbosityLevel = 1; + + /* Parse arguments */ + th_args_process(argc, argv, optList, optListN, + argHandleOpt, NULL); + + if (srcFile == NULL || (locFile == NULL && !optGetLoc)) { + THERR("Nothing to do. (try --help)\n"); + exit(0); + } + + /* Read initial map */ + THMSG(1, "Reading map '%s'\n", srcFile); + worldMap = parseFile(srcFile); + if (worldMap) { + THERR("World map could not be loaded!\n"); + exit(1); + } + + THMSG(2, "Initial dimensions (%d x %d)\n", worldMap->w, worldMap->h); + + + /* Output generated map + */ + if (worldMap) { + FILE *tmpFile; + + THMSG(1, "Outputting generated map of (%d x %d) ...\n", + worldMap->w, worldMap->h); + + if (destFile == NULL) + tmpFile = stdout; + else if ((tmpFile = fopen(destFile, "wb")) == NULL) { + THERR("Error opening output file '%s'!\n", + destFile); + exit(1); + } + + printBlock(tmpFile, worldMap); + + fclose(tmpFile); + } else { + THERR("No map generated?\n"); + } + + return 0; +}