Jump to content

Photo

moosh's npc script - clean version

npc script clean trade sequence

  • Please log in to reply
4 replies to this topic

#1 accela2me

accela2me

    Illustrious

  • Members
  • Real Name:Juliano
  • Location:Minas Gerais, Brazil

Posted 06 September 2015 - 02:05 PM

yo!

can someone help me to clean the npc script (by moosh)? well, when I say "clean" is because I need only the main feature, talk with the (ALWAYS solid) NPC.

so, the arguments Type, Arg1, Arg2, NoSolid and Script are unnecessary.
I tried to do that, but I failed to maintain the solid feature.

I need this script smaller to add functions to use the trade sequences (which will be different for some NPCs).

here:

 

ffc script NPC_Script
{
    void run(int String, int ItemCheck, int Type, int Arg1, int Arg2, int NoSolid, int Script){
        
        //Stores the NPC's combo, hides it, and waits until Link has the required item
        int Combo = this->Data;
        this->Data = CMB_NPC_HIDDEN;
        while(ItemCheck<0&&!Link->Item[Abs(ItemCheck)]){
            Waitframe();
        }
        this->Data = Combo;
        
        //Start facing in the default direction for turning NPCs
        if(Type==NPCBT_FACELINK){
            this->Data = Combo + Arg1;
        }
        
        //Saves the width and height of the FFC for collision checks
        int Width = 16;
        int Height = 16;
        if(this->EffectWidth!=16)
            Width = this->EffectWidth;
        else if(this->TileWidth>1)
            Width = this->TileWidth*16;
        if(this->EffectHeight!=16)
            Height = this->EffectHeight;
        else if(this->TileHeight>1)
            Height = this->TileHeight*16;
        
        //Sets the space below the NPC or the space a guard NPC occupies to be solid
        if(LAYER_NPC>-1&&NoSolid==0){
            if(Type==NPCBT_GUARDH){
                for(int x=Arg1; x<=Arg2+this->TileWidth-1; x++){
                    for(int y=Floor(this->Y/16); y<=Floor(this->Y/16)+this->TileHeight-1; y++){
                        SetLayerComboD(LAYER_NPC, y*16+x, CMB_NPC_SOLID);
                    }
                }
            }
            else if(Type==NPCBT_GUARDV){
                for(int x=Floor(this->X/16); x<=Floor(this->X/16)+this->TileWidth-1; x++){
                    for(int y=Arg1; y<=Arg2+this->TileHeight-1; y++){
                        SetLayerComboD(LAYER_NPC, y*16+x, CMB_NPC_SOLID);
                    }
                }
            }
            else{
                for(int x=Floor(this->X/16); x<=Floor(this->X/16)+this->TileWidth-1; x++){
                    for(int y=Floor(this->Y/16); y<=Floor(this->Y/16)+this->TileHeight-1; y++){
                        SetLayerComboD(LAYER_NPC, y*16+x, CMB_NPC_SOLID);
                    }
                }
            }
        }
        
        while(true){
            //Removes NPCs if Link has the required item
            if(ItemCheck>0&&Link->Item[ItemCheck]){
                this->Data = CMB_NPC_HIDDEN;
                
                if(LAYER_NPC>-1&&NoSolid==0){
                    if(Type==NPCBT_GUARDH){
                        for(int x=Arg1; x<=Arg2+this->TileWidth-1; x++){
                            for(int y=Floor(this->Y/16); y<=Floor(this->Y/16)+this->TileHeight-1; y++){
                                SetLayerComboD(LAYER_NPC, y*16+x, CMB_NPC_HIDDEN);
                            }
                        }
                    }
                    else if(Type==NPCBT_GUARDV){
                        for(int x=Floor(this->X/16); x<=Floor(this->X/16)+this->TileWidth-1; x++){
                            for(int y=Arg1; y<=Arg2+this->TileHeight-1; y++){
                                SetLayerComboD(LAYER_NPC, y*16+x, CMB_NPC_HIDDEN);
                            }
                        }
                    }
                    else{
                        for(int x=Floor(this->X/16); x<=Floor(this->X/16)+this->TileWidth-1; x++){
                            for(int y=Floor(this->Y/16); y<=Floor(this->Y/16)+this->TileHeight-1; y++){
                                SetLayerComboD(LAYER_NPC, y*16+x, CMB_NPC_HIDDEN);
                            }
                        }
                    }
                }
                Quit();
            }
            
            //Handles animation for turning NPCs
            if(Type==NPCBT_FACELINK&&(Link->X>0&&Link->X<240&&Link->Y>0&&Link->Y<160)){
                if(Distance(CenterLinkX(), CenterLinkY(), CenterX(this), CenterY(this))<Arg2)
                    this->Data = Combo + AngleDir4(Angle(CenterX(this), CenterY(this), CenterLinkX(), CenterLinkY()));
                else
                    this->Data = Combo + Arg1;
            }
            //Handles movement for guard NPCs
            else if(Type==NPCBT_GUARDH){
                if(Link->X>16*Arg1-32&&Link->X<16*Arg2+32&&Link->Y>this->Y-32&&Link->Y<this->Y+32){
                    this->X = Clamp(this->X+(-this->X + Link->X)/4, 16*Arg1, 16*Arg2);
                }
            }
            else if(Type==NPCBT_GUARDV){
                if(Link->X>this->X-32&&Link->X<this->X+32&&Link->Y>16*Arg1-32&&Link->Y<16*Arg2+32){
                    this->Y = Clamp(this->Y+(-this->Y + Link->Y)/4, 16*Arg1, 16*Arg2);
                }
            }
            
            //Facing Up
            if(Link->Dir==DIR_UP&&Link->Y>=this->Y&&Link->Y<=this->Y+Height-8&&Link->X>=this->X-8&&Link->X<=this->X+Width-8){
                if(CMB_NPC_CANTALK>0)
                    Screen->FastCombo(LAYER_NPC_CANTALK, Link->X, Link->Y-16, CMB_NPC_CANTALK, CS_NPC_CANTALK, 128);
                if(Link->PressA){
                    Link->InputA = false;
                    Link->PressA = false;
                    Screen->Message(String);
                    if(Script>0){
                        RunFFCScript(Script, 0);
                    }
                }
            }
            //Facing Down
            else if(Link->Dir==DIR_DOWN&&Link->Y>=this->Y-16&&Link->Y<=this->Y+Height-16&&Link->X>=this->X-8&&Link->X<=this->X+Width-8){
                if(CMB_NPC_CANTALK>0)
                    Screen->FastCombo(LAYER_NPC_CANTALK, Link->X, Link->Y-16, CMB_NPC_CANTALK, CS_NPC_CANTALK, 128);
                if(Link->PressA){
                    Link->InputA = false;
                    Link->PressA = false;
                    Screen->Message(String);
                    if(Script>0){
                        RunFFCScript(Script, 0);
                    }
                }
            }
            //Facing Left
            else if(Link->Dir==DIR_LEFT&&Link->Y>=this->Y-8&&Link->Y<=this->Y+Height-8&&Link->X>=this->X&&Link->X<=this->X+Width){
                if(CMB_NPC_CANTALK>0)
                    Screen->FastCombo(LAYER_NPC_CANTALK, Link->X, Link->Y-16, CMB_NPC_CANTALK, CS_NPC_CANTALK, 128);
                if(Link->PressA){
                    Link->InputA = false;
                    Link->PressA = false;
                    Screen->Message(String);
                    if(Script>0){
                        RunFFCScript(Script, 0);
                    }
                }
            }
            //Facing Right
            else if(Link->Dir==DIR_RIGHT&&Link->Y>=this->Y-8&&Link->Y<=this->Y+Height-8&&Link->X>=this->X-16&&Link->X<=this->X+Width-16){
                if(CMB_NPC_CANTALK>0)
                    Screen->FastCombo(LAYER_NPC_CANTALK, Link->X, Link->Y-16, CMB_NPC_CANTALK, CS_NPC_CANTALK, 128);
                if(Link->PressA){
                    Link->InputA = false;
                    Link->PressA = false;
                    Screen->Message(String);
                    if(Script>0){
                        RunFFCScript(Script, 0);
                    }
                }
            }
            Waitframe();
        }
    }
}


#2 Timelord

Timelord

    The Timelord

  • Banned
  • Location:Prydon Academy

Posted 06 September 2015 - 03:53 PM

Do you still want the NPC to move, and do you want the ItemCheck feature?

 

Why exactly are you concerned with its length? Is it the actual ling length, or just the complexity that is a problem? You can move the variables from the run() instruction, into local script variables with hardcoded values, if you need more room for arguments; or you can use one of many tricks for extracting more datum out of the args.


Edited by ZoriaRPG, 06 September 2015 - 03:56 PM.


#3 accela2me

accela2me

    Illustrious

  • Members
  • Real Name:Juliano
  • Location:Minas Gerais, Brazil

Posted 06 September 2015 - 04:16 PM

For the other NPCs of my quest, of course I'm going to use this script.

But for the trade sequences, I need a cleaner script (because my knowledge of zscript is very limited) to add different functions for each NPC.

For example: Moscowmodder once did this script for me:

const int SFX_FREQ_HAMMER = 25; //How often to loop the hammer sound

ffc script TradingNPC_Carpenter
{
    void run(int m1, int m2, int item_in, int item_out, int sfx){
        
        int sfxTimer; //Create the SFX timer
        
        if(Screen->State[ST_SECRET]){
            this->Data = 37; //transparent combo
        }
    
        while(!Screen->State[ST_SECRET]){
            GB_SolidifyFFC(this);
            if(GB_Talk(this, m1, 1, 0, 0)){
                NoMoveAction();
                Waitframe();                
                
                if(Link->Item[item_in]){                    
                                        
                    for(int i = 0; i < 90; i++){
                        Screen->DrawScreen(7, 38, 47, 0, 0, 0);
                        
                        sfxTimer++; //Increment the counter
                        sfxTimer %= SFX_FREQ_HAMMER; //When sfxTimer == FREQ, sfxTimer = 0
                        if (sfxTimer == 0)
                        Game->PlaySound(sfx);
                        
                        Waitframe();
                    }                    
                
                    Link->Item[item_in] = false;
                    Screen->State[ST_SECRET] = true;
                    Screen->TriggerSecrets();
                    Waitframes(10);
                    Game->PlaySound(SFX_SECRET);                    
                    
                    Screen->Message(m2);
                    Waitframe();
                
                    for(int i = 0; i < 30; i++){
                        Screen->DrawScreen(7, 38, 47, 0, 0, 0);
                        Waitframe();
                    }
                
                    item got = Screen->CreateItem(item_out);
                    got->X = this->X;
                    got->Y = this->Y;    
                    got->Pickup=IP_HOLDUP;

                    this->Data = 37; //transparent combo
                }
            }
            WaitPress(0,this);
        }
    }
}

