User Tag List

Results 1 to 1 of 1

Thread: Auto Turret Script

  1. #1
    Gel
    Join Date
    Mar 2015
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    338
    Level
    6
    vBActivity - Bars
    Lv. Percent
    71.73%

    Verified Bug A Bug in my Auto Turret Script

    This is my first post on AGN, so if I screw up, please don't complain too much.

    This script codes for a stationary (or mobile, your pick) enemy that will use any ranged EWeapon you choose to harm Link if he gets too close. At rapid fire, for as long as you want. This gives Link 2 options: he can destroy the stinking turret and lose out on an item of your choice, or he can de-activate the turret by slashing it from behind, allowing you to pick up the item.

    Uses

    You can have it guard something like the dungeon item. Pretty obvious if you ask me.
    Alternatively, you can give this ability to something like a Like-Like and inspire sheer terror.
    I do want to mention that wind EWeapons are supported. This means you can use all the wind you want. Your welcome.

    I'm still making a demo quest for this script as of writing. The quest editor froze in the middle of setting up the first enemy, so it is probably going to take a while to make. Once it is done I will upload it. Might as well share the code anyway.

    Code:
    //Auto Turret Script
    
    //This script allows you to create a FFC auto turret, which basically shoots an
    //EWeapon of your choosing automatically. Link can destroy the auto turret,
    //destroying the item. He can walk up behind it and slash it to safely 
    //de-activate it without destroying the item.
    
    //If you already have std.zh imported in your script file, just ignore this next
    //line. Otherwise, delete the 2 slashes from the next line.
    //import "std.zh"
    
    //Joe123's homing projectile function (really helped, thanks for releasing the 
    //function!) after some modification to fit this script
    void homingprojectile(int x, int y, int type, int dmg, int spd){
     	//Generate and set up projectile
     	eweapon w = Screen->CreateEWeapon(type);
     	w->X = x; w->Y = y;
     	w->Step = spd;
     	w->Damage = dmg;
     	//Shoot projectile at Link
     	w->Angular = true;
     	int angle = ArcTan((Link->X-x),(Link->Y-y));
     	w->Angle = angle;
     	if(angle==-PI || angle==PI) w->Dir=2;
     	else if(angle==-PI/2) w->Dir=0;
     	else if(angle==PI/2) w->Dir=1;
     	else if(angle==0) w->Dir=3;
     	else if(angle<-PI/2) w->Dir=4;
     	else if(angle<0) w->Dir=5;
    	else if(angle>(PI/2)) w->Dir=6;
    	else w->Dir=7;
    }
    
    //This is the actual auto turret script.
    
    //To use the weapon, you need to create an enemy with the following information.
    //Type: Other
    //Weapon Type: Fireball, Rock, Magic, Flame, Wind, Flame 2, or Fireball (rising)
    //Attribute 1: Direction the turret is facing (use std.zh constants)
    //Attribute 2: Item salvaged
    //Attribute 3: The amount of times the turret fires before it reloads
    //Attribute 4: The tile used by the turret when scanning for Link
    //Attribute 5: The tile used by the turret when firing it's weapon
    //Attribute 6: The tile used by the turret when reloading
    //Attribute 7: The tile used by the turret when disabled
    //Attribute 8: The vision of the turret. Don't set to 177+, or hell WILL ensue
    //Attribute 9: The speed of the turret's weapon
    //Attribute 10: The amount of time it takes the turret to reload
    
    //Also needed to use this script is an FFC with the Auto Turret script.
    
    ffc script Auto_Turret{
    	void run(int enum){
    		npc turret=Screen->LoadNPC(enum);
    		bool LinkInRng=false;
    		bool reload=false;
    		bool disabled=false;
    		int x=turret->X;
    		int y=turret->Y;
    		while(true){
    			int distance=Distance(x,y,Link->X,Link->Y);
    			int angle=ArcTan(Link->X-x, Link->Y-y)*180/PI;
    			int adjustedAngle=angle;
    			int direction=turret->Attributes[0];
    			if(direction==DIR_UP)adjustedAngle+=90;
    			else if(direction==DIR_DOWN)adjustedAngle-=90;
    			else if(direction==DIR_LEFT)adjustedAngle+=180;
    			if(adjustedAngle>180)adjustedAngle-=360;
    			if(distance>>turret->Attributes[7])LinkInRng=false;
    			else if(Abs(adjustedAngle)>75/2)LinkInRng=false;
    			else LinkInRng=true;
    			if(!turret->isValid()){
    				Quit();
    			}
    			if(Link->Dir==direction){
    				lweapon sword=Screen->LoadLWeapon(1);
    				if(Collision(sword,turret)){
    					disabled=true;
    					Screen->CreateItem(turret->Attributes[1]);
    				}
    			}
    		}
    		while(!LinkInRng && !disabled)turret->Tile=turret->Attributes[3];
    		while(LinkInRng){
    			if(!reload && !disabled){
    				turret->Tile=turret->Attributes[4];
    				for(int i=0;i<=turret->Attributes[2];i++){
    					homingprojectile(x,y,turret->Weapon,turret->WeaponDamage,turret->Attributes[8]);
    					if(i==turret->Attributes[2]-1) reload=true;
    				}
    			}
    			else if(reload){
    				turret->Tile=turret->Attributes[5];
    				for(int i=0;i<=turret->Attributes[9];i++){
    					if(i==turret->Attributes[9]) reload=false;
    				}
    			}
    		}
    		while(disabled){
    			turret->Tile=turret->Attributes[7];
    		}
    	}
    }
    Credits

    Saffith for making the Castle Guards script, from which I came up with most of the turret's Link detection system.
    Joe123 for making a simple function that saved me so much bother: the homing projectile function.
    Me, for making the script.
    And to all who inspired me to make this script (mostly Gleeok to be honest)

    (Hopefully I didn't screw up too badly for my first post)

    Edit: I found a huge bug with the script while testing it. While it compiles just fine, it doesn't seem to do anything at all once in the game. It runs all the things I edited in the enemy editor just fine, but not what I scripted. I've tried really hard to try and squash this stinking bug. Nothing happens to it.

    For those of you who can help me out with this glitch that I have been trying for a fair bit of time to squash on my own, here is the updated script file.

    Code:
    //Auto Turret Script
    
    //This script allows you to create a FFC auto turret, which basically shoots an
    //EWeapon of your choosing automatically. Link can destroy the auto turret,
    //destroying the item. He can walk up behind it and slash it to safely 
    //de-activate it without destroying the item.
    
    //If you already have std.zh imported in your script file, just ignore this next
    //line. Otherwise, delete the 2 slashes from the next line.
    import "std.zh"
    
    //Joe123's homing projectile function (really helped, thanks for releasing the 
    //function!) after some modification to fit this script
    void homingprojectile(int x, int y, int type, int dmg, int spd){
     	//Generate and set up projectile
     	eweapon w = Screen->CreateEWeapon(type);
     	w->X = x; w->Y = y;
     	w->Step = spd;
     	w->Damage = dmg;
     	//Shoot projectile at Link
     	w->Angular = true;
     	int angle = ArcTan((Link->X-x),(Link->Y-y));
     	w->Angle = angle;
     	if(angle==-PI || angle==PI) w->Dir=2;
     	else if(angle==-PI/2) w->Dir=0;
     	else if(angle==PI/2) w->Dir=1;
     	else if(angle==0) w->Dir=3;
     	else if(angle<-PI/2) w->Dir=4;
     	else if(angle<0) w->Dir=5;
    	else if(angle>(PI/2)) w->Dir=6;
    	else w->Dir=7;
    }
    
    //This is the actual auto turret script.
    
    //To use the weapon, you need to create an enemy with the following information.
    //Type: Other
    //Weapon Type: Fireball, Rock, Magic, Flame, Wind, Flame 2, or Fireball (rising)
    //Attribute 1: Direction the turret is facing (use std.zh constants)
    //Attribute 2: Item salvaged
    //Attribute 3: The amount of times the turret fires before it reloads
    //Attribute 4: The tile used by the turret when scanning for Link
    //Attribute 5: The tile used by the turret when firing it's weapon
    //Attribute 6: The tile used by the turret when reloading
    //Attribute 7: The tile used by the turret when disabled
    //Attribute 8: The vision of the turret. Don't set to 177+, or hell WILL ensue
    //Attribute 9: The speed of the turret's weapon
    //Attribute 10: The amount of time it takes the turret to reload
    
    //Also needed to use this script is an FFC with the Auto Turret script.
    
    ffc script Auto_Turret{
    	void run(int enum){
    		npc turret=Screen->LoadNPC(enum);
    		bool LinkInRng=true;
    		bool reload=false;
    		bool disabled=false;
    		int x=turret->X;
    		int y=turret->Y;
    		while(true){
    			int distance=Distance(x,y,Link->X,Link->Y);
    			int angle=ArcTan(Link->X-x, Link->Y-y)*180/PI;
    			int adjustedAngle=angle;
    			int direction=turret->Attributes[0];
    			if(direction==DIR_UP)adjustedAngle+=90;
    			else if(direction==DIR_DOWN)adjustedAngle-=90;
    			else if(direction==DIR_LEFT)adjustedAngle+=180;
    			if(adjustedAngle>180)adjustedAngle-=360;
    			if(distance>>turret->Attributes[7])LinkInRng=false;
    			else if(Abs(adjustedAngle)>75/2)LinkInRng=false;
    			else LinkInRng=true;
    			if(!turret->isValid()){
    				Quit();
    			}
    			if(Link->Dir==direction){
    				lweapon sword=Screen->LoadLWeapon(1);
    				if(Collision(sword,turret)){
    					CreateItemAt(turret->Attributes[1],x,y);
    					disabled=true;
    					turret->HP=0;
    				}
    			}
    		}
    		while(!LinkInRng && !disabled)turret->Tile=turret->Attributes[3];
    		while(LinkInRng){
    			if(!reload && !disabled){
    				turret->Tile=turret->Attributes[4];
    				for(int i=0;i<=turret->Attributes[2];i++){
    					homingprojectile(x,y,turret->Weapon,turret->WeaponDamage,turret->Attributes[8]);
    					if(i==turret->Attributes[2]-1) reload=true;
    				}
    			}
    			else if(reload){
    				turret->Tile=turret->Attributes[5];
    				for(int i=0;i<=turret->Attributes[9];i++){
    					if(i==turret->Attributes[9]) reload=false;
    				}
    			}
    		}
    		while(disabled){
    			turret->Tile=turret->Attributes[7];
    		}
    	}
    }
    Thanks in advance if you can figure out the problem with this script!
    Last edited by New Old Competitor; 03-30-2015 at 05:38 PM. Reason: Bug Notice

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Armageddon Games is a game development group founded in 1997. We are extremely passionate about our work and our inspirations are mostly drawn from games of the 8-bit and 16-bit era.
Social