This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts MenuQuoteAnother problem with global variables - we cannot serialize them easily and write into the savegame. So even if we had global variables (shared across all the scripts), it will be very easy to write bad code (which will prevent the game from properly saving the game state). So, we have storage scripting API instead. It's also very limited, but for the most cases, it was enough to get the things done.
using(npc);
using(console);
using(timer);
var SC_Salvage_Object_Storage =
{
AddSpaceObjectToStorage: function (system_id, SpaceObjectID, randomYieldToStartOff)
{
if (storage.IsSetGlobal("systemid_" + system_id + "_salvage"))
{
var toReceiveData = storage.GetGlobal("systemid_" + system_id + "_salvage");
var objectdata = { sys_id: system_id, id: SpaceObjectID, yield: randomYieldToStartOff, GameTime: timer.GetGameTime() };
toReceiveData.push(objectdata);
storage.SetGlobal("systemid_" + system_id + "_salvage", toReceiveData);
/*var someTest;
var toReceiveData = storage.GetGlobal("drone_mining_added_" + chosenPosition);
var droneData = { id: droneid, index: droneIndex };
toReceiveData[droneIndex] = droneData;
storage.SetGlobal("drone_mining_added_" + chosenPosition, toReceiveData);*/
}
else
{
var toSendData = [];
var objectdata = { sys_id: system_id, id: SpaceObjectID, yield: randomYieldToStartOff, GameTime: timer.GetGameTime()};
toSendData.push(objectdata);
storage.SetGlobal("systemid_" + system_id + "_salvage", toSendData);
}
},
GetSpaceObjectStorageLength: function (system_id)
{
if (storage.IsSetGlobal("systemid_" + system_id + "_salvage"))
{
var toReceiveData = storage.GetGlobal("systemid_" + system_id + "_salvage");
return toReceiveData.length;
}
else
{
return 0;
}
},
RemoveFromYield: function (system_id, SpaceObjectID, currentYieldMinus)
{
if (storage.IsSetGlobal("systemid_" + system_id + "_salvage"))
{
var toReceiveData = storage.GetGlobal("systemid_" + system_id + "_salvage");
for (var s = 0; s < toReceiveData.length;s++)
{
if (toReceiveData[s].id == SpaceObjectID)
{
toReceiveData[s].yield = toReceiveData[s].yield - currentYieldMinus;
break;
}
}
storage.SetGlobal("systemid_" + system_id + "_salvage", toReceiveData);
}
else
{
console.PrintError("the space object doesn't exist in the global array and this message is never supposed to print even with a console debug_reinit. If it prints, tell me. 0");
}
},
SetDepleted: function (system_id, SpaceObjectID)
{
if (storage.IsSetGlobal("systemid_" + system_id + "_salvage"))
{
var toReceiveData = storage.GetGlobal("systemid_" + system_id + "_salvage");
var s = 0
for (s = 0; s < toReceiveData.length; s++)
{
if (toReceiveData[s].id == SpaceObjectID)
{
toReceiveData[s] = null;
break;
}
}
toReceiveData.splice(s, 1);
storage.SetGlobal("systemid_" + system_id + "_salvage", toReceiveData);
}
else
{
console.PrintError("the space object doesn't exist in the global array and this message is never supposed to print even with a console debug_reinit. If it prints, tell me. 1");
}
},
GetSpaceObjectYield: function (system_id, SpaceObjectID)
{
if (storage.IsSetGlobal("systemid_" + system_id + "_salvage"))
{
var toReceiveData = storage.GetGlobal("systemid_" + system_id + "_salvage");
for (var s = 0; s < toReceiveData.length; s++)
{
if (toReceiveData[s].id == SpaceObjectID)
{
return toReceiveData[s].yield;
}
}
}
else
{
console.PrintError("the space object doesn't exist in the global array and this message is never supposed to print even with a console debug_reinit. If it prints, tell me. 2");
}
},
};
using(npc);
using(console);
using(timer);
var counterFrame = 0;
var counterDevice = 0;
var SC_Salvage_Object_Timer =
{
ClearCurrentDevice: function (sys_id, device_id, ship_id, slot_id) //IsWorking
{
game.IsShipPlayerControlled(ship_id);
var playerName = game.GetShipOwner(ship_id);
if (storage.IsSetGlobal("GlobalIndex_Player_" + playerName))
{
var someGlobalIndex = storage.GetGlobal("GlobalIndex_Player_" + playerName);
if (!storage.IsSetGlobal("systemid_" + sys_id + "_ship_" + ship_id + "_salvage_device_" + device_id))
{
storage.SetGlobal("systemid_" + sys_id + "_ship_" + ship_id + "_salvage_device_" + device_id, { swtch: 1, ship_id: ship_id, device_id: device_id, slot_id: slot_id, timer_id: null, reset: 1 });
}
else
{
var globalSalvageStorage = storage.GetGlobal("systemid_" + sys_id + "_ship_" + ship_id + "_salvage_device_" + device_id);
if (globalSalvageStorage != null) {
if (globalSalvageStorage.timer_id != null)
{
//timer.AddOrUpdate("CustomOnFrame", null);
//timer.AddOrUpdate(0.25, "CustomOnFrame", null, 1); //
game.ShipStopSound(ship_id, "mining_process_" + slot_id);
visual.DeviceDeactivateEffect(ship_id, device_id, "mining_visual_effect");
timer.ClearTimer(globalSalvageStorage.timer_id);
var data = { swtch: 1, ship_id: ship_id, device_id: device_id, slot_id: slot_id, timer_id: null, reset: 0 };
storage.SetGlobal("systemid_" + sys_id + "_ship_" + ship_id + "_salvage_device_" + device_id, data);
//console.PrintError("cleared device timer: " + globalSalvageStorage.timer_id);
return 1;
}
else
{
//console.PrintError("null timer0");
return 0;
}
}
else
{
//console.PrintError("null globalSalvageStorage");
return 0;
}
}
}
},
SetDevice: function (sys_id, device_id, ship_id, timer_id) //IsWorking
{
game.IsShipPlayerControlled(ship_id);
var playerName = game.GetShipOwner(ship_id);
if (storage.IsSetGlobal("GlobalIndex_Player_" + playerName))
{
var someGlobalIndex = storage.GetGlobal("GlobalIndex_Player_" + playerName);
}
},
AddDeviceID: function (sys_id, device_id, slot_id, ship_id, timer_id)
{
game.IsShipPlayerControlled(ship_id);
var playerName = game.GetShipOwner(ship_id);
if (storage.IsSetGlobal("GlobalIndex_Player_" + playerName))
{
var someGlobalIndex = storage.GetGlobal("GlobalIndex_Player_" + playerName);
//storage.GetGlobal("systemid_" + sys_id + "_ship_" + ship_id + "_salvage_device_" + device_id); //, { swtch: 1, ship_id: ship_id, device_id: device_id, slot_id: slot_id, timer_id: timer_id}
var globalSalvageStorage = storage.GetGlobal("systemid_" + sys_id + "_ship_" + ship_id + "_salvage_device_" + device_id);
if (globalSalvageStorage != null) {
if (globalSalvageStorage.timer_id != null)
{
timer.ClearTimer(globalSalvageStorage.timer_id);
//console.PrintError("cleared device timer: " + globalSalvageStorage.timer_id);
}
else {
//console.PrintError("null timer1");
}
}
else {
//console.PrintError("null globalSalvageStorage");
}
var data = { swtch: 1, ship_id: ship_id, device_id: device_id, slot_id: slot_id, timer_id: timer_id, reset: 0 };
storage.SetGlobal("systemid_" + sys_id + "_ship_" + ship_id + "_salvage_device_" + device_id, data );
}
var deviceArray = [];
if (deviceArray == null)
{
deviceArray = [];
}
var objectData = { sys_id: sys_id, device_id: device_id, ship_id: ship_id };
deviceArray.push(objectData);
var arrayData = { index: deviceArray.length - 1, length: deviceArray.length };
return arrayData;
},
AddCounterFrame: function ()
{
counterFrame++;
return counterFrame;
},
GetCounterFrame: function () {
return counterFrame;
},
ResetCounterFrame: function ()
{
counterFrame = 0;
return counterFrame;
},
AddCounterDevice: function () {
counterDevice++;
return counterDevice;
},
GetCounterDevice: function () {
return counterDevice;
},
ResetCounterDevice: function () {
counterDevice = 0;
return counterDevice;
}
};
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class SC_Neural_Network : MonoBehaviour
{
double[] input;
double[] weightsOne;
double[] hiddenLayer;
double[] lastHiddenLayer;
double[] biasValueOne;
double[] biasValueTwo;
double[] output;
double[] lastOutput;
double[] currentOutputError = new double[2];
double[] lastOutputError = new double[2];
double[] currentHiddenLayerOutputError = new double[3];
double[] lastHiddenLayerOutputError = new double[3];
double[] lastHiddenPercentChanged = new double[3];
double[] currentHiddenPercentChanged = new double[3];
double[] weightsTwo;
System.Random rand = new System.Random();
double[] lastPercentChanged = new double[2];
double[] currentPercentChanged = new double[2];
public void Start()
{
input = new double[2];
weightsOne = new double[6];
hiddenLayer = new double[3];
lastHiddenLayer = new double[3];
biasValueOne = new double[3];
biasValueTwo = new double[2];
output = new double[2];
weightsTwo = new double[6];
lastOutput = new double[2];
neuralNet(0.001, 100); // right now, can only use 0.001 or 0.999
}
double oriGoal = 0;
int starter = 0;
int finisher = 0;
int someCounter = 0;
//right now only working for input of 2 and hidden layer of 3 and output of 2 and biasOne 3 and biasTwo 2 and WeightsOne 6 and WeightsTwo 6
private void neuralNet(double goal,int counterMax)
{
oriGoal = goal;
while (someCounter < counterMax && finisher == 0)
{
if (starter == 0)
{
input[0] = rand.NextDouble();
input[1] = rand.NextDouble();
for (int c1 = 0; c1 < weightsOne.Length; c1++)
{
weightsOne[c1] = rand.NextDouble();
weightsTwo[c1] = rand.NextDouble();
}
for (int c1 = 0; c1 < biasValueOne.Length; c1++)
{
biasValueOne[c1] = rand.NextDouble();
}
for (int c1 = 0; c1 < biasValueTwo.Length; c1++)
{
biasValueTwo[c1] = rand.NextDouble();
}
starter = 1;
}
else
{
input[0] = output[0];
input[1] = output[1];
}
for (int c = 0; c < hiddenLayer.Length; c++)
{
hiddenLayer[c] = ActivationFunction(input[0] * weightsOne[c * 2 + 0] + input[1] * weightsOne[c * 2 + 1] + biasValueOne[c]);
}
for (int c = 0; c < output.Length; c++)
{
output[c] = ActivationFunction(hiddenLayer[0] * weightsTwo[c * 3 + 0] + hiddenLayer[1] * weightsTwo[c * 3 + 1] + hiddenLayer[2] * weightsTwo[c * 3 + 2] + biasValueTwo[c]);
if (output[c] <= goal && goal >= 0.999)
{
currentOutputError[c] = 1 - output[c];
lastOutputError[c] = 1 - lastOutput[c];
var totalDiffInError = Math.Abs(currentOutputError[c] - lastOutputError[c]);
currentPercentChanged[c] = totalDiffInError / currentOutputError[c];
var diffToGoal = Math.Abs(goal - currentOutputError[c]);
if (currentOutputError[c] >= lastOutputError[c])
{
for (int c1 = 0; c1 < weightsOne.Length; c1++)
{
currentPercentChanged[c] *= 1.19;
double someTest = (currentOutputError[c] * currentPercentChanged[c]);
weightsOne[c1] = weightsOne[c1] - someTest;
someTest = (currentOutputError[c] * currentPercentChanged[c]);
currentPercentChanged[c] *= 1.19;
weightsTwo[c1] = weightsTwo[c1] - someTest;
}
}
else if (currentOutputError[c] < lastOutputError[c])
{
for (int c1 = 0; c1 < weightsOne.Length; c1++)
{
currentPercentChanged[c] *= 1.19;
double someTest = (currentOutputError[c] * currentPercentChanged[c]);
weightsOne[c1] = weightsOne[c1] + someTest;
someTest = (currentOutputError[c] * currentPercentChanged[c]);
currentPercentChanged[c] *= 1.19;
weightsTwo[c1] = weightsTwo[c1] + someTest;
}
}
}
else if(output[c] >= goal && goal <= 0.001)
{
//Debug.Log("test00");
currentOutputError[c] = 1 - output[c];
lastOutputError[c] = 1 - lastOutput[c];
var totalDiffInError = Math.Abs(currentOutputError[c] - lastOutputError[c]);
currentPercentChanged[c] = totalDiffInError / currentOutputError[c];
var diffToGoal = Math.Abs(goal - currentOutputError[c]);
if (currentOutputError[c] >= lastOutputError[c])
{
for (int c1 = 0; c1 < weightsOne.Length; c1++)
{
currentPercentChanged[c] *= 1.33;
double someTest = (currentOutputError[c] * currentPercentChanged[c]);
weightsOne[c1] = weightsOne[c1] + someTest;
currentPercentChanged[c] *= 1.33;
someTest = (currentOutputError[c] * currentPercentChanged[c]);
weightsTwo[c1] = weightsTwo[c1] + someTest;
}
}
else if (currentOutputError[c] < lastOutputError[c])
{
for (int c1 = 0; c1 < weightsOne.Length; c1++)
{
currentPercentChanged[c] *= 1.33;
double someTest = (currentOutputError[c] * currentPercentChanged[c]);
weightsOne[c1] = weightsOne[c1] - someTest;
currentPercentChanged[c] *= 1.33;
someTest = (currentOutputError[c] * currentPercentChanged[c]);
weightsTwo[c1] = weightsTwo[c1] - someTest;
}
}
/*else //not used but if the above variable 1.33 is less than 1.19, it breaks the script and i gotta implement this part
{
}*/
}
else if(output[c] <= goal && goal <= 0.001 || output[c] >= goal && goal >= 0.999)
{
finisher = 1;
Debug.Log("FINISHED AND REACHED GOAL");
}
lastPercentChanged[c] = currentPercentChanged[c];
lastOutput[c] = output[c];
Debug.Log(output[c]);
}
someCounter++;
}
}
double ActivationFunction(double x)
{
return (1 / (1 + Mathf.Exp((float)-x)));
}
}
Page created in 0.021 seconds with 10 queries.