summaryrefslogtreecommitdiff
path: root/src/interface.c
diff options
context:
space:
mode:
authorjacksonmj <jacksonmj@jacksonmj.none>2011-01-31 16:36:40 (GMT)
committer jacksonmj <jacksonmj@jacksonmj.none>2011-01-31 16:36:40 (GMT)
commit5bf8755655c081610db47288c15e65f8e7b748ba (patch)
treee7f5904d39bce90bca6b3d7ea542a52421c9c88d /src/interface.c
parent363a3e38f1c5f7be91a060bbc33595d1c3ee9de4 (diff)
downloadpowder-5bf8755655c081610db47288c15e65f8e7b748ba.zip
powder-5bf8755655c081610db47288c15e65f8e7b748ba.tar.gz
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.
Diffstat (limited to 'src/interface.c')
-rw-r--r--src/interface.c113
1 files changed, 94 insertions, 19 deletions
diff --git a/src/interface.c b/src/interface.c
index af607c1..709a3fa 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<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;
- }
- return -1;
-} \ No newline at end of file
+ if (i>=0 && i<PT_NUM)
+ {
+ sprintf(num,"%d",i);
+ if (strcmp(txt,num)==0)
+ {
+ *element = i;
+ 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;
+}