Skip to content

Commit

Permalink
Animation and Generation!
Browse files Browse the repository at this point in the history
Best commit ever. Improved animation and made a text creator using
Node.JS!
  • Loading branch information
tomsmeding committed Apr 6, 2014
1 parent a794bbb commit 9697c29
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 27 deletions.
98 changes: 98 additions & 0 deletions genML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env node
function array_init(len,init){
return Array.apply(null,new Array(len)).map(function(){return init;});
}

function genChar(thechar){
var i,a,b,b1,b2,mina,minb,ascii,single;
single=array_init(7,[]);
ascii=thechar.charCodeAt(0);
mina=minb=1e9;
for(a=1;a<=ascii;a++){
for(b=1;b<=ascii;b++){
if(a+b+Math.abs(ascii-a*b)<mina+minb+Math.abs(ascii-mina*minb)){
mina=a;
minb=b;
}
}
}
a=mina;
b=minb;
if(b>a){
a=a+b;
b=a-b;
a=a-b;
}
single[0]=array_init(a," ").concat(" "," ");
single[0][~~(a/2)]=thechar;
single[1]=[")"].concat(array_init(a,"+"));
single[2]=array_init(a+1,"=");
if(b/2%1==0){
b1=b2=b/2;
} else {
b1=Math.ceil(b/2);
b2=Math.floor(b/2);
}
single[3]=[" "].concat(array_init(b1,"+"),"("," ",array_init(a+1-3-b1," "),"<"," ");
single[4]=[" "].concat(array_init(a+1,"=")," ");
//single[4][b1+2]='"';
single[5]=[">"].concat(
array_init(b2,"+"),
")","-","[","!"
);
if(ascii==a*b){
single[5]=single[5].concat(array_init(a-1-b2<0?0:a-1-b2," "));
} else {
single[5]=single[5].concat("(",
array_init(Math.abs(ascii-a*b),ascii>a*b?"+":"-"),
")",
array_init(a-3-b2-Math.abs(ascii-a*b)<0?0:a-3-b2-Math.abs(ascii-a*b)," "),
" "
);
}
single[6]=array_init(single[5].length,"=");
single[6][b2+4]="#";
single[3][b2+4]="<";
single[4][b2+4]='"';
if(single[5].length>single[0].length)single[0]=single[0].concat(array_init(single[5].length-single[0].length," "));
if(single[5].length>single[1].length)single[1]=single[1].concat(array_init(single[5].length-single[1].length," "));
if(single[5].length>single[2].length)single[2]=single[2].concat(array_init(single[5].length-single[2].length," "));
if(single[5].length>single[3].length)single[3]=single[3].concat(array_init(single[5].length-single[3].length," "));
if(single[5].length>single[4].length)single[4]=single[4].concat(array_init(single[5].length-single[4].length," "));
return single;
}

function genLine(line){
var code,single,i;
code=array_init(7,[]);
for(i=0;i<line.length;i++){
single=genChar(line[i]);
code=code.map(function(r,i){
if(i==1)return r.concat(">",single[i]);
if(i==2)return r.concat('"',single[i]);
if(i==5)return r.concat("!",single[i]);
if(i==6)return r.concat("#",single[i]);
return r.concat(" ",single[i]);
});
}
code=code.map(function(r){return r.slice(1);});
code[3]=code[3].concat(" "," ","(","<"," "," "," "," "," ","<"," ");
code[4]=code[4].concat(" "," ","=",'"'," "," ","=","=","=",'"'," ");
code[5]=code[5].concat("(",">","[","!",")",">",".",")","[","!"," ");
code[6]=code[6].concat("=","=","=","#","=","=","=","=","=","#","=");
return code;
}

function lineToStrings(line){
return line.map(function(r){return r.join("");}).join("\n");
}

process.stderr.write("Text? ");
process.stdin.resume();
process.stdin.once("data",function(data){
var code;
data=data.toString().slice(0,-1); //remove the newline
code=genLine(data);
console.log(lineToStrings(code));
process.exit();
});
7 changes: 0 additions & 7 deletions guess.mlg

This file was deleted.

67 changes: 49 additions & 18 deletions mariolang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ This is an interpreter for the language by Tom Smeding.
**********/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <climits>
#include <cmath>
#include <termios.h>
#include <unistd.h>

Expand All @@ -28,6 +30,7 @@ int debuglevel,animatedelay;
bool animate,animateShowTape;

template<class T>class negvector;
class mario;

