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

Mounted Bow and other vehicle changes #1251

Closed
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4d43c33
Make Mounted Bow not fall through small gaps
mugg91 May 4, 2022
5548506
Make Mounted bow able to be detached
mugg91 May 4, 2022
4bbf8e8
Changing airship while I'm at it
mugg91 May 4, 2022
21e9b5c
Change bomber while I'm at it
mugg91 May 4, 2022
3297992
Update MountedBow.cfg
mugg91 May 4, 2022
b1c5556
Update VehicleAttachment.as
mugg91 May 4, 2022
b2946bc
Fix Mounted Bow facing direction bug
mugg91 May 4, 2022
6d2e468
Better MountedBow facing bug fix
mugg91 May 7, 2022
8368219
Update Catapult.as
mugg91 May 7, 2022
e589e59
Update Ballista.as
mugg91 May 7, 2022
8e8899f
Update MountedBow.cfg
mugg91 May 8, 2022
11b1fce
Update MountedBow.as
mugg91 May 8, 2022
cdb12e8
Removing Infinite Ammo
mugg91 May 8, 2022
0c193f0
Add "SetTeamToCarrier.as"
mugg91 May 8, 2022
a0096c3
Update SetTeamToCarrier.as
mugg91 May 8, 2022
7ba4929
[fixed] being attached to or holding enemy vehicles
mugg91 May 28, 2022
ea21584
[dev] make sure you can still attach to neutral stuff
mugg91 May 28, 2022
ab67c23
[dev] delete unwanted file
mugg91 May 28, 2022
c0171e4
[dev] try to fix identation, export if-check to function
mugg91 Jun 1, 2022
83d3f73
[dev] merged if statements
mugg91 Jun 1, 2022
f319f22
[fixed] vehicle conversion bugs
mugg91 Jun 4, 2022
20f42b4
[dev] detachFromAll instead of detachAll
mugg91 Jun 4, 2022
6dfb653
[changed] team checking is more consistent now
mugg91 Jun 6, 2022
b5de4d3
Merge branch 'mugg91-mounted-bow-vehicle-change' of https://github.co…
mugg91 Jun 6, 2022
c0f8761
[dev] remove left-over
mugg91 Jun 6, 2022
130db67
[dev] Merged if statements
mugg91 Jun 6, 2022
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
13 changes: 11 additions & 2 deletions Entities/Common/Scripts/SetTeamToCarrier.as
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ void onAttach(CBlob@ this, CBlob@ attached, AttachmentPoint@ attachedPoint)
{
return;
}
this.server_setTeamNum(attached.getTeamNum());

CBlob@ carried = attached.getCarriedBlob();

if (carried !is null)
{
if (carried.getName() == this.getName())
mugg91 marked this conversation as resolved.
Show resolved Hide resolved
{
this.server_setTeamNum(attached.getTeamNum());
}
}
}

void onThisAddToInventory(CBlob@ this, CBlob@ inventoryBlob)
Expand All @@ -18,4 +27,4 @@ void onThisAddToInventory(CBlob@ this, CBlob@ inventoryBlob)
return;
}
this.server_setTeamNum(inventoryBlob.getTeamNum());
}
}
19 changes: 13 additions & 6 deletions Entities/Structures/MountedBow/MountedBow.as
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const Vec2f arm_offset = Vec2f(-6, 0);

