Author Topic: Need some logic help  (Read 10819 times)

kevinminion

  • Global Moderator
  • Full Member
  • *****
  • Posts: 147
  • Karma: +15/-0
    • View Profile
  • IGN: Kevin Minion
Need some logic help
« on: October 08, 2015, 01:08:13 am »
I'm trying to make a Life Support Control chip for my Gas Systems 3 components.
 
When I first spawn my ship, obviously everything is 0 so I want to turn on the collectors to start collecting Tritium, Methane, and Deuterium.
 
Once these have maxed out, I would like my Inverters to turn on so that I will start generating Oxygen, Water, and Nitrogen.
 
When my Tritium level gets to 10%, I would like the Tritium Inverter to turn off until Tritium is at 100% again (same for other 2 gas types)
 
So I should see my Tritium level increase until it gets to 100%, then go down to 10%, then back up to 100%, then down to 10% until Oxygen is at 100%
 
However, with the code below, it goes from 0% to 100%, then down to 10%, and hovers around 10%.  The inverters switch on and off every second or so.
 
The Tritium Collector should stay on until Oxygen = 100% and Tritium = 100%
 
I'm sure I may just be tired, but maybe someone will solve this by tomorrow night  :)
 
This is just a piece of the code.   LastTritium is the Tritium value of the previous clock cycle.  PctTritium is the current Tritium value divided by the max.
 
(LastTritium >= GS3StorageCache["Tritium",number])  should in essence be saying 
    if Tritium is discharging
 
But I think that line is most likely where I am screwing up.
 
Code: [Select]

        Trit_Inverter_On = 0
        Meth_Inverter_On = 0
        Deut_Inverter_On = 0
                   
        if ((PctOxygen  < 1) & (LastTritium >= GS3StorageCache["Tritium",number]) & (PctTritium > 0.1) ) {
            Trit_Inverter_On = 1
        }
       
        if ((PctWater   < 1) & (LastMethane >= GS3StorageCache["Methane",number]) & (PctMethane > 0.1) ) {
            Meth_Inverter_On = 1
        }
        if ((PctNitrogen < 1) & (LastDeuterium >= GS3StorageCache["Deuterium",number]) & (PctDeuterium > 0.1) ) {
            Deut_Inverter_On = 1
        }
        Trit_Collector_On = (PctOxygen   < 1) | (PctTritium < 1)
        Meth_Collector_On = (PctWater    < 1) | (PctMethane < 1)
        Deut_Collector_On = (PctNitrogen < 1) | (PctDeuterium < 1)

Thanks in advance,
Kevin

bloxgate

  • Supreme Forum Overlord
  • Administrator
  • Hero Member
  • ******
  • Posts: 546
  • Karma: +50/-4
    • View Profile
    • Bloxgate's Website
  • IGN: bloxgate
Re: Need some logic help
« Reply #1 on: October 08, 2015, 05:13:15 pm »
Untested, but I think something like this might work:


Code: [Select]

Target = 1
Hit = PctResource
Error = Target - Hit


if((Error <= 1 && Error > 0) && PctResource <= .1){
InverterOn = 1
} else {
InverterOn = 0
}

kevinminion

  • Global Moderator
  • Full Member
  • *****
  • Posts: 147
  • Karma: +15/-0
    • View Profile
  • IGN: Kevin Minion
Re: Need some logic help
« Reply #2 on: October 11, 2015, 10:39:43 pm »
Here's what I am currently using.  It may not be optimized, but I believe it works.
 
Code: [Select]

 if (PctOxygen < 1) {
    if ((!TritCharging) & (PctTritium < 0.1)) {
        Trit_Inverter_On = 0
        TritCharging = 1
    } elseif (PctTritium == 1) {
        Trit_Inverter_On = 1
        TritCharging = 0
    }
   
} else {
    Trit_Inverter_On = 0
}
 
Trit_Collector_On = (PctOxygen  < 1) | (PctTritium < 1)

kevinminion

  • Global Moderator
  • Full Member
  • *****
  • Posts: 147
  • Karma: +15/-0
    • View Profile
  • IGN: Kevin Minion
Re: Need some logic help
« Reply #3 on: October 27, 2015, 01:05:30 am »
Maybe just another late night oversight...
 
Any ideas on why this doesn't work?
 
   Probe is the Atmospheric Probe
   CAF is the Resource Cache with Energy, Oxygen, Water, Nitrogen, etc..
 
 
Code: [Select]
@name Terraform_v1.1
@inputs Probe:wirelink CAF:wirelink
@outputs
@persist
 
if (first() | duped()) {
     runOnChat(1)
}
 
    LastSaid = owner():lastSaid():lower():explode(" ")
    Arg1 = LastSaid[1,string]
    Arg2 = LastSaid[2,string]
    Arg3 = LastSaid[3,string]
 
    if (Arg1 == "!vent") {
        if (Arg2 == "amount") {
            print("Arg3 = :" + Arg3 + ":")
            if (Arg3 != "") {
                CAF["Vent Amount", number] = clamp(Arg3:toNumber(),0,1000000)
            }
            print("Vent Amount = " + CAF["Vent Amount", number])
        }
     }

The value is correctly set if I type !vent amount 1000 and it outputs Arg3 = :1000:
 
When I type !vent amount it should return the current value of Vent Amount from the CAF Storage Cache, but instead all I ever get is Vent Amount = 0
 

bloxgate

  • Supreme Forum Overlord
  • Administrator
  • Hero Member
  • ******
  • Posts: 546
  • Karma: +50/-4
    • View Profile
    • Bloxgate's Website
  • IGN: bloxgate
Re: Need some logic help
« Reply #4 on: October 27, 2015, 04:26:53 pm »
You're missing an else.

kevinminion

  • Global Moderator
  • Full Member
  • *****
  • Posts: 147
  • Karma: +15/-0
    • View Profile
  • IGN: Kevin Minion
Re: Need some logic help
« Reply #5 on: October 27, 2015, 08:16:34 pm »
Hmm i don't think so, since I want to see that value for either case. But I think I could move that last print statement outside of that bracket.  I'll try it and let you know. Thanks for looking.

Notice I also used the clamp() statement that I learned from you :)