char getch(){char b=0;struct termios o={0};fflush(stdout);if(tcgetattr(0,&o)<0)
perror("tcsetattr()");o.c_lflag&=~ICANON;o.c_lflag&=~ECHO;o.c_cc[VMIN]=1;o.c_cc[
Expand All @@ -39,12 +42,7 @@ void moveto(int x,int y){
cout<<"\x1B["<<y+1<<";"<<x+1<<"H"<<flush;
}

void drawTape(negvector<int> &mem,int tapex){
static int minidx=INT_MIN,maxidx=INT_MIN;
/*if INT_MIN, then detect bounds
*else, go draw stuff.
*/
}
int max(int a,int b){return a<b?b:a;}

template<class T>class negvector{
vector<T> pos,neg;
Expand All @@ -57,15 +55,54 @@ template<class T>class negvector{
T& operator[](int idx){return idx<0?neg[-idx-1]:pos[idx];}
};

typedef struct{
class mario{
public:
int ipx,ipy,memp,dir;
bool walking,skip;
}mario;
};

class Level{
private:
negvector<int> memory;
vector<string> code;
int outputx,outputy,inputy,tapex;

void drawTape(mario *m){
static int minidx=-1,maxidx=-1;
static bool inited=false;
int i,intwidth;
if(!inited){
inited=true;
minidx=0;
maxidx=0;
}
if(m->memp<minidx)minidx=m->memp;
if(m->memp>maxidx)maxidx=m->memp;
intwidth=max(1+log10(minidx),log10(maxidx));
cout<<"\x1B[34m"<<flush; //blue
for(i=minidx;i<=maxidx;i++){
moveto(tapex,i-minidx);
cout<<"\x1B[0K"<<flush;
cout<<setw(intwidth)<<i<<(m->memp==i?'#':'.')<<' '<<memory[i]<<flush;
}
cout<<"\x1B[0m"<<flush;
moveto(outputx,outputy);
}

void animationFrameStart(int x,int y,char c){
moveto(x,y);
cout<<"\x1B[41;1m"<<c<<"\x1B[0m"<<flush;
moveto(outputx,outputy);
}

void animationFrameEnd(mario *m,int x,int y,char c){
usleep(animatedelay);
moveto(x,y);
cout<<c<<flush;
moveto(outputx,outputy);
if(animateShowTape)drawTape(m);
}

public:
Level(void){Level(cin);}
Level(ifstream &cf){
Expand Down Expand Up @@ -138,9 +175,7 @@ class Level{
char c;
SHOW_DEBUG("ECS at "<<m->ipx<<","<<m->ipy<<": '"<<code[m->ipy][m->ipx]<<"'");
if(animate){
moveto(m->ipx,m->ipy);
cout<<"\x1B[41;1m"<<code[m->ipy][m->ipx]<<"\x1B[0m"<<flush;
moveto(outputx,outputy);
animationFrameStart(m->ipx,m->ipy,code[m->ipy][m->ipx]);
}
//if(animate)cout<<"\x1B[s\x1B["<<m->ipy+1<<";"<<m->ipx+1<<"H\x1B[41;1m"<<code[m->ipy][m->ipx]<<"\x1B[0m\x1B[u"<<flush;
switch(code[m->ipy][m->ipx]){
Expand Down Expand Up @@ -230,11 +265,7 @@ class Level{
break;
}
if(animate){
usleep(animatedelay);
moveto(m->ipx,m->ipy);
cout<<code[m->ipy][m->ipx]<<flush;
moveto(outputx,outputy);
if(animateShowTape)drawTape(memory,tapex);
animationFrameEnd(m,m->ipx,m->ipy,code[m->ipy][m->ipx]);
}
//if(animate)cout<<"\x1B[s\x1B["<<m->ipy+1+(code[m->ipy+1][m->ipx]=='^')<<";"<<m->ipx+1<<"H"<<code[m->ipy+(code[m->ipy+1][m->ipx]=='^')][m->ipx]<<"\x1B[u"<<flush;
return true;
Expand Down Expand Up @@ -271,7 +302,7 @@ int main(int argc,char **argv){
cerr<<"\t 2: Plus one debug line per command."<<endl;
cerr<<"\t 3: Frantic. Useless. Adds even more shit per executed command."<<endl;
cerr<<"\t-T Show the \x1B[46mT\x1B[0mape in animation mode."<<endl;
cerr<<"\t-w usecs Sets the time to \x1B[46mw\x1B[0mait between commands in microseconds if animating. Useless otherwise."<<endl;
cerr<<"\t-w msecs Sets the time to \x1B[46mw\x1B[0mait between commands in milliseconds if animating. Useless otherwise."<<endl;
return 0;
}
debuglevel=0;
Expand All @@ -287,7 +318,7 @@ int main(int argc,char **argv){
if(argv[i][j]=='a')animate=true;
else if(argv[i][j]=='d'){debuglevel=strtol(argv[i+1],NULL,10);skipNextArg=true;}
else if(argv[i][j]=='T')animateShowTape=true;
else if(argv[i][j]=='w'){animatedelay=strtol(argv[i+1],NULL,10);skipNextArg=true;}
else if(argv[i][j]=='w'){animatedelay=strtol(argv[i+1],NULL,10)*1000;skipNextArg=true;}
else {cerr<<"Unrecognised option "<<argv[i][j]<<endl;return 0;}
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions simple.mlg

This file was deleted.

7 changes: 7 additions & 0 deletions text.mlg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
H e l l o W o r l d !
)+++++++++ >)++++++++++ >)++++++++++++ >)++++++++++++ >)+++++++++++ >)++++++++ >)+++++++++++ >)+++++++++++ >)++++++++++++++ >)++++++++++++ >)++++++++++ >)++++++++
========== "=========== "============= "============= "============ "========= "============ "============ "=============== "============= "=========== "=========
++++( < < +++++( < < +++++( < < +++++( < < +++++( < < ++( < < ++++( < < +++++( < < ++++( < < +++++( < < +++++( < < ++( < < (< <
======="== ========"== ======="===== ======="===== ========"=== ====="=== ======="==== ========"=== ======="======= ======="===== ========"== ====="=== =" ==="
>++++)-[! !>+++++)-[!(+) !>++++)-[! !>++++)-[! !>+++++)-[!(+) !>++)-[! !>++++)-[!(-) !>+++++)-[!(+) !>++++)-[!(++) !>++++)-[! !>+++++)-[! !>++)-[!(+) (>[!)>.)[!
========#====#=========#=====#========#=======#========#=======#=========#======#======#=====#========#=======#=========#======#========#==========#========#=======#=========#====#======#=========#=====#=

0 comments on commit 9697c29

Please sign in to comment.