PDA

View Full Version : Anti Fairy



pkmnfrk
08-23-2010, 12:39 PM
http://www.youtube.com/watch?v=9q7qb1AXN_8

WARNING: This script uses a script command that is not present in Build 1296! You must have a newer build than that in order to use this script!


ffc script AntiFairy {
void run(int type, int dmg, int sfx) {
int dir = 4 + Rand(4);
int hurttimeout = 0;

while(true) {
dir = MoveBounce(this, dir);

if(hurttimeout == 0) {
if(LinkCollision(this) && Link->Action != LA_GOTHURTLAND && Link->Action != LA_GOTHURTWATER) {
Link->Action = LA_GOTHURTLAND;
Link->HitDir = RelativeDir(this);

if(sfx != 0) Game->PlaySound(sfx);

if(type == 0) {
Link->HP -= dmg;
} else if(type == 1) {
Link->MP -= dmg;
}

hurttimeout = 10;
}
} else {
hurttimeout -= 1;
}

for(int i = 1; i <= Screen->NumLWeapons(); i++) {
lweapon lw = Screen->LoadLWeapon(i);

if(lw->ID == LW_BRANG && Collision(this, lw)) {
//buh bye
lw->DeadState = WDS_BOUNCE;
Game->PlaySound(25);
item itm = Screen->CreateItem(I_FAIRY);
itm->X = this->X;
itm->Y = this->Y;
itm->Pickup = IP_TIMEOUT;
this->X = -100;
this->Y = -100;
return;
}
}

Waitframe();
}
}

int FlipH(int dir) {
if(dir == DIR_LEFTUP) {
return DIR_RIGHTUP;
} else if(dir == DIR_RIGHTUP) {
return DIR_LEFTUP;
} else if(dir == DIR_LEFTDOWN) {
return DIR_RIGHTDOWN;
} else if(dir == DIR_RIGHTDOWN) {
return DIR_LEFTDOWN;
}
}

int FlipV(int dir) {
if(dir == DIR_LEFTUP) {
return DIR_LEFTDOWN;
} else if(dir == DIR_RIGHTUP) {
return DIR_RIGHTDOWN;
} else if(dir == DIR_LEFTDOWN) {
return DIR_LEFTUP;
} else if(dir == DIR_RIGHTDOWN) {
return DIR_RIGHTUP;
}
}

int MoveBounce(ffc this, int dir) {
int newx = this->X;
int newy = this->Y;
if(dir == DIR_LEFTUP) {
newx -= 1;
newy -= 1;
} else if(dir == DIR_RIGHTUP) {
newx += 1;
newy -= 1;
} else if(dir == DIR_LEFTDOWN) {
newx -= 1;
newy += 1;
} else if(dir == DIR_RIGHTDOWN) {
newx += 1;
newy += 1;
}

//this doesn't check the extreme top or bottom!
if(newx <= 0 || newx + 15 >= 255) {
dir = FlipH(dir);
} else {
for(int y = newy+2; y < newy + 14; y++) {
if(Screen->isSolid(newx+1, y) || Screen->isSolid(newx + 14, y)) {
dir = FlipH(dir);
break;
}
}
}

//this doesn't check the extreme right or left!
if(newy <= 0 || newy + 15 >= 175) {
dir = FlipV(dir);
} else {
for(int x = newx+2; x < newx + 14; x++) {
if(Screen->isSolid(x, newy+1) || Screen->isSolid(x, newy + 14)) {
dir = FlipV(dir);
break;
}
}
}

newx = this->X;
newy = this->Y;
if(dir == DIR_LEFTUP) {
newx -= 1;
newy -= 1;
} else if(dir == DIR_RIGHTUP) {
newx += 1;
newy -= 1;
} else if(dir == DIR_LEFTDOWN) {
newx -= 1;
newy += 1;
} else if(dir == DIR_RIGHTDOWN) {
newx += 1;
newy += 1;
}

this->X = newx;
this->Y = newy;

return dir;
}
}

This script creates one of those enemies from the Zelda 3/the gameboy zeldas that bounces diagonally around the screen. In Zelda 3, they take magic instead of health, while on the game boy, they do damage. In addition, they turn into fairies when struck with the boomerang.

Fortunately for you, these ones do all of the above.

Usage:
No special set up is required. Just place the FFC and set the arguments.

Arguments:
D0 - The type of damage to do: 0 = HP, 1 = MP
D1 - The amount of damage.
D2 - The sound effect to play on contact. (0 = none)

Joe123
08-23-2010, 01:38 PM
That's pretty cool!
FlipV would work as dir ^= 10b and FlipH as dir ^= 1b, although it would only work for diagonals and not for the cardinal directions I think.

pkmnfrk
08-23-2010, 01:58 PM
The first rule of Programming club: Don't optimize prematurely.
The second rule of Programming club: Don't optimize prematurely.

;)

Joe123
08-23-2010, 02:50 PM
If it's inside a function that describes what it's doing what's wrong with it?

pkmnfrk
08-23-2010, 03:02 PM
Nothing is particularly wrong with it, it just looks like noise. I prefer to write code that doesn't require comments, since those can quickly go out of date. A chained if statement is clear, some bit twiddling isn't.

If it gets moved into std.zh (which is fine), then I understand that there are other, more pressing concerns (like, reducing compilation time and code size as much as possible).

(on a side note, I don't suppose you're working on a switch statement, are you?)

Joe123
08-23-2010, 03:10 PM
Eh, my thoughts are that if it's inside a function with a descriptive name it doesn't require any comments anyway.

Well I had thought about switch statements very fleetingly, my understanding of bison isn't anyway near good enough to implement them though. After co-incedentally having looked at the relative merits of speeds for different implementations of them compared with if/else if/else and arrays of function pointers and things a few hours ago I'm definitely not working on one.

violetwasp
05-30-2011, 06:39 PM
when I try to compile this script it gives me this message:
tmp, line 13: error s10: function relativedir is undeclared.:confused: