mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-04-22 15:41:57 +02:00
console things, set commands use keywords for arguments.
This commit is contained in:
parent
13e90886c1
commit
9fffbb88da
10
build/example.py
Normal file
10
build/example.py
Normal file
@ -0,0 +1,10 @@
|
||||
import tpt
|
||||
import random
|
||||
import time
|
||||
# example script
|
||||
|
||||
def noise(typ,amount=50):
|
||||
for xx in range(4,612):
|
||||
for yy in range(4,384):
|
||||
if(random.randrange(0,100)<amount):
|
||||
tpt.create(xx,yy,typ)
|
108
build/tpt_console.py
Normal file
108
build/tpt_console.py
Normal file
@ -0,0 +1,108 @@
|
||||
import tpt
|
||||
from tpt import *
|
||||
import sys
|
||||
import code
|
||||
import ctypes
|
||||
import traceback
|
||||
from threading import Thread
|
||||
print "console module loaded."
|
||||
#redirect stdout like this:
|
||||
class logger:
|
||||
def write(self,txt):
|
||||
txt=txt.strip().split("\n")[-1]
|
||||
repr(txt)
|
||||
tpt.log(txt)
|
||||
sys.stdout=logger()
|
||||
sys.stderr=logger()
|
||||
|
||||
element={"none":0,"dust":1,"watr":2,"oil":3,"fire":4,"stne":5,"lava":6,"gunp":7,
|
||||
"nitr":8,"clne":9,"gas":10,"plex":11,"goo":12,"icei":13,"metl":14,"sprk":15,
|
||||
"snow":16,"wood":17,"neut":18,"plut":19,"plnt":20,"acid":21,"void":22,
|
||||
"wtrv":23,"cnct":24,"dstw":25,"salt":26,"sltw":27,"dmnd":28,"bmtl":29,
|
||||
"brmt":30,"phot":31,"uran":32,"wax":33,"mwax":34,"pscn":35,"nscn":36,
|
||||
"lntg":37,"insl":38,"bhol":39,"whol":40,"rbdm":41,"lrbd":42,"ntct":43,
|
||||
"sand":44,"glas":45,"ptct":46,"bgla":47,"thdr":48,"plsm":49,"etrd":50,
|
||||
"nice":51,"nble":52,"btry":53,"lcry":54,"stkm":55,"swch":56,"smke":57,
|
||||
"desl":58,"coal":59,"lo2":60,"o2":61,"inwr":62,"yest":63,"dyst":64,
|
||||
"thrm":65,"glow":66,"brck":67,"hflm":68,"firw":69,"fuse":70,"fsep":71,
|
||||
"amtr":72,"bcol":73,"pcln":74,"hswc":75,"iron":76,"mort":77,"gol":78,
|
||||
"hlif":79,"asim":80,"2x2":81,"dani":82,"amoe":83,"move":84,"pgol":85,
|
||||
"dmoe":86,"34":87,"llif":88,"stan":89,"spng":90,"rime":91,"fog":92,
|
||||
"bcln":93,"love":94,"deut":95,"warp":96,"pump":97,"fwrk":98,"pipe":99,
|
||||
"frzz":100,"frzw":101,"grav":102,"bizr":103,"bizrg":104,"bizrs":105,
|
||||
"inst":106,"isoz":107,"iszs":108,"prti":109,"prto":110,"pste":111,
|
||||
"psts":112,"anar":113,"vine":114,"invis":115,"equalvel":116,"spawn2":117,
|
||||
"spawn":118,"shld1":119,"shld2":120,"shld3":121,"shld4":122,"lolz":123,
|
||||
"wifi":124,"filt":125,"aray":126,"bray":127,"stkm2":128,"bomb":129,
|
||||
"c5":130,"sing":131,"qrtz":132,"pqrt":133,"seed":134,"maze":135,
|
||||
"coag":136,"wall":137,"gnar":138,"repl":139,"myst":140,"boyl":141,
|
||||
"lote":142,"frg2":143,"star":144,"frog":145,"bran":146,"wind":147,
|
||||
"num":148}
|
||||
|
||||
class _fork(Thread):
|
||||
def __init__ (self,func):
|
||||
Thread.__init__(self)
|
||||
self.func=func
|
||||
def run(self):
|
||||
self.func(self)
|
||||
def fork(func):
|
||||
try:
|
||||
a=fork.threads
|
||||
except:
|
||||
fork.threads={}
|
||||
fork.i=0
|
||||
tmp=_fork(func)
|
||||
fork.threads[fork.i]=tmp
|
||||
fork.i+=1
|
||||
tmp.start()
|
||||
return tmp
|
||||
tpt.log("Thread #%d started"%(fork.i-1))
|
||||
def fork_status():
|
||||
count=0
|
||||
remove=[]
|
||||
for key in fork.threads:
|
||||
if(fork.threads[key].is_alive()):
|
||||
count+=1
|
||||
else:
|
||||
remove.append[key]
|
||||
for item in remove:
|
||||
del fork.threads[item]
|
||||
tpt.log("%d threads alive. %d threads stopped since last status."%(count,len(remove)))
|
||||
def fork_unblock():
|
||||
pass#i need to implement this some day.
|
||||
def error(ex):
|
||||
err=traceback.format_exc()
|
||||
sys.stdout.write(err)
|
||||
|
||||
def clean():
|
||||
#add any functions that must be reachable here.
|
||||
"""copy=["__builtins__","__name__","__doc__","__package__",'tpt','clean',
|
||||
'element','fork','_fork','fork_status','fork_unblock','sys']
|
||||
handle.glob={}
|
||||
for item in copy:
|
||||
handle.glob[item]=globals()[item]"""
|
||||
handle.glob=globals()
|
||||
handle.buf=""
|
||||
|
||||
def handle(txt):
|
||||
try:
|
||||
a=handle.glob
|
||||
except:
|
||||
clean()
|
||||
try:
|
||||
_handle(txt)
|
||||
except Exception as ex:
|
||||
error(ex)
|
||||
|
||||
def _handle(txt):
|
||||
#print "handling '%s'"%txt
|
||||
try:
|
||||
sys.stdout.write(repr(eval(txt,handle.glob)))
|
||||
except:
|
||||
try:
|
||||
exec txt in handle.glob
|
||||
except Exception as ex:
|
||||
error(ex)
|
||||
|
||||
|
||||
|
47
console_README
Normal file
47
console_README
Normal file
@ -0,0 +1,47 @@
|
||||
### CONSOLE ###
|
||||
the console in this version is a python console and will execute any command you enter.
|
||||
the api for interacting with the powder toy is contained in the tpt module and is already imported at startup.
|
||||
currently implemented api functions:
|
||||
tpt.create(x,y,type) create a particle of type <type> at <x>,<y>
|
||||
tpt.reset_velocity() resets all velocity
|
||||
tpt.reset_pressure() resets all pressure
|
||||
tpt.reset_sparks() resets all sparks
|
||||
tpt.set_life(type,life) sets the life of all particles with type <type>
|
||||
to <life>(set <type> to -1 for all)
|
||||
tpt.set_type(type1,type2) sets the type of all particles with type <type1>
|
||||
to <type2>(set <type1> to -1 for all)
|
||||
tpt.set_temp(type,temp) sets the temp of all particles with type <type>
|
||||
to <temp>(set <type> to -1 for all)
|
||||
tpt.set_tmp(type,tmp) sets the tmp of all particles with type <type>
|
||||
to <tmp>(set <type> to -1 for all)
|
||||
tpt.set_x(type,x) sets the x of all particles with type <type>
|
||||
to <x>(set <type> to -1 for all)
|
||||
tpt.set_y(type,y) sets the y of all particles with type <type>
|
||||
to <y>(set <type> to -1 for all)
|
||||
tpt.set_vx(type,vx) sets the vx of all particles with type <type>
|
||||
to <vx>(set <type> to -1 for all)
|
||||
tpt.set_vy(type,vy) sets the vy of all particles with type <type>
|
||||
to <vy>(set <type> to -1 for all)
|
||||
tpt.set_ctype(type,ctype) sets the ctype of all particles with type
|
||||
<type> to <ctype>(set <type> to -1 for all)
|
||||
tpt.pause() pause the game(note that closing the console
|
||||
pauses the game)
|
||||
tpt.unpause() unpause the game(note that closing the console
|
||||
pauses the game)
|
||||
tpt.toggle_pause() toggle pause(note that closing the console
|
||||
pauses the game)
|
||||
tpt.close_console() closes the console and pauses the game
|
||||
tpt.open_console() opens the console
|
||||
tpt.toggle_console() toggles the console
|
||||
|
||||
NOTE: these functions don't do bounds checking, which means that they CAN AND
|
||||
WILL CRASH the powder toy. be carefull.
|
||||
|
||||
### TIPS&TRICKS ###
|
||||
all functions that need a particle type expect a number. but instead of doing
|
||||
tpt.set_life(32,life) you can do tpt.set_life(element['uran'],life)
|
||||
|
||||
### COMPILING ###
|
||||
before you compile you should have python 2.x installed.
|
||||
you must run the "getheader.py" script to get the correct linking options, add
|
||||
these options to the makefile
|
13
getheader.py
Normal file
13
getheader.py
Normal file
@ -0,0 +1,13 @@
|
||||
import sys
|
||||
import os.path
|
||||
path=os.path.join(sys.exec_prefix,"include","python%s"%sys.version[:3])
|
||||
#print "headers at ",repr(path)
|
||||
#-lpython2.3 -lm -L/usr/lib/python2.3/config
|
||||
args="-lpython%s -lm -L%s"%(sys.version[:3],os.path.join(sys.exec_prefix,"lib","python%s"%sys.version[:3],"config"))
|
||||
print " linux args are"
|
||||
print args,"-I%s"%path
|
||||
|
||||
path=os.path.join(sys.exec_prefix,"include")
|
||||
args="-lpython%s -lm -L%s"%(sys.version[:3],os.path.join(sys.exec_prefix,"lib","config"))#,"python%s"%sys.version[:3]
|
||||
print "\n windows args are"
|
||||
print args,"-I%s"%path
|
@ -225,7 +225,7 @@ void open_link(char *uri);
|
||||
|
||||
int report_ui(pixel *vid_buf, char *save_id);
|
||||
|
||||
char *console_ui(pixel *vid_buf, char error[255]);
|
||||
char *console_ui(pixel *vid_buf, char error[255],char console_more);
|
||||
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);
|
||||
|
@ -3839,13 +3839,15 @@ struct command_history {
|
||||
};
|
||||
typedef struct command_history command_history;
|
||||
command_history *last_command = NULL;
|
||||
char *console_ui(pixel *vid_buf,char error[255]) { //TODO: error messages, show previous commands
|
||||
command_history *last_command2 = NULL;
|
||||
char *console_ui(pixel *vid_buf,char error[255],char console_more) {
|
||||
int mx,my,b,cc,ci = -1;
|
||||
pixel *old_buf=calloc((XRES+BARSIZE)*(YRES+MENUSIZE), PIXELSIZE);
|
||||
command_history *currentcommand;
|
||||
command_history *currentcommand2;
|
||||
ui_edit ed;
|
||||
ed.x = 15;
|
||||
ed.y = 210;
|
||||
ed.y = 207;
|
||||
ed.w = XRES;
|
||||
ed.nx = 1;
|
||||
ed.def = "";
|
||||
@ -3854,9 +3856,19 @@ char *console_ui(pixel *vid_buf,char error[255]) { //TODO: error messages, show
|
||||
ed.hide = 0;
|
||||
ed.multiline = 0;
|
||||
ed.cursor = 0;
|
||||
//fillrect(vid_buf, -1, -1, XRES, 220, 0, 0, 0, 190);
|
||||
memcpy(old_buf,vid_buf,(XRES+BARSIZE)*YRES*PIXELSIZE);
|
||||
fillrect(old_buf, -1, -1, XRES, 220, 0, 0, 0, 190);
|
||||
|
||||
currentcommand2 = malloc(sizeof(command_history));
|
||||
memset(currentcommand2, 0, sizeof(command_history));
|
||||
currentcommand2->prev_command = last_command2;
|
||||
currentcommand2->command = mystrdup(error);
|
||||
last_command2 = currentcommand2;
|
||||
cc = 0;
|
||||
while(cc < 80){
|
||||
fillrect(old_buf, -1, -1+cc, XRES+BARSIZE, 2, 0, 0, 0, 160-(cc*2));
|
||||
cc++;
|
||||
}
|
||||
while (!sdl_poll())
|
||||
{
|
||||
b = SDL_GetMouseState(&mx, &my);
|
||||
@ -3866,11 +3878,10 @@ char *console_ui(pixel *vid_buf,char error[255]) { //TODO: error messages, show
|
||||
|
||||
//clearrect(vid_buf, 0, 0, XRES+BARSIZE, 220);//anyway to make it transparent?
|
||||
memcpy(vid_buf,old_buf,(XRES+BARSIZE)*YRES*PIXELSIZE);
|
||||
draw_line(vid_buf, 1, 219, XRES, 219, 228, 228, 228, XRES+BARSIZE);
|
||||
drawtext(vid_buf, 100, 15, "Welcome to The Powder Toy console v.3 (by cracker64)\n"
|
||||
"Current commands are quit, set, reset, load, create, file, kill, sound\n" //TODO: help command
|
||||
,255, 187, 187, 255);
|
||||
|
||||
draw_line(vid_buf, 0, 219, XRES+BARSIZE-1, 219, 228, 228, 228, XRES+BARSIZE);
|
||||
drawtext(vid_buf, 15, 15, "Welcome to The Powder Toy console v.3 (by cracker64, python by Doxin)" //TODO: help command
|
||||
,255, 255, 255, 255);
|
||||
|
||||
cc = 0;
|
||||
currentcommand = last_command;
|
||||
while(cc < 10)
|
||||
@ -3893,9 +3904,36 @@ char *console_ui(pixel *vid_buf,char error[255]) { //TODO: error messages, show
|
||||
break;
|
||||
}
|
||||
}
|
||||
cc = 0;
|
||||
currentcommand2 = last_command2;
|
||||
while(cc < 10)
|
||||
{
|
||||
if(currentcommand2==NULL)
|
||||
break;
|
||||
drawtext(vid_buf, 215, 175-(cc*12), currentcommand2->command, 255, 225, 225, 255);
|
||||
if(currentcommand2->prev_command!=NULL)
|
||||
{
|
||||
if(cc<9) {
|
||||
currentcommand2 = currentcommand2->prev_command;
|
||||
} else if(currentcommand2->prev_command!=NULL) {
|
||||
free(currentcommand2->prev_command);
|
||||
currentcommand2->prev_command = NULL;
|
||||
}
|
||||
cc++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(error)
|
||||
drawtext(vid_buf, 15, 190, error,255, 187, 187, 255);
|
||||
//if(error && ed.str[0]=='\0')
|
||||
//drawtext(vid_buf, 20, 207, error, 255, 127, 127, 200);
|
||||
if(console_more==0)
|
||||
drawtext(vid_buf, 5, 207, ">", 255, 255, 255, 240);
|
||||
else
|
||||
drawtext(vid_buf, 5, 207, "...", 255, 255, 255, 240);
|
||||
|
||||
ui_edit_draw(vid_buf, &ed);
|
||||
ui_edit_process(mx, my, b, &ed);
|
||||
sdl_blit(0, 0, (XRES+BARSIZE), YRES+MENUSIZE, vid_buf, (XRES+BARSIZE));
|
||||
|
1103
src/main.c
1103
src/main.c
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user