NXC code to NBC code

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC code to NBC code

Post by muntoo »

nxtboyiii wrote:Aren't you supposed to add CopyBits to the gmtilesb.ric(you have not copybits in it!).
Not for RIC fonts... There must be something wrong with the RIC I posted, I guess.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC code to NBC code

Post by spillerrec »

The RIC file is fine, however the code is not. The limitation with RIC fonts is that you must not use 0 since it is working with NULL-terminated strings. However it is the first value you assign it in the for loop...
There should be no issue with adding an CopyBits to the RIC font, in other words you can include it so it can be used with both FontTextOut() and GraphicOutEx().
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC code to NBC code

Post by muntoo »

spillerrec wrote:The RIC file is fine, however the code is not. The limitation with RIC fonts is that you must not use 0 since it is working with NULL-terminated strings. However it is the first value you assign it in the for loop...
Ah, I can't believe I didn't think of my string having NULL characters! I'll upload a fixed RIC file later.

In the mean time, try this:

Code: Select all

task main()
{
    string all = "";

    for(byte tile = 1; tile < 28; ++tile)
        all += FlattenVar(tile);

    FontTextOut(0, 64 - 16,
                "gmtilesb.ric", all,
                DRAW_OPT_NORMAL | DRAW_OPT_FONT_DIR_L2RB | DRAW_OPT_FONT_WRAP);

    Wait(2000);
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: NXC code to NBC code

Post by nxtboyiii »

Can someone recreate the gmtilesb.ric I had but make it with VarMaps so I just input 1 value(Not for FontTextOut() but for GraphicOutEx())?

EDIT: Can someone also help me make the for-loop at the end of the task "dostuff"(It goes slow when variable eati equals 3 because there is more code to run)? I really need help.

BTW, here is mapt1.txt, put it on your NXT and put the RIC's and RSO's from the other post on your NXT and compile the entire program I posted and download it to your nxt so you can see what the game is like and how slow it is(thats a run-on sentence!).

ARGH!! Forget all what I said, I made it run super fast because of the VarMaps!

Thanks guys! :D
Attachments
mapt1.zip
(317 Bytes) Downloaded 191 times
Thanks, and have a nice day,
nxtboy III

programnxt.com
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: NXC code to NBC code

Post by nxtboyiii »

Hi again,

My code is going slow! Specifically the for-loop at the end of the task dostuff.
Can someone please help me make it run faster?

Here's the code(Just look for the commented stars to know where I need help):

Code: Select all


struct brk
{
    byte p;
    bool sld;
// byte alv;
// char T;
};
struct enemy
{
    int xxe;
    int yye;
    float fex;
    float fey;
    bool eli;
    byte eat;
    char sc;
    int ex;
    int ey;
    int pex;
    int pey;
    int mex;
    int mey;
    bool U;
};

#download "gmtilesb.ric"
#download "Platformer!!!!\guy2.ric"
#download "Platformer!!!!\T.ric"
#download "Platformer!!!!\B.ric"
#download "Platformer!!!!\R.ric"
#download "Platformer!!!!\mjump2.rso"
#download "Platformer!!!!\crack.rso"
#download "Platformer!!!!\ld.ric"
#download "Platformer!!!!\Mario Theme Son.rmd"
#download "C:\Users\Lucas\Documents\Users\Lucas\Documents\castle_001.rmd"
#define l2 80
#define r2 64
int EM;
const int MAXEM=10;
enemy emy[MAXEM];
brk map[][];
unsigned long t0;
bool bu;
/*int xxe[MAXEM];
int yye[MAXEM];
float fex[MAXEM];
float fey[MAXEM];
bool eli[MAXEM];
byte eat[MAXEM];
char sc[MAXEM];
int ex[MAXEM];
int ey[MAXEM];
int pex[MAXEM];
int pey[MAXEM];
int mex[MAXEM];
int mey[MAXEM];
bool U[MAXEM];*/
bool ong;
string estr="";
float fmapx,fmapy;
int mapx,mapy;
bool gl;
int x,y;
byte paddx,paddy;
int px,py;
int xx;
int yy;
int xadd,yadd;
bool s,done,jumping;
int xpp,ypp;
byte height=11;
byte width=15;
byte var[2];
byte yj;
int life;
byte prx,pry,pw,ph;
byte vare[1];
byte gvar[1];
byte eati;
byte eati2;
bool sldc;
int mxc,myc;
string sf="";
//byte ric_gmtilesb_data[];

#define EPSILON 0.125 // Make this "as small as possible", and make sure it's a power of 2.
// Try 0.015625, but that may fail with larger numbers.

inline long myfloor(float x)
{
    return(x + EPSILON);
}

/*void ric_init()
{
    byte handle;
    unsigned int fsize;

    OpenFileRead("gmtilesb.ric", fsize, handle);
    ReadBytes(handle, fsize, ric_gmtilesb_data);
    CloseFile(handle);
}*/

bool OnTile(int pointx, int pointy, int paddx2, int paddy2, byte ptile)
{
    return((map[pointx][pointy].p == ptile) || (map[pointx+paddx2][pointy+paddy2].p == ptile) || (map[pointx][pointy+paddy2].p == ptile) || (map[pointx+paddx2][pointy].p == ptile));
}
bool OnSLDTile(int pointx, int pointy, int paddx2, int paddy2, byte ptile)
{
    return((map[pointx][pointy].sld == ptile) || (map[pointx+paddx2][pointy+paddy2].sld == ptile) || (map[pointx][pointy+paddy2].sld == ptile) || (map[pointx+paddx2][pointy].sld == ptile));
}
bool OnTextTile(string text, char v1, char v2, char v3, byte on)
{
    char dat[3];
    StrToByteArray(text,dat);
    return(v1 == dat[0] && v2 == dat[1] && v3 == dat[2]);
}
bool OnETile(int pa,int pox, int poy)
{
    return(map[pox][poy].p == pa);
}

void CheckPY(enemy & em[], byte p)
{
                     if(em[p].fey == floor(em[p].fey))
                    {
                        em[p].pey=0;
                    }
                    else
                    {
                        em[p].pey=1;
                    }
}
void CheckPX(enemy & em[], byte p)
{
                     if(em[p].fex == floor(em[p].fex))
                    {
                        em[p].pex=0;
                    }
                    else
                    {
                        em[p].pex=1;
                    }
}

void DrawEMY()
{
    for(int e=0; e < EM; e++)
    {
        eati2=emy[e].eat;
        if(sldc >= 3 && emy[e].ex <= 100  && emy[e].ex >= 0 && emy[e].ey >= -32 && ((emy[e].ey <= 64 && eati2 == 3) || ((eati2 == 4 || eati2 == 5) && emy[e].ey <= 100)) && emy[e].eli == 1)
        {
            if(eati2 == 3)
            {
                estr="T.ric";
            }
            if(eati2 == 4)
            {
                estr="R.ric";
            }
            if(eati2 == 5)
            {
                estr="B.ric";
            }
            GraphicOut(emy[e].ex,emy[e].ey,estr);
        }
    }
}

void amove(int xp,int yp)
{
    s=0;
    until(done);
    xadd+=xp;
    yadd+=yp;
    s=1;
    until(!done);
}

void LoadMap()
{
    byte handle;
    byte fsize=1500;
    string hei="";
    string wid="";
    byte hon,won;
    string line="";
    byte data[300];
    int ccx,ccy;
    int con;
    char vv1,vv2,vv3;
    int eex,eey,eon;
    OpenFileRead("mapt1.txt",fsize,handle);
    ReadLnString(handle,wid);
    width=StrToNum(wid);
    ReadLnString(handle,hei);
    height=StrToNum(hei);
    ReadLnString(handle,sf);
    ccy=height-1;
    byte tmp[height];
    ArrayInit(map,tmp,width);
    for(int c=0; c < width; c++)
    {
        for(int d=0; d < height; d++)
        {
            map[c][d].p=3;
            map[c][d].sld=0;
        }
    }
    eex=0;
    eey=(height-1)*16;
    until(hon == height)
    {
        ReadLnString(handle,line);
        StrToByteArray(line,data);
        until(won == width)
        {
            vv1=data[con];
            vv2=data[con+1];
            vv3=data[con+2];
            if(OnTextTile("SEK",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=0;
                map[ccx][ccy].sld=3;
                emy[eon].xxe=ccx*100;
                emy[eon].yye=ccy*100;
                emy[eon].fex=ccx;
                emy[eon].fey=ccy;
                emy[eon].ex=eex;
                emy[eon].ey=eey;
                emy[eon].mex=ccx;
                emy[eon].mey=ccy;
                emy[eon].sc='N';
                emy[eon].eli=1;
                emy[eon].eat=3;
                eon++;
                EM++;
            }
            if(OnTextTile("SPI",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=0;
                map[ccx][ccy].sld=5;
                emy[eon].xxe=ccx*100;
                emy[eon].yye=ccy*100;
                emy[eon].fex=ccx;
                emy[eon].fey=ccy;
                emy[eon].ex=eex;
                emy[eon].ey=eey;
                emy[eon].mex=ccx;
                emy[eon].mey=ccy;
                emy[eon].sc='N';
                emy[eon].eli=1;
                emy[eon].eat=5;
                eon++;
                EM++;
            }
            if(OnTextTile("ROK",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=0;
                map[ccx][ccy].sld=4;
                emy[eon].xxe=ccx*100;
                emy[eon].yye=ccy*100;
                emy[eon].fex=ccx;
                emy[eon].fey=ccy;
                emy[eon].ex=eex;
                emy[eon].ey=eey;
                emy[eon].mex=ccx;
                emy[eon].mey=ccy;
                emy[eon].sc='N';
                emy[eon].eli=1;
                emy[eon].eat=4;
                eon++;
                EM++;
            }
            if(OnTextTile("BRK",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=1;
                map[ccx][ccy].sld=1;
            }
            if(OnTextTile("LDR",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=4;
            }
            if(OnTextTile("STN",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=12;
                map[ccx][ccy].sld=1;
            }
            if(OnTextTile("FIR",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=2;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("QES",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=25;
                map[ccx][ccy].sld=1;
            }
            if(OnTextTile("UNS",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=26;
                map[ccx][ccy].sld=1;
            }
            if(OnTextTile("CBR",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=27;
                map[ccx][ccy].sld=1;
            }
            if(OnTextTile("BLO",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=23;
                map[ccx][ccy].sld=1;
            }
            if(OnTextTile("CSN",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=13;
                map[ccx][ccy].sld=2;
            }
            if(OnTextTile("BBK",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=5;
                map[ccx][ccy].sld=2;
            }
            if(OnTextTile("BLK",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=6;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("GRY",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=9;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("TNT",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=7;
                map[ccx][ccy].sld=2;
            }
            if(OnTextTile("GRV",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=8;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("WTR",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=10;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("WTP",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=11;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("END",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=14;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("BAG",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=15;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("GRS",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=16;
                map[ccx][ccy].sld=2;
            }
            if(OnTextTile("DRT",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=17;
                map[ccx][ccy].sld=2;
            }
            if(OnTextTile("LEV",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=18;
                map[ccx][ccy].sld=2;
            }
            if(OnTextTile("TRE",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=19;
                map[ccx][ccy].sld=2;
            }
            if(OnTextTile("LAV",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=20;
                map[ccx][ccy].sld=0;
            }
            if(OnTextTile("LAB",vv1,vv2,vv3,con))
            {
                map[ccx][ccy].p=21;
                map[ccx][ccy].sld=0;
            }
            con+=4;
            won++;
            ccx++;
            eex+=16;
        }
        eex=0;
        eey-=16;
        ccx=0;
        ccy--;
        hon++;
        won=0;
        con=0;
    }
    CloseFile(handle);
}

task draw()
{
// Drawing stuff
    DrawGraphicType dgArgs2;
    DrawGraphicType dgArgs;
// This part IS necessary.
    dgArgs.Filename = "gmtilesb.ric";
    ArrayInit(dgArgs.Variables, 0, 2);
    dgArgs.Options = 0;
    dgArgs2.Filename = "guy2.ric";
    ArrayInit(dgArgs2.Variables, 0, 1);
    dgArgs2.Options = 0;
    int cen,den;
    while(1)
    {
        until(s);
        done=0;
        cen=ceil(xadd/-16)+6;
        den=ceil(yadd/-16)+4;
            dgArgs2.Location.X=x;
            dgArgs2.Location.Y=y;
        for(int c=floor(xadd/-16); c < cen; c++)
        {
            for(int d=floor(yadd/-16); d < den; d++)
            {
                dgArgs.Location.X = (c << 4)+xadd;
                dgArgs.Location.Y = (d << 4)+yadd;
                dgArgs.Variables[0] = map[c][d].p;
                SysDrawGraphic(dgArgs);
            }
            dgArgs2.Variables[0]=gvar[0];
            SysDrawGraphic(dgArgs2);
        }
        RectOut(96,0,4,64,DRAW_OPT_FILL_SHAPE | DRAW_OPT_CLEAR);
        DrawEMY();
        done=1;
    }
}

void DieScreen(string s1, string s2)
{
         StopTask(draw);
        ClearScreen();
        TextOut(0,LCD_LINE1,s1);
        TextOut(0,LCD_LINE2,s2);
        Wait(1000);
        StopAllTasks();
}

void Tile()
{
    if(OnTile(mapx,mapy,paddx,paddy,2))
    {
        life-=10;
    }
    if(map[mapx+paddx][mapy-1].sld == 3 || map[mapx][mapy-1].sld == 3 || (map[mapx-1][mapy+paddy].sld == 3 && fmapx == floor(fmapx)) || (map[mapx-1][mapy].sld == 3 && fmapx == floor(fmapx)) || map[mapx+1][mapy+paddy].sld == 3 || map[mapx+1][mapy].sld == 3)
    {
        life-=20;
    }
    if(OnSLDTile(mapx,mapy,paddx,paddy,5))
    {
     DieScreen("You were killed by","a spiked ball!");
    }
    if(map[mapx][mapy].sld == 4 || map[mapx+paddx][mapy].sld == 4 || map[mapx][mapy+paddy].sld == 4 || map[mapx+paddx][mapy+paddy].sld == 4)
    {
     DieScreen("You were killed by","a spiked turtle!");
    }
    if(OnTile(mapx,mapy,paddx,paddy,14))
    {
        //Display winning screen or go to next level
        DieScreen("You WIN!","");
    }
    if((OnETile(26,mapx,mapy-1) || OnETile(26,mapx+paddx,mapy-1)) && fmapy == floor(fmapy) && bu == 0)
    {
        t0=CurrentTick()/1000;
        bu=1;
    }
    if(OnETile(26,mapx,mapy-1) && fmapy == floor(fmapy) && CurrentTick()/1000-t0 >= 1 && bu == 1)
    {
        //PlayFileEx("crack.rso",Volume(),0);
        map[mapx][mapy-1].sld=0;
        map[mapx][mapy-1].p=3;
        bu=0;
    }
    if(OnETile(26,mapx+paddx,mapy-1) && fmapy == floor(fmapy) && CurrentTick()/1000-t0 >= 1 && bu == 1)
    {
        //PlayFileEx("crack.rso",Volume(),0);
        map[mapx+paddx][mapy-1].sld=0;
        map[mapx+paddx][mapy-1].p=3;
        bu=0;
    }
}

task dostuff()
{
 int ps,ds;
    while(1)
    {
        if(fmapx == floor(fmapx))
        {
            paddx=0;
        }
        else
        {
            paddx=1;
        }
        if((ButtonPressed(BTNCENTER,0) && jumping == 0 && map[mapx][mapy+1].sld == 0 && map[mapx+paddx][mapy+1].sld == 0 && yj < 16) || (jumping == 1 && !OnTile(mapx,mapy,paddx,paddy,4)))
        {
            if(SENSOR_2)
            {
                StopAllTasks();
            }
            if((gvar[0] == 0 || gvar[0] == r2) && !OnTile(mapx,mapy,paddx,paddy,4))
            {
                gvar[0]=32;
            }
            if((gvar[0] == 16 || gvar[0] == l2) && !OnTile(mapx,mapy,paddx,paddy,4))
            {
                gvar[0]=48;
            }
            ong=0;
            yy+=25;
            fmapy=yy/100.0;
            if(yadd/-16 < height-4)
            {
                ypp+=-4;
            }
            else
            {
                y+=4;
                ypp=0;
            }
            jumping=1;
            yj+=4;
        }
        if(yj >= 16)
        {
            jumping=0;
        }
        mapy=floor(fmapy);
        mapx=floor(fmapx);
        if(fmapy == floor(fmapy))
        {
            paddy=0;
        }
        else
        {
            paddy=1;
        }
        paddx=fmapx == floor(fmapx)?0:1;
/*        if((map[mapx][mapy+1].sld >= 1 || map[mapx+paddx][mapy+1].sld >= 1) && fmapy == floor(fmapy))
        {
            jumping=0;
            yj=16;
        }*/
        if(ButtonPressed(BTNLEFT,0))
        {
            if(gvar[0] == 16 && jumping == 0)
            {
                gvar[0]=l2;
            }
            else if(gvar[0] == l2 && jumping == 0)
            {
                gvar[0]=16;
            }
            else if(jumping == 0)
            {
                gvar[0]=16;
            }
        }
        if(ButtonPressed(BTNLEFT,0) && ((map[mapx-1][mapy].sld == 0 && map[mapx-1][mapy+paddy].sld == 0) || fmapx > floor(fmapx)))
        {
            xx-=25;
            fmapx=xx/100.0;
            if(x == 16)
            {
                xpp+=4;
            }
            else
            {
                x-=4;
                xpp=0;
            }
//   amove(xpp,0);
        }
        mapy=floor(fmapy);
        mapx=floor(fmapx);
        if(fmapy == floor(fmapy))
        {
            paddy=0;
        }
        else
        {
            paddy=1;
        }

        if(ButtonPressed(BTNRIGHT,0))
        {
            if(gvar[0] == 0 && jumping == 0)
            {
                gvar[0]=r2;
            }
            else if(gvar[0] == r2 && jumping == 0)
            {
                gvar[0]=0;
            }
            else if(jumping == 0)
            {
                gvar[0]=0;
            }
        }
        if(ButtonPressed(BTNRIGHT,0) && map[mapx+1][mapy].sld == 0 && map[mapx+1][mapy+paddy].sld == 0)
        {
            xx+=25;
            fmapx=xx/100.0;
            if(xadd/-16 < width-6)
            {
                xpp+=-4;
            }
            else
            {
                x+=4;
                xpp=0;
            }
        }
        mapy=floor(fmapy);
        mapx=floor(fmapx);
        if(fmapx == floor(fmapx))
        {
            paddx=0;
        }
        else
        {
            paddx=1;
        }
        if(SENSOR_1)
        {
            if(ButtonPressed(BTNEXIT,0))
            {
                px=0;
                py=-1;
                prx=x;
                pry=y;
                pw=2;
                ph=5;
            }
            else if(ButtonPressed(BTNLEFT,0))
            {
                px=-1;
                py=0;
                prx=x;
                pry=y;
                pw=5;
                ph=2;
            }
            else if(ButtonPressed(BTNRIGHT,0))
            {
                px=1;
                py=0;
                prx=x+11;
                pry=y;
                pw=5;
                ph=2;
            }
            else if(ButtonPressed(BTNCENTER,0))
            {
                px=0;
                py=1;
                prx=x;
                pry=y+11;
                pw=2;
                ph=5;
            }
            else
            {
                goto endp;
            }
            if(map[mapx+px][mapy+py].sld >= 2)
            {
                repeat(300)
                {
                    RectOut(prx,pry,pw,ph,DRAW_OPT_FILL_SHAPE);
                    Wait(1);
                }
                if(map[mapx+px][mapy+py].sld >= 3)
                {
                    if(map[mapx+px][mapy+py].sld == 3)
                    {
                        estr="T.ric";
                    }
                    for(vare[0]=16; vare[0] < 80; vare[0]+=16)
                    {
                        repeat(50)
                        {
                            //GraphicOutEx(xg,yl,"guy2.ric",varg);
                            GraphicOutEx(x+(px*16),y+(py*16),estr,vare);
                            Wait(1);
                        }
                    }
                    for(int e=0; e < EM; e++)
                    {
                        if(emy[e].mex == mapx+px && emy[e].mey == mapy+py)
                        {
                            emy[e].eli=0;
                        }
                    }
                    for(int c=mapx+px-1; c < mapx+px+2; c++)
                    {
                        for(int d=mapy+py-1; d < mapy+py+2; d++)
                        {
                            if(OnETile(1,c,d))
                            {
                                map[c][d].sld=1;
                            }
                            if(OnETile(25,c,d))
                            {
                                map[c][d].sld=1;
                            }
                            if(OnETile(27,c,d))
                            {
                                map[c][d].sld=1;
                            }
                            if(OnETile(23,c,d))
                            {
                                map[c][d].sld=1;
                            }
                            if(OnETile(4,c,d))
                            {
                                map[c][d].sld=2;
                            }
                            if(OnETile(3,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(6,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(9,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(2,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(24,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(7,c,d))
                            {
                                map[c][d].sld=2;
                            }
                            if(OnETile(4,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(8,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(10,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(11,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(12,c,d))
                            {
                                map[c][d].sld=1;
                            }
                            if(OnETile(13,c,d))
                            {
                                map[c][d].sld=2;
                            }
                            if(OnETile(15,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(14,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(17,c,d))
                            {
                                map[c][d].sld=2;
                            }
                            if(OnETile(16,c,d))
                            {
                                map[c][d].sld=2;
                            }
                            if(OnETile(19,c,d))
                            {
                                map[c][d].sld=2;
                            }
                            if(OnETile(18,c,d))
                            {
                                map[c][d].sld=2;
                            }
                            if(OnETile(19,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(20,c,d))
                            {
                                map[c][d].sld=0;
                            }
                            if(OnETile(21,c,d))
                            {
                                map[c][d].sld=0;
                            }
                        }
                    }
                }
                else
                {
                    map[mapx+px][mapy+py].p=3;
                }
                map[mapx+px][mapy+py].sld=0;
                for(int e=0; e < EM; e++)
                {
                    if(emy[e].eli)
                    {
                        map[emy[e].mex][emy[e].mey].sld=emy[e].eat;
                    }
                }
            }
        }
endp:
        if((jumping == 0 && (!OnTile(mapx,mapy,paddx,paddy,4) || (ButtonPressed(BTNEXIT,0) && OnTile(mapx,mapy,paddx,paddy,4)))) && ((map[mapx][mapy-1].sld == 0 && map[mapx+paddx][mapy-1].sld == 0) || fmapy > floor(fmapy)))
        {
            jumping=0;
            yy-=25;
            ong=0;
            fmapy=yy/100.0;
            if(y == 16)
            {
                ypp+=4;
            }
            else
            {
                y-=4;
                ypp=0;
            }
        }
        s=0;
        mapy=floor(fmapy);
        mapx=floor(fmapx);
        if((map[mapx][mapy-1].sld >= 1 || map[mapx+paddx][mapy-1].sld >= 1 || OnTile(mapx,mapy,paddx,paddy,4)) && (paddy == 0 || OnTile(mapx,mapy,paddx,paddy,4)) && ong == 0)
        {
            if(gvar[0] == 32)
            {
                gvar[0]=0;
            }
            if(gvar[0] == 48)
            {
                gvar[0]=16;
            }
            ong=1;
            yj=0;
            jumping=0;
        }
        Tile();
//********************************************************************************************************************
        for(int c=0; c < EM; c++)
        {
            eati=emy[c].eat;
            emy[c].ex+=xpp;
            emy[c].ey+=ypp;
            sldc=map[emy[c].mex][emy[c].mey].sld;
            if(emy[c].eli == 1)
            {
                if(sldc >= 3 && emy[c].ex <= 100  && emy[c].ex >= 0 && emy[c].ey >= -32 && ((emy[c].ey <= 64 && eati == 3) || ((eati == 4 || eati == 5) && emy[c].ey <= 100)))
                {
                    switch(eati)
                    {
                     case 3:
                     estr="T.ric";
                     break;
                     case 4:
                     estr="R.ric";
                     break;
                     case 5:
                     estr="B.ric";
                     break;
                    }
                    CheckPY(emy,c);
                    mxc=emy[c].mex;
                    myc=emy[c].mey;
                    if(map[mxc-1][myc].sld == 0 || emy[c].sc == 'L')
                    {
                    if((mxc > mapx && map[mxc-1][myc].sld == 0  && eati != 4 && eati != 5 && map[mxc-1][myc+emy[c].pey].sld == 0 && emy[c].sc == 'N' && mxc-1 != mapx && mxc-1 != mapx+paddx) || emy[c].sc == 'L')
                    {
                        emy[c].ex-=4;
                        emy[c].xxe-=25;
                        emy[c].fex=emy[c].xxe/100.0;
                        emy[c].sc='L';
                    }
                    }
                    emy[c].mex=floor(emy[c].fex);
                    mxc=emy[c].mex;
                    myc=emy[c].mey;
                    CheckPY(emy,c);
                    if(map[mxc+1][myc].sld == 0 || emy[c].sc == 'R')
                    {
                    if((mxc < mapx && map[mxc+1][myc].sld == 0 && eati != 4 && eati != 5 && map[mxc+1][myc+emy[c].pey].sld == 0 && emy[c].sc == 'N' && mxc+1 != mapx) || emy[c].sc == 'R')
                    {
                        emy[c].ex+=4;
                        emy[c].xxe+=25;
                        emy[c].fex=emy[c].xxe/100.0;
                        emy[c].sc='R';
                    }
                    }
                    emy[c].mex=floor(emy[c].fex);
                    mxc=emy[c].mex;
                    myc=emy[c].mey;
                    CheckPX(emy,c);
                    if(map[mxc][myc-1].sld == 0 || emy[c].sc == 'D')
                    {
                    if(( ((map[mxc][myc-1].sld == 0 && map[mxc+emy[c].pex][myc-1].sld == 0)) && emy[c].sc == 'N' && (eati != 5 || (eati == 5 && emy[c].U == 0)) && ((myc-1 != mapy && myc-1 != mapy+paddy && eati == 3) || eati == 4 || eati == 5)) || emy[c].sc == 'D')
                    {
                        emy[c].ey-=4;
                        emy[c].yye-=25;
                        emy[c].fey=emy[c].yye/100.0;
                        emy[c].sc='D';
                    }
                    }
                    emy[c].mey=floor(emy[c].fey);
                    mxc=emy[c].mex;
                    myc=emy[c].mey;
                    if(eati == 5)
                    {
                    if(map[emy[c].mex][emy[c].mey-1].sld >= 1 && emy[c].fey == floor(emy[c].fey))
                    {
                        switch(emy[c].sc)
                        {
                        case 'U':
                            map[mxc][myc-1].sld=0;
                            map[mxc][myc].sld=eati;
                            emy[c].sc='N';
                            break;
                        case 'D':
                            map[mxc][myc+1].sld=0;
                            map[mxc][myc].sld=eati;
                            emy[c].sc='N';
                            break;
                        }
                        emy[c].U=1;
                    }
                    }
                    if(((eati == 5 && emy[c].U == 1) && emy[c].sc == 'N') || emy[c].sc == 'U')
                    {
                        emy[c].ey+=4;
                        emy[c].yye+=25;
                        emy[c].fey=emy[c].yye/100.0;
                        emy[c].sc='U';
                    }
                    emy[c].mex=floor(emy[c].fex);
                    emy[c].mey=floor(emy[c].fey);
                    mxc=emy[c].mex;
                    myc=emy[c].mey;
                    if(map[emy[c].mex][emy[c].mey+1].sld >= 1 && emy[c].fey == floor(emy[c].fey) && eati == 5)
                    {
                        switch(emy[c].sc)
                        {
                        case 'U':
                            map[mxc][myc-1].sld=0;
                            map[mxc][myc].sld=eati;
                            emy[c].sc='N';
                            break;
                        case 'D':
                            map[mxc][myc+1].sld=0;
                            map[mxc][myc].sld=eati;
                            emy[c].sc='N';
                            break;
                        }
                        emy[c].U=0;
                    }
                    map[emy[c].mex][emy[c].mey].sld=eati;
                    mxc=emy[c].mex;
                    myc=emy[c].mey;
                    if(emy[c].fex == floor(emy[c].fex))
                    {
                        switch(emy[c].sc)
                        {
                        case 'L':
                            map[mxc+1][myc].sld=0;
                            map[mxc][myc].sld=eati;
                            emy[c].sc='N';
                            break;
                        case 'R':
                            map[mxc-1][myc].sld=0;
                            map[mxc][myc].sld=eati;
                            emy[c].sc='N';
                            break;
                        }
                    }
                    if(emy[c].fey == floor(emy[c].fey))
                    {
                        switch(emy[c].sc)
                        {
                        case 'U':
                            map[mxc][myc-1].sld=0;
                            map[mxc][myc].sld=eati;
                            emy[c].sc='N';
                            break;
                        case 'D':
                            map[mxc][myc+1].sld=0;
                            map[mxc][myc].sld=eati;
                            emy[c].sc='N';
                            break;
                        }
                    }
                    if(eati == 4 && map[emy[c].mex][emy[c].mey-1].sld <= 2 && map[emy[c].mex][emy[c].mey-1].sld > 0 && emy[c].fey == floor(emy[c].fey))
                    {
                        emy[c].eli=0;
                        map[emy[c].mex][emy[c].mey].sld=0;
                    }
                    GraphicOut(emy[c].ex,emy[c].ey,estr);
                }
            }
        }
//********************************************************************************************************************
        until(done);
        xadd+=xpp;
        yadd+=ypp;
        s=1;
        until(!done);
        xpp=0;
        ypp=0;
    }
}

task main()
{
    SetAbortFlag(BTNSTATE_NONE);
    SetSensorTouch(S1);
    SetSensorTouch(S2);
    mapx=1;
    mapy=1;
    xx=100;
    yy=100;
    fmapx=1;
    fmapy=1;
    x=16;
    y=16;
    s=1;
    GraphicOut(0,0,"ld.ric");
    LoadMap();
    life=300;
    PlayFileEx(sf,Volume(),1);
    Precedes(draw,dostuff);
}

Thanks,
nxtboy III
Thanks, and have a nice day,
nxtboy III

programnxt.com
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC code to NBC code

Post by muntoo »

So you want us to work on this:

Code: Select all

for (int c = 0; c < EM; c++)
{
    eati = emy[c].eat;
    emy[c].ex += xpp;
    emy[c].ey += ypp;
    sldc = map[emy[c].mex][emy[c].mey].sld;
    if (emy[c].eli == 1)
    {
        if (sldc >= 3 && emy[c].ex <= 100 && emy[c].ex >= 0 && emy[c].ey >= -32 && ((emy[c].ey <= 64 && eati == 3) || ((eati == 4 || eati == 5) && emy[c].ey <= 100)))
        {
            switch (eati)
            {
            case 3:
                estr = "T.ric";
                break;
            case 4:
                estr = "R.ric";
                break;
            case 5:
                estr = "B.ric";
                break;
            }
            CheckPY(emy, c);
            mxc = emy[c].mex;
            myc = emy[c].mey;
            if (map[mxc - 1][myc].sld == 0 || emy[c].sc == 'L')
            {
                if ((mxc > mapx && map[mxc - 1][myc].sld == 0 && eati != 4 && eati != 5 && map[mxc - 1][myc + emy[c].pey].sld == 0 && emy[c].sc == 'N' && mxc - 1 != mapx && mxc - 1 != mapx + paddx) || emy[c].sc == 'L')
                {
                    emy[c].ex -= 4;
                    emy[c].xxe -= 25;
                    emy[c].fex = emy[c].xxe / 100.0;
                    emy[c].sc = 'L';
                }
            }
            emy[c].mex = floor(emy[c].fex);
            mxc = emy[c].mex;
            myc = emy[c].mey;
            CheckPY(emy, c);
            if (map[mxc + 1][myc].sld == 0 || emy[c].sc == 'R')
            {
                if ((mxc < mapx && map[mxc + 1][myc].sld == 0 && eati != 4 && eati != 5 && map[mxc + 1][myc + emy[c].pey].sld == 0 && emy[c].sc == 'N' && mxc + 1 != mapx) || emy[c].sc == 'R')
                {
                    emy[c].ex += 4;
                    emy[c].xxe += 25;
                    emy[c].fex = emy[c].xxe / 100.0;
                    emy[c].sc = 'R';
                }
            }
            emy[c].mex = floor(emy[c].fex);
            mxc = emy[c].mex;
            myc = emy[c].mey;
            CheckPX(emy, c);
            if (map[mxc][myc - 1].sld == 0 || emy[c].sc == 'D')
            {
                if ((((map[mxc][myc - 1].sld == 0 && map[mxc + emy[c].pex][myc - 1].sld == 0)) && emy[c].sc == 'N' && (eati != 5 || (eati == 5 && emy[c].U == 0)) && ((myc - 1 != mapy && myc - 1 != mapy + paddy && eati == 3) || eati == 4 || eati == 5)) || emy[c].sc == 'D')
                {
                    emy[c].ey -= 4;
                    emy[c].yye -= 25;
                    emy[c].fey = emy[c].yye / 100.0;
                    emy[c].sc = 'D';
                }
            }
            emy[c].mey = floor(emy[c].fey);
            mxc = emy[c].mex;
            myc = emy[c].mey;
            if (eati == 5)
            {
                if (map[emy[c].mex][emy[c].mey - 1].sld >= 1 && emy[c].fey == floor(emy[c].fey))
                {
                    switch (emy[c].sc)
                    {
                    case 'U':
                        map[mxc][myc - 1].sld = 0;
                        map[mxc][myc].sld = eati;
                        emy[c].sc = 'N';
                        break;
                    case 'D':
                        map[mxc][myc + 1].sld = 0;
                        map[mxc][myc].sld = eati;
                        emy[c].sc = 'N';
                        break;
                    }
                    emy[c].U = 1;
                }
            }
            if (((eati == 5 && emy[c].U == 1) && emy[c].sc == 'N') || emy[c].sc == 'U')
            {
                emy[c].ey += 4;
                emy[c].yye += 25;
                emy[c].fey = emy[c].yye / 100.0;
                emy[c].sc = 'U';
            }
            emy[c].mex = floor(emy[c].fex);
            emy[c].mey = floor(emy[c].fey);
            mxc = emy[c].mex;
            myc = emy[c].mey;
            if (map[emy[c].mex][emy[c].mey + 1].sld >= 1 && emy[c].fey == floor(emy[c].fey) && eati == 5)
            {
                switch (emy[c].sc)
                {
                case 'U':
                    map[mxc][myc - 1].sld = 0;
                    map[mxc][myc].sld = eati;
                    emy[c].sc = 'N';
                    break;
                case 'D':
                    map[mxc][myc + 1].sld = 0;
                    map[mxc][myc].sld = eati;
                    emy[c].sc = 'N';
                    break;
                }
                emy[c].U = 0;
            }
            map[emy[c].mex][emy[c].mey].sld = eati;
            mxc = emy[c].mex;
            myc = emy[c].mey;
            if (emy[c].fex == floor(emy[c].fex))
            {
                switch (emy[c].sc)
                {
                case 'L':
                    map[mxc + 1][myc].sld = 0;
                    map[mxc][myc].sld = eati;
                    emy[c].sc = 'N';
                    break;
                case 'R':
                    map[mxc - 1][myc].sld = 0;
                    map[mxc][myc].sld = eati;
                    emy[c].sc = 'N';
                    break;
                }
            }
            if (emy[c].fey == floor(emy[c].fey))
            {
                switch (emy[c].sc)
                {
                case 'U':
                    map[mxc][myc - 1].sld = 0;
                    map[mxc][myc].sld = eati;
                    emy[c].sc = 'N';
                    break;
                case 'D':
                    map[mxc][myc + 1].sld = 0;
                    map[mxc][myc].sld = eati;
                    emy[c].sc = 'N';
                    break;
                }
            }
            if (eati == 4 && map[emy[c].mex][emy[c].mey - 1].sld <= 2 && map[emy[c].mex][emy[c].mey - 1].sld > 0 && emy[c].fey == floor(emy[c].fey))
            {
                emy[c].eli = 0;
                map[emy[c].mex][emy[c].mey].sld = 0;
            }
            GraphicOut(emy[c].ex, emy[c].ey, estr);
        }
    }
}
First of all, I can't tell what any of that code does. (I bet I could if I looked at it for an hour, but I'd rather not.) Could you break this down into smaller functions so it's easier to read? And tell us what all those variables stand for, what they mean, etc...

(You'll have to do less work if you break them down into smaller functions, though. task dostuff() should be as small as possible - 30 lines max. And each function should be ~10 lines. The smaller [within reason] the better.)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC code to NBC code

Post by afanofosc »

One thing you should do with NXC, since the underlying firmware does not have real pointers, is minimize the number of times that you read an element from an array or modify an element in an array. Within a loop if you find that you have array[n] more than one time either on the RHS or the LHS of an expression you should grab the element once at the start of the loop and then use it instead of array[n]. If you do modify the element then at the end of the loop you put the modified element back into the array. Like this:

Code: Select all

for (int c=0; i < EM; c++)
{
    enemy E = emy[c]; // grab the element from the array one time
    eati = E.eat;
    E.ex += xpp;
    E.ey += ypp;
    sldc = map[E.mex][E.mey].sld;
    if (E.eli == 1)
    {
        if (sldc >= 3 && 
            E.ex <= 100 && 
            E.ex >= 0 && 
            E.ey >= -32 && 
            ((E.ey <= 64 && eati == 3) || ((eati == 4 || eati == 5) && E.ey <= 100)))
        {
          // etc....
        }
        // etc....
    }
    // etc...
    emy[c] = E; // put the modified struct back into the array
}
Also, if your array is global (such as emy or map) then never pass it into or out of a function. That is to avoid the memory and speed overhead of copying arrays since the underlying firmware has no true reference or pointer type.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: NXC code to NBC code

Post by nxtboyiii »

Thanks afanofosc. That helped a lot! Now it runs a lot faster.
Thanks, and have a nice day,
nxtboy III

programnxt.com
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: NXC code to NBC code

Post by nxtboyiii »

Ok, so I have made the code more easily understood.
How do I make that section faster(the code between the commented stars)?
Here's the code:
Credits:

Game artists: Nintendo, Me, turiqwalrus, sidneys1, zippydee, yunhua98, ashbad, stryker001, my brother,

minecraft game, and xedall2358
Attachments
Mario.zip
(102.2 KiB) Downloaded 192 times
Thanks, and have a nice day,
nxtboy III

programnxt.com
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC code to NBC code

Post by afanofosc »

Could you do me a favor and try your Mario program with the latest BricxCC test release (http://bricxcc.sourceforge.net/test_releases/) with optimization levels 2 and 3 and let me know (post here) if you notice any problems with your program?

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests