Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chicken Breeding Rework #2141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 23 additions & 43 deletions Entities/Natural/Animals/Chicken/Chicken.as
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@
//script for a chicken

#include "AnimalConsts.as";
#include "ChickenCommon.as";

const u8 DEFAULT_PERSONALITY = SCARED_BIT;
const int MAX_EGGS = 2; //maximum symultaneous eggs
const int MAX_CHICKENS = 6;
const f32 CHICKEN_LIMIT_RADIUS = 120.0f;

int g_lastSoundPlayedTime = 0;
int g_layEggInterval = 0;
const string ALLOW_SOUND_TIME = "last sound time";
const u8 SOUND_DELAY = getTicksASecond();
const string EGG_INTERVAL = "egg interval";

//sprite

void onInit(CSprite@ this)
{
this.ReloadSprites(0, 0); //always blue

}

void onTick(CSprite@ this)
Expand Down Expand Up @@ -82,24 +79,19 @@ void onInit(CBlob@ this)
this.set_f32(terr_rad_property, 75.0f);
this.set_u8(target_lose_random, 14);

//for shape
// for shape
this.getShape().SetRotationsAllowed(false);

//for flesh hit
// for flesh hit
this.set_f32("gib health", -0.0f);
this.Tag("flesh");

this.getShape().SetOffset(Vec2f(0, 6));

this.getCurrentScript().runFlags |= Script::tick_blob_in_proximity;
this.getCurrentScript().runProximityTag = "player";
this.getCurrentScript().runProximityRadius = 320.0f;

// attachment
// breeding/sound

//todo: some tag-based keys to take interference (doesn't work on net atm)
/*AttachmentPoint@ att = this.getAttachments().getAttachmentPointByName("PICKUP");
att.SetKeysToTake(key_action1);*/
this.set_u32(ALLOW_SOUND_TIME, 0);
this.set_u8(EGG_INTERVAL, 0);

// movement

Expand Down Expand Up @@ -142,6 +134,7 @@ void onTick(CBlob@ this)
}
}

