1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/**
Shark
Very strong water animal.
@author Armin
*/
local BiteStrength = 25;
local attacking, walking, swimming, swim_animation, base_transform, turned;
private func Construction()
{
StartGrowth(15);
SetAction("Swim");
if (!Random(2))
SetComDir(COMD_Right);
else
SetComDir(COMD_Left);
if (GetAlive())
AddTimer(this.Activity, 10);
return _inherited(...);
}
public func IsAnimal() { return true; }
private func Attack(object target, int x, int y)
{
if (!attacking || (target->GetAction() != "Swim" && target->GetAction() != "Jump") || !target->GetAlive() || !PathFree(GetX(), GetY(), target->GetX(), target->GetY()))
{
SetCommand("None");
attacking = false;
SetAction("Swim");
return 1;
}
var dis = Distance(GetX(), GetY(), target->GetX(), target->GetY());
if (dis > 30)
{
var current_angle=Angle(GetX(), GetY(), target->GetX(), target->GetY());
if (current_angle < 0) current_angle = 360 + current_angle;
var t = current_angle - 270;
var t2 = Cos(t, 90) - 90; // Expand the areas around 0deg and 180deg a bit so you see fish from the side more
this.MeshTransformation = Trans_Mul(Trans_Rotate(t, 0, 0, 1), Trans_Rotate(t2, 1, 0, 0));
}
this->SetCommand("MoveTo", target);
if (dis < 20)
{
// Victim near. Bite him.
SetAction("Bite");
Sound("Animals::Fish::Munch*");
if (target->GetAlive())
target->DoEnergy(-BiteStrength);
DoEnergy(BiteStrength);
// Mission succesful. Swim back to the initial position.
SetCommand("None");
this["MeshTransformation"] = Trans_Identity();
this->SetCommand("MoveTo", nil, x, y);
ScheduleCall(this, "ResetHunger", 300);
return 1;
}
ScheduleCall(this, "Attack", 5, 0, target, x, y);
}
private func Turn()
{
if (GetCommand() != nil)
return 1;
SetComDir(COMD_None);
SetXDir(0);
SetYDir(0);
SetAction("Turn");
// The shark should now turn around in very short intervalls.
turned = true;
ScheduleCall(this, "ResetTurn", 80);
}
private func Activity()
{
if (GetAction() == "Turn")
return;
if (swimming)
{
if (Inside(GetXDir(), -6, 6) && !turned && GetAction() != "FastSwim")
{
SetCommand("None");
Turn();
}
else if (!attacking)
{
var target = FindObject(Find_ID(Clonk), Find_Distance(380), Find_Action("Swim"));
if (target)
if (PathFree(GetX(), GetY(), target->GetX(), target->GetY()))
{
attacking = true;
SetAction("FastSwim");
Attack(target, GetX(), GetY());
return 1;
}
// Avoid surface.
if (!GBackLiquid(0, -10))
{
if (GetDir())
SetComDir(COMD_DownRight);
else
SetComDir(COMD_DownLeft);
return 1;
}
// Avoid ground.
if (!GBackLiquid(0, 25))
{
if (GetDir())
SetComDir(COMD_UpRight);
else
SetComDir(COMD_UpLeft);
return 1;
}
if (!turned)
{
// Wall?
if ((GBackSolid(+75, 0) && GetDir() == DIR_Right) ^ (GBackSolid(-75, 0) && GetDir() == DIR_Left))
{
Turn();
return;
}
if (!Random(40))
{
Turn();
return 1;
}
}
}
}
else if (walking)
{
// PANIC WHERE THE WATER AT
var rx = RandomX(10, 25);
if (!Random(2)) rx *= -1;
var has_water = false;
for (var y = 0; y <= 12; y += 4)
{
if (!GBackLiquid(rx, y)) continue;
has_water = true;
break;
}
if (has_water)
{
if (rx < 0) SetComDir(COMD_Left);
else SetComDir(COMD_Right);
if (!Random(2))
DoJump();
return true;
}
else
{
if (!Random(2))
{
if (!Random(2)) SetComDir(COMD_Left);
else SetComDir(COMD_Right);
}
if (!Random(3))
DoJump();
}
}
return 1;
}
private func Place(int amount, proplist rectangle, proplist settings)
{
var max_tries = 2 * amount;
var loc_area = nil;
if (rectangle) loc_area = Loc_InArea(rectangle);
var f;
while ((amount > 0) && (--max_tries > 0))
{
var spot = FindLocation(Loc_Material("Water"), Loc_Space(20), loc_area);
if (!spot) continue;
f = CreateObjectAbove(this, spot.x, spot.y, NO_OWNER);
if (!f) continue;
// Randomly add some large/slim fish
if (Random(3))
{
// There are naturally smaller and larger sharks.
f->SetCon(RandomX(80, 120));
// make sure the smaller ones don't grow larger any more
f->StopGrowth();
// Slim fish.
if (f->GetCon() > 100 || !Random(3))
f->SetYZScale(1000 - Random(300));
}
if (f->Stuck())
{
f->RemoveObject();
continue;
}
--amount;
}
return f; // return last created fish
}
private func SetYZScale(int new_scale)
{
base_transform = Trans_Scale(1000, new_scale, new_scale);
return true;
}
private func Death()
{
RemoveTimer(this.UpdateSwim);
RemoveTimer(this.Activity);
this.MeshTransformation = Trans_Rotate(160 + Random(41), 1, 0, 0);
if (base_transform) this.MeshTransformation = Trans_Mul(base_transform, this.MeshTransformation);
StopAnimation(swim_animation);
Decay();
this.Collectible = true;
// Maybe respawn a new fish if roe is near.
var roe = FindObject(Find_Distance(200), Find_ID(FishRoe));
if (roe)
roe->Hatch(GetID());
return _inherited(...);
}
private func DoJump()
{
SetAction("Jump");
Sound("Hits::SoftTouch*");
var x_dir = RandomX(ActMap.Jump.Speed/2, ActMap.Jump.Speed);
if (GetComDir() == COMD_Left) x_dir *= -1;
var y_dir = -RandomX(ActMap.Jump.Speed/3, ActMap.Jump.Speed);
SetSpeed(x_dir, y_dir);
}
private func StartWalk()
{
var len = GetAnimationLength("Swim");
var pos = GetAnimationPosition(swim_animation);
SetAnimationPosition(swim_animation, Anim_Linear(pos, 0, len, 10, ANIM_Loop));
this.MeshTransformation = Trans_Mul(Trans_Rotate(90 + RandomX(-10, 10), 1, 0, 0), base_transform);
SetObjDrawTransform(0,0,0,0,0,0);
swim_animation = PlayAnimation("Swim", 5, Anim_Linear(0, 0, len, 100, ANIM_Loop), Anim_Const(500));
if (GBackLiquid())
{
SetAction("Swim");
return;
}
walking = true;
swimming = false;
ResetHunger();
}
private func StartSwim()
{
StopAnimation(GetRootAnimation(5));
swimming = true;
walking = false;
this["MeshTransformation"] = Trans_Identity();
if (GetCommand() != nil)
return 1;
if (GetDir() == DIR_Left)
{
SetDir(DIR_Right);
if (!Random(5)) SetComDir(COMD_UpRight);
else if (!Random(6)) SetComDir(COMD_DownRight);
else SetComDir(COMD_Right);
}
else
{
SetDir(DIR_Left);
if (!Random(5)) SetComDir(COMD_UpLeft);
else if (!Random(6)) SetComDir(COMD_DownLeft);
else SetComDir(COMD_Left);
}
}
private func StartJump()
{
if (GBackLiquid())
{
SetAction("Swim");
return;
}
swimming = false;
walking = false;
}
private func ResetHunger()
{
attacking = false;
}
private func ResetTurn()
{
turned = false;
}
local ActMap = {
Swim = {
Prototype = Action,
Name = "Swim",
Procedure = DFA_SWIM,
Speed = 100,
Accel = 64,
Decel = 16,
Length = 52,
Delay = 10,
Directions = 2,
FlipDir = 1,
StartCall = "StartSwim",
Animation = "Swim"
},
FastSwim = {
Prototype = Action,
Name = "FastSwim",
Procedure = DFA_SWIM,
Speed = 200,
Accel = 48,
Decel = 16,
Length = 20,
Delay = 5,
Directions = 2,
FlipDir = 0,
NextAction = "FastSwim",
Animation = "Swim"
},
Turn = {
Prototype = Action,
Name = "Turn",
Procedure = DFA_SWIM,
Speed = 100,
Accel = 16,
Decel = 16,
Length = 52,
Delay = 10,
Directions = 2,
FlipDir = 1,
NextAction = "Swim",
Animation = "Turn"
},
Bite = {
Prototype = Action,
Name = "Bite",
Procedure = DFA_SWIM,
Speed = 100,
Accel = 16,
Decel = 16,
Length = 2,
Delay = 10,
Directions = 2,
NextAction = "Swim",
Animation = "Bite"
},
Walk = {
Prototype = Action,
Name = "Walk",
Procedure = DFA_WALK,
Speed = 30,
Accel = 16,
Decel = 16,
Length = 1,
Delay = 0,
FacetBase=1,
Directions = 2,
FlipDir = 1,
NextAction = "Walk",
StartCall = "StartWalk",
InLiquidAction = "Swim",
},
Jump = {
Prototype = Action,
Name = "Jump",
Procedure = DFA_FLIGHT,
Speed = 30,
Accel = 16,
Decel = 16,
Length = 1,
Delay = 0,
FacetBase=1,
Directions = 2,
FlipDir = 1,
NextAction = "Jump",
StartCall = "StartJump",
InLiquidAction = "Swim",
},
Dead = {
Prototype = Action,
Name = "Dead",
Procedure = DFA_NONE,
Speed = 10,
Length = 1,
Delay = 0,
FacetBase=1,
Directions = 2,
FlipDir = 1,
NextAction = "Hold",
NoOtherAction = 1,
ObjectDisabled = 1,
Animation = "Death"
}
};
local Name = "$Name$";
local Description = "$Description$";
local MaxEnergy = 150000;
local MaxBreath = 360; // 360 = ten seconds
local Placement = 1;
local NoBurnDecay = true;
local BreatheWater = 1;
local BorderBound = C4D_Border_Sides | C4D_Border_Top | C4D_Border_Bottom;
func IsPrey() { return false; }
func IsPredator() { return true; }
public func Definition(proplist def)
{
def.PictureTransformation = Trans_Mul(Trans_Translate(12000, 5000, 0), Trans_Scale(1600), Trans_Rotate(20, 1, 0, 0), Trans_Rotate(65, 0, 1, 0));
}
|