diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 963 |
1 files changed, 863 insertions, 100 deletions
@@ -220,7 +220,7 @@ void sdl_seticon(void) //SDL_Surface *icon = SDL_CreateRGBSurfaceFrom(app_icon_w32, 32, 32, 32, 128, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); //SDL_WM_SetIcon(icon, NULL/*app_icon_mask*/); #else - SDL_Surface *icon = SDL_CreateRGBSurfaceFrom(app_icon, 16, 16, 32, 128, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); + SDL_Surface *icon = SDL_CreateRGBSurfaceFrom(app_icon, 16, 16, 32, 64, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); SDL_WM_SetIcon(icon, NULL/*app_icon_mask*/); #endif #endif @@ -306,11 +306,12 @@ void *build_thumb(int *size, int bzip2) return d; } -void *build_save(int *size, int x0, int y0, int w, int h) +void *build_save(int *size, int x0, int y0, int w, int h, unsigned char bmap[YRES/CELL][XRES/CELL], float fvx[YRES/CELL][XRES/CELL], float fvy[YRES/CELL][XRES/CELL], sign signs[MAXSIGNS], void* partsptr) { unsigned char *d=calloc(1,3*(XRES/CELL)*(YRES/CELL)+(XRES*YRES)*11+MAXSIGNS*262), *c; int i,j,x,y,p=0,*m=calloc(XRES*YRES, sizeof(int)); int bx0=x0/CELL, by0=y0/CELL, bw=(w+CELL-1)/CELL, bh=(h+CELL-1)/CELL; + particle *parts = partsptr; // normalize coordinates x0 = bx0*CELL; @@ -349,7 +350,8 @@ void *build_save(int *size, int x0, int y0, int w, int h) y = (int)(parts[i].y+0.5f); if (x>=x0 && x<x0+w && y>=y0 && y<y0+h) { if (!m[(x-x0)+(y-y0)*w] || - parts[m[(x-x0)+(y-y0)*w]-1].type == PT_PHOT) + parts[m[(x-x0)+(y-y0)*w]-1].type == PT_PHOT || + parts[m[(x-x0)+(y-y0)*w]-1].type == PT_NEUT) m[(x-x0)+(y-y0)*w] = i+1; } } @@ -475,12 +477,13 @@ void *build_save(int *size, int x0, int y0, int w, int h) return c; } -int parse_save(void *save, int size, int replace, int x0, int y0) +int parse_save(void *save, int size, int replace, int x0, int y0, unsigned char bmap[YRES/CELL][XRES/CELL], float fvx[YRES/CELL][XRES/CELL], float fvy[YRES/CELL][XRES/CELL], sign signs[MAXSIGNS], void* partsptr, unsigned pmap[YRES][XRES]) { unsigned char *d,*c=save; int q,i,j,k,x,y,p=0,*m=calloc(XRES*YRES, sizeof(int)), ver, pty, ty, legacy_beta=0; int bx0=x0/CELL, by0=y0/CELL, bw, bh, w, h; int fp[NPART], nf=0, new_format = 0, ttv = 0; + particle *parts = partsptr; //New file header uses PSv, replacing fuC. This is to detect if the client uses a new save format for temperatures //This creates a problem for old clients, that display and "corrupt" error instead of a "newer version" error @@ -560,19 +563,7 @@ int parse_save(void *save, int size, int replace, int x0, int y0) gravityMode = 0; airMode = 0; } - memset(bmap, 0, sizeof(bmap)); - memset(emap, 0, sizeof(emap)); - memset(signs, 0, sizeof(signs)); - memset(parts, 0, sizeof(particle)*NPART); - memset(pmap, 0, sizeof(pmap)); - memset(vx, 0, sizeof(vx)); - memset(vy, 0, sizeof(vy)); - memset(pv, 0, sizeof(pv)); - memset(photons, 0, sizeof(photons)); - memset(wireless, 0, sizeof(wireless)); - memset(gol2, 0, sizeof(gol2)); - memset(portal, 0, sizeof(portal)); - death = death2 = ISSPAWN1 = ISSPAWN2 = 0; + clear_sim(); } // make a catalog of free parts @@ -904,13 +895,35 @@ corrupt: if (replace) { legacy_enable = 0; - memset(signs, 0, sizeof(signs)); - memset(parts, 0, sizeof(particle)*NPART); - memset(bmap, 0, sizeof(bmap)); + clear_sim(); } return 1; } +void clear_sim(void) +{ + memset(bmap, 0, sizeof(bmap)); + memset(emap, 0, sizeof(emap)); + memset(signs, 0, sizeof(signs)); + memset(parts, 0, sizeof(particle)*NPART); + memset(pmap, 0, sizeof(pmap)); + memset(pv, 0, sizeof(pv)); + memset(vx, 0, sizeof(vx)); + memset(vy, 0, sizeof(vy)); + memset(fvx, 0, sizeof(fvx)); + memset(fvy, 0, sizeof(fvy)); + memset(photons, 0, sizeof(photons)); + memset(wireless, 0, sizeof(wireless)); + memset(gol2, 0, sizeof(gol2)); + memset(portal, 0, sizeof(portal)); + death = death2 = ISSPAWN1 = ISSPAWN2 = 0; + memset(pers_bg, 0, (XRES+BARSIZE)*YRES*PIXELSIZE); + memset(fire_bg, 0, XRES*YRES*PIXELSIZE); + memset(fire_r, 0, sizeof(fire_r)); + memset(fire_g, 0, sizeof(fire_g)); + memset(fire_b, 0, sizeof(fire_b)); +} + // stamps library stamp stamps[STAMP_MAX];//[STAMP_X*STAMP_Y]; @@ -995,7 +1008,7 @@ void stamp_save(int x, int y, int w, int h) FILE *f; int n; char fn[64], sn[16]; - void *s=build_save(&n, x, y, w, h); + void *s=build_save(&n, x, y, w, h, bmap, fvx, fvy, signs, parts); #ifdef WIN32 _mkdir("stamps"); @@ -1943,6 +1956,7 @@ int main(int argc, char *argv[]) #ifdef INTERNAL int vs = 0; #endif + int wavelength_gfx = 0; int x, y, b = 0, sl=1, sr=0, su=0, c, lb = 0, lx = 0, ly = 0, lm = 0;//, tx, ty; int da = 0, db = 0, it = 2047, mx, my, bsx = 2, bsy = 2; float nfvx, nfvy; @@ -1952,6 +1966,7 @@ int main(int argc, char *argv[]) int save_mode=0, save_x=0, save_y=0, save_w=0, save_h=0, copy_mode=0; SDL_AudioSpec fmt; int username_flash = 0, username_flash_t = 1; + pers_bg = calloc((XRES+BARSIZE)*YRES, PIXELSIZE); GSPEED = 1; #ifdef PYCONSOLE PyObject *pname,*pmodule,*pfunc,*pvalue,*pargs,*pstep,*pkey; @@ -2044,8 +2059,7 @@ int main(int argc, char *argv[]) parts[NPART-1].life = -1; pfree = 0; fire_bg=calloc(XRES*YRES, PIXELSIZE); - pers_bg=calloc((XRES+BARSIZE)*YRES, PIXELSIZE); - memset(signs, 0, sizeof(signs)); + clear_sim(); //fbi_img = render_packed_rgb(fbi, FBI_W, FBI_H, FBI_CMP); @@ -2676,6 +2690,365 @@ int main(int argc, char *argv[]) } counterthing = 0; } + if ((sdl_key=='s' && (sdl_mod & (KMOD_CTRL))) || (sdl_key=='s' && !isplayer2)) + { + if (it > 50) + it = 50; + save_mode = 1; + } + if (sdl_key=='1') + { + set_cmode(CM_VEL); + } + if (sdl_key=='2') + { + set_cmode(CM_PRESS); + } + if (sdl_key=='3') + { + set_cmode(CM_PERS); + } + if (sdl_key=='4') + { + set_cmode(CM_FIRE); + } + if (sdl_key=='5') + { + set_cmode(CM_BLOB); + } + if (sdl_key=='6') + { + set_cmode(CM_HEAT); + } + if (sdl_key=='7') + { + set_cmode(CM_FANCY); + } + if (sdl_key=='8') + { + set_cmode(CM_NOTHING); + } + if (sdl_key=='9') + { + set_cmode(CM_GRAD); + } + if (sdl_key=='0') + { + set_cmode(CM_CRACK); + } + if (sdl_key=='1'&& (sdl_mod & (KMOD_SHIFT)) && DEBUG_MODE) + { + if (counterthing+1>=vs) + { + dump_frame(vid_buf, XRES, YRES, XRES+BARSIZE); + counterthing = 0; + } + counterthing = (counterthing+1)%3; + } +#endif + + if (sdl_wheel) + { + if (sdl_zoom_trig==1) + { + ZSIZE += sdl_wheel; + if (ZSIZE>60) + ZSIZE = 60; + if (ZSIZE<2) + ZSIZE = 2; + ZFACTOR = 256/ZSIZE; + sdl_wheel = 0; + } + else + { + if (!(sdl_mod & (KMOD_SHIFT|KMOD_CTRL))) + { + bsx += sdl_wheel; + bsy += sdl_wheel; + } + else if (sdl_mod & (KMOD_SHIFT) && !(sdl_mod & (KMOD_CTRL))) + { + bsx += sdl_wheel; + } + else if (sdl_mod & (KMOD_CTRL) && !(sdl_mod & (KMOD_SHIFT))) + { + bsy += sdl_wheel; + } + if (bsx>1180) + bsx = 1180; + if (bsx<0) + bsx = 0; + if (bsy>1180) + bsy = 1180; + if (bsy<0) + bsy = 0; + sdl_wheel = 0; + /*if(su >= PT_NUM) { + if(sl < PT_NUM) + su = sl; + if(sr < PT_NUM) + su = sr; + }*/ + } + } + if (sdl_key==SDLK_RIGHTBRACKET) { + if (sdl_zoom_trig==1) + { + ZSIZE += 1; + if (ZSIZE>60) + ZSIZE = 60; + if (ZSIZE<2) + ZSIZE = 2; + ZFACTOR = 256/ZSIZE; + } + else + { + if (sdl_mod & (KMOD_LALT|KMOD_RALT) && !(sdl_mod & (KMOD_SHIFT|KMOD_CTRL))) + { + bsx += 1; + bsy += 1; + } + else if (sdl_mod & (KMOD_SHIFT) && !(sdl_mod & (KMOD_CTRL))) + { + bsx += 1; + } + else if (sdl_mod & (KMOD_CTRL) && !(sdl_mod & (KMOD_SHIFT))) + { + bsy += 1; + } + else + { + bsx += ceil((bsx/5)+0.5f); + bsy += ceil((bsy/5)+0.5f); + } + if (bsx>1180) + bsx = 1180; + if (bsy>1180) + bsy = 1180; + if (bsx<0) + bsx = 0; + if (bsy<0) + bsy = 0; + } + } + if ((sdl_key=='d'&&(sdl_mod & (KMOD_CTRL))) || (sdl_key=='d' && !isplayer2)) + DEBUG_MODE = !DEBUG_MODE; + if (sdl_key=='i') + { + int nx, ny; + for (nx = 0; nx<XRES/CELL; nx++) + for (ny = 0; ny<YRES/CELL; ny++) + { + pv[ny][nx] = -pv[ny][nx]; + vx[ny][nx] = -vx[ny][nx]; + vy[ny][nx] = -vy[ny][nx]; + } + } + if ((sdl_mod & (KMOD_RCTRL) )&&( sdl_mod & (KMOD_RALT))) + active_menu = 11; + if (sdl_key==SDLK_INSERT)// || sdl_key==SDLK_BACKQUOTE) + REPLACE_MODE = !REPLACE_MODE; + if (sdl_key==SDLK_BACKQUOTE) + { + console_mode = !console_mode; + //hud_enable = !console_mode; + } + if (sdl_key=='g') + { + if (sdl_mod & (KMOD_SHIFT)) + GRID_MODE = (GRID_MODE+9)%10; + else + GRID_MODE = (GRID_MODE+1)%10; + } + if (sdl_key=='=') + { + int nx, ny; + if(sdl_mod & (KMOD_CTRL)) + { + for(i=0; i<NPART; i++) + if(parts[i].type==PT_SPRK) + { + parts[i].type = parts[i].ctype; + parts[i].life = 0; + } + } + else + { + for (nx = 0; nx<XRES/CELL; nx++) + for (ny = 0; ny<YRES/CELL; ny++) + { + pv[ny][nx] = 0; + vx[ny][nx] = 0; + vy[ny][nx] = 0; + } + } + } + + if (sdl_key=='w' && (!isplayer2 || (sdl_mod & (KMOD_SHIFT)))) //Gravity, by Moach + { + ++gravityMode; // cycle gravity mode + itc = 51; + + switch (gravityMode) + { + default: + gravityMode = 0; + case 0: + strcpy(itc_msg, "Gravity: Vertical"); + break; + case 1: + strcpy(itc_msg, "Gravity: Off"); + break; + case 2: + strcpy(itc_msg, "Gravity: Radial"); + break; + + } + } + if (sdl_key=='y') + { + ++airMode; + itc = 52; + + switch (airMode) + { + default: + airMode = 0; + case 0: + strcpy(itc_msg, "Air: On"); + break; + case 1: + strcpy(itc_msg, "Air: Pressure Off"); + break; + case 2: + strcpy(itc_msg, "Air: Velocity Off"); + break; + case 3: + strcpy(itc_msg, "Air: Off"); + break; + case 4: + strcpy(itc_msg, "Air: No Update"); + break; + } + } + + if (sdl_key=='t') + VINE_MODE = !VINE_MODE; + if (sdl_key==SDLK_SPACE) + sys_pause = !sys_pause; + if (sdl_key=='h') + hud_enable = !hud_enable; + if (sdl_key=='p') + dump_frame(vid_buf, XRES, YRES, XRES+BARSIZE); + if (sdl_key=='v'&&(sdl_mod & (KMOD_LCTRL|KMOD_RCTRL))) + { + if (clipboard_ready==1) + { + load_data = malloc(clipboard_length); + memcpy(load_data, clipboard_data, clipboard_length); + load_size = clipboard_length; + if (load_data) + { + load_img = prerender_save(load_data, load_size, &load_w, &load_h); + if (load_img) + load_mode = 1; + else + free(load_data); + } + } + } + if (load_mode==1) + { + matrix2d transform = m2d_identity; + vector2d translate = v2d_zero; + void *ndata; + int doTransform = 0; + if (sdl_key=='r'&&(sdl_mod & (KMOD_CTRL))&&(sdl_mod & (KMOD_SHIFT))) + { + transform = m2d_new(-1,0,0,1); //horizontal invert + doTransform = 1; + } + else if (sdl_key=='r'&&(sdl_mod & (KMOD_LCTRL|KMOD_RCTRL))) + { + transform = m2d_new(0,1,-1,0); //rotate anticlockwise 90 degrees + doTransform = 1; + } + else if (sdl_mod & (KMOD_CTRL)) + { + doTransform = 1; + if (sdl_key==SDLK_LEFT) translate = v2d_new(-1,0); + else if (sdl_key==SDLK_RIGHT) translate = v2d_new(1,0); + else if (sdl_key==SDLK_UP) translate = v2d_new(0,-1); + else if (sdl_key==SDLK_DOWN) translate = v2d_new(0,1); + else doTransform = 0; + } + if (doTransform) + { + ndata = transform_save(load_data, &load_size, transform, translate); + if (ndata!=load_data) free(load_data); + free(load_img); + load_data = ndata; + load_img = prerender_save(load_data, load_size, &load_w, &load_h); + } + } + if (sdl_key=='r'&&!(sdl_mod & (KMOD_CTRL|KMOD_SHIFT))) + GENERATION = 0; + if (sdl_key=='x'&&(sdl_mod & (KMOD_LCTRL|KMOD_RCTRL))) + { + save_mode = 1; + copy_mode = 2; + } + if (sdl_key=='c'&&(sdl_mod & (KMOD_LCTRL|KMOD_RCTRL))) + { + save_mode = 1; + copy_mode = 1; + } + else if (sdl_key=='c') + { + set_cmode((cmode+1) % CM_COUNT); + if (it > 50) + it = 50; + } + if (sdl_key=='z'&&(sdl_mod & (KMOD_LCTRL|KMOD_RCTRL))) // Undo + { + int cbx, cby, cbi; + + for (cbi=0; cbi<NPART; cbi++) + parts[cbi] = cb_parts[cbi]; + + for (cby = 0; cby<YRES; cby++) + for (cbx = 0; cbx<XRES; cbx++) + pmap[cby][cbx] = cb_pmap[cby][cbx]; + + for (cby = 0; cby<(YRES/CELL); cby++) + for (cbx = 0; cbx<(XRES/CELL); cbx++) + { + vx[cby][cbx] = cb_vx[cby][cbx]; + vy[cby][cbx] = cb_vy[cby][cbx]; + pv[cby][cbx] = cb_pv[cby][cbx]; + bmap[cby][cbx] = cb_bmap[cby][cbx]; + emap[cby][cbx] = cb_emap[cby][cbx]; + } + } +#ifdef INTERNAL + int counterthing; + if (sdl_key=='v'&&!(sdl_mod & (KMOD_LCTRL|KMOD_RCTRL))) + { + if (sdl_mod & (KMOD_SHIFT)) { + if (vs>=1) + vs = 0; + else + vs = 3;//every other frame + } + else + { + if (vs>=1) + vs = 0; + else + vs = 1; + } + counterthing = 0; + } if (vs) { if (counterthing+1>=vs) @@ -2769,8 +3142,13 @@ int main(int argc, char *argv[]) if (DEBUG_MODE) { int tctype = parts[cr>>8].ctype; - if (tctype>=PT_NUM) + if (tctype>=PT_NUM || (cr&0xFF)==PT_PHOT) tctype = 0; + if ((cr&0xFF)==PT_PIPE) + { + if (parts[cr>>8].tmp<PT_NUM) tctype = parts[cr>>8].tmp; + else tctype = 0; + } sprintf(heattext, "%s (%s), Pressure: %3.2f, Temp: %4.2f C, Life: %d", ptypes[cr&0xFF].name, ptypes[tctype].name, pv[(y/sdl_scale)/CELL][(x/sdl_scale)/CELL], parts[cr>>8].temp-273.15f, parts[cr>>8].life); sprintf(coordtext, "#%d, X:%d Y:%d", cr>>8, x/sdl_scale, y/sdl_scale); } else { @@ -2780,6 +3158,7 @@ int main(int argc, char *argv[]) sprintf(heattext, "%s, Pressure: %3.2f, Temp: %4.2f C", ptypes[cr&0xFF].name, pv[(y/sdl_scale)/CELL][(x/sdl_scale)/CELL], parts[cr>>8].temp-273.15f); #endif } + if ((cr&0xFF)==PT_PHOT) wavelength_gfx = parts[cr>>8].ctype; } else { @@ -2953,7 +3332,7 @@ int main(int argc, char *argv[]) if (load_y<0) load_y=0; if (bq==1 && !b) { - parse_save(load_data, load_size, 0, load_x, load_y); + parse_save(load_data, load_size, 0, load_x, load_y, bmap, fvx, fvy, signs, parts, pmap); free(load_data); free(load_img); load_mode = 0; @@ -2995,35 +3374,19 @@ int main(int argc, char *argv[]) { if (copy_mode==1) { - clipboard_data=build_save(&clipboard_length, save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL); + clipboard_data=build_save(&clipboard_length, save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL, bmap, fvx, fvy, signs, parts); clipboard_ready = 1; save_mode = 0; copy_mode = 0; } else if (copy_mode==2) { - clipboard_data=build_save(&clipboard_length, save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL); + clipboard_data=build_save(&clipboard_length, save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL, bmap, fvx, fvy, signs, parts); clipboard_ready = 1; save_mode = 0; copy_mode = 0; clear_area(save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL); } - else if (copy_mode==3)//rotation - { - if (save_h>save_w) - save_w = save_h; - rotate_area(save_x*CELL, save_y*CELL, save_w*CELL, save_w*CELL,0);//just do squares for now - save_mode = 0; - copy_mode = 0; - } - else if (copy_mode==4)//invertion - { - if (save_h>save_w) - save_w = save_h; - rotate_area(save_x*CELL, save_y*CELL, save_w*CELL, save_w*CELL,1);//just do squares for now - save_mode = 0; - copy_mode = 0; - } else { stamp_save(save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL); @@ -3081,19 +3444,7 @@ int main(int argc, char *argv[]) } if (x>=(XRES+BARSIZE-(510-367)) && x<=(XRES+BARSIZE-(510-383)) && !bq) { - memset(signs, 0, sizeof(signs)); - memset(pv, 0, sizeof(pv)); - memset(vx, 0, sizeof(vx)); - memset(vy, 0, sizeof(vy)); - memset(fvx, 0, sizeof(fvx)); - memset(fvy, 0, sizeof(fvy)); - memset(bmap, 0, sizeof(bmap)); - memset(emap, 0, sizeof(emap)); - memset(parts, 0, sizeof(particle)*NPART); - memset(photons, 0, sizeof(photons)); - memset(wireless, 0, sizeof(wireless)); - memset(gol2, 0, sizeof(gol2)); - memset(portal, 0, sizeof(portal)); + clear_sim(); for (i=0; i<NPART-1; i++) parts[i].life = i+1; parts[NPART-1].life = -1; @@ -3110,6 +3461,7 @@ int main(int argc, char *argv[]) svf_description[0] = 0; gravityMode = 0; airMode = 0; + death = death2 = 0; isplayer2 = 0; isplayer = 0; @@ -3155,7 +3507,7 @@ int main(int argc, char *argv[]) } if (x>=19 && x<=35 && svf_last && svf_open && !bq) { //int tpval = sys_pause; - parse_save(svf_last, svf_lsize, 1, 0, 0); + parse_save(svf_last, svf_lsize, 1, 0, 0, bmap, fvx, fvy, signs, parts, pmap); //sys_pause = tpval; } if (x>=(XRES+BARSIZE-(510-476)) && x<=(XRES+BARSIZE-(510-491)) && !bq) @@ -3252,7 +3604,9 @@ int main(int argc, char *argv[]) } } else + { create_line(lx, ly, x, y, bsx, bsy, c); + } lx = x; ly = y; } @@ -3364,14 +3718,7 @@ int main(int argc, char *argv[]) if (save_mode) { - if (copy_mode==3||copy_mode==4)//special drawing for rotate, can remove once it can do rectangles - { - if (save_h>save_w) - save_w = save_h; - xor_rect(vid_buf, save_x*CELL, save_y*CELL, save_w*CELL, save_w*CELL); - } - else - xor_rect(vid_buf, save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL); + xor_rect(vid_buf, save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL); da = 51; db = 269; } @@ -3526,6 +3873,8 @@ int main(int argc, char *argv[]) fillrect(vid_buf, XRES-20-textwidth(coordtext), 280, textwidth(coordtext)+8, 13, 0, 0, 0, 140); drawtext(vid_buf, XRES-16-textwidth(coordtext), 282, coordtext, 255, 255, 255, 200); } + if (wavelength_gfx) + draw_wavelengths(vid_buf,XRES-20-textwidth(heattext),265,2,wavelength_gfx); } else { @@ -3536,6 +3885,8 @@ int main(int argc, char *argv[]) fillrect(vid_buf, 12, 280, textwidth(coordtext)+8, 13, 0, 0, 0, 140); drawtext(vid_buf, 16, 282, coordtext, 255, 255, 255, 200); } + if (wavelength_gfx) + draw_wavelengths(vid_buf,12,265,2,wavelength_gfx); } } else @@ -3547,7 +3898,10 @@ int main(int argc, char *argv[]) fillrect(vid_buf, XRES-20-textwidth(coordtext), 26, textwidth(coordtext)+8, 11, 0, 0, 0, 140); drawtext(vid_buf, XRES-16-textwidth(coordtext), 27, coordtext, 255, 255, 255, 200); } + if (wavelength_gfx) + draw_wavelengths(vid_buf,XRES-20-textwidth(heattext),11,2,wavelength_gfx); } + wavelength_gfx = 0; fillrect(vid_buf, 12, 12, textwidth(uitext)+8, 15, 0, 0, 0, 140); drawtext(vid_buf, 16, 16, uitext, 32, 216, 255, 200); @@ -3555,14 +3909,13 @@ int main(int argc, char *argv[]) if(console_mode) { - #ifdef PYCONSOLE char *console; //char error[255] = "error!"; sys_pause = 1; - console = console_ui(vid_buf,console_error,console_more); + console = console_ui(vid_buf,console_error); console = mystrdup(console); strcpy(console_error,""); - if(process_command(vid_buf,console,&console_error,pfunc)==-1) + if(process_command(vid_buf,console,console_error)==-1) { free(console); break; @@ -3570,26 +3923,7 @@ int main(int argc, char *argv[]) free(console); if(!console_mode) hud_enable = 1; - #else - console_mode=0; - #endif } - - //execute python step hook - #ifdef PYCONSOLE - if(pstep!=NULL) - { - pargs=Py_BuildValue("()"); - pvalue = PyObject_CallObject(pstep, pargs); - Py_DECREF(pargs); - pargs=NULL; - if(pvalue==NULL) - strcpy(console_error,"failed to execute step code."); - //Py_DECREF(pvalue); - //puts("a"); - pvalue=NULL; - } - #endif sdl_blit(0, 0, XRES+BARSIZE, YRES+MENUSIZE, vid_buf, XRES+BARSIZE); //Setting an element for the stick man @@ -3632,20 +3966,449 @@ int process_command(pixel *vid_buf,char *console,char *console_error,PyObject *p { return -1; } + else if(strcmp(console2, "file")==0) + { + if(file_script){ + FILE *f=fopen(console3, "r"); + if(f) + { + char fileread[5000];//TODO: make this change with file size + char pch[5000]; + char tokens[10]; + int tokensize; + nx = 0; + ny = 0; + j = 0; + m = 0; + if(console4) + console_parse_coords(console4, &nx , &ny, console_error); + memset(pch,0,sizeof(pch)); + memset(fileread,0,sizeof(fileread)); + fread(fileread,1,5000,f); + for(i=0; i<strlen(fileread); i++) + { + if(fileread[i] != '\n') + { + pch[i-j] = fileread[i]; + if(fileread[i] != ' ') + tokens[i-m] = fileread[i]; + } + if(fileread[i] == ' ' || fileread[i] == '\n') + { + if(sregexp(tokens,"^x.[0-9],y.[0-9]")==0)//TODO: fix regex matching to work with x,y ect, right now it has to have a +0 or -0 + { + char temp[5]; + int starty = 0; + tokensize = strlen(tokens); + x = 0; + y = 0; + sscanf(tokens,"x%d,y%d",&x,&y); + sscanf(tokens,"%9s,%9s",xcoord,ycoord); + x += nx; + y += ny; + sprintf(xcoord,"%d",x); + sprintf(ycoord,"%d",y); + for(k = 0; k<strlen(xcoord);k++)//rewrite pch with numbers + { + pch[i-j-tokensize+k] = xcoord[k]; + starty = k+1; + } + pch[i-j-tokensize+starty] = ','; + starty++; + for(k=0;k<strlen(ycoord);k++) + { + pch[i-j-tokensize+starty+k] = ycoord[k]; + + } + pch[i-j-tokensize +strlen(xcoord) +1 +strlen(ycoord)] = ' '; + j = j -tokensize +strlen(xcoord) +1 +strlen(ycoord); + } + memset(tokens,0,sizeof(tokens)); + m = i+1; + } + if(fileread[i] == '\n') + { + + if(do_next) + { + if(strcmp(pch,"else")==0) + do_next = 0; + else + do_next = process_command(vid_buf, pch, console_error); + } + else if(strcmp(pch,"endif")==0 || strcmp(pch,"else")==0) + do_next = 1; + memset(pch,0,sizeof(pch)); + j = i+1; + } + } + //sprintf(console_error, "%s exists", console3); + fclose(f); + } + else + { + sprintf(console_error, "%s does not exist", console3); + } + } + else + { + sprintf(console_error, "Scripts are not enabled"); + } + + } + else if(strcmp(console2, "sound")==0) + { + if (sound_enable) play_sound(console3); + else strcpy(console_error, "Audio device not available - cannot play sounds"); + } + else if(strcmp(console2, "load")==0) + { + j = atoi(console3); + if(j) + { + open_ui(vid_buf, console3, NULL); + console_mode = 0; + } + } + else if(strcmp(console2, "if")==0) + { + if(strcmp(console3, "type")==0)//TODO: add more than just type, and be able to check greater/less than + { + if (console_parse_partref(console4, &i, console_error) + && console_parse_type(console5, &j, console_error)) + { + if(parts[i].type==j) + return 1; + else + return 0; + } + else + return 0; + } + } + else if (strcmp(console2, "create")==0) + { + 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) + { + if (console_parse_partref(console3, &i, console_error)) + kill_part(i); + } + else if(strcmp(console2, "reset")==0) + { + if(strcmp(console3, "pressure")==0) + { + for (nx = 0; nx<XRES/CELL; nx++) + for (ny = 0; ny<YRES/CELL; ny++) + { + pv[ny][nx] = 0; + } + } + else if(strcmp(console3, "velocity")==0) + { + for (nx = 0; nx<XRES/CELL; nx++) + for (ny = 0; ny<YRES/CELL; ny++) + { + vx[ny][nx] = 0; + vy[ny][nx] = 0; + } + } + else if(strcmp(console3, "sparks")==0) + { + for(i=0; i<NPART; i++) + { + if(parts[i].type==PT_SPRK) + { + parts[i].type = parts[i].ctype; + parts[i].life = 4; + } + } + } + else if(strcmp(console3, "temp")==0) + { + for(i=0; i<NPART; i++) + { + if(parts[i].type) + { + parts[i].temp = ptypes[parts[i].type].heat; + } + } + } + } + else if(strcmp(console2, "set")==0) + { + if(strcmp(console3, "life")==0) + { + if(strcmp(console4, "all")==0) + { + j = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].life = j; + } + } + else if (console_parse_type(console4, &j, console_error)) + { + k = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].life = k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error)) + { + j = atoi(console5); + parts[i].life = j; + } + } + } + if(strcmp(console3, "type")==0) + { + if(strcmp(console4, "all")==0) + { + if (console_parse_type(console5, &j, console_error)) + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].type = j; + } + } + else if (console_parse_type(console4, &j, console_error) + && console_parse_type(console5, &k, console_error)) + { + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].type = k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error) + && console_parse_type(console5, &j, console_error)) + { + parts[i].type = j; + } + } + } + if(strcmp(console3, "temp")==0) + { + if(strcmp(console4, "all")==0) + { + j = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].temp = j; + } + } + else if (console_parse_type(console4, &j, console_error)) + { + k = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].temp= k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error)) + { + j = atoi(console5); + parts[i].temp = j; + } + } + } + if(strcmp(console3, "tmp")==0) + { + if(strcmp(console4, "all")==0) + { + j = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].tmp = j; + } + } + else if (console_parse_type(console4, &j, console_error)) + { + k = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].tmp = k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error)) + { + j = atoi(console5); + parts[i].tmp = j; + } + } + } + if(strcmp(console3, "x")==0) + { + if(strcmp(console4, "all")==0) + { + j = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].x = j; + } + } + else if (console_parse_type(console4, &j, console_error)) + { + k = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].x = k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error)) + { + j = atoi(console5); + parts[i].x = j; + } + } + } + if(strcmp(console3, "y")==0) + { + if(strcmp(console4, "all")==0) + { + j = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].y = j; + } + } + else if (console_parse_type(console4, &j, console_error)) + { + k = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].y = k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error)) + { + j = atoi(console5); + parts[i].y = j; + } + } + } + if(strcmp(console3, "ctype")==0) + { + if(strcmp(console4, "all")==0) + { + if (console_parse_type(console5, &j, console_error)) + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].ctype = j; + } + } + else if (console_parse_type(console4, &j, console_error) + && console_parse_type(console5, &k, console_error)) + { + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].ctype = k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error) + && console_parse_type(console5, &j, console_error)) + { + parts[i].ctype = j; + } + } + } + if(strcmp(console3, "vx")==0) + { + if(strcmp(console4, "all")==0) + { + j = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].vx = j; + } + } + else if (console_parse_type(console4, &j, console_error)) + { + k = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].vx = k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error)) + { + j = atoi(console5); + parts[i].vx = j; + } + } + } + if(strcmp(console3, "vy")==0) + { + if(strcmp(console4, "all")==0) + { + j = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type) + parts[i].vy = j; + } + } + else if (console_parse_type(console4, &j, console_error)) + { + k = atoi(console5); + for(i=0; i<NPART; i++) + { + if(parts[i].type == j) + parts[i].vy = k; + } + } + else + { + if (console_parse_partref(console4, &i, console_error)) + { + j = atoi(console5); + parts[i].vy = j; + } + } + } + } else - { - // sprintf(console_error, "Invalid Command", console2); - //handle them command - pargs=Py_BuildValue("(s)",console); - pvalue = PyObject_CallObject(pfunc, pargs); - Py_DECREF(pargs); - pargs=NULL; - if(pvalue==NULL) - strcpy(console_error,"failed to execute code."); - //Py_DECREF(pvalue); - //puts("a"); - pvalue=NULL; - } + strcpy(console_error, "Invalid Command"); } return 1; } |
