Skip to content

Commit

Permalink
Got terracc finally working on actual programs
Browse files Browse the repository at this point in the history
  • Loading branch information
misprit7 committed Oct 16, 2023
1 parent 4f94bf2 commit 7d3df67
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
29 changes: 19 additions & 10 deletions Accelerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ internal static class Accelerator
public static int clockGroup = -1;
public static int clockCount = 0;
public static int clockMax = int.MaxValue;
public static Point default_clock_coord = new Point(3194,153);
public static int default_clock_clr = 3;

/**********************************************************************
* Construction Variables
Expand Down Expand Up @@ -325,7 +327,7 @@ private static void TogglePixelBox(int x, int y)
/*
* Hit a single wire
*/
private static void HitWireSingle(uint p)
public static void HitWireSingle(uint p)
{
// _wireSkip should 100% be a hashset, come on relogic
//if (WiringWrapper._wireSkip.ContainsKey(p)) return;
Expand Down Expand Up @@ -453,6 +455,7 @@ public static void HitWire(DoubleStack<Point16> next, int wireType)
var p = next.PopFront();
int group = wireGroup[p.X, p.Y, c];


/* if(WireHead.useTerracc && triggerable[group].Length == groupStandardLamps[group].Count) continue; */

if (alreadyHit.Contains(group)) continue;
Expand All @@ -465,11 +468,9 @@ public static void HitWire(DoubleStack<Point16> next, int wireType)
if (group == clockGroup)
{
++clockCount;
if (clockCount > clockMax)
if (clockCount == clockMax)
{
Console.WriteLine("Clock finished");
clockCount = 0;
clockGroup = -1;
clockMax = int.MaxValue;
}
}
Expand Down Expand Up @@ -678,7 +679,8 @@ public static void Preprocess()
// Inverts pbId2Coord to a dictionary with inverse mapping
pbCoord2Id = pbId2Coord.Select((s, i) => new { s, i }).ToDictionary(x => x.s, x => x.i);
numPb = pbId2Coord.Count();

// Hardcoded default
clockGroup = wireGroup[default_clock_coord.X, default_clock_coord.Y, default_clock_clr];
}

/*
Expand Down Expand Up @@ -706,16 +708,23 @@ public static void BringInSync(bool full=true)
if(WireHead.useTerracc){
byte[] states = new byte[numGroups];
TerraCC.read_states(states);
for(int i = 0; i < numGroups; ++i){
for(int g = 0; g < numGroups; ++g){
/* if((states[i]==1) != groupState[i]){ */
/* Console.WriteLine($"State {i} new state: {states[i]}"); */
/* } */
groupOutOfSync[i] = (states[i]==1) != groupState[i];
groupState[i] = states[i] == 1;


groupOutOfSync[g] = (states[g]==1) != groupState[g];

