Skip to content

Commit

Permalink
Merge changes from develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Tran-Foxxo committed Jun 8, 2020
1 parent 9bdf5d7 commit 280ce67
Show file tree
Hide file tree
Showing 35 changed files with 1,079 additions and 197 deletions.
2 changes: 1 addition & 1 deletion src/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ private void Start(int frameid)
Camera.SetFrameCenter(Timeline.GetFrame(frameid).CalculateCenter());

game.UpdateCursor();
switch (Settings.PlaybackZoomType)
switch (TrackRecorder.Recording ? 0 : Settings.PlaybackZoomType)
{
case 0://default
UseUserZoom = false;
Expand Down
73 changes: 73 additions & 0 deletions src/Game/GameTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Author:
// Noah Ablaseau <[email protected]>
//
// Copyright (c) 2017
//
// This program is free software: you can redistribute it and/or modify
// it 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;

namespace linerider.Game
{
public class GameTrigger
{
public const int TriggerTypes = 1;
public int Start;
public int End;
public TriggerType TriggerType;
public float ZoomTarget = 4;

public bool CompareTo(GameTrigger other)
{
if (other == null)
return false;
return TriggerType == other.TriggerType &&
Start == other.Start &&
End == other.End &&
ZoomTarget == other.ZoomTarget;
}
public bool ActivateZoom(int hitdelta, ref float currentzoom)
{
bool handled = false;
if (TriggerType == TriggerType.Zoom)
{
int zoomframes = End - Start;
if (currentzoom != ZoomTarget)
{
if (hitdelta >= 0 && hitdelta < zoomframes)
{
var diff = ZoomTarget - currentzoom;
currentzoom = currentzoom + (diff / (zoomframes - hitdelta));
handled = true;
}
else
{
currentzoom = ZoomTarget;
}
}
}
return handled;
}
public GameTrigger Clone()
{
return new GameTrigger()
{
Start = Start,
End = End,
TriggerType = TriggerType,
ZoomTarget = ZoomTarget
};
}
}
}
7 changes: 7 additions & 0 deletions src/Game/HitTestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ public bool IsHitBy(int id, int frame)
return false;
}

public bool HasUniqueCollisions(int frame)
{
if (frame >= _unique_frame_collisions.Count)
throw new IndexOutOfRangeException("Frame does not have hit test calculations");
return _unique_frame_collisions[frame].Count != 0;
}

