From 5bf8755655c081610db47288c15e65f8e7b748ba Mon Sep 17 00:00:00 2001 From: jacksonmj Date: Mon, 31 Jan 2011 16:36:40 +0000 Subject: [PATCH] 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. --- includes/interface.h | 5 +- src/interface.c | 111 ++++++++++++++++++++++++++++++++++++------- src/main.c | 79 +++++++++++++----------------- 3 files changed, 130 insertions(+), 65 deletions(-) diff --git a/includes/interface.h b/includes/interface.h index 870e917e1..2cb64dd2d 100644 --- a/includes/interface.h +++ b/includes/interface.h @@ -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 diff --git a/src/interface.c b/src/interface.c index af607c118..709a3fa4e 100644 --- a/src/interface.c +++ b/src/interface.c @@ -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); - sprintf(num,"%d",i); - if (i>=0 && i=0 && i=0) + { + *element = i; + return 1; + } + for (i=1; i=0 && nx=0 && ny>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 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