MCPcopy Index your code
hub / github.com/assaultcube/AC / modifyvar

Function modifyvar

source/src/command.cpp:1338–1373  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1336#endif
1337
1338void modifyvar(const char *name, int arg, char op)
1339{
1340 ident *id = idents->access(name);
1341 if(!id) return;
1342 if(identaccessdenied(id))
1343 {
1344 conoutf("not allowed in this execution context: %s", id->name);
1345 scripterr();
1346 return;
1347 }
1348 if((id->type == ID_VAR && id->minval > id->maxval) || (id->type == ID_FVAR && id->minvalf > id->maxvalf)) { conoutf("variable %s is read-only", id->name); return; }
1349 int val = 0;
1350 switch(id->type)
1351 {
1352 case ID_VAR: val = *id->storage.i; break;
1353 case ID_FVAR: val = int(*id->storage.f); break;
1354 case ID_SVAR: { { if(id->getfun) ((void (__cdecl *)())id->getfun)(); } val = ATOI(*id->storage.s); break; }
1355 case ID_ALIAS: val = ATOI(id->action); break;
1356 default: return;
1357 }
1358 switch(op)
1359 {
1360 case '+': val += arg; break;
1361 case '-': val -= arg; break;
1362 case '*': val *= arg; break;
1363 case '/': val = arg ? val / arg : 0; break;
1364 }
1365 switch(id->type)
1366 {
1367 case ID_VAR: *id->storage.i = clamp(val, id->minval, id->maxval); break;
1368 case ID_FVAR: *id->storage.f = clamp((float)val, id->minvalf, id->maxvalf); break;
1369 case ID_SVAR: { string str; itoa(str, val); *id->storage.s = exchangestr(*id->storage.s, str); break; }
1370 case ID_ALIAS: { string str; itoa(str, val); alias(name, str); return; }
1371 }
1372 if(id->fun) ((void (__cdecl *)())id->fun)();
1373}
1374
1375void addeq(char *name, int *arg) { modifyvar(name, *arg, '+'); } COMMANDN(+=, addeq, "si");
1376void subeq(char *name, int *arg) { modifyvar(name, *arg, '-'); } COMMANDN(-=, subeq, "si");

Callers 4

addeqFunction · 0.85
subeqFunction · 0.85
muleqFunction · 0.85
diveqFunction · 0.85

Calls 6

identaccessdeniedFunction · 0.85
scripterrFunction · 0.85
exchangestrFunction · 0.85
aliasFunction · 0.85
accessMethod · 0.80
conoutfFunction · 0.70

Tested by

no test coverage detected