/** * Powder Toy - Main source * * Copyright (c) 2008 - 2010 Stanislaw Skowronek. * Copyright (c) 2010 Simon Robertshaw * Copyright (c) 2010 Skresanov Savely * Copyright (c) 2010 Bryan Hoyle * Copyright (c) 2010 Nathan Cousins * Copyright (c) 2010 cracker64 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */ #include #ifdef PYCONSOLE #include "Python.h" #ifndef PYEXT #include "pyconsole.h" #endif #endif #include #include #include #include #include #include #include #include #ifdef WIN32 #include #else #include #include #endif #include #include #include #include #include #include #include #include #include #include #include pixel *vid_buf; #define NUM_SOUNDS 2 struct sample { Uint8 *data; Uint32 dpos; Uint32 dlen; } sounds[NUM_SOUNDS]; void mixaudio(void *unused, Uint8 *stream, int len) { int i; Uint32 amount; for ( i=0; i len ) { amount = len; } SDL_MixAudio(stream, &sounds[i].data[sounds[i].dpos], amount, SDL_MIX_MAXVOLUME); sounds[i].dpos += amount; } } void play_sound(char *file) { int index; SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; SDL_AudioCVT cvt; if (!sound_enable) return; /* Look for an empty (or finished) sound slot */ for ( index=0; index1) printf("Multithreading enabled\n"); else printf("Multithreading disabled\n"); #endif return numCPU; } int mousex = 0, mousey = 0; //They contain mouse position int kiosk_enable = 0; void sdl_seticon(void) { #ifdef WIN32 //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 #ifdef MACOSX //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_WM_SetIcon(icon, NULL/*app_icon_mask*/); #endif #endif } int frame_idx=0; void dump_frame(pixel *src, int w, int h, int pitch) { char frame_name[32]; int j,i; unsigned char c[3]; FILE *f; sprintf(frame_name,"frame%04d.ppm",frame_idx); f=fopen(frame_name,"wb"); fprintf(f,"P6\n%d %d\n255\n",w,h); for (j=0; j=0 && x=0 && y255) i=255; d[p++] = i; } for (y=by0; y255) i=255; d[p++] = i; } // save the particle map for (i=0; i=x0 && x=y0 && y255) x=255; if (y<0) y=0; if (y>255) y=255; d[p++] = x; d[p++] = y; } } for (j=0; j>8); d[p++] = (ttlife&0x00FF); } } for (j=0; j>8); d[p++] = (tttmp&0x00FF); } } for (j=0; j>8); d[p++] = (tttemp&0x00FF); } } for (j=0; j=x0 && signs[i].x=y0 && signs[i].y=x0 && signs[i].x=y0 && signs[i].y>8; d[p++] = (signs[i].y-y0); d[p++] = (signs[i].y-y0)>>8; d[p++] = signs[i].ju; x = strlen(signs[i].text); d[p++] = x; memcpy(d+p, signs[i].text, x); p+=x; } i = (p*101+99)/100 + 612; c = malloc(i); //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 c[0] = 0x50; //0x66; c[1] = 0x53; //0x75; c[2] = 0x76; //0x43; c[3] = legacy_enable|((sys_pause<<1)&0x02)|((gravityMode<<2)&0x0C)|((airMode<<4)&0x70); c[4] = SAVE_VERSION; c[5] = CELL; c[6] = bw; c[7] = bh; c[8] = p; c[9] = p >> 8; c[10] = p >> 16; c[11] = p >> 24; i -= 12; if (BZ2_bzBuffToBuffCompress((char *)(c+12), (unsigned *)&i, (char *)d, p, 9, 0, 0) != BZ_OK) { free(d); free(c); return NULL; } free(d); *size = i+12; return c; } int parse_save(void *save, int size, int replace, int x0, int y0) { 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; //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 if (size<16) return 1; if (!(c[2]==0x43 && c[1]==0x75 && c[0]==0x66) && !(c[2]==0x76 && c[1]==0x53 && c[0]==0x50)) return 1; if (c[2]==0x76 && c[1]==0x53 && c[0]==0x50) { new_format = 1; } if (c[4]>SAVE_VERSION) return 2; ver = c[4]; if (ver<34) { legacy_enable = 1; } else { if (ver>=44) { legacy_enable = c[3]&0x01; if (!sys_pause) { sys_pause = (c[3]>>1)&0x01; } if(ver>=46 && replace) { gravityMode = ((c[3]>>2)&0x03);// | ((c[3]>>2)&0x01); airMode = ((c[3]>>4)&0x07);// | ((c[3]>>4)&0x02) | ((c[3]>>4)&0x01); } } else { if (c[3]==1||c[3]==0) { legacy_enable = c[3]; } else { legacy_beta = 1; } } } bw = c[6]; bh = c[7]; if (bx0+bw > XRES/CELL) bx0 = XRES/CELL - bw; if (by0+bh > YRES/CELL) by0 = YRES/CELL - bh; if (bx0 < 0) bx0 = 0; if (by0 < 0) by0 = 0; if (c[5]!=CELL || bx0+bw>XRES/CELL || by0+bh>YRES/CELL) return 3; i = (unsigned)c[8]; i |= ((unsigned)c[9])<<8; i |= ((unsigned)c[10])<<16; i |= ((unsigned)c[11])<<24; d = malloc(i); if (!d) return 1; if (BZ2_bzBuffToBuffDecompress((char *)d, (unsigned *)&i, (char *)(c+12), size-12, 0, 0)) return 1; size = i; if (size < bw*bh) return 1; // normalize coordinates x0 = bx0*CELL; y0 = by0*CELL; w = bw *CELL; h = bh *CELL; if (replace) { if(ver<46){ 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; } // make a catalog of free parts memset(pmap, 0, sizeof(pmap)); for (i=0; i= size) goto corrupt; fvx[y][x] = (d[p++]-127.0f)/64.0f; } for (y=by0; y= size) goto corrupt; fvy[y][x] = (d[p++]-127.0f)/64.0f; } // load the particle map i = 0; pty = p; for (y=y0; y= size) goto corrupt; j=d[p++]; if (j >= PT_NUM) { //TODO: Possibly some server side translation j = PT_DUST;//goto corrupt; } gol[x][y]=0; if (j)// && !(isplayer == 1 && j==PT_STKM)) { if (pmap[y][x]) { k = pmap[y][x]>>8; parts[k].type = j; if (j == PT_PHOT) parts[k].ctype = 0x3fffffff; parts[k].x = (float)x; parts[k].y = (float)y; m[(x-x0)+(y-y0)*w] = k+1; } else if (i < nf) { parts[fp[i]].type = j; if (j == PT_COAL) parts[fp[i]].tmp = 50; if (j == PT_FUSE) parts[fp[i]].tmp = 50; if (j == PT_PHOT) parts[fp[i]].ctype = 0x3fffffff; parts[fp[i]].x = (float)x; parts[fp[i]].y = (float)y; m[(x-x0)+(y-y0)*w] = fp[i]+1; i++; } else m[(x-x0)+(y-y0)*w] = NPART+1; } } // load particle properties for (j=0; j= size) goto corrupt; if (i < NPART) { parts[i].vx = (d[p++]-127.0f)/16.0f; parts[i].vy = (d[p++]-127.0f)/16.0f; if (parts[i].type == PT_STKM) { //player[2] = PT_DUST; player[3] = parts[i].x-1; //Setting legs positions player[4] = parts[i].y+6; player[5] = parts[i].x-1; player[6] = parts[i].y+6; player[7] = parts[i].x-3; player[8] = parts[i].y+12; player[9] = parts[i].x-3; player[10] = parts[i].y+12; player[11] = parts[i].x+1; player[12] = parts[i].y+6; player[13] = parts[i].x+1; player[14] = parts[i].y+6; player[15] = parts[i].x+3; player[16] = parts[i].y+12; player[17] = parts[i].x+3; player[18] = parts[i].y+12; } if (parts[i].type == PT_STKM2) { //player[2] = PT_DUST; player2[3] = parts[i].x-1; //Setting legs positions player2[4] = parts[i].y+6; player2[5] = parts[i].x-1; player2[6] = parts[i].y+6; player2[7] = parts[i].x-3; player2[8] = parts[i].y+12; player2[9] = parts[i].x-3; player2[10] = parts[i].y+12; player2[11] = parts[i].x+1; player2[12] = parts[i].y+6; player2[13] = parts[i].x+1; player2[14] = parts[i].y+6; player2[15] = parts[i].x+3; player2[16] = parts[i].y+12; player2[17] = parts[i].x+3; player2[18] = parts[i].y+12; } } else p += 2; } } for (j=0; j=44) { if (p >= size) { goto corrupt; } if (i <= NPART) { ttv = (d[p++])<<8; ttv |= (d[p++]); parts[i-1].life = ttv; } else { p+=2; } } else { if (p >= size) goto corrupt; if (i <= NPART) parts[i-1].life = d[p++]*4; else p++; } } } if (ver>=44) { for (j=0; j= size) { goto corrupt; } if (i <= NPART) { ttv = (d[p++])<<8; ttv |= (d[p++]); parts[i-1].tmp = ttv; if(ptypes[parts[i-1].type].properties&PROP_LIFE && !parts[i-1].tmp) for(q = 1; q<=NGOL ; q++) { if(parts[i-1].type==goltype[q-1] && grule[q][9]==2) parts[i-1].tmp = grule[q][9]-1; } } else { p+=2; } } } } for (j=0; j=34&&legacy_beta==0) { if (p >= size) { goto corrupt; } if (i <= NPART) { if (ver>=42) { if (new_format) { ttv = (d[p++])<<8; ttv |= (d[p++]); if (parts[i-1].type==PT_PUMP) { parts[i-1].temp = ttv + 0.15;//fix PUMP saved at 0, so that it loads at 0. } else { parts[i-1].temp = ttv; } } else { parts[i-1].temp = (d[p++]*((MAX_TEMP+(-MIN_TEMP))/255))+MIN_TEMP; } } else { parts[i-1].temp = ((d[p++]*((O_MAX_TEMP+(-O_MIN_TEMP))/255))+O_MIN_TEMP)+273; } } else { p++; if (new_format) { p++; } } } else { parts[i-1].temp = ptypes[parts[i-1].type].heat; } } } for (j=0; j=43) || (ty==PT_BCLN && ver>=44) || (ty==PT_SPRK && ver>=21) || (ty==PT_LAVA && ver>=34) || (ty==PT_PIPE && ver>=43))) { if (p >= size) goto corrupt; if (i <= NPART) parts[i-1].ctype = d[p++]; else p++; } } if (p >= size) goto version1; j = d[p++]; for (i=0; i size) goto corrupt; for (k=0; k size) goto corrupt; if (kXRES/GRID_S || stamps[i].thumb_h>YRES/GRID_S)) { factor_x = ceil((float)stamps[i].thumb_w/(float)(XRES/GRID_S)); factor_y = ceil((float)stamps[i].thumb_h/(float)(YRES/GRID_S)); if (factor_y > factor_x) factor_x = factor_y; tmp = rescale_img(stamps[i].thumb, stamps[i].thumb_w, stamps[i].thumb_h, &(stamps[i].thumb_w), &(stamps[i].thumb_h), factor_x); free(stamps[i].thumb); stamps[i].thumb = tmp; } } free(data); } int clipboard_ready = 0; void *clipboard_data = 0; int clipboard_length = 0; 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); #ifdef WIN32 _mkdir("stamps"); #else mkdir("stamps", 0755); #endif stamp_gen_name(sn); sprintf(fn, "stamps" PATH_SEP "%s.stm", sn); f = fopen(fn, "wb"); if (!f) return; fwrite(s, n, 1, f); fclose(f); free(s); if (stamps[STAMP_MAX-1].thumb) free(stamps[STAMP_MAX-1].thumb); memmove(stamps+1, stamps, sizeof(struct stamp)*(STAMP_MAX-1)); memset(stamps, 0, sizeof(struct stamp)); if (stamp_count0) { memcpy(&tmp, stamps+i, sizeof(struct stamp)); memmove(stamps+1, stamps, sizeof(struct stamp)*i); memcpy(stamps, &tmp, sizeof(struct stamp)); stamp_update(); } return data; } void stamp_init(void) { int i; FILE *f; memset(stamps, 0, sizeof(stamps)); f=fopen("stamps" PATH_SEP "stamps.def", "rb"); if (!f) return; for (i=0; i= THUMB_CACHE_SIZE) return; free(thumb_cache_id[i]); free(thumb_cache_data[i]); thumb_cache_id[i] = NULL; for (j=0; j thumb_cache_lru[i]) thumb_cache_lru[j]--; } void thumb_cache_add(char *id, void *thumb, int size) { int i,m=-1,j=-1; thumb_cache_inval(id); for (i=0; i m) { m = thumb_cache_lru[i]; j = i; } } if (i >= THUMB_CACHE_SIZE) { thumb_cache_inval(thumb_cache_id[j]); i = j; } for (j=0; j= THUMB_CACHE_SIZE) return 0; for (j=0; jcommand, 255, 255, 255, 255); emb_reset_pressure(PyObject *self, PyObject *args) { if(!PyArg_ParseTuple(args, ":reset_pressure")) return NULL; // for (int nx = 0; nx=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].life = life; } return Py_BuildValue("i",1); } emb_set_type(PyObject *self, PyObject *args, PyObject *keywds) { int i = -1,life,j=-1,x=-1,y=-1; char *name = ""; char *type = ""; char *kwlist[] = {"setto", "settoint", "from", "i", "x", "y", NULL}; if(!PyArg_ParseTupleAndKeywords(args, keywds, "|sIsIII:set_type",kwlist ,&type,&life,&name,&i,&x,&y)) return NULL; // if(strcmp(name,"")==0 && x==-1 && y==-1 && i==-1 && j==-1) return Py_BuildValue("s","Need more args(coords,i,or a particle name)"); console_parse_type(type, &life, console_error); if(strcmp(name,"all")==0) { for(i=0; i=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].type = life; } return Py_BuildValue("i",1); } emb_set_temp(PyObject *self, PyObject *args, PyObject *keywds) { int i = -1,life,j,x=-1,y=-1; char *name = ""; char *kwlist[] = {"setto", "from", "i", "x", "y", NULL}; if(!PyArg_ParseTupleAndKeywords(args, keywds, "I|sIII:set_type",kwlist ,&life,&name,&i,&x,&y)) return NULL; // if(strcmp(name,"")==0 && x==-1 && y==-1 && i==-1) return Py_BuildValue("s","Need more args(coords,i,or a particle name)"); if(strcmp(name,"all")==0) { for(i=0; i=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].temp = life; } return Py_BuildValue("i",1); } emb_set_tmp(PyObject *self, PyObject *args, PyObject *keywds) { int i = -1,life,j,x=-1,y=-1; char *name = ""; char *kwlist[] = {"setto", "from", "i", "x", "y", NULL}; if(!PyArg_ParseTupleAndKeywords(args, keywds, "I|sIII:set_type",kwlist ,&life,&name,&i,&x,&y)) return NULL; // if(strcmp(name,"")==0 && x==-1 && y==-1 && i==-1) return Py_BuildValue("s","Need more args(coords,i,or a particle name)"); if(strcmp(name,"all")==0) { for(i=0; i=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].tmp = life; } return Py_BuildValue("i",1); } emb_set_x(PyObject *self, PyObject *args, PyObject *keywds) { int i = -1,life,j,x=-1,y=-1; char *name = ""; char *type = ""; char *kwlist[] = {"setto", "from", "i", "x", "y", NULL}; if(!PyArg_ParseTupleAndKeywords(args, keywds, "I|sIII:set_type",kwlist ,&life,&name,&i,&x,&y)) return NULL; // if(strcmp(name,"")==0 && x==-1 && y==-1 && i==-1) return Py_BuildValue("s","Need more args(coords,i,or a particle name)"); if(strcmp(name,"all")==0) { for(i=0; i=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].x = life; } return Py_BuildValue("i",1); } emb_set_y(PyObject *self, PyObject *args, PyObject *keywds) { int i = -1,life,j,x=-1,y=-1; char *name = ""; char *kwlist[] = {"setto", "from", "i", "x", "y", NULL}; if(!PyArg_ParseTupleAndKeywords(args, keywds, "I|sIII:set_type",kwlist ,&life,&name,&i,&x,&y)) return NULL; // if(strcmp(name,"")==0 && x==-1 && y==-1 && i==-1) return Py_BuildValue("s","Need more args(coords,i,or a particle name)"); if(strcmp(name,"all")==0) { for(i=0; i=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].y = life; } return Py_BuildValue("i",1); } emb_set_ctype(PyObject *self, PyObject *args, PyObject *keywds) { int i = -1,life,j,x=-1,y=-1; char *name = ""; char *type = ""; char *kwlist[] = {"setto", "toctypeint", "from", "i", "x", "y", NULL}; if(!PyArg_ParseTupleAndKeywords(args, keywds, "s|IsIII:set_type",kwlist ,&type, &life, &name,&i,&x,&y)) return NULL; // if(strcmp(name,"")==0 && x==-1 && y==-1 && i==-1) return Py_BuildValue("s","Need more args(coords,i,or a particle name)"); if(!life) console_parse_type(type, &life, console_error); if(strcmp(name,"all")==0) { for(i=0; i=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].ctype = life; } return Py_BuildValue("i",1); } emb_set_vx(PyObject *self, PyObject *args, PyObject *keywds) { int i = -1,life,j,x=-1,y=-1; char *name = ""; char *kwlist[] = {"setto", "from", "i", "x", "y", NULL}; if(!PyArg_ParseTupleAndKeywords(args, keywds, "I|sIII:set_type",kwlist ,&life,&name,&i,&x,&y)) return NULL; // if(strcmp(name,"")==0 && x==-1 && y==-1 && i==-1) return Py_BuildValue("s","Need more args(coords,i,or a particle name)"); if(strcmp(name,"all")==0) { for(i=0; i=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].vx = life; } return Py_BuildValue("i",1); } emb_set_vy(PyObject *self, PyObject *args, PyObject *keywds) { int i = -1,life,j,x=-1,y=-1; char *name = ""; char *kwlist[] = {"setto", "from", "i", "x", "y", NULL}; if(!PyArg_ParseTupleAndKeywords(args, keywds, "I|sIII:set_type",kwlist ,&life,&name,&i,&x,&y)) return NULL; // if(strcmp(name,"")==0 && x==-1 && y==-1 && i==-1) return Py_BuildValue("s","Need more args(coords,i,or a particle name)"); if(strcmp(name,"all")==0) { for(i=0; i=0 && x=0 && y>8].type != PT_NONE) parts[pmap[y][x]>>8].vy = life; } return Py_BuildValue("i",1); } emb_get_pmap(PyObject *self, PyObject *args) { int x,y; if(!PyArg_ParseTuple(args, "II:get_pmap",&x,&y)) return NULL; // if(x<0 || y<0 || x>=XRES || y>=YRES) return Py_BuildValue("i",-1); return Py_BuildValue("I",pmap[y][x]); } emb_get_prop(PyObject *self, PyObject *args) { int i; char *prop = ""; if(!PyArg_ParseTuple(args, "Is:get_pmap",&i,&prop)) return NULL; // if(parts[i].type) { if(strcmp(prop,"type")==0) return Py_BuildValue("i",parts[i].type); if(strcmp(prop,"life")==0) return Py_BuildValue("i",parts[i].life); if(strcmp(prop,"ctype")==0) return Py_BuildValue("i",parts[i].ctype); if(strcmp(prop,"temp")==0) return Py_BuildValue("i",parts[i].temp); if(strcmp(prop,"tmp")==0) return Py_BuildValue("i",parts[i].tmp); if(strcmp(prop,"vy")==0) return Py_BuildValue("f",parts[i].vy); if(strcmp(prop,"vx")==0) return Py_BuildValue("f",parts[i].vx); if(strcmp(prop,"x")==0) return Py_BuildValue("i",parts[i].x); if(strcmp(prop,"y")==0) return Py_BuildValue("i",parts[i].y); } return Py_BuildValue("i",-1); } emb_draw_pixel(PyObject *self, PyObject *args) { int x,y,r,g,b,a; a=255; if(!PyArg_ParseTuple(args, "IIIII|I:draw_pixel",&x,&y,&r,&g,&b,&a)) return NULL; if(vid_buf!=NULL) { drawpixel(vid_buf,x,y,r,g,b,a); return Py_BuildValue("i",1); } return Py_BuildValue("i",-1); } //drawtext(pixel *vid, int x, int y, const char *s, int r, int g, int b, int a) emb_draw_text(PyObject *self, PyObject *args) { int x,y,r,g,b,a; char *txt; a=255; if(!PyArg_ParseTuple(args, "IIsIII|I:draw_text",&x,&y,&txt,&r,&g,&b,&a)) return NULL; if(vid_buf!=NULL) { drawtext(vid_buf,x,y,txt,r,g,b,a); return Py_BuildValue("i",1); } return Py_BuildValue("i",-1); } //drawrect(pixel *vid, int x, int y, int w, int h, int r, int g, int b, int a) emb_draw_rect(PyObject *self, PyObject *args) { int x,y,w,h,r,g,b,a; a=255; if(!PyArg_ParseTuple(args, "IIIIIII|I:draw_rect",&x,&y,&w,&h,&r,&g,&b,&a)) return NULL; if(vid_buf!=NULL) { drawrect(vid_buf,x,y,w,h,r,g,b,a); //fillrect return Py_BuildValue("i",1); } return Py_BuildValue("i",-1); } emb_draw_fillrect(PyObject *self, PyObject *args) { int x,y,w,h,r,g,b,a; a=255; if(!PyArg_ParseTuple(args, "IIIIIII|I:draw_fillrect",&x,&y,&w,&h,&r,&g,&b,&a)) return NULL; if(vid_buf!=NULL) { fillrect(vid_buf,x,y,w,h,r,g,b,a); //fillrect return Py_BuildValue("i",1); } return Py_BuildValue("i",-1); } //int textwidth(char *s) emb_get_width(PyObject *self, PyObject *args) { char *txt; if(!PyArg_ParseTuple(args, "s:get_width",&txt)) return NULL; return Py_BuildValue("i",textwidth(txt)); } //SDL_GetMouseState(&x, &y) emb_get_mouse(PyObject *self, PyObject *args) { int x,y,mask,b1,b2,b3; if(!PyArg_ParseTuple(args, ":get_mouse")) return NULL; mask=SDL_GetMouseState(&x, &y); b1=mask&SDL_BUTTON(1); b2=mask&SDL_BUTTON(2); b3=mask&SDL_BUTTON(3); return Py_BuildValue("(ii(iii))",x,y,b1,b2,b3); } //svf_name emb_get_name(PyObject *self, PyObject *args) { if(!PyArg_ParseTuple(args, ":get_name")) return NULL; if(svf_login) return Py_BuildValue("s",svf_user); else return Py_BuildValue("s",""); } emb_shortcuts_disable(PyObject *self, PyObject *args) { if(!PyArg_ParseTuple(args, ":shortcuts_disable")) return NULL; // sys_shortcuts=0; return Py_BuildValue("i",1); } emb_shortcuts_enable(PyObject *self, PyObject *args) { if(!PyArg_ParseTuple(args, ":shortcuts_enable")) return NULL; // sys_shortcuts=1; return Py_BuildValue("i",1); } emb_get_modifier(PyObject *self, PyObject *args) { if(!PyArg_ParseTuple(args, ":get_modifier")) return NULL; return Py_BuildValue("(iiiiii)",sdl_mod&KMOD_LCTRL,sdl_mod&KMOD_RCTRL,sdl_mod&KMOD_LALT,sdl_mod&KMOD_RALT,sdl_mod&KMOD_LSHIFT,sdl_mod&KMOD_RSHIFT); } static PyMethodDef EmbMethods[] = { //WARNING! don't forget to register your function here! {"create", emb_create, METH_VARARGS|METH_KEYWORDS, "create a particle."}, {"log", emb_log, METH_VARARGS, "logs an error string to the console."}, {"reset_pressure", emb_reset_pressure, METH_VARARGS, "resets all the pressure."}, {"reset_velocity", emb_reset_velocity, METH_VARARGS, "resets all the velocity."}, {"reset_sparks", emb_reset_sparks, METH_VARARGS, "resets all the sparks."}, {"set_life", emb_set_life, METH_VARARGS|METH_KEYWORDS, "sets life of a specified particle."}, {"set_type", emb_set_type, METH_VARARGS|METH_KEYWORDS, "sets type of a specified particle."}, {"set_temp", emb_set_temp, METH_VARARGS|METH_KEYWORDS, "sets temp of a specified particle."}, {"set_tmp", emb_set_tmp, METH_VARARGS|METH_KEYWORDS, "sets tmp of a specified particle."}, {"set_x", emb_set_x, METH_VARARGS|METH_KEYWORDS, "sets x of a specified particle."}, {"set_y", emb_set_y, METH_VARARGS|METH_KEYWORDS, "sets y of a specified particle."}, {"set_ctype", emb_set_y, METH_VARARGS|METH_KEYWORDS, "sets ctype of a specified particle."}, {"set_vx", emb_set_vx, METH_VARARGS|METH_KEYWORDS, "sets vx of a specified particle."}, {"set_vy", emb_set_vy, METH_VARARGS|METH_KEYWORDS, "sets vy of a specified particle."}, {"pause", emb_pause, METH_VARARGS, "pause the game."}, {"unpause", emb_unpause, METH_VARARGS, "unpause the game."}, {"pause_toggle", emb_toggle_pause, METH_VARARGS, "toggle game pause."}, {"console_open", emb_open_console, METH_VARARGS, "open the game console."}, {"console_close", emb_close_console, METH_VARARGS, "close the game console."}, {"console_toggle", emb_toggle_console, METH_VARARGS, "toggle the game console."}, {"console_more", emb_console_more, METH_VARARGS, "turns the more indicator on."}, {"console_less", emb_console_less, METH_VARARGS, "turns the more indicator off."}, {"get_pmap", emb_get_pmap, METH_VARARGS, "get the pmap value."}, {"get_prop", emb_get_prop, METH_VARARGS, "get some properties."}, {"draw_pixel", emb_draw_pixel, METH_VARARGS, "draw a pixel."}, {"draw_text", emb_draw_text, METH_VARARGS, "draw some text."}, {"draw_rect", emb_draw_rect, METH_VARARGS, "draw a rect."}, {"draw_fillrect", emb_draw_fillrect, METH_VARARGS, "draw a rect."}, {"get_width", emb_get_width, METH_VARARGS, "get string width."}, {"get_mouse", emb_get_mouse, METH_VARARGS, "get mouse status."}, {"get_name", emb_get_name, METH_VARARGS, "get name of logged in user"}, {"shortcuts_disable", emb_shortcuts_disable, METH_VARARGS, "disable keyboard shortcuts"}, {"shortcuts_enable", emb_shortcuts_enable, METH_VARARGS, "enable keyboard shortcuts"}, {"get_modifier", emb_get_modifier, METH_VARARGS, "get pressed modifier keys"}, {NULL, NULL, 0, NULL} }; #endif int main(int argc, char *argv[]) { int hud_enable = 1; int active_menu = 0; #ifdef BETA int is_beta = 0; #endif char uitext[512] = ""; char heattext[128] = ""; char coordtext[128] = ""; int currentTime = 0; int FPS = 0; int pastFPS = 0; int past = 0; void *http_ver_check; void *http_session_check = NULL; vid_buf=calloc((XRES+BARSIZE)*(YRES+MENUSIZE), PIXELSIZE); char *ver_data=NULL, *check_data=NULL, *tmp; //char console_error[255] = ""; int i, j, bq, fire_fc=0, do_check=0, do_s_check=0, old_version=0, http_ret=0,http_s_ret=0, major, minor, old_ver_len; #ifdef INTERNAL int vs = 0; #endif 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; int load_mode=0, load_w=0, load_h=0, load_x=0, load_y=0, load_size=0; void *load_data=NULL; pixel *load_img=NULL;//, *fbi_img=NULL; 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; GSPEED = 1; #ifdef PYCONSOLE PyObject *pname,*pmodule,*pfunc,*pvalue,*pargs,*pstep,*pkey; #endif /* Set 16-bit stereo audio at 22Khz */ fmt.freq = 22050; fmt.format = AUDIO_S16; fmt.channels = 2; fmt.samples = 512; fmt.callback = mixaudio; fmt.userdata = NULL; #ifdef PYCONSOLE //initialise python console Py_Initialize(); Py_InitModule("tpt", EmbMethods); //change the path to find all the correct modules PyRun_SimpleString("import sys\nsys.path.append('.')"); PyRun_SimpleString("print 'python present.'"); //load the console module and whatnot #ifdef PYEXT printf("using external python console file.\n"); pname=PyString_FromString("tpt_console");//create string object pmodule = PyImport_Import(pname);//import module Py_DECREF(pname);//throw away string #else PyObject *tpt_console_obj = PyMarshal_ReadObjectFromString(tpt_console_pyc+8, sizeof(tpt_console_pyc)-8); pmodule=PyImport_ExecCodeModule("tpt_console", tpt_console_obj); #endif if(pmodule!=NULL) { //Py_DECREF(pname);//throw away the string object pfunc=PyObject_GetAttrString(pmodule,"handle");//get the handler function if(pfunc && PyCallable_Check(pfunc))//check if it's really a function { printf("python console ready to go.\n"); } else { PyErr_Print(); printf("unable to find handle function, mangled console.py?\n"); return -1; } pstep=PyObject_GetAttrString(pmodule,"step");//get the handler function if(pstep && PyCallable_Check(pstep))//check if it's really a function { printf("step function found.\n"); } else { printf("unable to find step function. ignoring.\n"); } pkey=PyObject_GetAttrString(pmodule,"keypress");//get the handler function if(pstep && PyCallable_Check(pkey))//check if it's really a function { printf("key function found.\n"); } else { printf("unable to find key function. ignoring.\n"); } } else { //sys.stderr PyErr_Print(); printf("unable to find console module, missing file or mangled console.py?\n"); return -1; } #else printf("python console disabled at compile time."); #endif #ifdef MT numCores = core_count(); #endif //TODO: Move out version stuff #ifdef BETA if (is_beta) { old_ver_len = textwidth(old_ver_msg_beta); } else { old_ver_len = textwidth(old_ver_msg); } #else old_ver_len = textwidth(old_ver_msg); #endif menu_count(); parts = calloc(sizeof(particle), NPART); cb_parts = calloc(sizeof(particle), NPART); for (i=0; i1180) bsx = 1180; if (bsx<0) bsx = 0; if (bsy>1180) bsy = 1180; if (bsy<0) bsy = 0; update_particles(vid_buf); draw_parts(vid_buf); if (cmode==CM_PERS) { if (!fire_fc) { dim_copy_pers(pers_bg, vid_buf); } else { memcpy(pers_bg, vid_buf, (XRES+BARSIZE)*YRES*PIXELSIZE); } fire_fc = (fire_fc+1) % 3; } if (cmode==CM_FIRE||cmode==CM_BLOB||cmode==CM_FANCY) render_fire(vid_buf); render_signs(vid_buf); memset(vid_buf+((XRES+BARSIZE)*YRES), 0, (PIXELSIZE*(XRES+BARSIZE))*MENUSIZE); clearrect(vid_buf, XRES-1, 0, BARSIZE+1, YRES); draw_svf_ui(vid_buf); if (http_ver_check) { if (!do_check && http_async_req_status(http_ver_check)) { ver_data = http_async_req_stop(http_ver_check, &http_ret, NULL); if (http_ret==200 && ver_data) { #ifdef BETA if (sscanf(ver_data, "%d.%d.%d", &major, &minor, &is_beta)==3) if (major>SAVE_VERSION || (major==SAVE_VERSION && minor>MINOR_VERSION) || (major==SAVE_VERSION && is_beta == 0)) old_version = 1; #else if (sscanf(ver_data, "%d.%d", &major, &minor)==2) if (major>SAVE_VERSION || (major==SAVE_VERSION && minor>MINOR_VERSION)) old_version = 1; #endif free(ver_data); } http_ver_check = NULL; } do_check = (do_check+1) & 15; } if(http_session_check) { if(!do_s_check && http_async_req_status(http_session_check)) { check_data = http_async_req_stop(http_session_check, &http_s_ret, NULL); if(http_ret==200 && check_data) { if(!strncmp(check_data, "EXPIRED", 7)) { //Session expired strcpy(svf_user, ""); strcpy(svf_pass, ""); strcpy(svf_user_id, ""); strcpy(svf_session_id, ""); svf_login = 0; svf_own = 0; svf_admin = 0; svf_mod = 0; } else if(!strncmp(check_data, "BANNED", 6)) { //User banned strcpy(svf_user, ""); strcpy(svf_pass, ""); strcpy(svf_user_id, ""); strcpy(svf_session_id, ""); svf_login = 0; svf_own = 0; svf_admin = 0; svf_mod = 0; error_ui(vid_buf, "Unable to log in", "Your account has been suspended, consider reading the rules."); } else if(!strncmp(check_data, "OK", 2)) { //Session valid if(strlen(check_data)>2){ //User is elevated if (!strncmp(check_data+3, "ADMIN", 5)) { svf_admin = 1; svf_mod = 0; } else if (!strncmp(check_data+3, "MOD", 3)) { svf_admin = 0; svf_mod = 1; } } } else { //No idea, but log the user out anyway strcpy(svf_user, ""); strcpy(svf_pass, ""); strcpy(svf_user_id, ""); strcpy(svf_session_id, ""); svf_login = 0; svf_own = 0; svf_admin = 0; svf_mod = 0; } save_presets(0); free(check_data); } http_session_check = NULL; } else { clearrect(vid_buf, XRES-125+BARSIZE/*385*/, YRES+(MENUSIZE-16), 91, 14); drawrect(vid_buf, XRES-125+BARSIZE/*385*/, YRES+(MENUSIZE-16), 91, 14, 255, 255, 255, 255); drawtext(vid_buf, XRES-122+BARSIZE/*388*/, YRES+(MENUSIZE-13), "\x84", 255, 255, 255, 255); if(username_flash>30){ username_flash_t = -1; username_flash = 30; } else if(username_flash<0) { username_flash_t = 1; username_flash = 0; } username_flash += username_flash_t; if (svf_login) drawtext(vid_buf, XRES-104+BARSIZE/*406*/, YRES+(MENUSIZE-12), svf_user, 255, 255, 255, 175-(username_flash*5)); else drawtext(vid_buf, XRES-104+BARSIZE/*406*/, YRES+(MENUSIZE-12), "[checking]", 255, 255, 255, 255); } do_s_check = (do_s_check+1) & 15; } if(sys_shortcuts==1) { if (sdl_key=='q' || sdl_key==SDLK_ESCAPE) { if (confirm_ui(vid_buf, "You are about to quit", "Are you sure you want to quit?", "Quit")) { break; } } //if(sdl_key=='d' && isplayer) //{ // death = 1; // //death = !(death); //} if (sdl_key=='f') { framerender = 1; } if ((sdl_key=='l' || sdl_key=='k') && stamps[0].name[0]) { if (load_mode) { free(load_img); free(load_data); load_mode = 0; load_data = NULL; load_img = NULL; } if (it > 50) it = 50; if (sdl_key=='k' && stamps[1].name[0]) { j = stamp_ui(vid_buf); if (j>=0) load_data = stamp_load(j, &load_size); else load_data = NULL; } else load_data = stamp_load(0, &load_size); 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 (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) { set_cmode(CM_LIFE); } if (sdl_key==SDLK_TAB) { CURRENT_BRUSH =(CURRENT_BRUSH + 1)%BRUSH_NUM ; } if (sdl_key==SDLK_LEFTBRACKET) { 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==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 50) it = 50; } if (sdl_key=='z'&&(sdl_mod & (KMOD_LCTRL|KMOD_RCTRL))) // Undo { int cbx, cby, cbi; for (cbi=0; cbi=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) { 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; }*/ } } bq = b; b = SDL_GetMouseState(&x, &y); for (i=0; i=sdl_scale*(XRES-2) && x= sdl_scale*((i*16)+YRES+MENUSIZE-16-(SC_TOTAL*16)) && y=sdl_scale*zoom_wx && y>=sdl_scale*zoom_wy && x0 && y0 && x>8)>=NPART || !cr)) { if (DEBUG_MODE) { int tctype = parts[cr>>8].ctype; if (tctype>=PT_NUM) 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 { #ifdef BETA sprintf(heattext, "%s, Pressure: %3.2f, Temp: %4.2f C, Life: %d", ptypes[cr&0xFF].name, pv[(y/sdl_scale)/CELL][(x/sdl_scale)/CELL], parts[cr>>8].temp-273.15f, parts[cr>>8].life); #else 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 } } else { sprintf(heattext, "Empty, Pressure: %3.2f", pv[(y/sdl_scale)/CELL][(x/sdl_scale)/CELL]); if (DEBUG_MODE) { sprintf(coordtext, "X:%d Y:%d", x/sdl_scale, y/sdl_scale); } } } mx = x; my = y; if (update_flag) { info_box(vid_buf, "Finalizing update..."); if (last_major>SAVE_VERSION || (last_major==SAVE_VERSION && last_minor>=MINOR_VERSION)) { update_cleanup(); error_ui(vid_buf, 0, "Update failed - try downloading a new version."); } else { if (update_finish()) error_ui(vid_buf, 0, "Update failed - try downloading a new version."); else info_ui(vid_buf, "Update success", "You have successfully updated the Powder Toy!"); } update_flag = 0; } if (b && !bq && x>=(XRES-19-old_ver_len)*sdl_scale && x<=(XRES-14)*sdl_scale && y>=(YRES-22)*sdl_scale && y<=(YRES-9)*sdl_scale && old_version) { tmp = malloc(64); #ifdef BETA if (is_beta) { sprintf(tmp, "Your version: %d (Beta %d), new version: %d (Beta %d).", SAVE_VERSION, MINOR_VERSION, major, minor); } else { sprintf(tmp, "Your version: %d (Beta %d), new version: %d.%d.", SAVE_VERSION, MINOR_VERSION, major, minor); } #else sprintf(tmp, "Your version: %d.%d, new version: %d.%d.", SAVE_VERSION, MINOR_VERSION, major, minor); #endif if (confirm_ui(vid_buf, "Do you want to update The Powder Toy?", tmp, "Update")) { free(tmp); tmp = download_ui(vid_buf, my_uri, &i); if (tmp) { save_presets(1); if (update_start(tmp, i)) { update_cleanup(); save_presets(0); error_ui(vid_buf, 0, "Update failed - try downloading a new version."); } else return 0; } } else free(tmp); } if (y>=sdl_scale*(YRES+(MENUSIZE-20))) { if (x>=189*sdl_scale && x<=202*sdl_scale && svf_login && svf_open && svf_myvote==0) { db = svf_own ? 275 : 272; if (da < 51) da ++; } else if (x>=204 && x<=217 && svf_login && svf_open && svf_myvote==0) { db = svf_own ? 275 : 272; if (da < 51) da ++; } else if (x>=189 && x<=217 && svf_login && svf_open && svf_myvote!=0) { db = (svf_myvote==1) ? 273 : 274; if (da < 51) da ++; } else if (x>=219*sdl_scale && x<=((XRES+BARSIZE-(510-349))*sdl_scale) && svf_login && svf_open) { db = svf_own ? 257 : 256; if (da < 51) da ++; } else if (x>=((XRES+BARSIZE-(510-351))*sdl_scale) && x<((XRES+BARSIZE-(510-366))*sdl_scale)) { db = 270; if (da < 51) da ++; } else if (x>=((XRES+BARSIZE-(510-367))*sdl_scale) && x<((XRES+BARSIZE-(510-383))*sdl_scale)) { db = 266; if (da < 51) da ++; } else if (x>=37*sdl_scale && x<=187*sdl_scale && svf_login) { db = 259; if (svf_open && svf_own && x<=55*sdl_scale) db = 258; if (da < 51) da ++; } else if (x>=((XRES+BARSIZE-(510-385))*sdl_scale) && x<=((XRES+BARSIZE-(510-476))*sdl_scale)) { db = svf_login ? 261 : 260; if (svf_admin) { db = 268; } else if (svf_mod) { db = 271; } if (da < 51) da ++; } else if (x>=sdl_scale && x<=17*sdl_scale) { db = 262; if (da < 51) da ++; } else if (x>=((XRES+BARSIZE-(510-494))*sdl_scale) && x<=((XRES+BARSIZE-(510-509))*sdl_scale)) { db = sys_pause ? 264 : 263; if (da < 51) da ++; } else if (x>=((XRES+BARSIZE-(510-476))*sdl_scale) && x<=((XRES+BARSIZE-(510-491))*sdl_scale)) { db = 267; if (da < 51) da ++; } else if (x>=19*sdl_scale && x<=35*sdl_scale && svf_open) { db = 265; if (da < 51) da ++; } else if (da > 0) da --; } else if (da > 0) da --; if (!sdl_zoom_trig && zoom_en==1) zoom_en = 0; if (sdl_key=='z' && zoom_en==2 && sys_shortcuts==1) zoom_en = 1; if (load_mode) { load_x = CELL*((mx/sdl_scale-load_w/2+CELL/2)/CELL); load_y = CELL*((my/sdl_scale-load_h/2+CELL/2)/CELL); if (load_x+load_w>XRES) load_x=XRES-load_w; if (load_y+load_h>YRES) load_y=YRES-load_h; if (load_x<0) load_x=0; if (load_y<0) load_y=0; if (bq==1 && !b) { parse_save(load_data, load_size, 0, load_x, load_y); free(load_data); free(load_img); load_mode = 0; } else if (bq==4 && !b) { free(load_data); free(load_img); load_mode = 0; } } else if (save_mode==1) { save_x = (mx/sdl_scale)/CELL; save_y = (my/sdl_scale)/CELL; if (save_x >= XRES/CELL) save_x = XRES/CELL-1; if (save_y >= YRES/CELL) save_y = YRES/CELL-1; save_w = 1; save_h = 1; if (b==1) { save_mode = 2; } else if (b==4) { save_mode = 0; copy_mode = 0; } } else if (save_mode==2) { save_w = (mx/sdl_scale+CELL/2)/CELL - save_x; save_h = (my/sdl_scale+CELL/2)/CELL - save_y; if (save_w>XRES/CELL) save_w = XRES/CELL; if (save_h>YRES/CELL) save_h = YRES/CELL; if (save_w<1) save_w = 1; if (save_h<1) save_h = 1; if (!b) { if (copy_mode==1) { clipboard_data=build_save(&clipboard_length, save_x*CELL, save_y*CELL, save_w*CELL, save_h*CELL); 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_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); save_mode = 0; } } } else if (sdl_zoom_trig && zoom_en<2) { x /= sdl_scale; y /= sdl_scale; x -= ZSIZE/2; y -= ZSIZE/2; if (x<0) x=0; if (y<0) y=0; if (x>XRES-ZSIZE) x=XRES-ZSIZE; if (y>YRES-ZSIZE) y=YRES-ZSIZE; zoom_x = x; zoom_y = y; zoom_wx = (x 50) it = 50; x /= sdl_scale; y /= sdl_scale; if (y>=YRES+(MENUSIZE-20)) { if (!lb) { if (x>=189 && x<=202 && svf_login && svf_open && svf_myvote==0 && svf_own==0) { if (execute_vote(vid_buf, svf_id, "Up")) { svf_myvote = 1; } } if (x>=204 && x<=217 && svf_login && svf_open && svf_myvote==0 && svf_own==0) { if (execute_vote(vid_buf, svf_id, "Down")) { svf_myvote = -1; } } if (x>=219 && x<=(XRES+BARSIZE-(510-349)) && svf_login && svf_open) tag_list_ui(vid_buf); if (x>=(XRES+BARSIZE-(510-351)) && x<(XRES+BARSIZE-(510-366)) && !bq) { legacy_enable = !legacy_enable; } 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)); for (i=0; i=(XRES+BARSIZE-(510-385)) && x<=(XRES+BARSIZE-(510-476))) { login_ui(vid_buf); if (svf_login){ save_presets(0); http_session_check = NULL; } } if (x>=37 && x<=187 && svf_login) { if (!svf_open || !svf_own || x>51) { if (save_name_ui(vid_buf)) execute_save(vid_buf); } else execute_save(vid_buf); while (!sdl_poll()) if (!SDL_GetMouseState(&x, &y)) break; b = bq = 0; } if (x>=1 && x<=17) { search_ui(vid_buf); memset(fire_bg, 0, XRES*YRES*PIXELSIZE); memset(pers_bg, 0, (XRES+BARSIZE)*YRES*PIXELSIZE); memset(fire_r, 0, sizeof(fire_r)); memset(fire_g, 0, sizeof(fire_g)); memset(fire_b, 0, sizeof(fire_b)); } if (x>=19 && x<=35 && svf_last && svf_open && !bq) { //int tpval = sys_pause; parse_save(svf_last, svf_lsize, 1, 0, 0); //sys_pause = tpval; } if (x>=(XRES+BARSIZE-(510-476)) && x<=(XRES+BARSIZE-(510-491)) && !bq) { if (b & SDL_BUTTON_LMASK) { set_cmode((cmode+1) % CM_COUNT); } if (b & SDL_BUTTON_RMASK) { if ((cmode+(CM_COUNT-1)) % CM_COUNT == CM_LIFE) { set_cmode(CM_GRAD); } else { set_cmode((cmode+(CM_COUNT-1)) % CM_COUNT); } } } if (x>=(XRES+BARSIZE-(510-494)) && x<=(XRES+BARSIZE-(510-509)) && !bq) sys_pause = !sys_pause; lb = 0; } } else if (y=signx && x<=signx+signw && y>=signy && y<=signy+signh) { char buff[256]; int sldr; memset(buff, 0, sizeof(buff)); for(sldr=3; signs[signi].text[sldr] != '|'; sldr++) buff[sldr-3] = signs[signi].text[sldr]; buff[sldr-3] = '\0'; open_ui(vid_buf, buff, 0); } } } if (c==WL_SIGN+100) { if (!bq) add_sign_ui(vid_buf, x, y); } else if (lb) { if (lm == 1) { xor_line(lx, ly, x, y, vid_buf); if (c==WL_FAN+100 && lx>=0 && ly>=0 && lx0 && y0 && x>8)>=NPART || !cr)) { c = sl = cr&0xFF; } else { //Something } } lx = x; ly = y; lb = 0; lm = 0; } else { //Copy state before drawing any particles (for undo)7 int cbx, cby, cbi; for (cbi=0; cbi=XRES || ly>=YRES || bmap[ly/CELL][lx/CELL]!=WL_FAN) create_line(lx, ly, x, y, bsx, bsy, c); } else create_box(lx, ly, x, y, c); lm = 0; } lb = 0; } if (load_mode) { draw_image(vid_buf, load_img, load_x, load_y, load_w, load_h, 128); xor_rect(vid_buf, load_x, load_y, load_w, load_h); } 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); da = 51; db = 269; } if (zoom_en!=1 && !load_mode && !save_mode) { render_cursor(vid_buf, mx/sdl_scale, my/sdl_scale, su, bsx, bsy); mousex = mx/sdl_scale; mousey = my/sdl_scale; } if (zoom_en) render_zoom(vid_buf); if (da) switch (db) { case 256: drawtext(vid_buf, 16, YRES-24, "Add simulation tags.", 255, 255, 255, da*5); break; case 257: drawtext(vid_buf, 16, YRES-24, "Add and remove simulation tags.", 255, 255, 255, da*5); break; case 258: drawtext(vid_buf, 16, YRES-24, "Save the simulation under the current name.", 255, 255, 255, da*5); break; case 259: drawtext(vid_buf, 16, YRES-24, "Save the simulation under a new name.", 255, 255, 255, da*5); break; case 260: drawtext(vid_buf, 16, YRES-24, "Sign into the Simulation Server.", 255, 255, 255, da*5); break; case 261: drawtext(vid_buf, 16, YRES-24, "Sign into the Simulation Server under a new name.", 255, 255, 255, da*5); break; case 262: drawtext(vid_buf, 16, YRES-24, "Find & open a simulation", 255, 255, 255, da*5); break; case 263: drawtext(vid_buf, 16, YRES-24, "Pause the simulation", 255, 255, 255, da*5); break; case 264: drawtext(vid_buf, 16, YRES-24, "Resume the simulation", 255, 255, 255, da*5); break; case 265: drawtext(vid_buf, 16, YRES-24, "Reload the simulation", 255, 255, 255, da*5); break; case 266: drawtext(vid_buf, 16, YRES-24, "Erase all particles and walls", 255, 255, 255, da*5); break; case 267: drawtext(vid_buf, 16, YRES-24, "Change display mode", 255, 255, 255, da*5); break; case 268: drawtext(vid_buf, 16, YRES-24, "Annuit C\245ptis", 255, 255, 255, da*5); break; case 269: drawtext(vid_buf, 16, YRES-24, "Click-and-drag to specify a rectangle to copy (right click = cancel).", 255, 216, 32, da*5); break; case 270: drawtext(vid_buf, 16, YRES-24, "Enable or disable compatibility mode (disables heat simulation).", 255, 255, 255, da*5); break; case 271: drawtext(vid_buf, 16, YRES-24, "You're a moderator", 255, 255, 255, da*5); break; case 272: drawtext(vid_buf, 16, YRES-24, "Like/Dislike this save.", 255, 255, 255, da*5); break; case 273: drawtext(vid_buf, 16, YRES-24, "You like this.", 255, 255, 255, da*5); break; case 274: drawtext(vid_buf, 16, YRES-24, "You dislike this.", 255, 255, 255, da*5); break; case 275: drawtext(vid_buf, 16, YRES-24, "You cannot vote on your own save.", 255, 255, 255, da*5); break; default: drawtext(vid_buf, 16, YRES-24, (char *)ptypes[db].descs, 255, 255, 255, da*5); } if (itc) { itc--; drawtext(vid_buf, (XRES-textwidth(itc_msg))/2, ((YRES/2)-10), itc_msg, 255, 255, 255, itc>51?255:itc*5); } if (it) { it--; drawtext(vid_buf, 16, 20, it_msg, 255, 255, 255, it>51?255:it*5); } if (old_version) { clearrect(vid_buf, XRES-21-old_ver_len, YRES-24, old_ver_len+9, 17); #ifdef BETA if (is_beta) { drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg_beta, 255, 216, 32, 255); } else { drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg, 255, 216, 32, 255); } #else drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg, 255, 216, 32, 255); #endif drawrect(vid_buf, XRES-19-old_ver_len, YRES-22, old_ver_len+5, 13, 255, 216, 32, 255); } if (hud_enable) { currentTime = SDL_GetTicks(); if (currentTime-past>=16) { past = SDL_GetTicks(); FPS++; } if (currentTime-pastFPS>=1000) { FPSB = FPS; FPS = 0; pastFPS = currentTime; } #ifdef BETA sprintf(uitext, "Version %d Beta %d FPS:%d Parts:%d Generation:%d Gravity:%d Air:%d", SAVE_VERSION, MINOR_VERSION, FPSB, NUM_PARTS, GENERATION, gravityMode, airMode); #else if (DEBUG_MODE) sprintf(uitext, "Version %d.%d FPS:%d Parts:%d Generation:%d Gravity:%d Air:%d", SAVE_VERSION, MINOR_VERSION, FPSB, NUM_PARTS, GENERATION, gravityMode, airMode); else sprintf(uitext, "Version %d.%d FPS:%d", SAVE_VERSION, MINOR_VERSION, FPSB); #endif if (REPLACE_MODE) strappend(uitext, " [REPLACE MODE]"); if (sdl_mod&(KMOD_CAPS)) strappend(uitext, " [CAP LOCKS]"); if (GRID_MODE) sprintf(uitext, "%s [GRID: %d]", uitext, GRID_MODE); #ifdef INTERNAL if (vs) strappend(uitext, " [FRAME CAPTURE]"); #endif if (sdl_zoom_trig||zoom_en) { if (zoom_x0 || sr == PT_NEUT || sr == PT_PHOT) player[2] = sr; else player[2] = PT_DUST; } if (isplayer2==0) { if (ptypes[sr].falldown>0 || sr == PT_NEUT || sr == PT_PHOT) player2[2] = sr; else player2[2] = PT_DUST; } } SDL_CloseAudio(); http_done(); Py_Finalize();//cleanup any python stuff. return 0; } #ifdef PYCONSOLE int process_command(pixel *vid_buf,char *console,char *console_error,PyObject *pfunc) { int y,x,nx,ny,i,j,k,m; int do_next = 1; char xcoord[10]; char ycoord[10]; char console2[15]; char console3[15]; char console4[15]; char console5[15]; PyObject *pvalue,*pargs; //sprintf(console_error, "%s", console); if(console && strcmp(console, "")!=0 && strncmp(console, " ", 1)!=0) { sscanf(console,"%14s %14s %14s %14s", console2, console3, console4, console5);//why didn't i know about this function?! if(strcmp(console2, "quit")==0) { return -1; } 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; } } return 1; } #endif