Console improvements

Can use x,y coordinates to specify which particle.
ctype now also accepts element names.
More sanity checking of input.
delete command added.
This commit is contained in:
jacksonmj 2011-01-31 16:36:40 +00:00
parent 363a3e38f1
commit 5bf8755655
3 changed files with 130 additions and 65 deletions

View File

@ -227,8 +227,9 @@ void open_link(char *uri);
int report_ui(pixel *vid_buf, char *save_id);
char *console_ui(pixel *vid_buf, char error[255]);
int console_get_type(char *element);
int console_parse_coords(char *txt, int *x, int *y, char *err);
int console_parse_type(char *txt, int *element, char *err);
int console_parse_partref(char *txt, int *which, char *err);
#endif

View File

@ -3847,7 +3847,7 @@ char *console_ui(pixel *vid_buf,char error[255]) { //TODO: error messages, show
"You can now use particle names (ex. set type all deut)\n"
"Reset works with pressure, velocity, sparks, temp (ex. 'reset pressure')\n"
"To load a save use load saveID (ex. load 1337)\n"
"Create particles with 'create deut x y' where x and y are the coords\n"
"Create particles with 'create deut x,y' where x and y are the coords\n"
"Run scripts from file 'file filename'"
,255, 187, 187, 255);
@ -3924,26 +3924,101 @@ char *console_ui(pixel *vid_buf,char error[255]) { //TODO: error messages, show
}
}
}
}
return NULL;
}
}
int console_get_type(char *element)
int console_parse_type(char *txt, int *element, char *err)
{
int i;
int i = atoi(txt);
char num[4];
i = atoi(element);
if (i>=0 && i<PT_NUM)
{
sprintf(num,"%d",i);
if (i>=0 && i<PT_NUM && strcmp(element,num)==0)
return i;
if (strcasecmp(element,"C4")==0) return PT_PLEX;
if (strcasecmp(element,"C5")==0) return PT_C5;
if (strcasecmp(element,"NONE")==0) return PT_NONE;
for (i=0; i<PT_NUM; i++) {
if (strcasecmp(element,ptypes[i].name)==0)
return i;
if (strcmp(txt,num)==0)
{
*element = i;
return 1;
}
return -1;
}
i = -1;
// alternative names for some elements
if (strcasecmp(txt,"C4")==0) i = PT_PLEX;
else if (strcasecmp(txt,"C5")==0) i = PT_C5;
else if (strcasecmp(txt,"NONE")==0) i = PT_NONE;
if (i>=0)
{
*element = i;
return 1;
}
for (i=1; i<PT_NUM; i++) {
if (strcasecmp(txt,ptypes[i].name)==0)
{
*element = i;
return 1;
}
}
strcpy(err, "Particle type not recognised");
return 0;
}
int console_parse_coords(char *txt, int *x, int *y, char *err)
{
// TODO: use regex?
char *coordtxt;
char num[10] = "";
int nx = -1, ny = -1;
txt = mystrdup(txt);
coordtxt = strtok(txt, ",");
if (coordtxt) nx = atoi(coordtxt);
if (nx>=0 && nx<XRES) sprintf(num,"%d",nx);
if (!coordtxt || strcmp(coordtxt, num)!=0)
{
strcpy(err,"Invalid coordinates");
free(txt);
return 0;
}
strcpy(num,"");
coordtxt = strtok(NULL, ",");
if (coordtxt) ny = atoi(coordtxt);
if (ny>=0 && ny<YRES) sprintf(num,"%d",ny);
if (!coordtxt || strcmp(coordtxt, num)!=0)
{
strcpy(err,"Invalid coordinates");
free(txt);
return 0;
}
*x = nx;
*y = ny;
free(txt);
return 1;
}
int console_parse_partref(char *txt, int *which, char *err)
{
// TODO: use regex?
int i = -1, nx, ny;
if (console_parse_coords(txt, &nx, &ny, err))
{
i = pmap[ny][nx];
if (!i || (i>>8)>=NPART)
i = -1;
else
i = i>>8;
}
else if (txt)
{
char *num = (char*)malloc(strlen(txt)+3);
strcpy(err,""); // suppress error message from failed coordinate parsing
i = atoi(txt);
sprintf(num,"%d",i);
if (!txt || strcmp(txt,num)!=0)
i = -1;
free(num);
}
if (i>=0 && i<NPART && parts[i].type)
{
*which = i;
return 1;
}
strcpy(err,"Particle does not exist");
return 0;
}

View File

@ -2503,7 +2503,7 @@ int main(int argc, char *argv[])
http_done();
return 0;
}
int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO: delete with coords, have 'set' work with coords as well
int process_command(pixel *vid_buf,char *console,char *console_error) {
int nx,ny,i,j;
char *console2;
@ -2556,17 +2556,21 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
console_mode = 0;
}
}
else if(strcmp(console2, "create")==0 && console3 && console4 && console5)
else if (strcmp(console2, "create")==0 && console3 && console4)
{
j = console_get_type(console3);
if (j<0)
sprintf(console_error, "Particle type not recognised", console2);
nx = atoi(console4);
ny = atoi(console5);
if(ny < 0 || nx < 0 || ny > YRES || nx > XRES)
sprintf(console_error, "Invalid Coordinates", console2);
else
create_part(-1,nx,ny,j);
if (console_parse_type(console3, &j, console_error)
&& console_parse_coords(console4, &nx, &ny, console_error))
{
if (!j)
strcpy(console_error, "Cannot create particle with type NONE");
else if (create_part(-1,nx,ny,j)<0)
strcpy(console_error, "Could not create particle");
}
}
else if ((strcmp(console2, "delete")==0 || strcmp(console2, "kill")==0) && console3)
{
if (console_parse_partref(console3, &i, console_error))
kill_part(i);
}
else if(strcmp(console2, "reset")==0 && console3)
{
@ -2624,8 +2628,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error))
{
j = atoi(console5);
parts[i].life = j;
@ -2636,10 +2639,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
{
if(strcmp(console4, "all")==0)
{
j = console_get_type(console5);
if (j<0)
sprintf(console_error, "Particle type not recognised", console2);
else
if (console_parse_type(console5, &j, console_error))
for(i=0; i<NPART; i++)
{
if(parts[i].type)
@ -2648,13 +2648,9 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error)
&& console_parse_type(console5, &j, console_error))
{
j = console_get_type(console5);
if (j<0)
sprintf(console_error, "Particle type not recognised", console2);
else
parts[i].type = j;
}
}
@ -2672,8 +2668,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error))
{
j = atoi(console5);
parts[i].temp = j;
@ -2693,8 +2688,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error))
{
j = atoi(console5);
parts[i].tmp = j;
@ -2714,8 +2708,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error))
{
j = atoi(console5);
parts[i].x = j;
@ -2735,8 +2728,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error))
{
j = atoi(console5);
parts[i].y = j;
@ -2747,7 +2739,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
{
if(strcmp(console4, "all")==0)
{
j = atoi(console5);
if (console_parse_type(console5, &j, console_error))
for(i=0; i<NPART; i++)
{
if(parts[i].type)
@ -2756,10 +2748,9 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error)
&& console_parse_type(console5, &j, console_error))
{
j = atoi(console5);
parts[i].ctype = j;
}
}
@ -2777,8 +2768,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error))
{
j = atoi(console5);
parts[i].vx = j;
@ -2798,8 +2788,7 @@ int process_command(pixel *vid_buf,char *console,char *console_error) { //TODO:
}
else
{
i = atoi(console4);
if(parts[i].type)
if (console_parse_partref(console4, &i, console_error))
{
j = atoi(console5);
parts[i].vy = j;