From eebdcf5d936b59c60d8a3258afff6a468fa6503e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 20 May 2016 20:55:27 +0200 Subject: [PATCH] control log level from command line (with -l, like -v) make newplayer.lua use the autoseed module, eliminate duplication seeding new players is broken Conflicts: src/gmtool.c src/main.c src/util/log.c src/util/log.h --- crypto | 2 +- scripts/newplayer.lua | 92 +++++++------------------------------------ src/gmtool.c | 8 +++- src/main.c | 51 ++++++++++++++---------- src/util/log.c | 16 ++++---- src/util/log.h | 2 + 6 files changed, 62 insertions(+), 109 deletions(-) diff --git a/crypto b/crypto index f00139338..913358a8d 160000 --- a/crypto +++ b/crypto @@ -1 +1 @@ -Subproject commit f0013933852154eccc78536dd813774df5ee7f5d +Subproject commit 913358a8d7d961ffc35b238c744ca6ce823ffdd9 diff --git a/scripts/newplayer.lua b/scripts/newplayer.lua index 9e536595b..c946248fb 100644 --- a/scripts/newplayer.lua +++ b/scripts/newplayer.lua @@ -1,32 +1,13 @@ -dofile("config.lua") -p = require("populate") - -local function read_players() --- return {{ email = "noreply@mailinator.com", race = "dwarf", lang = "de" }} - local players = {} - local input = io.open("newfactions", "r") - while input do - local str = input:read("*line") - if str==nil then break end - local email, race, lang = str:match("([^ ]*) ([^ ]*) ([^ ]*)") - if string.char(string.byte(email, 1))~='#' then - table.insert(players, { race = race, lang = lang, email = email }) - end - end - return players +local path = 'scripts' +if config.install then + path = config.install .. '/' .. path end +package.path = package.path .. ';' .. path .. '/?.lua;' .. path .. '/?/init.lua' +require 'eressea' +require 'eressea.xmlconf' -- read xml data -local function seed(r, email, race, lang) - local f = faction.create(email, race, lang) - local u = unit.create(f, r) - equip_unit(u, "new_faction") - equip_unit(u, "first_unit") - equip_unit(u, "first_" .. race, 7) -- disable old callbacks - unit.create(f, r, 5):set_skill("mining", 30) - unit.create(f, r, 5):set_skill("quarrying", 30) - f:set_origin(r) - return f -end +require 'config' +auto = require 'eressea.autoseed' local function dump_selection(sel) local best = { score = 0, r = nil } @@ -42,54 +23,9 @@ local function dump_selection(sel) return best end -players = read_players() -local peasants = 20000 -local trees = 1000 -local turn = get_turn() -local sel -if #players > 0 then - eressea.read_game(("%d.dat"):format(turn)) - sel = p.select(regions(), peasants, trees) - if #sel > 0 then - local best = dump_selection(sel) - print("finest region, " .. best.score .. " points: " .. tostring(best.r)) - end -end -math.randomseed(os.time()) - -local newbs = {} -local per_region = 2 -local num_seeded = 2 -local start = nil -for _, p in ipairs(players) do - if num_seeded == per_region then - while not start or start.units() do - local index = math.random(#sel) - start = sel[index] - end - num_seeded = 0 - end - local dupe = false - for f in factions() do - if f.email==p.email then - print("seed: duplicate email " .. p.email .. " already used by " .. tostring(f)) - dupe = true - break - end - end - if not dupe then - num_seeded = num_seeded + 1 - f = seed(start, p.email, p.race or "human", p.lang or "de") - print("new faction ".. tostring(f) .. " starts in ".. tostring(start)) - table.insert(newbs, f) - end -end - -if #newbs > 0 then - init_reports() - for _, f in ipairs(newbs) do - write_report(f) - end - eressea.write_game(("%d.dat.new"):format(turn)) -end - +print("read game") +eressea.read_game(get_turn() .. ".dat") +print("auto-seed") +auto.init() +print("editor") +gmtool.editor() diff --git a/src/gmtool.c b/src/gmtool.c index 3a48535a0..3af7814a2 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -1239,7 +1239,10 @@ void run_mapper(void) int split = 20; state *st; point tl; - +/* FIXME: dsiable logging + int old_flags = log_flags; + log_flags &= ~(LOG_CPERROR | LOG_CPWARNING); +*/ init_curses(); curs_set(1); @@ -1331,6 +1334,9 @@ void run_mapper(void) set_readline(NULL); curs_set(1); endwin(); +/* FIXME: reset logging + log_flags = old_flags; +*/ state_close(st); } diff --git a/src/main.c b/src/main.c index f96c04d2e..4285df150 100644 --- a/src/main.c +++ b/src/main.c @@ -128,9 +128,31 @@ static int get_arg(int argc, char **argv, size_t len, int index, const char **re return index; } +static int verbosity_to_flags(int verbosity) { + int flags = 0; + switch (verbosity) { + case 0: + flags = 0; + break; + case 1: + flags = LOG_CPERROR; + break; + case 2: + flags = LOG_CPERROR | LOG_CPWARNING; + break; + case 3: + flags = LOG_CPERROR | LOG_CPWARNING | LOG_CPINFO; + break; + default: + flags = LOG_CPERROR | LOG_CPWARNING | LOG_CPINFO | LOG_CPDEBUG; + break; + } + return flags; +} + static int parse_args(int argc, char **argv, int *exitcode) { - int i, log_stderr = 0; + int i, log_stderr = 0, log_flags = 0; for (i = 1; i != argc; ++i) { char *argi = argv[i]; @@ -168,7 +190,8 @@ static int parse_args(int argc, char **argv, int *exitcode) i = get_arg(argc, argv, 2, i, &luafile, 0); break; case 'l': - i = get_arg(argc, argv, 2, i, &logfile, 0); + i = get_arg(argc, argv, 2, i, &arg, 0); + log_flags = arg ? atoi(arg) : 0xff; break; case 't': i = get_arg(argc, argv, 2, i, &arg, 0); @@ -192,28 +215,15 @@ static int parse_args(int argc, char **argv, int *exitcode) } } - switch (verbosity) { - case 0: - log_stderr = 0; - break; - case 1: - log_stderr = LOG_CPERROR; - break; - case 2: - log_stderr = LOG_CPERROR | LOG_CPWARNING; - break; - case 3: - log_stderr = LOG_CPERROR | LOG_CPWARNING | LOG_CPINFO; - break; - default: - log_stderr = LOG_CPERROR | LOG_CPWARNING | LOG_CPINFO | LOG_CPDEBUG; - break; - } + // open logfile on disk: + log_flags = verbosity_to_flags(log_flags); + log_open(logfile, log_flags); + // also log to stderr: + log_stderr = verbosity_to_flags(verbosity); if (log_stderr) { log_to_file(log_stderr | LOG_FLUSH | LOG_BRIEF, stderr); } - return 0; } @@ -296,7 +306,6 @@ int main(int argc, char **argv) /* parse arguments again, to override ini file */ parse_args(argc, argv, &err); - log_open(logfile, LOG_CPERROR | LOG_CPWARNING | LOG_CPDEBUG | LOG_FLUSH); locale_init(); #ifdef CRTDBG diff --git a/src/util/log.c b/src/util/log.c index 6d8d5a7f4..7be2f9a06 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -22,9 +22,6 @@ without prior permission by the authors of Eressea. #include #include -/* TODO: set from external function */ -int log_flags = LOG_FLUSH | LOG_CPERROR | LOG_CPWARNING | LOG_CPDEBUG; - #ifdef STDIO_CP static int stdio_codepage = STDIO_CP; #else @@ -126,7 +123,7 @@ static const char *log_prefix(int level) { return prefix; } -static int check_dupe(const char *format, int type) +static int check_dupe(const char *format, int level) { static int last_type; /* STATIC_XCALL: used across calls */ static char last_message[32]; /* STATIC_XCALL: used across calls */ @@ -136,12 +133,14 @@ static int check_dupe(const char *format, int type) return 1; } if (dupes) { - fprintf(stderr, "%s: last message repeated %d times\n", log_prefix(last_type), - dupes + 1); + if (level & LOG_CPERROR) { + fprintf(stderr, "%s: last message repeated %d times\n", log_prefix(last_type), + dupes + 1); + } dupes = 0; } strlcpy(last_message, format, sizeof(last_message)); - last_type = type; + last_type = level; return 0; } @@ -176,6 +175,7 @@ static void log_stdio(void *data, int level, const char *module, const char *for if (format[len - 1] != '\n') { fputc('\n', out); } + fflush(out); } log_t *log_to_file(int flags, FILE *out) { @@ -280,7 +280,7 @@ log_t *log_open(const char *filename, int log_flags) fprintf(logfile, "===\n=== Logfile started at %s===\n", ctime(<ime)); return log_create(log_flags, logfile, log_stdio); } - return 0; + return NULL; } int log_level(log_t * log, int flags) diff --git a/src/util/log.h b/src/util/log.h index bd38cfa7d..06d150751 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -43,6 +43,8 @@ extern "C" { #define LOG_FLUSH 0x10 #define LOG_BRIEF 0x20 + + extern int log_stderr; #ifdef __cplusplus } #endif