public void Reset()
{
if (_currentframe != -1)
Expand Down
1 change: 1 addition & 0 deletions src/Game/Lines/LineTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class LineTrigger
public bool ZoomTrigger = false;
public float ZoomTarget = 4;
public int ZoomFrames = 40;
public int LineID = -1;
public LineTrigger()
{
}
Expand Down
18 changes: 3 additions & 15 deletions src/Game/Physics/Rider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ private unsafe static void ProcessLines(
ISimulationGrid grid,
SimulationPoint[] body,
ref RectLRTB physinfo,
ref int activetriggers,
LinkedList<int> collisions = null)
{
int bodylen = body.Length;
Expand All @@ -199,16 +198,8 @@ private unsafe static void ProcessLines(
if (line.Interact(ref body[i]))
{
collisions?.AddLast(line.ID);
if (line.Trigger != null)
{
if (activetriggers != line.ID)
{
activetriggers = line.ID;
}
}
}
}

}
}
}
Expand Down Expand Up @@ -377,13 +368,11 @@ public static DoubleRect GetBounds(Rider r)
}
public Rider Simulate(Track track, int maxiteration = 6, LinkedList<int> collisions = null)
{
int trig = 0;
return Simulate(track.Grid, track.Bones, ref trig, collisions, maxiteration);
return Simulate(track.Grid, track.Bones, collisions, maxiteration);
}
public Rider Simulate(
ISimulationGrid grid,
Bone[] bones,
ref int activetriggers,
LinkedList<int> collisions,
int maxiteration = 6,
bool stepscarf = true,
Expand All @@ -401,7 +390,7 @@ public Rider Simulate(
for (int i = 0; i < maxiteration; i++)
{
ProcessBones(bones, body, ref dead, ref rState);
ProcessLines(grid, body, ref phys, ref activetriggers, collisions);
ProcessLines(grid, body, ref phys, collisions);
}
}
if (maxiteration == 6)
Expand Down Expand Up @@ -502,8 +491,7 @@ public List<int> Diagnose(
{
return breaks;
}
int trig = 0;
ProcessLines(grid, body, ref phys, ref trig);
ProcessLines(grid, body, ref phys);
}
}
if (maxiteration == 6)
Expand Down
2 changes: 1 addition & 1 deletion src/Game/Physics/SimulationGridStatic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static List<CellLocation> GetGridPositions(Vector2d linestart, Vector2d l
var gridend = CellInfo(lineend.X, lineend.Y);

ret.Add(cell);
if ((diff.X == 0 && diff.Y == 0) || (cell.X == gridend.X && cell.Y == gridend.Y))
if (cell.X == gridend.X && cell.Y == gridend.Y)
return ret;

int p1X = Math.Min(cell.X, gridend.X),
Expand Down
2 changes: 0 additions & 2 deletions src/Game/Timeline.Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,9 @@ private bool CheckInteraction(int frame)
{
// even though its this frame that may need changing, we have to
// regenerate it using the previos frame.
int trig = 0;
var newsimulated = _frames[frame - 1].Rider.Simulate(
_track.Grid,
_track.Bones,
ref trig,
null,
6,
false);
Expand Down
71 changes: 41 additions & 30 deletions src/Game/Timeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public partial class Timeline
struct frameinfo
{
public Rider Rider;
public int TriggerLineID;
public int TriggerHitFrame;
public float Zoom;
}
private HitTestManager _hittest = new HitTestManager();
Expand Down Expand Up @@ -95,7 +93,7 @@ public void Restart(Rider state, float zoom)
{
_hittest.Reset();
_frames.Clear();
_frames.Add(new frameinfo() { Rider = state, TriggerHitFrame = -1, TriggerLineID = -1, Zoom = zoom });
_frames.Add(new frameinfo() { Rider = state, Zoom = zoom });
using (changesync.AcquireWrite())
{
_first_invalid_frame = _frames.Count;
Expand Down Expand Up @@ -143,11 +141,9 @@ public Rider GetFrame(int frame, int iteration = 6)
bool isiteration = iteration != 6 && frame > 0;
if (iteration != 6 && frame > 0)
{
int trig = 0;
return GetFrame(frame - 1).Simulate(
_track.Grid,
_track.Bones,
ref trig,
null,
iteration,
frameid: frame);
Expand Down Expand Up @@ -193,21 +189,23 @@ public Rider[] GetFrames(int framestart, int count)
return ret;
}
}
public void TriggerChanged(GameLine line)
public bool IsFrameUniqueCollision(int frame)
{
int framehit;
using (_framesync.AcquireWrite())
{
framehit = _hittest.GetHitFrame(line.ID);
}
if (framehit != -1)
GetFrame(frame);
return _hittest.HasUniqueCollisions(frame);
}

public void TriggerChanged(GameTrigger oldtrigger, GameTrigger newtrigger)
{
using (changesync.AcquireWrite())
{
using (changesync.AcquireWrite())
{
_first_invalid_frame = Math.Min(framehit, _first_invalid_frame);
}
var earliest = Math.Min(oldtrigger.Start, newtrigger.Start);
_first_invalid_frame = Math.Max(
1,
Math.Min(earliest, _first_invalid_frame));
}
}

private void UnsafeEnsureFrameValid(int frame)
{
int start;
Expand All @@ -225,6 +223,27 @@ private void UnsafeEnsureFrameValid(int frame)
ThreadUnsafeRunFrames(start, count);
}
}
private bool GetTriggersForFrame(GameTrigger[] triggers, int frame)
{
bool foundtrigger = false;
for (int i = 0; i < triggers.Length; i++)
{
triggers[i] = null;
}
foreach (var trigger in _track.Triggers)
{
if (trigger.Start <= frame && trigger.End >= frame)
{
int index = (int)trigger.TriggerType;
var prev = triggers[index];
if (!(prev?.Start > trigger.Start))
triggers[index] = trigger;

foundtrigger = true;
}
}
return foundtrigger;
}
/// <summary>
/// The meat of the recompute engine, updating hit test and the
/// cached frames.
Expand All @@ -249,31 +268,23 @@ private void ThreadUnsafeRunFrames(int start, int count)
using (var sync = changesync.AcquireRead())
{
_hittest.MarkFirstInvalid(start);
GameTrigger[] triggers = new GameTrigger[GameTrigger.TriggerTypes];
for (int i = 0; i < count; i++)
{
int currentframe = start + i;
var collisions = new LinkedList<int>();
var oldtrigid = current.TriggerLineID;
var zoom =
current.Rider = current.Rider.Simulate(
_savedcells,
bones,
ref current.TriggerLineID,
collisions,
frameid: currentframe);
if (current.TriggerLineID != oldtrigid)
{
current.TriggerHitFrame = currentframe;
}
if (current.TriggerLineID != -1)
if (GetTriggersForFrame(triggers, currentframe))
{
var std = (StandardLine)_track.LineLookup[current.TriggerLineID];
Debug.Assert(std.Trigger != null, "Trigger line proc on line with no trigger");
var delta = currentframe - current.TriggerHitFrame;
if (!std.Trigger.Activate(delta, ref current.Zoom))
var zoomtrigger = triggers[(int)TriggerType.Zoom];
if (zoomtrigger != null)
{
current.TriggerLineID = -1;
current.TriggerHitFrame = -1;
var delta = currentframe - zoomtrigger.Start;
zoomtrigger.ActivateZoom(delta, ref current.Zoom);
}
}
steps[i] = current;
Expand Down
1 change: 1 addition & 0 deletions src/Game/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Track
public SimulationGrid Grid = new SimulationGrid();
public LinkedList<int> Lines = new LinkedList<int>();
public Dictionary<int, GameLine> LineLookup = new Dictionary<int, GameLine>();
public List<GameTrigger> Triggers = new List<GameTrigger>();

public string Name = Constants.DefaultTrackName;
public string Filename = null;
Expand Down
3 changes: 1 addition & 2 deletions src/Game/TrackReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public IEnumerable<GameLine> GetLinesInRect(DoubleRect rect, bool precise)
/// </summary>
public Rider TickBasic(Rider state, int maxiteration = 6)
{
int trig = 0;
return state.Simulate(_track.Grid, _track.Bones, ref trig, null, maxiteration);
return state.Simulate(_track.Grid, _track.Bones, null, maxiteration);
}
public string SaveTrackTrk(string savename)
{
Expand Down
25 changes: 15 additions & 10 deletions src/Game/TrackWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ public EditorGrid Cells
return _editorcells;
}
}
public List<GameTrigger> Triggers
{
get
{
if (_disposed)
throw new ObjectDisposedException("TrackWriter");
return _track.Triggers;
}
set
{
if (_disposed)
throw new ObjectDisposedException("TrackWriter");
_track.Triggers = value;
}
}
protected TrackWriter(ResourceSync.ResourceLock sync, Track track)
: base(sync, track)
{
Expand Down Expand Up @@ -174,7 +189,6 @@ public void ReplaceLine(GameLine oldline, GameLine newline)
RegisterUndoAction(oldline, newline);

var std = oldline as StandardLine;
bool triggerchange = false;
if (std != null)
{
SaveCells(oldline.Position, oldline.Position2);
Expand All @@ -190,21 +204,12 @@ public void ReplaceLine(GameLine oldline, GameLine newline)
}
if (_updateextensions)
AddExtensions(newstd);
if (std.Trigger != null || newstd.Trigger != null)
{
if (std.Trigger == null)
triggerchange = true;
else if (!std.Trigger.CompareTo(newstd.Trigger))
triggerchange = true;
}
}

_editorcells.RemoveLine(oldline);
_editorcells.AddLine(newline);

Track.LineLookup[newline.ID] = newline;
if (triggerchange)
_timeline.TriggerChanged(newline);
_renderer.RedrawLine(newline);
}

Expand Down
Loading

0 comments on commit 280ce67

Please sign in to comment.