Move JumpJet traits to the C&C library.

This commit is contained in:
Matthias Mailänder
2023-03-05 11:41:05 +01:00
committed by Pavel Penev
parent 63b9f18d05
commit f6e5bee334
3 changed files with 7 additions and 3 deletions

View File

@@ -0,0 +1,59 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using OpenRA.Mods.Common.Traits;
namespace OpenRA.Mods.Cnc.Traits
{
public class GrantConditionOnJumpjetLayerInfo : GrantConditionOnLayerInfo
{
public override object Create(ActorInitializer init) { return new GrantConditionOnJumpjetLayer(this); }
public override void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
var mobileInfo = ai.TraitInfoOrDefault<MobileInfo>();
if (mobileInfo == null || !(mobileInfo.LocomotorInfo is JumpjetLocomotorInfo))
throw new YamlException("GrantConditionOnJumpjetLayer requires Mobile to be linked to a JumpjetLocomotor!");
base.RulesetLoaded(rules, ai);
}
}
public class GrantConditionOnJumpjetLayer : GrantConditionOnLayer<GrantConditionOnJumpjetLayerInfo>, INotifyFinishedMoving
{
bool jumpjetInAir;
public GrantConditionOnJumpjetLayer(GrantConditionOnJumpjetLayerInfo info)
: base(info, CustomMovementLayerType.Jumpjet) { }
void INotifyFinishedMoving.FinishedMoving(Actor self, byte oldLayer, byte newLayer)
{
if (jumpjetInAir && oldLayer != ValidLayerType && newLayer != ValidLayerType)
UpdateConditions(self, oldLayer, newLayer);
}
protected override void UpdateConditions(Actor self, byte oldLayer, byte newLayer)
{
if (!jumpjetInAir && newLayer == ValidLayerType && oldLayer != ValidLayerType && conditionToken == Actor.InvalidConditionToken)
{
conditionToken = self.GrantCondition(Info.Condition);
jumpjetInAir = true;
}
// By the time the condition is meant to be revoked, the 'oldLayer' is already no longer the Jumpjet layer, either
if (jumpjetInAir && newLayer != ValidLayerType && oldLayer != ValidLayerType && conditionToken != Actor.InvalidConditionToken)
{
conditionToken = self.RevokeCondition(conditionToken);
jumpjetInAir = false;
}
}
}
}

View File

@@ -0,0 +1,108 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using OpenRA.Mods.Common.Pathfinder;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
{
[TraitLocation(SystemActors.World)]
public class JumpjetActorLayerInfo : TraitInfo, ICustomMovementLayerInfo
{
[Desc("Terrain type of the airborne layer.")]
public readonly string TerrainType = "Jumpjet";
[Desc("Height offset relative to the smoothed terrain for movement.")]
public readonly WDist HeightOffset = new WDist(3992);
[Desc("Cell radius for smoothing adjacent cell heights.")]
public readonly int SmoothingRadius = 2;
public override object Create(ActorInitializer init) { return new JumpjetActorLayer(init.Self, this); }
}
public class JumpjetActorLayer : ICustomMovementLayer
{
readonly Map map;
readonly byte terrainIndex;
readonly CellLayer<int> height;
public JumpjetActorLayer(Actor self, JumpjetActorLayerInfo info)
{
map = self.World.Map;
terrainIndex = self.World.Map.Rules.TerrainInfo.GetTerrainIndex(info.TerrainType);
height = new CellLayer<int>(map);
var cellHeight = self.World.Map.CellHeightStep.Length;
foreach (var c in map.AllCells)
{
var neighbourCount = 0;
var neighbourHeight = 0;
for (var dy = -info.SmoothingRadius; dy <= info.SmoothingRadius; dy++)
{
for (var dx = -info.SmoothingRadius; dx <= info.SmoothingRadius; dx++)
{
var neighbour = c + new CVec(dx, dy);
if (!map.AllCells.Contains(neighbour))
continue;
neighbourCount++;
neighbourHeight += map.Height[neighbour];
}
}
height[c] = info.HeightOffset.Length + neighbourHeight * cellHeight / neighbourCount;
}
}
bool ICustomMovementLayer.EnabledForLocomotor(LocomotorInfo li) { return li is JumpjetLocomotorInfo; }
byte ICustomMovementLayer.Index => CustomMovementLayerType.Jumpjet;
bool ICustomMovementLayer.InteractsWithDefaultLayer => true;
bool ICustomMovementLayer.ReturnToGroundLayerOnIdle => true;
WPos ICustomMovementLayer.CenterOfCell(CPos cell)
{
var pos = map.CenterOfCell(cell);
return pos + new WVec(0, 0, height[cell] - pos.Z);
}
bool ValidTransitionCell(CPos cell, LocomotorInfo li)
{
var terrainType = map.GetTerrainInfo(cell).Type;
var jli = (JumpjetLocomotorInfo)li;
if (!jli.JumpjetTransitionTerrainTypes.Contains(terrainType) && jli.JumpjetTransitionTerrainTypes.Count > 0)
return false;
if (jli.JumpjetTransitionOnRamps)
return true;
return map.Ramp[cell] == 0;
}
short ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell)
{
var jli = (JumpjetLocomotorInfo)li;
return ValidTransitionCell(cell, jli) ? jli.JumpjetTransitionCost : PathGraph.MovementCostForUnreachableCell;
}
short ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell)
{
var jli = (JumpjetLocomotorInfo)li;
return ValidTransitionCell(cell, jli) ? jli.JumpjetTransitionCost : PathGraph.MovementCostForUnreachableCell;
}
byte ICustomMovementLayer.GetTerrainIndex(CPos cell)
{
return terrainIndex;
}
}
}

View File

@@ -0,0 +1,41 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Used by Mobile. Required for jumpjet actors. Attach these to the world actor. You can have multiple variants by adding @suffixes.")]
public class JumpjetLocomotorInfo : LocomotorInfo
{
[Desc("Pathfinding cost for taking off or landing.")]
public readonly short JumpjetTransitionCost = 0;
[Desc("The terrain types that this actor can transition on. Leave empty to allow any.")]
public readonly HashSet<string> JumpjetTransitionTerrainTypes = new HashSet<string>();
[Desc("Can this actor transition on slopes?")]
public readonly bool JumpjetTransitionOnRamps = true;
public override bool DisableDomainPassabilityCheck => true;
public override object Create(ActorInitializer init) { return new JumpjetLocomotor(init.Self, this); }
}
public class JumpjetLocomotor : Locomotor
{
public JumpjetLocomotor(Actor self, JumpjetLocomotorInfo info)
: base(self, info) { }
}
}