Introduce command encapsulation structure.

This commit is contained in:
Eric S. Raymond 2017-06-20 16:04:50 -04:00
parent 6c4f140333
commit 7a3f3ec7a6
3 changed files with 85 additions and 79 deletions

118
actions.c
View file

@ -995,14 +995,14 @@ static int wave(token_t verb, token_t obj)
} }
} }
int action(FILE *input, enum speechpart part, token_t verb, token_t obj) int action(FILE *input, struct command_t command)
/* Analyse a verb. Remember what it was, go back for object if second word /* Analyse a verb. Remember what it was, go back for object if second word
* unless verb is "say", which snarfs arbitrary second word. * unless verb is "say", which snarfs arbitrary second word.
*/ */
{ {
token_t spk = ACTSPK[verb]; token_t spk = ACTSPK[command.verb];
if (part == unknown) { if (command.part == unknown) {
/* Analyse an object word. See if the thing is here, whether /* Analyse an object word. See if the thing is here, whether
* we've got a verb yet, and so on. Object must be here * we've got a verb yet, and so on. Object must be here
* unless verb is "find" or "invent(ory)" (and no new verb * unless verb is "find" or "invent(ory)" (and no new verb
@ -1010,35 +1010,35 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
* they are never actually dropped at any location, but might * they are never actually dropped at any location, but might
* be here inside the bottle or urn or as a feature of the * be here inside the bottle or urn or as a feature of the
* location. */ * location. */
if (HERE(obj)) if (HERE(command.obj))
/* FALL THROUGH */; /* FALL THROUGH */;
else if (obj == GRATE) { else if (command.obj == GRATE) {
if (game.loc == LOC_START || game.loc == LOC_VALLEY || game.loc == LOC_SLIT) if (game.loc == LOC_START || game.loc == LOC_VALLEY || game.loc == LOC_SLIT)
obj = DPRSSN; command.obj = DPRSSN;
if (game.loc == LOC_COBBLE || game.loc == LOC_DEBRIS || game.loc == LOC_AWKWARD || if (game.loc == LOC_COBBLE || game.loc == LOC_DEBRIS || game.loc == LOC_AWKWARD ||
game.loc == LOC_BIRD || game.loc == LOC_PITTOP) game.loc == LOC_BIRD || game.loc == LOC_PITTOP)
obj = ENTRNC; command.obj = ENTRNC;
if (obj != GRATE) if (command.obj != GRATE)
return GO_MOVE; return GO_MOVE;
} else if (obj == DWARF && ATDWRF(game.loc) > 0) } else if (command.obj == DWARF && ATDWRF(game.loc) > 0)
/* FALL THROUGH */; /* FALL THROUGH */;
else if ((LIQUID() == obj && HERE(BOTTLE)) || obj == LIQLOC(game.loc)) else if ((LIQUID() == command.obj && HERE(BOTTLE)) || command.obj == LIQLOC(game.loc))
/* FALL THROUGH */; /* FALL THROUGH */;
else if (obj == OIL && HERE(URN) && game.prop[URN] != 0) { else if (command.obj == OIL && HERE(URN) && game.prop[URN] != 0) {
obj = URN; command.obj = URN;
/* FALL THROUGH */; /* FALL THROUGH */;
} else if (obj == PLANT && AT(PLANT2) && game.prop[PLANT2] != 0) { } else if (command.obj == PLANT && AT(PLANT2) && game.prop[PLANT2] != 0) {
obj = PLANT2; command.obj = PLANT2;
/* FALL THROUGH */; /* FALL THROUGH */;
} else if (obj == KNIFE && game.knfloc == game.loc) { } else if (command.obj == KNIFE && game.knfloc == game.loc) {
game.knfloc = -1; game.knfloc = -1;
spk = KNIVES_VANISH; spk = KNIVES_VANISH;
RSPEAK(spk); RSPEAK(spk);
return GO_CLEAROBJ; return GO_CLEAROBJ;
} else if (obj == ROD && HERE(ROD2)) { } else if (command.obj == ROD && HERE(ROD2)) {
obj = ROD2; command.obj = ROD2;
/* FALL THROUGH */; /* FALL THROUGH */;
} else if ((verb == FIND || verb == INVENT) && WD2 <= 0) } else if ((command.verb == FIND || command.verb == INVENT) && WD2 <= 0)
/* FALL THROUGH */; /* FALL THROUGH */;
else { else {
SETPRM(1, WD1, WD1X); SETPRM(1, WD1, WD1X);
@ -1048,36 +1048,36 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
if (WD2 > 0) if (WD2 > 0)
return GO_WORD2; return GO_WORD2;
if (verb != 0) if (command.verb != 0)
part = transitive; command.part = transitive;
} }
switch (part) { switch (command.part) {
case intransitive: case intransitive:
if (WD2 > 0 && verb != SAY) if (WD2 > 0 && command.verb != SAY)
return GO_WORD2; return GO_WORD2;
if (verb == SAY)obj = WD2; if (command.verb == SAY)command.obj = WD2;
if (obj == 0 || obj == INTRANSITIVE) { if (command.obj == 0 || command.obj == INTRANSITIVE) {
/* Analyse an intransitive verb (ie, no object given yet). */ /* Analyse an intransitive verb (ie, no object given yet). */
switch (verb - 1) { switch (command.verb - 1) {
case 0: /* CARRY */ case 0: /* CARRY */
return carry(verb, INTRANSITIVE); return carry(command.verb, INTRANSITIVE);
case 1: /* DROP */ case 1: /* DROP */
return GO_UNKNOWN; return GO_UNKNOWN;
case 2: /* SAY */ case 2: /* SAY */
return GO_UNKNOWN; return GO_UNKNOWN;
case 3: /* UNLOC */ case 3: /* UNLOC */
return lock(verb, INTRANSITIVE); return lock(command.verb, INTRANSITIVE);
case 4: { /* NOTHI */ case 4: { /* NOTHI */
RSPEAK(OK_MAN); RSPEAK(OK_MAN);
return (GO_CLEAROBJ); return (GO_CLEAROBJ);
} }
case 5: /* LOCK */ case 5: /* LOCK */
return lock(verb, INTRANSITIVE); return lock(command.verb, INTRANSITIVE);
case 6: /* LIGHT */ case 6: /* LIGHT */
return light(verb, INTRANSITIVE); return light(command.verb, INTRANSITIVE);
case 7: /* EXTIN */ case 7: /* EXTIN */
return extinguish(verb, INTRANSITIVE); return extinguish(command.verb, INTRANSITIVE);
case 8: /* WAVE */ case 8: /* WAVE */
return GO_UNKNOWN; return GO_UNKNOWN;
case 9: /* CALM */ case 9: /* CALM */
@ -1087,13 +1087,13 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
return GO_CLEAROBJ; return GO_CLEAROBJ;
} }
case 11: /* ATTAC */ case 11: /* ATTAC */
return attack(input, verb, obj); return attack(input, command.verb, command.obj);
case 12: /* POUR */ case 12: /* POUR */
return pour(verb, obj); return pour(command.verb, command.obj);
case 13: /* EAT */ case 13: /* EAT */
return eat(verb, INTRANSITIVE); return eat(command.verb, INTRANSITIVE);
case 14: /* DRINK */ case 14: /* DRINK */
return drink(verb, obj); return drink(command.verb, command.obj);
case 15: /* RUB */ case 15: /* RUB */
return GO_UNKNOWN; return GO_UNKNOWN;
case 16: /* TOSS */ case 16: /* TOSS */
@ -1107,7 +1107,7 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
case 20: /* FEED */ case 20: /* FEED */
return GO_UNKNOWN; return GO_UNKNOWN;
case 21: /* FILL */ case 21: /* FILL */
return fill(verb, obj); return fill(command.verb, command.obj);
case 22: /* BLAST */ case 22: /* BLAST */
blast(); blast();
return GO_CLEAROBJ; return GO_CLEAROBJ;
@ -1119,7 +1119,7 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
case 25: /* BRIEF */ case 25: /* BRIEF */
return brief(); return brief();
case 26: /* READ */ case 26: /* READ */
return read(verb, INTRANSITIVE); return read(command.verb, INTRANSITIVE);
case 27: /* BREAK */ case 27: /* BREAK */
return GO_UNKNOWN; return GO_UNKNOWN;
case 28: /* WAKE */ case 28: /* WAKE */
@ -1129,7 +1129,7 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
case 30: /* RESU */ case 30: /* RESU */
return resume(); return resume();
case 31: /* FLY */ case 31: /* FLY */
return fly(verb, INTRANSITIVE); return fly(command.verb, INTRANSITIVE);
case 32: /* LISTE */ case 32: /* LISTE */
return listen(); return listen();
case 33: /* ZZZZ */ case 33: /* ZZZZ */
@ -1140,27 +1140,27 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
/* FALLTHRU */ /* FALLTHRU */
case transitive: case transitive:
/* Analyse a transitive verb. */ /* Analyse a transitive verb. */
switch (verb - 1) { switch (command.verb - 1) {
case 0: /* CARRY */ case 0: /* CARRY */
return carry(verb, obj); return carry(command.verb, command.obj);
case 1: /* DROP */ case 1: /* DROP */
return discard(verb, obj, false); return discard(command.verb, command.obj, false);
case 2: /* SAY */ case 2: /* SAY */
return say(); return say();
case 3: /* UNLOC */ case 3: /* UNLOC */
return lock(verb, obj); return lock(command.verb, command.obj);
case 4: { /* NOTHI */ case 4: { /* NOTHI */
RSPEAK(OK_MAN); RSPEAK(OK_MAN);
return (GO_CLEAROBJ); return (GO_CLEAROBJ);
} }
case 5: /* LOCK */ case 5: /* LOCK */
return lock(verb, obj); return lock(command.verb, command.obj);
case 6: /* LIGHT */ case 6: /* LIGHT */
return light(verb, obj); return light(command.verb, command.obj);
case 7: /* EXTI */ case 7: /* EXTI */
return extinguish(verb, obj); return extinguish(command.verb, command.obj);
case 8: /* WAVE */ case 8: /* WAVE */
return wave(verb, obj); return wave(command.verb, command.obj);
case 9: { /* CALM */ case 9: { /* CALM */
RSPEAK(spk); RSPEAK(spk);
return GO_CLEAROBJ; return GO_CLEAROBJ;
@ -1170,29 +1170,29 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
return GO_CLEAROBJ; return GO_CLEAROBJ;
} }
case 11: /* ATTAC */ case 11: /* ATTAC */
return attack(input, verb, obj); return attack(input, command.verb, command.obj);
case 12: /* POUR */ case 12: /* POUR */
return pour(verb, obj); return pour(command.verb, command.obj);
case 13: /* EAT */ case 13: /* EAT */
return eat(verb, obj); return eat(command.verb, command.obj);
case 14: /* DRINK */ case 14: /* DRINK */
return drink(verb, obj); return drink(command.verb, command.obj);
case 15: /* RUB */ case 15: /* RUB */
return rub(verb, obj); return rub(command.verb, command.obj);
case 16: /* TOSS */ case 16: /* TOSS */
return throw (input, verb, obj); return throw (input, command.verb, command.obj);
case 17: { /* QUIT */ case 17: { /* QUIT */
RSPEAK(spk); RSPEAK(spk);
return GO_CLEAROBJ; return GO_CLEAROBJ;
} }
case 18: /* FIND */ case 18: /* FIND */
return find(verb, obj); return find(command.verb, command.obj);
case 19: /* INVEN */ case 19: /* INVEN */
return find(verb, obj); return find(command.verb, command.obj);
case 20: /* FEED */ case 20: /* FEED */
return feed(verb, obj); return feed(command.verb, command.obj);
case 21: /* FILL */ case 21: /* FILL */
return fill(verb, obj); return fill(command.verb, command.obj);
case 22: /* BLAST */ case 22: /* BLAST */
blast(); blast();
return GO_CLEAROBJ; return GO_CLEAROBJ;
@ -1209,11 +1209,11 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
return GO_CLEAROBJ; return GO_CLEAROBJ;
} }
case 26: /* READ */ case 26: /* READ */
return read(verb, obj); return read(command.verb, command.obj);
case 27: /* BREAK */ case 27: /* BREAK */
return vbreak(verb, obj); return vbreak(command.verb, command.obj);
case 28: /* WAKE */ case 28: /* WAKE */
return wake(verb, obj); return wake(command.verb, command.obj);
case 29: { /* SUSP */ case 29: { /* SUSP */
RSPEAK(spk); RSPEAK(spk);
return GO_CLEAROBJ; return GO_CLEAROBJ;
@ -1223,7 +1223,7 @@ int action(FILE *input, enum speechpart part, token_t verb, token_t obj)
return GO_CLEAROBJ; return GO_CLEAROBJ;
} }
case 31: /* FLY */ case 31: /* FLY */
return fly(verb, obj); return fly(command.verb, command.obj);
case 32: { /* LISTE */ case 32: { /* LISTE */
RSPEAK(spk); RSPEAK(spk);
return GO_CLEAROBJ; return GO_CLEAROBJ;

View file

@ -181,8 +181,14 @@ extern long AMBER, ATTACK, AXE, BACK, BATTERY, BEAR,
enum speechpart {unknown, intransitive, transitive}; enum speechpart {unknown, intransitive, transitive};
struct command_t {
enum speechpart part;
vocab_t verb;
vocab_t obj;
};
void initialise(void); void initialise(void);
int action(FILE *input, enum speechpart part, token_t verb, token_t obj); int action(FILE *input, struct command_t command);
/* Phase codes for action returns. /* Phase codes for action returns.
* These were at one time FORTRAN line numbers. * These were at one time FORTRAN line numbers.

38
main.c
View file

@ -925,11 +925,11 @@ static void listobjects(void)
static bool do_command(FILE *cmdin) static bool do_command(FILE *cmdin)
/* Get and execute a command */ /* Get and execute a command */
{ {
long verb = 0, V1, V2; long V1, V2;
long kmod, defn; long kmod, defn;
static long igo = 0; static long igo = 0;
static long obj = 0; static struct command_t command;
enum speechpart part; command.verb = 0;
/* Can't leave cave once it's closing (except by main office). */ /* Can't leave cave once it's closing (except by main office). */
if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) { if (OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
@ -979,7 +979,7 @@ static bool do_command(FILE *cmdin)
if (TOTING(BEAR))RSPEAK(TAME_BEAR); if (TOTING(BEAR))RSPEAK(TAME_BEAR);
speak(msg); speak(msg);
if (FORCED(game.loc)) { if (FORCED(game.loc)) {
if (playermove(verb, 1)) if (playermove(command.verb, 1))
return true; return true;
else else
continue; /* back to top of main interpreter loop */ continue; /* back to top of main interpreter loop */
@ -990,9 +990,9 @@ static bool do_command(FILE *cmdin)
listobjects(); listobjects();
L2012: L2012:
verb = 0; command.verb = 0;
game.oldobj = obj; game.oldobj = command.obj;
obj = 0; command.obj = 0;
L2600: L2600:
checkhints(); checkhints();
@ -1032,10 +1032,10 @@ L2607:
if (game.trndex <= TRNVLS) if (game.trndex <= TRNVLS)
game.thresh = MOD(TRNVAL[game.trndex], 100000) + 1; game.thresh = MOD(TRNVAL[game.trndex], 100000) + 1;
} }
if (verb == SAY && WD2 > 0) if (command.verb == SAY && WD2 > 0)
verb = 0; command.verb = 0;
if (verb == SAY) { if (command.verb == SAY) {
part = transitive; command.part = transitive;
goto Laction; goto Laction;
} }
if (closecheck()) { if (closecheck()) {
@ -1091,17 +1091,17 @@ Lookup:
kmod = MOD(defn, 1000); kmod = MOD(defn, 1000);
switch (defn / 1000) { switch (defn / 1000) {
case 0: case 0:
if (playermove(verb, kmod)) if (playermove(command.verb, kmod))
return true; return true;
else else
continue; /* back to top of main interpreter loop */ continue; /* back to top of main interpreter loop */
case 1: case 1:
part = unknown; command.part = unknown;
obj = kmod; command.obj = kmod;
break; break;
case 2: case 2:
part = intransitive; command.part = intransitive;
verb = kmod; command.verb = kmod;
break; break;
case 3: case 3:
RSPEAK(kmod); RSPEAK(kmod);
@ -1111,11 +1111,11 @@ Lookup:
} }
Laction: Laction:
switch (action(cmdin, part, verb, obj)) { switch (action(cmdin, command)) {
case GO_TERMINATE: case GO_TERMINATE:
return true; return true;
case GO_MOVE: case GO_MOVE:
playermove(verb, NUL); playermove(command.verb, NUL);
return true; return true;
case GO_TOP: case GO_TOP:
continue; /* back to top of main interpreter loop */ continue; /* back to top of main interpreter loop */
@ -1138,7 +1138,7 @@ Laction:
* (see attack()). */ * (see attack()). */
SETPRM(1, WD1, WD1X); SETPRM(1, WD1, WD1X);
RSPEAK(DO_WHAT); RSPEAK(DO_WHAT);
obj = 0; command.obj = 0;
goto L2600; goto L2600;
case GO_DWARFWAKE: case GO_DWARFWAKE:
/* Oh dear, he's disturbed the dwarves. */ /* Oh dear, he's disturbed the dwarves. */