new command looplisti

same as looplist, but it automatically counts the loops in "i", starting
with 0.

Example:

  a = [u v w] ; b = [x y z]
  looplisti $a m [echo $m (at $b $i) $i]

  u x 0
  v y 1
  w z 2

DOCREF looplisti
This commit is contained in:
ac-stef
2016-09-02 21:58:39 +02:00
parent a8db184eef
commit a6bffc1858
8 changed files with 38 additions and 33 deletions
+1 -1
View File
@@ -1051,7 +1051,7 @@ void preparectf(bool cleanonly=false)
if(e.type==CTF_FLAG)
{
e.spawned = true;
if(e.attr2>=2) { conoutf("\f3invalid ctf-flag entity (%i)", i); e.attr2 = 0; }
if(e.attr2>=2) { conoutf("\f3invalid ctf-flag entity (%d)", i); e.attr2 = 0; }
flaginfo &f = flaginfos[e.attr2];
f.flagent = &e;
f.pos.x = (float) e.x;
+19 -7
View File
@@ -523,7 +523,7 @@ const char *floatstr(float v, bool neat)
static int i = 0;
if(i > MAXSTRLEN - 100) i = 0;
char *t = s + i;
sprintf(t, !neat && (v) == int(v) ? "%.1f" : "%.7g", v); // was ftoa()
snprintf(t, 99, !neat && (v) == int(v) ? "%.1f" : "%.7g", v); // was ftoa()
i += strlen(t) + 1;
return t;
}
@@ -1216,27 +1216,38 @@ void explodelist(const char *s, vector<char *> &elems)
}
}
void looplist(char *list, char *varlist, char *body)
void looplist(char *list, char *varlist, char *body, bool withi)
{
vector<char *> vars;
explodelist(varlist, vars);
if(vars.length() < 1) return;
int varslen = vars.length();
if(withi) vars.add(newstring("i"));
vector<ident *> ids;
bool ok = true;
loopv(vars) if(ids.add(newident(vars[i]))->type != ID_ALIAS) { conoutf("looplist error: \"%s\" is readonly", vars[i]); ok = false; }
if(ok)
{
int ii = 0;
vector<char *> elems;
explodelist(list, elems);
loopv(ids) pushident(*ids[i], newstring(""));
loop_level++;
for(int i = 0; i <= elems.length() - vars.length(); i += vars.length())
for(int i = 0; i <= elems.length() - varslen; i += varslen)
{
loopvj(vars)
loopj(vars.length())
{
if(ids[j]->action != ids[j]->executing) delete[] ids[j]->action;
ids[j]->action = elems[i + j];
elems[i + j] = NULL;
if(j < varslen)
{
ids[j]->action = elems[i + j];
elems[i + j] = NULL;
}
else
{
defformatstring(sii)("%d", ii++);
ids[j]->action = newstring(sii);
}
}
execute(body);
loop_skip = false;
@@ -1249,7 +1260,8 @@ void looplist(char *list, char *varlist, char *body)
}
loopv(vars) delete[] vars[i];
}
COMMAND(looplist, "sss");
COMMANDF(looplist, "sss", (char *list, char *varlist, char *body) { looplist(list, varlist, body, false); });
COMMANDF(looplisti, "sss", (char *list, char *varlist, char *body) { looplist(list, varlist, body, true); });
char *indexlist(const char *s, int pos)
{
+3 -3
View File
@@ -199,7 +199,7 @@ void findkeycode(const char* s)
{
if(strcmp(s, keyms[i].name) == 0)
{
defformatstring(out)("%i", keyms[i].code);
defformatstring(out)("%d", keyms[i].code);
result(out);
return;
}
@@ -239,7 +239,7 @@ void keybind(const char *key, int type)
{
keym *km = findbind(key);
if(!km) { conoutf("unknown key \"%s\"", key); return; }
if(type < keym::ACTION_DEFAULT || type >= keym::NUMACTIONS) { conoutf("invalid bind type \"%i\"", type); return; }
if(type < keym::ACTION_DEFAULT || type >= keym::NUMACTIONS) { conoutf("invalid bind type \"%d\"", type); return; }
result(km->actions[type]);
}
@@ -253,7 +253,7 @@ bool bindc(int code, const char *action, int type)
void searchbinds(const char *action, int type)
{
if(!action || !action[0]) return;
if(type < keym::ACTION_DEFAULT || type >= keym::NUMACTIONS) { conoutf("invalid bind type \"%i\"", type); return; }
if(type < keym::ACTION_DEFAULT || type >= keym::NUMACTIONS) { conoutf("invalid bind type \"%d\"", type); return; }
vector<char> names;
loopv(keyms)
{
+1 -1
View File
@@ -335,7 +335,7 @@ void docwriteref(int allidents, const char *ref, const char *schemalocation, con
switch(id->type)
{
case ID_VAR:
f->printf("\t\t\t\t\t<value %s description=\"TODO\" minValue=\"%i\" maxValue=\"%i\" defaultValue=\"%i\" %s/>\n", id->minval>id->maxval ? "" : "token=\"N\"", id->minval, id->maxval, *id->storage.i, id->minval>id->maxval ? "readOnly=\"true\"" : "");
f->printf("\t\t\t\t\t<value %s description=\"TODO\" minValue=\"%d\" maxValue=\"%d\" defaultValue=\"%d\" %s/>\n", id->minval>id->maxval ? "" : "token=\"N\"", id->minval, id->maxval, *id->storage.i, id->minval>id->maxval ? "readOnly=\"true\"" : "");
break;
case ID_FVAR:
f->printf("\t\t\t\t\t<value %s description=\"TODO\" minValue=\"%s\" maxValue=\"%s\" defaultValue=\"%s\" %s/>\n", id->minvalf>id->maxvalf ? "" : "token=\"N\"",
+2 -2
View File
@@ -3346,7 +3346,7 @@ void process(ENetPacket *packet, int sender, int chan)
{
disconnect_client(sender, DISC_CN);
#ifndef STANDALONE
conoutf("ERROR: invalid client (msg %i)", type);
conoutf("ERROR: invalid client (msg %d)", type);
#endif
return;
}
@@ -3374,7 +3374,7 @@ void process(ENetPacket *packet, int sender, int chan)
{
disconnect_client(sender, DISC_CN);
#ifndef STANDALONE
conoutf("ERROR: invalid client (msg %i)", type);
conoutf("ERROR: invalid client (msg %d)", type);
#endif
return;
}
+4 -4
View File
@@ -1425,12 +1425,12 @@ struct killmessagesfile : serverconfigfile
if(gun < 0 || gun >= NUMGUNS)
{
logline(ACLOG_INFO, " error in line %i, invalid gun : %i", line, gun);
logline(ACLOG_INFO, " error in line %d, invalid gun : %d", line, gun);
errors++;
}
if(strlen(message)>MAXKILLMSGLEN)
{
logline(ACLOG_INFO, " error in line %i, too long message : string length is %i, max. allowed is %i", line, int(strlen(message)), MAXKILLMSGLEN);
logline(ACLOG_INFO, " error in line %d, too long message : string length is %d, max. allowed is %d", line, int(strlen(message)), MAXKILLMSGLEN);
errors++;
}
if(!errors)
@@ -1438,12 +1438,12 @@ struct killmessagesfile : serverconfigfile
if(fragmsg)
{
copystring(killmessages[0][gun], message, MAXKILLMSGLEN);
logline(ACLOG_VERBOSE, " added msg '%s' for frags with weapon %i ", message, gun);
logline(ACLOG_VERBOSE, " added msg '%s' for frags with weapon %d ", message, gun);
}
else
{
copystring(killmessages[1][gun], message, MAXKILLMSGLEN);
logline(ACLOG_VERBOSE, " added msg '%s' for gibs with weapon %i ", message, gun);
logline(ACLOG_VERBOSE, " added msg '%s' for gibs with weapon %d ", message, gun);
}
}
s = NULL;
+3 -3
View File
@@ -831,13 +831,13 @@ VARP(oldfashionedgunstats, 0, 0, 1);
void weapon::renderstats()
{
char gunstats[64];
if(oldfashionedgunstats) sprintf(gunstats, "%i/%i", mag, ammo); else sprintf(gunstats, "%i", mag);
if(oldfashionedgunstats) snprintf(gunstats, 64, "%d/%d", mag, ammo); else snprintf(gunstats, 64, "%d", mag);
draw_text(gunstats, HUDPOS_WEAPON + HUDPOS_NUMBERSPACING, 823);
if(!oldfashionedgunstats)
{
int offset = text_width(gunstats);
glScalef(0.5f, 0.5f, 1.0f);
sprintf(gunstats, "%i", ammo);
snprintf(gunstats, 64, "%d", ammo);
draw_text(gunstats, (HUDPOS_WEAPON + HUDPOS_NUMBERSPACING + offset)*2, 826*2);
glLoadIdentity();
}
@@ -1179,7 +1179,7 @@ void grenades::dropnade()
void grenades::renderstats()
{
char gunstats[64];
sprintf(gunstats, "%i", mag);
sprintf(gunstats, "%d", mag);
draw_text(gunstats, oldfashionedgunstats ? HUDPOS_GRENADE + HUDPOS_NUMBERSPACING + 25 : HUDPOS_GRENADE + HUDPOS_NUMBERSPACING, 823);
}