u32 allow_sound_time = this.get_u32(ALLOW_SOUND_TIME);
if (this.isAttached())
{
AttachmentPoint@ att = this.getAttachmentPoint(0); //only have one
Expand All @@ -150,18 +143,6 @@ void onTick(CBlob@ this)
CBlob@ b = att.getOccupied();
if (b !is null)
{
// too annoying

//if (g_lastSoundPlayedTime+20+XORRandom(10) < getGameTime())
//{
// if (XORRandom(2) == 1)
// this.getSprite().PlaySound("/ScaredChicken");
// else
// this.getSprite().PlaySound("/Pluck");
//
// g_lastSoundPlayedTime = getGameTime();
//}

Vec2f vel = b.getVelocity();
if (vel.y > 0.5f)
{
Expand All @@ -178,28 +159,28 @@ void onTick(CBlob@ this)
this.AddForce(Vec2f(0, -10));
}
}
else if (XORRandom(128) == 0 && g_lastSoundPlayedTime + 30 < getGameTime())
else if (XORRandom(128) == 0 && allow_sound_time < getGameTime())
{
this.getSprite().PlaySound("/Pluck");
g_lastSoundPlayedTime = getGameTime();
this.set_u32(ALLOW_SOUND_TIME, getGameTime() + SOUND_DELAY);

// lay eggs
if (getNet().isServer())
{
g_layEggInterval++;
if (g_layEggInterval % 13 == 0)
u8 egg_interval = this.get_u8(EGG_INTERVAL) + 1;
this.set_u8(EGG_INTERVAL, egg_interval);
if (egg_interval % 13 == 0)
{
Vec2f pos = this.getPosition();
bool otherChicken = false;
int eggsCount = 0;
int chickenCount = 0;
int count = 1;
string name = this.getName();
CBlob@[] blobs;
this.getMap().getBlobsInRadius(pos, CHICKEN_LIMIT_RADIUS, @blobs);
for (uint step = 0; step < blobs.length; ++step)
{
CBlob@ other = blobs[step];
if (other is this)
if (other is this || other.isAttached() || other.isInInventory())
continue;

const string otherName = other.getName();
Expand All @@ -209,15 +190,15 @@ void onTick(CBlob@ this)
{
otherChicken = true;
}
chickenCount++;
count++;
}
if (otherName == "egg")
else if (otherName == "egg")
{
eggsCount++;
count++;
}
}

if (otherChicken && eggsCount < MAX_EGGS && chickenCount < MAX_CHICKENS)
if (otherChicken && count < MAX_CHICKENS)
{
server_CreateBlob("egg", this.getTeamNum(), this.getPosition() + Vec2f(0.0f, 5.0f));
}
Expand All @@ -231,10 +212,9 @@ void onCollision(CBlob@ this, CBlob@ blob, bool solid, Vec2f normal, Vec2f point
if (blob is null)
return;

if (blob.getRadius() > this.getRadius() && g_lastSoundPlayedTime + 25 < getGameTime() && blob.hasTag("flesh"))
if (blob.getRadius() > this.getRadius() && this.get_u32(ALLOW_SOUND_TIME) < getGameTime() && blob.hasTag("flesh"))
{
this.getSprite().PlaySound("/ScaredChicken");
g_lastSoundPlayedTime = getGameTime();
this.set_u32(ALLOW_SOUND_TIME, getGameTime() + SOUND_DELAY);
}
}

2 changes: 2 additions & 0 deletions Entities/Natural/Animals/Chicken/ChickenCommon.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const int MAX_CHICKENS = 4;
const f32 CHICKEN_LIMIT_RADIUS = 80.0f;
72 changes: 59 additions & 13 deletions Entities/Natural/Animals/Chicken/Egg.as
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

const int grow_time = 50 * getTicksASecond();
#include "ChickenCommon.as";

const int MAX_CHICKENS_TO_HATCH = 5;
const f32 CHICKEN_LIMIT_RADIUS = 120.0f;
const int GROW_TIME = 20 * getTicksASecond();
const string CAN_GROW_TIME = "can grow time";

void onInit(CBlob@ this)
{
this.getCurrentScript().tickFrequency = 120;
this.addCommandID("hatch client");
ResetGrowTime(this);
this.getCurrentScript().runFlags |= Script::tick_not_attached;
}

bool canBePickedUp(CBlob@ this, CBlob@ byBlob)
Expand All @@ -17,32 +19,59 @@ bool canBePickedUp(CBlob@ this, CBlob@ byBlob)

void onTick(CBlob@ this)
{
if (isServer() && this.getTickSinceCreated() > grow_time)
if (isServer() && getGameTime() > this.get_u32(CAN_GROW_TIME))
{
int chickenCount = 0;
int count = 0;
CBlob@[] blobs;
this.getMap().getBlobsInRadius(this.getPosition(), CHICKEN_LIMIT_RADIUS, @blobs);
for (uint step = 0; step < blobs.length; ++step)
{
CBlob@ other = blobs[step];
if (other.getName() == "chicken")
if (other.getName() == "chicken" && !other.isAttached() && !other.isInInventory())
{
chickenCount++;
count++;
}
}

if (chickenCount < MAX_CHICKENS_TO_HATCH)
this.server_SetHealth(-1);
this.server_Die();
this.SendCommand(this.getCommandID("hatch client"));

// Prevent chickens from spawning in blocks
CMap@ map = getMap();
if (map !is null && count < MAX_CHICKENS)
{
this.server_SetHealth(-1);
this.server_Die();
server_CreateBlob("chicken", -1, this.getPosition() + Vec2f(0, -5.0f));
f32 chicken_radius = 5.0f;
Vec2f chicken_height_offset = Vec2f(0, -chicken_radius);
Vec2f spawn_pos = this.getPosition() + chicken_height_offset;
Vec2f tile_pos = map.getTileSpacePosition(spawn_pos);

// Solids in or above our block, abort
if (hasSolid(map, tile_pos))
{
return;
}

bool tiles_left = hasSolid(map, tile_pos + Vec2f(-1, 0));
bool tiles_right = hasSolid(map, tile_pos + Vec2f(1, 0));

// 1 wide gap, abort
if (tiles_left && tiles_right)
{
return;
}

// Check if we need to shift
if (tiles_left ^^ tiles_right)
{
spawn_pos.x = map.getAlignedWorldPos(spawn_pos).x + (tiles_left ? chicken_radius : 2); // Not sure why these offsets had to be so weird
}

this.SendCommand(this.getCommandID("hatch client"));
server_CreateBlob("chicken", this.getTeamNum(), spawn_pos + chicken_height_offset);
}
}
}


void onCommand(CBlob@ this, u8 cmd, CBitStream @params)
{
if (cmd == this.getCommandID("hatch client") && isClient())
Expand All @@ -54,3 +83,20 @@ void onCommand(CBlob@ this, u8 cmd, CBitStream @params)
}
}
}

void onDetach(CBlob@ this, CBlob@ detached, AttachmentPoint@ attachedPoint)
{
ResetGrowTime(this);
}

void ResetGrowTime(CBlob@ this)
{
this.set_u32(CAN_GROW_TIME, getGameTime() + GROW_TIME);
}

bool hasSolid(CMap@ map, Vec2f tile_pos)
{
Tile this_tile = map.getTileFromTileSpace(tile_pos);
Tile above_tile = map.getTileFromTileSpace(tile_pos + Vec2f(0, -1));
return map.isTileSolid(this_tile) || map.hasTileSolidBlobs(this_tile) || map.isTileSolid(above_tile) || map.hasTileSolidBlobs(above_tile);
}