void onInit(CBlob@ this)
{
this.set_bool("facing_left", false);

Vehicle_Setup(this,
0.0f, // move speed
0.31f, // turn speed
Expand Down Expand Up @@ -62,8 +64,6 @@ void onInit(CBlob@ this)

sprite.SetZ(-10.0f);

this.getCurrentScript().runFlags |= Script::tick_hasattached;

// auto-load on creation
if (getNet().isServer())
{
Expand Down Expand Up @@ -114,9 +114,9 @@ f32 getAimAngle(CBlob@ this, VehicleInfo@ v)
}

void onTick(CBlob@ this)
{
if (this.hasAttached() || this.getTickSinceCreated() < 30) //driver, seat or gunner, or just created
{
{
if (this.hasAttached() || this.isFacingLeft() != this.get_bool("facing_left") || this.getTickSinceCreated() < 30) //is being used or held by player or vehicle, or just created
mugg91 marked this conversation as resolved.
Show resolved Hide resolved
{
VehicleInfo@ v;
if (!this.get("VehicleInfo", @v))
{
Expand Down Expand Up @@ -150,9 +150,11 @@ void onTick(CBlob@ this)
arm.RotateBy(rotation, Vec2f(facing_left ? -4.0f : 4.0f, 0.0f));
}


Vehicle_StandardControls(this, v);
}

this.set_bool("facing_left", this.isFacingLeft());
this.Sync("facing_left", isServer());
}

void onHealthChange(CBlob@ this, f32 oldHealth)
Expand Down Expand Up @@ -235,3 +237,8 @@ void onCollision(CBlob@ this, CBlob@ blob, bool solid)
TryToAttachVehicle(this, blob);
}
}

bool doesCollideWithBlob(CBlob@ this, CBlob@ blob)
{
return blob.getShape().isStatic();
}
26 changes: 13 additions & 13 deletions Entities/Structures/MountedBow/MountedBow.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Catapult config file
# MountedBow config file
# $ string
# @ array

Expand Down Expand Up @@ -61,9 +61,9 @@ bool shape_ladder = no
bool shape_platform = no
#block_collider
@f32 verticesXY = 0.5; 0.5;
7.5; 0.5;
7.5; 15.5;
0.5; 15.5;
10; 0.5;
10; 15.5;
0.5; 15.5;
mugg91 marked this conversation as resolved.
Show resolved Hide resolved
u8 block_support = 0
bool block_background = no
bool block_lightpasses = no
Expand All @@ -89,15 +89,15 @@ $inventory_name = Load

$name = mounted_bow
@$scripts = DecayInWater.as;
Seats.as; # set this first
IsFlammable.as;
WoodVehicleDamages.as;
Vehicle.as;
MountedBow.as;
Wooden.as;
GenericHit.as;
CollidesOnlyWithLarger.as;
AutoGrabFromGround.as;
Seats.as; # set this first
IsFlammable.as;
WoodVehicleDamages.as;
Vehicle.as;
MountedBow.as;
Wooden.as;
GenericHit.as;
SetTeamToCarrier.as;
AutoGrabFromGround.as;
mugg91 marked this conversation as resolved.
Show resolved Hide resolved
f32 health = 2.0
# looks & behaviour inside inventory
$inventory_name = Mounted Bow
Expand Down
5 changes: 3 additions & 2 deletions Entities/Vehicles/Airships/Airship.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Boat config file
# Airship config file
# $ string
# @ array

Expand All @@ -11,6 +11,8 @@ $sprite_factory = generic_sprite
FireAnim.as;
Airship.as;
DynamicForegroundLayer.as;
HealthBar.as;
VehicleConvert.as;
$sprite_texture = Airship.png
s32_sprite_frame_width = 96
s32_sprite_frame_height = 56
Expand Down Expand Up @@ -106,7 +108,6 @@ $inventory_name = Boat Compartment

$name = airship
@$scripts = Seats.as;
DecayIfLeftAlone.as;
DecayIfFlipped.as;
WoodVehicleDamages.as;
Wooden.as;
Expand Down
5 changes: 3 additions & 2 deletions Entities/Vehicles/Airships/Bomber.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Boat config file
# Bomber config file
# $ string
# @ array

Expand All @@ -10,6 +10,8 @@ $sprite_factory = generic_sprite
Wooden.as;
FireAnim.as;
Bomber.as;
HealthBar.as;
VehicleConvert.as;
$sprite_texture = Balloon.png
s32_sprite_frame_width = 48
s32_sprite_frame_height = 16
Expand Down Expand Up @@ -99,7 +101,6 @@ $inventory_name = Bomber Compartment

$name = bomber
@$scripts = Seats.as;
DecayIfLeftAlone.as;
DecayIfFlipped.as;
WoodVehicleDamages.as;
Wooden.as;
Expand Down
9 changes: 6 additions & 3 deletions Entities/Vehicles/Ballista/Ballista.as
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void onInit(CBlob@ this)

InitCosts();
this.set_s32("gold building amount", CTFCosts::ballista_gold);

this.set_bool("facing_left", false);

AddIconToken("$Normal_Bolt$", "BallistaBolt.png", Vec2f(32, 8), 0);
AddIconToken("$Explosive_Bolt$", "BallistaBolt.png", Vec2f(32, 8), 1);
Expand Down Expand Up @@ -180,8 +182,8 @@ f32 getAngle(CBlob@ this, const u8 charge, VehicleInfo@ v)

void onTick(CBlob@ this)
{
if (this.hasAttached() || this.getTickSinceCreated() < 30)
{
if (this.hasAttached() || this.isFacingLeft() != this.get_bool("facing_left") || this.getTickSinceCreated() < 30) //player is attached, facing direction changed or just created
{
VehicleInfo@ v;
if (!this.get("VehicleInfo", @v))
{
Expand Down Expand Up @@ -232,7 +234,8 @@ void onTick(CBlob@ this)
}
}
}


this.set_bool("facing_left", this.isFacingLeft());
}

void GetButtonsFor(CBlob@ this, CBlob@ caller)
Expand Down
2 changes: 1 addition & 1 deletion Entities/Vehicles/Boats/Dinghy.as
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ bool canBePickedUp(CBlob@ this, CBlob@ byBlob)
void onTick(CBlob@ this)
{
const int time = this.getTickSinceCreated();
if (this.hasAttached() || time < 30) //driver, seat or gunner, or just created
if (this.hasAttached() || this.isAttached() || time < 30) //driver, seat, gunner, pickup, or just created
{
VehicleInfo@ v;
if (!this.get("VehicleInfo", @v))
Expand Down
14 changes: 7 additions & 7 deletions Entities/Vehicles/Catapult/Catapult.as
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const u8 startStone = 100;

void onInit(CBlob@ this)
{
this.set_bool("facing_left", false);

Vehicle_Setup(this,
30.0f, // move speed
0.31f, // turn speed
Expand Down Expand Up @@ -73,8 +75,6 @@ void onInit(CBlob@ this)
void onTick(CBlob@ this)
{
const int time = this.getTickSinceCreated();
const bool hasAttached = this.hasAttached();
const bool hadAttached = this.get_bool("had_attached");

VehicleInfo@ v;
if (!this.get("VehicleInfo", @v))
Expand All @@ -83,9 +83,9 @@ void onTick(CBlob@ this)
const u16 delay = float(v.getCurrentAmmo().fire_delay);
const f32 time_til_fire = Maths::Max(0, Maths::Min(v.fire_time - getGameTime(), delay));

// hadAttached is here so it sets the arm angle the tick after the last player detaches
if (hasAttached || hadAttached || time < 30 || time_til_fire > 0) //driver, seat or gunner, or just created
{
//is being used by player (on any seat), or just created, or is about to fire or has finished firing, or when changing facing direction
if ( this.hasAttached() || time < 30 || time_til_fire > 0 || this.isFacingLeft() != this.get_bool("facing_left"))
{
// load new item if present in inventory
Vehicle_StandardControls(this, v);

Expand Down Expand Up @@ -135,8 +135,8 @@ void onTick(CBlob@ this)
}
else if (time % 30 == 0)
Vehicle_StandardControls(this, v); //just make sure it's updated

this.set_bool("had_attached", hasAttached);
this.set_bool("facing_left", this.isFacingLeft());
}

void GetButtonsFor(CBlob@ this, CBlob@ caller)
Expand Down
10 changes: 10 additions & 0 deletions Entities/Vehicles/Common/Vehicle.as
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ void onCommand(CBlob@ this, u8 cmd, CBitStream @params)
if (caller !is null)
this.server_AttachTo(caller, "MAG");
}
/// LET GO
else if (isServer && cmd == this.getCommandID("vehicle letgo"))
{
CBlob@ caller = getBlobByNetworkID(params.read_u16());

if (caller !is null)
{
this.server_DetachFrom(caller);
}
}
/// GET OUT
else if (isServer && cmd == this.getCommandID("vehicle getout"))
{
Expand Down
2 changes: 1 addition & 1 deletion Entities/Vehicles/Common/VehicleAttachment.as
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void GetButtonsFor(CBlob@ this, CBlob@ caller)
CBlob@ occBlob = ap.getOccupied();
if (occBlob !is null) //detach button
{
if (this.isOnGround() && occBlob.getName() != "mounted_bow") // HACK:
if (this.isOnGround() || !this.isInWater() || occBlob.getName() == "mounted_bow") // HACK:
{
CBitStream params;
params.write_netid(occBlob.getNetworkID());
Expand Down
53 changes: 35 additions & 18 deletions Entities/Vehicles/Common/VehicleCommon.as
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void Vehicle_Setup(CBlob@ this,
this.addCommandID("sync_ammo");
this.addCommandID("sync_last_fired");
this.addCommandID("putin_mag");
this.addCommandID("vehicle letgo");
this.addCommandID("vehicle getout");
this.addCommandID("reload");
this.addCommandID("recount ammo");
Expand Down Expand Up @@ -135,11 +136,6 @@ void Vehicle_AddAmmo(CBlob@ this, VehicleInfo@ v, int fireDelay, int fireAmount,
a.ammo_stocked = 0;
a.infinite_ammo = false;

if (getRules().hasTag("singleplayer"))
{
a.infinite_ammo = true;
}

v.ammo_types.push_back(a);
}

Expand Down Expand Up @@ -551,15 +547,36 @@ void Vehicle_StandardControls(CBlob@ this, VehicleInfo@ v)
for (uint i = 0; i < aps.length; i++)
{
AttachmentPoint@ ap = aps[i];
CBlob@ blob = ap.getOccupied();
CBlob@ occupied = ap.getOccupied();

//LET GO
AttachmentPoint@ pickup = this.getAttachments().getAttachmentPointByName("PICKUP");
if (pickup !is null)
{
CBlob@ carrier = pickup.getOccupied();

if (carrier !is null)
{
if (this.getTeamNum() != carrier.getTeamNum())
mugg91 marked this conversation as resolved.
Show resolved Hide resolved
{
CBitStream params;
params.write_u16(carrier.getNetworkID());
this.SendCommand(this.getCommandID("vehicle letgo"), params);
return;
}
}
} // let go

if (blob !is null && ap.socket)
if (occupied !is null && ap.socket)
{
// GET OUT
if (blob.isMyPlayer() && ap.isKeyJustPressed(key_up))
{
if (occupied.isMyPlayer() && ap.isKeyJustPressed(key_up)
|| (this.getTeamNum() != occupied.getTeamNum()
&& (this.getTeamNum() >= 0 && this.getTeamNum() <= 7)
&& (occupied.getName() == "knight" || occupied.getName() == "archer" || occupied.getName() == "builder")))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two checks are not good. You shouldn't be hardcoding team numbers and classes like this since it won't work with mods that add more teams or new classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"knight", "archer" and "builder is hard coded in a lot more places.
The ItemLimits script I made will also check for team number like this.
How to avoid this?
Although neutral blobs are usually set to -1 or 255, you can change to teams 8~254 via !team (number) in Sandbox. Is there a way to check if a blob is in grey team that doesn't involve checking team number?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"knight", "archer" and "builder is hard coded in a lot more places.
How to avoid this?

You should always endeavour to leave code in a better state than you found it. A potential solution would be to check for "player" tag, but migrants also have this tag which could cause rare side effects.

Although neutral blobs are usually set to -1 or 255, you can change to teams 8~254 via !team (number) in Sandbox. Is there a way to check if a blob is in grey team that doesn't involve checking team number?

There is nothing special with grey teams. They are only grey because there aren't enough colours to cover all team numbers. The only team that should be treated differently from the rest is spectator team (getRules().getSpectatorTeamNum()). -1 == 255 because team is u8 which wraps negative numbers around to the maximum.

Copy link
Contributor Author

@mugg91 mugg91 Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change to occupied.hasTag("player"). I don't see a problem since migrants are never attached to vehicles. It would seem there are already several such checks in the same file, anyway.

With the team check, this kind of check is in many places. I guess teams 0-254 should be treated as player teams and team 255 as a neutral team for animals and stuff that can be used by anyone (vehicles, doors).
I have seen places where the team check says "anything that is higher than team 100" or "anything that is higher than team 10". Those checks seem arbitrary and inconsistent. Up until now, I tried to be a more precise by handling teams 0-7 as player teams, because they are colored, and anything else as neutral team.

I can change that so that 0-254 are player teams instead. And 255 is a neutral team.
Is that what should be done?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change that so that 0-254 are player teams instead. And 255 is a neutral team. Is that what should be done?

Yes, this sounds like the most appropriate way of dealing with teams. Please remember to check for getRules().getSpectatorTeamNum() rather than specifically team 255.

Copy link
Contributor Author

@mugg91 mugg91 Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if it was unclear. I was not talking about spectator team.
I'm still talking about teams that are currently on the field playing. Of those, 0 to 254 should be handled as teams that players could get assigned to. Players can also get assigned to team 255, but behavior will be different. I'd like to make it so that blobs in team 255 can be used by anyone (except for special cases like when trampolines are loaded with the map, they can't be used by anyone).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hold up... why is there even a team or player check here? If the blob is already sitting on the vehicle, they must've been the correct team to get on it in the first place. And there is no need to check if it's a player since non-player blobs will never be pressing key_up to get out. Even if they somehow did, there is no harm in allowing them to detach from the vehicle like players do.

Looking at SeatHop.as line 32, there already appears to be a check to allow neutral teams to sit in vehicles.

Copy link
Contributor Author

@mugg91 mugg91 Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is so that when you are sitting in your vehicle and it is getting converted to the enemy, you are kicked out.
The player check is so that enemy crates and enemy Dinghies can be loaded/can stay in the catapult's mag.

I'm not working on "neutral players doing stuff" but I'm working on "any players using neutral stuff".

About getRules().getSpectatorTeamNum(), I can use it, but the Map loader will set blobs to -1 so I'm afraid of mismatch errors. Will getRules().getSpectatorTeamNum() always return -1? Can the Map loader call getRules().getSpectatorTeamNum()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Team is a u8 which means -1 wraps around to 255 (the maximum). getRules().getSpectatorTeamNum() returns 255 so it should match correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Sandbox mode, getRules().getSpectatorTeamNum() returns 200 for me.
I'm reworking team checks and adding another commit later.

{
CBitStream params;
params.write_u16(blob.getNetworkID());
params.write_u16(occupied.getNetworkID());
this.SendCommand(this.getCommandID("vehicle getout"), params);
return;
} // get out
Expand All @@ -571,7 +588,7 @@ void Vehicle_StandardControls(CBlob@ this, VehicleInfo@ v)
bool moveUp = false;
const f32 angle = this.getAngleDegrees();
// set facing
blob.SetFacingLeft(this.isFacingLeft());
occupied.SetFacingLeft(this.isFacingLeft());
const bool left = ap.isKeyPressed(key_left);
const bool right = ap.isKeyPressed(key_right);
const bool onground = this.isOnGround();
Expand Down Expand Up @@ -694,12 +711,12 @@ void Vehicle_StandardControls(CBlob@ this, VehicleInfo@ v)
}
} // driver

if (ap.name == "GUNNER" && !isKnocked(blob))
if (ap.name == "GUNNER" && !isKnocked(occupied))
{
// set facing
blob.SetFacingLeft(this.isFacingLeft());
occupied.SetFacingLeft(this.isFacingLeft());

if (blob.isMyPlayer() && ap.isKeyJustPressed(key_inventory) && v.charge == 0)
if (occupied.isMyPlayer() && ap.isKeyJustPressed(key_inventory) && v.charge == 0)
{
this.SendCommand(this.getCommandID("swap_ammo"));
}
Expand All @@ -713,10 +730,10 @@ void Vehicle_StandardControls(CBlob@ this, VehicleInfo@ v)
if (ap.isKeyPressed(key_action1))
{
v.firing = true;
if (canFire(this, v) && blob.isMyPlayer())
if (canFire(this, v) && occupied.isMyPlayer())
{
CBitStream fireParams;
fireParams.write_u16(blob.getNetworkID());
fireParams.write_u16(occupied.getNetworkID());
fireParams.write_u8(0);
this.SendCommand(this.getCommandID("fire"), fireParams);
}
Expand All @@ -734,10 +751,10 @@ void Vehicle_StandardControls(CBlob@ this, VehicleInfo@ v)
v.charge = 0;
v.cooldown_time = Maths::Max(v.cooldown_time, 15);
}
else if (Vehicle_canFire(this, v, ap.isKeyPressed(key_action1), ap.wasKeyPressed(key_action1), charge) && canFire(this, v) && blob.isMyPlayer())
else if (Vehicle_canFire(this, v, ap.isKeyPressed(key_action1), ap.wasKeyPressed(key_action1), charge) && canFire(this, v) && occupied.isMyPlayer())
{
CBitStream fireParams;
fireParams.write_u16(blob.getNetworkID());
fireParams.write_u16(occupied.getNetworkID());
fireParams.write_u8(charge);
this.SendCommand(this.getCommandID("fire"), fireParams);
}
Expand Down