It's one of the NPCs with special features of the trade sequence... and I need to combine it with the NPC Script.

The NPC can be static. What I couldn't do is keep the NPC solid.


Edited by accela2me, 06 September 2015 - 04:17 PM.


#4 Jamian

Jamian

    ZC enthusiast

  • Members

Posted 06 September 2015 - 04:48 PM

For static NPCs you could just use the signpost script.



#5 accela2me

accela2me

    Illustrious

  • Members
  • Real Name:Juliano
  • Location:Minas Gerais, Brazil

Posted 06 September 2015 - 06:03 PM

I think I got to clean the script :)
 

ffc script NPC_Script_CleanVersion
{
    void run(int String, int ItemCheck){


        //Stores the NPC's combo, hides it, and waits until Link has the required item
        int Combo = this->Data;
        this->Data = CMB_NPC_HIDDEN;
        while(ItemCheck<0&&!Link->Item[Abs(ItemCheck)]){
            Waitframe();
        }
        this->Data = Combo;


        //Saves the width and height of the FFC for collision checks
        int Width = 16;
        int Height = 16;
        if(this->EffectWidth!=16)
        Width = this->EffectWidth;
        else if(this->TileWidth>1)
        Width = this->TileWidth*16;
        if(this->EffectHeight!=16)
        Height = this->EffectHeight;
        else if(this->TileHeight>1)
        Height = this->TileHeight*16;


        //Sets the space below the NPC or the space a guard NPC occupies to be solid
        if(LAYER_NPC>-1){

            for(int x=Floor(this->X/16); x<=Floor(this->X/16)+this->TileWidth-1; x++){
                for(int y=Floor(this->Y/16); y<=Floor(this->Y/16)+this->TileHeight-1; y++){
                    SetLayerComboD(LAYER_NPC, y*16+x, CMB_NPC_SOLID);
                }
            }

        }


        while(true){

            //Removes NPCs if Link has the required item
            if(ItemCheck>0&&Link->Item[ItemCheck]){
                this->Data = CMB_NPC_HIDDEN;

                if(LAYER_NPC>-1){

                    for(int x=Floor(this->X/16); x<=Floor(this->X/16)+this->TileWidth-1; x++){
                        for(int y=Floor(this->Y/16); y<=Floor(this->Y/16)+this->TileHeight-1; y++){
                            SetLayerComboD(LAYER_NPC, y*16+x, CMB_NPC_HIDDEN);
                        }
                    }

                }

                Quit();
            }


            //Facing Up
            if(Link->Dir==DIR_UP&&Link->Y>=this->Y&&Link->Y<=this->Y+Height-8&&Link->X>=this->X-8&&Link->X<=this->X+Width-8){
                if(CMB_NPC_CANTALK>0)
                Screen->FastCombo(LAYER_NPC_CANTALK, Link->X, Link->Y-16, CMB_NPC_CANTALK, CS_NPC_CANTALK, 128);
                if(Link->PressA){
                    Link->InputA = false;
                    Link->PressA = false;
                    Screen->Message(String);

                }
            }
            Waitframe();
        }
    }
}

Edited by accela2me, 06 September 2015 - 06:04 PM.




Also tagged with one or more of these keywords: npc, script, clean, trade, sequence

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users