Add script to check for unused command letters

WIP: autoexplore code
This commit is contained in:
rob 2022-09-18 17:24:50 +10:00
parent e1507aa7d6
commit 3f162673cf
9 changed files with 164 additions and 5 deletions

4
data.c
View File

@ -174,8 +174,10 @@ void initcommands(void) {
addcommand(CMD_UP, '<', "Go up stairs."); addcommand(CMD_UP, '<', "Go up stairs.");
addcommand(CMD_DOWN, '>', "Go down stairs, enter a shop/portal."); addcommand(CMD_DOWN, '>', "Go down stairs, enter a shop/portal.");
addcommand(CMD_GO, 'G', "Go to a set position (pathfind)."); addcommand(CMD_GO, 'G', "Go to a set position (pathfind).");
addcommand(CMD_EXPLORE, 'z', "Auto-explore the current level");
// Actions // Actions
addcommand(CMD_AGAIN, 'g', "Repeat last action."); addcommand(CMD_AGAIN, 'g', "Repeat last action (do aGain).");
addcommand(CMD_REPEATSPELL, 'p', "RePeat last spell/ability.");
addcommand(CMD_REST, '.', "Rest once."); addcommand(CMD_REST, '.', "Rest once.");
addcommand(CMD_PICKUP, ',', "Pick up something from the ground."); addcommand(CMD_PICKUP, ',', "Pick up something from the ground.");
addcommand(CMD_COMMS, 'c', "Chat/Communicate with someone."); addcommand(CMD_COMMS, 'c', "Chat/Communicate with someone.");

6
defs.h
View File

@ -3748,6 +3748,7 @@ enum FLAG {
F_CAREFULMOVE, // moving slowly on purpose to avoid slipping. F_CAREFULMOVE, // moving slowly on purpose to avoid slipping.
F_AUTOCMD, // val0 = how many times to repeat this F_AUTOCMD, // val0 = how many times to repeat this
F_LASTCMD, // text[0] = last command performed, v0/1 = x/y of cell, v2=various F_LASTCMD, // text[0] = last command performed, v0/1 = x/y of cell, v2=various
F_LASTSPELL, // val0=id of last spell cast, for player only
F_WILLTHROW, // this lf will treat obid v0 as a thrown missile. F_WILLTHROW, // this lf will treat obid v0 as a thrown missile.
F_CANSTUDY, // lf can study spells from school v0 F_CANSTUDY, // lf can study spells from school v0
F_CANLEARN, // lf is able to learn skill val0 F_CANLEARN, // lf is able to learn skill val0
@ -4373,8 +4374,9 @@ enum FLAG {
F_XRAYVIS, //val0=num of walls we can see through F_XRAYVIS, //val0=num of walls we can see through
F_CANSEETHROUGHMAT, //val0=kind of material you can see through F_CANSEETHROUGHMAT, //val0=kind of material you can see through
F_CANSEETHROUGHLF, // larger lifeforms don't block los for us F_CANSEETHROUGHLF, // larger lifeforms don't block los for us
F_PATHFINDING, // you are following a path via 'G' F_PATHFINDING, // you are following a path via 'G' or 'z'
// to coords v0,v1 on mapid v2 // to coords v0,v1 on mapid v2
// str = G or z
F_SPRINTING, // you are sprinting. F_SPRINTING, // you are sprinting.
F_WOUNDING, // increase all damage done by this lf by v0 F_WOUNDING, // increase all damage done by this lf by v0
F_WINDSHIELD,// has a windshield protecting against missiles of speed F_WINDSHIELD,// has a windshield protecting against missiles of speed
@ -4752,6 +4754,7 @@ enum COMMAND {
CMD_DROP, CMD_DROP,
CMD_DROPMULTI, CMD_DROPMULTI,
CMD_EAT, CMD_EAT,
CMD_EXPLORE,
CMD_FIRE, CMD_FIRE,
CMD_FIRENEW, CMD_FIRENEW,
CMD_FORCEATTACK, CMD_FORCEATTACK,
@ -4778,6 +4781,7 @@ enum COMMAND {
CMD_QUAFF, CMD_QUAFF,
CMD_QUIT, CMD_QUIT,
CMD_READ, CMD_READ,
CMD_REPEATSPELL,
CMD_REST, CMD_REST,
CMD_RESTFULL, CMD_RESTFULL,
CMD_SAVEQUIT, CMD_SAVEQUIT,

2
find_unused_letters.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
echo {a..z} {A..Z} | tr ' ' '\n' | egrep -v $(grep addcom *.c | grep -v '//' | grep -v \\\\ | awk -F\' '{print $2}' | egrep "[a-zA-Z]" | tr '\n' '|' | sed 's/^|//;s/|$//')

41
io.c
View File

@ -11272,6 +11272,9 @@ void handleinput(void) {
case CMD_GO: // go somewhere (pathfind) case CMD_GO: // go somewhere (pathfind)
startpathfind(); startpathfind();
break; break;
case CMD_EXPLORE: // autoexplore
startexplore(B_TRUE);
break;
case CMD_HELP: // help case CMD_HELP: // help
dohelp('?'); dohelp('?');
break; break;
@ -11313,6 +11316,14 @@ void handleinput(void) {
case CMD_MEMMAGIC: // 'M'emorise magic/ability shortcut case CMD_MEMMAGIC: // 'M'emorise magic/ability shortcut
domemmagic(); domemmagic();
break; break;
case CMD_REPEATSPELL: // 're'p'eat last spell/ability
f = hasflag(player->flags, F_LASTSPELL);
if (f) {
domagic(f->val[0], NA, NA);
} else {
msg("There is no previous spell to repeat.");
}
break;
case CMD_UP: // go up case CMD_UP: // go up
if (isprone(player)) { if (isprone(player)) {
standup(player); standup(player);
@ -15262,6 +15273,33 @@ void showpath(lifeform_t *lf) {
restoregamewindows(); restoregamewindows();
} }
// TODO: not quite working. sometimes seems to flip back and forth between 2 positions
int startexplore(int announce) {
cell_t *c;
c = get_explore_target(player);
if (!c) {
msg("Can't find any unknown cells to explore.");
return B_TRUE;
}
killflagsofid(player->flags, F_PATHFINDING);
if (ai_createpathto(player, c)) {
addflag(player->flags, F_PATHFINDING, c->x, c->y, player->cell->map->id, "z");
//msg("Going from %d,%d to unexplored %d,%d", player->cell->x, player->cell->y, c->x, c->y);
if (announce) {
msg("Starting auto-explore.");
} else {
msg("Continuing auto-explore.");
}
} else {
msg("Pathfind failed from %d,%d to unexplored %d,%d", player->cell->x, player->cell->y, c->x, c->y);
}
return B_FALSE;
}
void startpathfind(void) { void startpathfind(void) {
cell_t *c = NULL; cell_t *c = NULL;
c = askcoords("Go to where?", "Goto->", TT_NONE, player, UNLIMITED, LOF_DONTNEED, B_FALSE); c = askcoords("Go to where?", "Goto->", TT_NONE, player, UNLIMITED, LOF_DONTNEED, B_FALSE);
@ -15274,11 +15312,10 @@ void startpathfind(void) {
(haslos(player, c) && !cellwalkable(player, c, NULL))) { (haslos(player, c) && !cellwalkable(player, c, NULL))) {
msg("You don't know how to get there."); msg("You don't know how to get there.");
return; return;
} }
// try to find path. // try to find path.
if (ai_createpathto(player, c)) { if (ai_createpathto(player, c)) {
addflag(player->flags, F_PATHFINDING, c->x, c->y, player->cell->map->id, NULL); addflag(player->flags, F_PATHFINDING, c->x, c->y, player->cell->map->id, "G");
} else { } else {
msg("You don't know how to get there."); msg("You don't know how to get there.");
} }

1
io.h
View File

@ -158,6 +158,7 @@ int showhiscoreline(void *hilitescore, int ncols, char **argv, char **colname);
void showlfarmour(lifeform_t *lf); void showlfarmour(lifeform_t *lf);
void showlfstats(lifeform_t *lf, int showall); void showlfstats(lifeform_t *lf, int showall);
void showpath(lifeform_t *lf); void showpath(lifeform_t *lf);
int startexplore(int announce);
void startpathfind(void); void startpathfind(void);
void textwithcol(WINDOW *win, char *buf); void textwithcol(WINDOW *win, char *buf);
void textwithcol_real(WINDOW *win, char *buf, int resetcolatend); void textwithcol_real(WINDOW *win, char *buf, int resetcolatend);

9
lf.c
View File

@ -2156,6 +2156,11 @@ int castspell(lifeform_t *lf, enum OBTYPE sid, lifeform_t *targlf, object_t *tar
// lose mp // lose mp
losemp(lf, cost); losemp(lf, cost);
// remember spell for rePeat command
if (isplayer(lf)) {
modflag(lf->flags, F_LASTSPELL, sid, NA, NA, NULL);
}
// spell fails? // spell fails?
// miscast chance? // miscast chance?
if (isplayer(lf) && !hasjob(lf, J_GOD)) { if (isplayer(lf) && !hasjob(lf, J_GOD)) {
@ -26512,6 +26517,10 @@ int useability(lifeform_t *lf, enum OBTYPE aid, lifeform_t *who, cell_t *where)
cwflag = lfhasflagval(lf, F_CANWILL, aid, NA, NA, NULL); cwflag = lfhasflagval(lf, F_CANWILL, aid, NA, NA, NULL);
rv = abilityeffects(lf, aid, where, who, cwflag); rv = abilityeffects(lf, aid, where, who, cwflag);
// remember ability for rePeat command
if (isplayer(lf)) {
modflag(lf->flags, F_LASTSPELL, aid, NA, NA, NULL);
}
if (!rv) { if (!rv) {
flag_t *f; flag_t *f;

91
map.c
View File

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "ai.h"
#include "astar.h" #include "astar.h"
#include "attack.h" #include "attack.h"
#include "defs.h" #include "defs.h"
@ -3044,6 +3045,62 @@ cell_t *get_closest_adjcell(cell_t *src, cell_t *dst) {
return NULL; return NULL;
} }
// find the closest known & walkable cell, with an adjacent unknown one
// note: only works when lf == player at the moment
cell_t *get_explore_target(lifeform_t *lf) {
int i;
int maxdist = MAXOF(MAX_MAPW, MAX_MAPH);
cell_t *src, **poss, *sel = NULL;
int x,y;
int nposs;
if (!lf) return NULL;
src = lf->cell;
for (i = 1;(i< maxdist && !sel); i++) {
int nok = 0;
nposs = 0;
poss = malloc(sizeof(cell_t *)*(i+2)*4);
//msg("player is %d,%d, dist %d.", lf->cell->x, lf->cell->y, i);
//msg("Box is %d,%d to %d, %d.", lf->cell->x - i, lf->cell->y - i,
// lf->cell->x + i, lf->cell->y + i);
for (y = lf->cell->y - i; y <= lf->cell->y + i; y++) {
for (x = lf->cell->x - i; x <= lf->cell->x + i; ) {
cell_t *newcell = NULL;
//msg("Checking %d,%d.",x, y);
newcell = getcellat(lf->cell->map,x,y);
if (newcell) {
nok++;
if (newcell->known != KG_UNKNOWN &&
cellwalkable(lf, newcell, NULL) &&
countadjcellsknown(newcell, B_FALSE, DT_COMPASS)) {
poss[nposs] = newcell;
nposs++;
}
}
// change x based on where we are in te square of
// possible cells
if (y == lf->cell->y - i || y== lf->cell->y + i) {
x++;
} else {
x += (i*2);
}
}
}
//msg("%d/%d possible cells at distance %d.",nposs, nok, i);
if (nposs) {
sel = poss[rnd(0,nposs-1)];
}
free(poss);
if (nok == 0) {
// no valid cells
break;
}
}
return sel;
}
int getdoorlockdiff(int depth) { int getdoorlockdiff(int depth) {
return 70 + (depth*3); return 70 + (depth*3);
} }
@ -3456,6 +3513,31 @@ int countadjcellswithflag(cell_t *cell, enum FLAG fid, int dirtype) {
return count; return count;
} }
int countadjcellsknown(cell_t *cell, int wantknown, int dirtype) {
int d;
int count = 0;
int start,end;
cell_t *newcell;
if (dirtype == DT_ORTH) {
start = D_N;
end = D_W;
} else { // ie. DT_COMPASS
start = DC_N;
end = DC_NW;
}
for (d = start; d <= end; d++) {
newcell = getcellindir(cell, d);
if (newcell) {
if (wantknown && (newcell->known != KG_UNKNOWN)) {
count++;
} else if (!wantknown && (newcell->known == KG_UNKNOWN)) {
count++;
}
}
}
return count;
}
int countadjcellsoftype(cell_t *cell, enum CELLTYPE id, int dirtype) { int countadjcellsoftype(cell_t *cell, enum CELLTYPE id, int dirtype) {
int d; int d;
int count = 0; int count = 0;
@ -7963,6 +8045,15 @@ void forgetcells(map_t *map, int amt) {
} }
} }
cell_t *getcellindirdist(cell_t *src, int dir, int dist) {
cell_t *c = src;
int i;
for (i = 0; (i < dist && c); i++) {
c = getcellindir(c,dir);
}
return c;
}
cell_t *getcellindir(cell_t *cell, int dir) { cell_t *getcellindir(cell_t *cell, int dir) {
cell_t *newcell; cell_t *newcell;
int newx,newy; int newx,newy;

3
map.h
View File

@ -72,6 +72,7 @@ object_t *gettopobject(cell_t *where, int forglyph);
//void calclight(map_t *map); //void calclight(map_t *map);
int calcroompos(map_t *map, int w, int h, int xmargin, int ymargin, int *bx, int *by, int force, int stayclose); int calcroompos(map_t *map, int w, int h, int xmargin, int ymargin, int *bx, int *by, int force, int stayclose);
int compassdir(int orthdir); int compassdir(int orthdir);
int countadjcellsknown(cell_t *cell, int wantknown, int dirtype);
int countadjcellsoftype(cell_t *cell, enum CELLTYPE id, int dirtype); int countadjcellsoftype(cell_t *cell, enum CELLTYPE id, int dirtype);
int countadjrooms(cell_t *cell, int dirtype); int countadjrooms(cell_t *cell, int dirtype);
int countadjcellswithflag(cell_t *cell, enum FLAG fid, int dirtype); int countadjcellswithflag(cell_t *cell, enum FLAG fid, int dirtype);
@ -141,8 +142,10 @@ room_t *findroom(map_t *m, int roomid);
map_t *findsurfaceexitmap(map_t *m); map_t *findsurfaceexitmap(map_t *m);
void forgetcells(map_t *map, int amt); void forgetcells(map_t *map, int amt);
cell_t *getcellindir(cell_t *cell, int dir); cell_t *getcellindir(cell_t *cell, int dir);
cell_t *getcellindirdist(cell_t *src, int dir, int dist);
enum TEMPERATURE getcelltemp(cell_t *c, int *actualtemp); enum TEMPERATURE getcelltemp(cell_t *c, int *actualtemp);
vault_t *getcellvault(cell_t *c); vault_t *getcellvault(cell_t *c);
cell_t *get_explore_target(lifeform_t *lf);
cell_t *getclosestroomcell(lifeform_t *lf, int roomid); cell_t *getclosestroomcell(lifeform_t *lf, int roomid);
int getnewdigdir(cell_t *cell, int lastdir, int turnpct, int *moved); int getnewdigdir(cell_t *cell, int lastdir, int turnpct, int *moved);
int getobchance(int habitat); int getobchance(int habitat);

12
nexus.c
View File

@ -1145,7 +1145,9 @@ void donextturn(map_t *map) {
// pathfinding? // pathfinding?
if (donormalmove && isplayer(who)) { if (donormalmove && isplayer(who)) {
if (lfhasflag(who, F_PATHFINDING)) { flag_t *pff;
pff = lfhasflag(who, F_PATHFINDING);
if (pff) {
cell_t *c; cell_t *c;
// follow..... // follow.....
c = ai_getnextcellinpath(who); c = ai_getnextcellinpath(who);
@ -1175,8 +1177,16 @@ void donextturn(map_t *map) {
} }
} }
} else { } else {
int keepgoing = B_FALSE;
msg("Arrived at destination."); msg("Arrived at destination.");
if (streq(pff->text, "z")) {
keepgoing = B_TRUE;
}
stoppathfinding(who); stoppathfinding(who);
if (keepgoing) {
startexplore(B_FALSE);
}
} }
} }
} }