// Do our best to handle triggered blocks, this will only work
// if they've triggered at most once since the last sync though
if(groupOutOfSync[i]){
if(groupOutOfSync[g]){
// Don't update internal state if too big an update
if(!full && toggleable[g].Length >= maxFastRefresh) continue;

// Note: this means that after a fast sync some states will won't be pulled in
groupState[g] = states[g] == 1;

// This might not be robust, I think if you make purposefully
// confusing chained teleporters this breaks.
//
Expand All @@ -725,7 +734,7 @@ public static void BringInSync(bool full=true)
WiringWrapper._teleport[0].Y = -1f;
WiringWrapper._teleport[1].X = -1f;
WiringWrapper._teleport[1].Y = -1f;
foreach(uint tile in triggerable[i]){
foreach(uint tile in triggerable[g]){
Point16 p = uint2Point(tile);
if(standardLamps[p.X,p.Y]) break;
HitWireSingle(tile);
Expand Down
5 changes: 4 additions & 1 deletion Commands/AccelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void Exec(string[] args)
case "s":
// Print to console later once sync is actually finished
WireHead.toExec.Enqueue(() => {
Accelerator.BringInSync();
Accelerator.BringInSync(true);
Console.WriteLine("Sync complete");
});
break;
Expand Down Expand Up @@ -52,7 +52,9 @@ public static void Exec(string[] args)
case "compile":
case "terracc":
case "c":
Console.WriteLine("Received compile command");
WireHead.toExec.Enqueue(() => {
Console.WriteLine("Starting toExec");
if (WireHead.vanillaWiring){
WireHead.AddEvents();
Accelerator.Preprocess();
Expand All @@ -63,6 +65,7 @@ public static void Exec(string[] args)
TerraCC.compile();
}
TerraCC.enable();
Console.WriteLine("Executed compile command");
});
break;
default:
Expand Down
1 change: 1 addition & 0 deletions Commands/Monitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static void Exec(string[] args)
}

Accelerator.clockGroup = group;
Accelerator.clockCount = 0;
if(WireHead.useTerracc)
WireHead.toExec.Enqueue(()=>TerraCC.set_clock(group));
if (max > 0)
Expand Down
16 changes: 15 additions & 1 deletion Items/GroupStaff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Terraria.ID;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
using System.Text;

namespace WireHead.Items
{
Expand Down Expand Up @@ -32,7 +33,20 @@ public override void SetDefaults()
{
int x = Player.tileTargetX;
int y = Player.tileTargetY;
Main.NewText($"Red: {Accelerator.wireGroup[x,y,0]}, Blue: {Accelerator.wireGroup[x,y,1]}, Green: {Accelerator.wireGroup[x,y,2]}, Yellow: {Accelerator.wireGroup[x,y,3]}");
StringBuilder sb = new StringBuilder();
string[] clrs = {"Red", "Blue", "Green", "Yellow"};
for(int c = 0; c < Accelerator.colors; ++c){
sb.Append(clrs[c] + ": ");
int g = Accelerator.wireGroup[x,y,c];
sb.Append(g);
if(g>= 0){
sb.Append($" ({(Accelerator.groupState[g] ? 1 : 0)}, {(Accelerator.groupOutOfSync[g] ? 1 : 0)})");
}
if(c != 3){
sb.Append(", ");
}
}
Main.NewText(sb.ToString());

return true;
}
Expand Down
7 changes: 4 additions & 3 deletions TerraCC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ private static string faulty_str(){
*/
public static void transpile()
{

string c_file = $@"
Accelerator.Preprocess();
string c_file = $@"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
Expand All @@ -263,7 +263,7 @@ public static void transpile()
static bool pb_s[num_pb] = {{0}};

// Clock monitor
static int clock_group = {Accelerator.clockCount};
static int clock_group = {Accelerator.clockGroup};
static int clock_count = 0;

// Standard lamp connections
Expand Down Expand Up @@ -375,6 +375,7 @@ public static void compile(){
process.StartInfo = processInfo;

// Start the process
Console.WriteLine($"Started compiling");
process.Start();

// Read the standard output of the command
Expand Down
32 changes: 26 additions & 6 deletions WireHead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ public override void PreSaveAndQuit()
// This used to be PostUpdateWorld, have no idea why it randomly broke
public override void PostUpdateEverything()
{
foreach (var action in WireHead.toExec)
{
action();
}
WireHead.toExec.Clear();

if(WireHead.useTerracc){
if(Accelerator.numToHit > 0){
Expand All @@ -274,21 +279,36 @@ public override void PostUpdateEverything()
TerraCC.trigger(Accelerator.toHit, Accelerator.numToHit);
Accelerator.numToHit = 0;
Accelerator.SyncPb();
Accelerator.clockCount = TerraCC.read_clock();
int cc = TerraCC.read_clock();
// Only do even number of times, bringinsync handles last one if odd
/* Console.WriteLine($"Diff: {(cc-Accelerator.clockCount)}, to trigger: {2*((cc-Accelerator.clockCount)/2)}, clockGroup: {Accelerator.clockGroup}, size: {Accelerator.triggerable[Accelerator.clockGroup].Length}"); */
for(int i = 0; i < 2*((cc-Accelerator.clockCount)/2); ++i){
WiringWrapper._teleport[0].X = -1f;
WiringWrapper._teleport[0].Y = -1f;
WiringWrapper._teleport[1].X = -1f;
WiringWrapper._teleport[1].Y = -1f;
foreach(uint tile in Accelerator.triggerable[Accelerator.clockGroup]){
Point16 p = Accelerator.uint2Point(tile);
if(Accelerator.standardLamps[p.X,p.Y]) continue;
Accelerator.HitWireSingle(tile);
/* Console.WriteLine($"Triggering, x:{p.X}, y:{p.Y}"); */
}
if (WiringWrapper._teleport[0].X >= 0f && WiringWrapper._teleport[1].X >= 0f)
WiringWrapper.Teleport();
}
Accelerator.clockCount = cc;
if(Accelerator.clockCount > Accelerator.clockMax){
Console.WriteLine("Clock finished");
Accelerator.clockCount = 0;
TerraCC.set_clock(-1);
}
}
Accelerator.BringInSync(false);

foreach (var action in WireHead.toExec)
{
action();
// Only fast refresh in single player to minimize network stuff
if (Main.netMode == NetmodeID.SinglePlayer){
Accelerator.BringInSync(false);
}

WireHead.toExec.Clear();
base.PostUpdateWorld();
}
}
Expand Down
8 changes: 4 additions & 4 deletions WiringWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace Terraria
{
public static class WiringWrapper
{
public static bool blockPlayerTeleportationForOneIteration;
// Use Wiring.blockPlayerTeleportationForOneIteration since other files check it
/* public static bool blockPlayerTeleportationForOneIteration; */
public static bool running;
public static Dictionary<Point16, bool> _wireSkip;
public static DoubleStack<Point16> _wireList;
Expand Down Expand Up @@ -644,10 +645,9 @@ private static void LogicGatePass()
}

_GatesDone.Clear();
if (blockPlayerTeleportationForOneIteration)
if (Wiring.blockPlayerTeleportationForOneIteration)
{
// Other files check this so we need to make sure it stays in sync
blockPlayerTeleportationForOneIteration = false;
Wiring.blockPlayerTeleportationForOneIteration = false;
}
}
Expand Down Expand Up @@ -2560,7 +2560,7 @@ public static void Teleport()
if (i == 1)
value = new Vector2(array[0].X - array[1].X, array[0].Y - array[1].Y);

if (!blockPlayerTeleportationForOneIteration)
if (!Wiring.blockPlayerTeleportationForOneIteration)
{
for (int j = 0; j < 255; j++)
{
Expand Down

0 comments on commit 7d3df67

Please sign in to comment.