2017-10-07 19:44:23 +02:00
|
|
|
|
#include <platform.h>
|
|
|
|
|
#include <kernel/config.h>
|
2017-10-07 18:03:22 +02:00
|
|
|
|
#include "orderfile.h"
|
|
|
|
|
|
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
|
#include <kernel/unit.h>
|
2017-10-07 19:44:23 +02:00
|
|
|
|
#include <kernel/order.h>
|
|
|
|
|
#include <kernel/messages.h>
|
2017-10-07 18:03:22 +02:00
|
|
|
|
|
2017-10-07 19:44:23 +02:00
|
|
|
|
#include <util/base36.h>
|
|
|
|
|
#include <util/message.h>
|
|
|
|
|
#include <util/language.h>
|
|
|
|
|
#include <util/log.h>
|
|
|
|
|
#include <util/filereader.h>
|
|
|
|
|
#include <util/parser.h>
|
2017-10-07 18:03:22 +02:00
|
|
|
|
|
2017-10-07 19:44:23 +02:00
|
|
|
|
#include <assert.h>
|
2017-10-07 18:03:22 +02:00
|
|
|
|
#include <stdio.h>
|
2017-10-07 19:44:23 +02:00
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
|
static unit *unitorders(input *in, faction *f)
|
2017-10-07 19:44:23 +02:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
|
|
if (!f)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
i = getid();
|
|
|
|
|
u = findunitg(i, NULL);
|
|
|
|
|
|
|
|
|
|
if (u && u->faction == f) {
|
|
|
|
|
order **ordp;
|
|
|
|
|
|
|
|
|
|
if (!fval(u, UFL_ORDERS)) {
|
|
|
|
|
/* alle wiederholbaren, langen befehle werden gesichert: */
|
|
|
|
|
fset(u, UFL_ORDERS);
|
|
|
|
|
u->old_orders = u->orders;
|
|
|
|
|
ordp = &u->old_orders;
|
|
|
|
|
while (*ordp) {
|
|
|
|
|
order *ord = *ordp;
|
|
|
|
|
keyword_t kwd = getkeyword(ord);
|
|
|
|
|
if (!is_repeated(kwd)) {
|
|
|
|
|
*ordp = ord->next;
|
|
|
|
|
ord->next = NULL;
|
|
|
|
|
free_order(ord);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ordp = &ord->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
free_orders(&u->orders);
|
|
|
|
|
}
|
|
|
|
|
u->orders = 0;
|
|
|
|
|
|
|
|
|
|
ordp = &u->orders;
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
const char *s;
|
|
|
|
|
/* Erst wenn wir sicher sind, dass kein Befehl
|
|
|
|
|
* eingegeben wurde, checken wir, ob nun eine neue
|
|
|
|
|
* Einheit oder ein neuer Spieler drankommt */
|
|
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
|
s = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
if (s == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (s[0]) {
|
|
|
|
|
if (s[0] != '@') {
|
|
|
|
|
char token[64];
|
|
|
|
|
const char *stok = s;
|
|
|
|
|
stok = parse_token(&stok, token, sizeof(token));
|
|
|
|
|
|
|
|
|
|
if (stok) {
|
|
|
|
|
bool quit = false;
|
|
|
|
|
param_t param = findparam(stok, u->faction->locale);
|
|
|
|
|
switch (param) {
|
|
|
|
|
case P_UNIT:
|
|
|
|
|
case P_REGION:
|
|
|
|
|
quit = true;
|
|
|
|
|
break;
|
|
|
|
|
case P_FACTION:
|
|
|
|
|
case P_NEXT:
|
|
|
|
|
case P_GAMENAME:
|
|
|
|
|
/* these terminate the orders, so we apply extra checking */
|
|
|
|
|
if (strlen(stok) >= 3) {
|
|
|
|
|
quit = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
quit = false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (quit) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-20 08:51:06 +01:00
|
|
|
|
/* Nun wird der Befehl erzeut und eingeh<65>ngt */
|
2017-10-07 19:44:23 +02:00
|
|
|
|
*ordp = parse_order(s, u->faction->locale);
|
|
|
|
|
if (*ordp) {
|
|
|
|
|
ordp = &(*ordp)->next;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ADDMSG(&f->msgs, msg_message("parse_error", "unit command", u, s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static faction *factionorders(void)
|
2017-10-07 18:03:22 +02:00
|
|
|
|
{
|
|
|
|
|
faction *f = NULL;
|
2017-10-07 19:44:23 +02:00
|
|
|
|
int fid = getid();
|
2017-10-07 18:03:22 +02:00
|
|
|
|
|
2017-10-07 19:44:23 +02:00
|
|
|
|
f = findfaction(fid);
|
|
|
|
|
|
|
|
|
|
if (f != NULL && !fval(f, FFL_NPC)) {
|
|
|
|
|
char token[128];
|
|
|
|
|
const char *pass = gettoken(token, sizeof(token));
|
|
|
|
|
|
|
|
|
|
if (!checkpasswd(f, (const char *)pass)) {
|
|
|
|
|
log_debug("Invalid password for faction %s", itoa36(fid));
|
|
|
|
|
ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", pass));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* Die Partei hat sich zumindest gemeldet, so dass sie noch
|
2017-11-20 08:51:06 +01:00
|
|
|
|
* nicht als unt<EFBFBD>tig gilt */
|
2017-10-07 19:44:23 +02:00
|
|
|
|
f->lastorders = turn;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
log_debug("orders for invalid faction %s", itoa36(fid));
|
|
|
|
|
}
|
|
|
|
|
return f;
|
2017-10-07 18:03:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
|
int read_orders(input *in)
|
2017-10-07 18:03:22 +02:00
|
|
|
|
{
|
2017-10-07 19:44:23 +02:00
|
|
|
|
const char *b;
|
|
|
|
|
int nfactions = 0;
|
|
|
|
|
struct faction *f = NULL;
|
2017-11-20 08:51:06 +01:00
|
|
|
|
const struct locale *lang = default_locale;
|
2017-10-07 19:44:23 +02:00
|
|
|
|
|
|
|
|
|
/* TODO: recognize UTF8 BOM */
|
2017-10-07 20:17:04 +02:00
|
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
|
|
|
|
|
/* Auffinden der ersten Partei, und danach abarbeiten bis zur letzten
|
|
|
|
|
* Partei */
|
|
|
|
|
|
|
|
|
|
while (b) {
|
|
|
|
|
char token[128];
|
|
|
|
|
param_t p;
|
|
|
|
|
const char *s;
|
|
|
|
|
init_tokens_str(b);
|
|
|
|
|
s = gettoken(token, sizeof(token));
|
|
|
|
|
p = findparam_block(s, lang, true);
|
|
|
|
|
switch (p) {
|
|
|
|
|
case P_GAMENAME:
|
|
|
|
|
case P_FACTION:
|
|
|
|
|
f = factionorders();
|
|
|
|
|
if (f) {
|
2017-11-20 08:51:06 +01:00
|
|
|
|
lang = f->locale;
|
2017-10-07 19:44:23 +02:00
|
|
|
|
++nfactions;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* in factionorders wird nur eine zeile gelesen:
|
|
|
|
|
* diejenige mit dem passwort. Die befehle der units
|
|
|
|
|
* werden geloescht, und die Partei wird als aktiv
|
|
|
|
|
* vermerkt. */
|
|
|
|
|
|
|
|
|
|
case P_UNIT:
|
2017-10-07 20:17:04 +02:00
|
|
|
|
if (!f || !unitorders(in, f)) {
|
2017-10-07 19:44:23 +02:00
|
|
|
|
do {
|
2017-10-07 20:17:04 +02:00
|
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
if (!b) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
init_tokens_str(b);
|
|
|
|
|
s = gettoken(token, sizeof(token));
|
|
|
|
|
p = (s && s[0] != '@') ? findparam(s, lang) : NOPARAM;
|
|
|
|
|
} while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT
|
|
|
|
|
&& p != P_GAMENAME);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Falls in unitorders() abgebrochen wird, steht dort entweder eine neue
|
|
|
|
|
* Partei, eine neue Einheit oder das File-Ende. Das switch() wird erneut
|
|
|
|
|
* durchlaufen, und die entsprechende Funktion aufgerufen. Man darf buf
|
2017-11-20 08:51:06 +01:00
|
|
|
|
* auf alle F<EFBFBD>lle nicht <EFBFBD>berschreiben! Bei allen anderen Eintr<EFBFBD>gen hier
|
|
|
|
|
* muss buf erneut gef<EFBFBD>llt werden, da die betreffende Information in nur
|
|
|
|
|
* einer Zeile steht, und nun die n<EFBFBD>chste gelesen werden muss. */
|
2017-10-07 19:44:23 +02:00
|
|
|
|
|
|
|
|
|
case P_NEXT:
|
|
|
|
|
f = NULL;
|
2017-11-20 08:51:06 +01:00
|
|
|
|
lang = default_locale;
|
2017-10-07 20:17:04 +02:00
|
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2017-10-07 20:17:04 +02:00
|
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_info("done reading orders for %d factions", nfactions);
|
|
|
|
|
return 0;
|
2017-10-07 18:03:22 +02:00
|
|
|
|
}
|
2017-10-07 20:17:04 +02:00
|
|
|
|
|
|
|
|
|
static const char * file_getbuf(void *data)
|
|
|
|
|
{
|
|
|
|
|
FILE *F = (FILE *)data;
|
|
|
|
|
return getbuf(F, ENCODING_UTF8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int readorders(const char *filename)
|
|
|
|
|
{
|
|
|
|
|
input in;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
FILE *F = NULL;
|
|
|
|
|
F = fopen(filename, "r");
|
|
|
|
|
if (!F) {
|
|
|
|
|
perror(filename);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
log_info("reading orders from %s", filename);
|
|
|
|
|
in.getbuf = file_getbuf;
|
|
|
|
|
in.data = F;
|
|
|
|
|
result = read_orders(&in);
|
|
|
|
|
fclose(F);
|
|
|
|
|
return result;
|
|
|
|
|
}
|