Skip to content

Commit

Permalink
alpha v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tgruetzm committed Aug 17, 2014
1 parent 4280da8 commit a457b2f
Show file tree
Hide file tree
Showing 9 changed files with 449 additions and 126 deletions.
2 changes: 2 additions & 0 deletions Snacks/Snacks/Config/PluginData/Snacks/snacks.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
SNACKS
{
initialized = 0
snacksPerMeal = 0.25
repLossPercent = 0.0025
}
9 changes: 9 additions & 0 deletions Snacks/Snacks/Config/Resources.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
RESOURCE_DEFINITION
{
name = Snacks
density = .001
unitCost = 1
flowMode = ALL_VESSEL
transfer = PUMP
isTweakable = true
}
11 changes: 1 addition & 10 deletions Snacks/Snacks/Config/patch.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

@PART[*]:HAS[@MODULE[ModuleCommand],#CrewCapacity[1]]
{
RESOURCE
Expand Down Expand Up @@ -217,16 +218,6 @@
}
}

@PART[landerCabinSmall]
{
@RESOURCE[Snacks]
{
@amount += -25
@maxAmount += -25
}
}


@PART[cupola]
{
@RESOURCE[Snacks]
Expand Down
65 changes: 65 additions & 0 deletions Snacks/Snacks/FlightController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Snacks
{
//[KSPAddon(KSPAddon.Startup.Flight, false)]
public class FlightController : MonoBehaviour
{
bool passedOut = false;
double passedOutTime = -1;
double currentTime = -1;
double passedOutEnd = -1;


void Awake()
{
Debug.Log("Snacks - Awake FlightController");
}

void Start() //Called when vessel is placed on the launchpad
{
Debug.Log("Snacks - Start FlightController");
currentTime = Planetarium.GetUniversalTime();
passedOutTime = currentTime + 30;
passedOutTime = passedOutTime + 5;
FlightGlobals.ActiveVessel.OnFlyByWire += new FlightInputCallback(PassedOutKerbal);


}

//remove the fly-by-wire function when we get destroyed:
void OnDestroy()
{
Debug.Log("Snacks - Start FlightController Destroyed");
FlightGlobals.ActiveVessel.OnFlyByWire -= new FlightInputCallback(PassedOutKerbal);

}


void PassedOutKerbal(FlightCtrlState state)
{
currentTime = Planetarium.GetUniversalTime();
if (currentTime > passedOutTime && currentTime < passedOutEnd)
{
Debug.Log("passout starting");
state.Y = -1f;
}
if (currentTime > passedOutEnd)
{
Debug.Log("passout ended, resetting");
passedOutTime = currentTime + 30;
passedOutEnd = passedOutTime + 5;
}


}




}
}
158 changes: 158 additions & 0 deletions Snacks/Snacks/SnackConsumer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
The MIT License (MIT)
Copyright (c) 2014 Troy Gruetzmacher
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*
*
* */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Snacks
{
class SnackConsumer
{
private System.Random random = new System.Random();
private PartResourceDefinition snacksResource = PartResourceLibrary.Instance.GetDefinition("Snacks");
private double snacksPer;
private double lossPerDayPerKerbal;

public SnackConsumer(double snacksPer, double loss)
{
this.snacksPer = snacksPer;
lossPerDayPerKerbal = loss;
}

private bool GetRandomChance(double prob)
{
if (random.NextDouble() < prob)
return true;
return false;
}

public double GetSnackResource(Part p, double demand)
{
List<PartResource> resources = new List<PartResource>();
p.GetConnectedResources(snacksResource.id, ResourceFlowMode.ALL_VESSEL, resources);

double supplied = 0;
foreach (PartResource res in resources)
{
if (res.amount >= demand)
{
res.amount -= demand;
supplied += demand;
return supplied;
}
else
{
supplied += res.amount;
demand -= res.amount;
res.amount = 0;
}

}
return supplied;

}

public double GetSnackResource(List<ProtoPartSnapshot> protoPartSnapshots, double demand)
{
double supplied = 0;
foreach (ProtoPartSnapshot pps in protoPartSnapshots)
{
var res = from r in pps.resources
where r.resourceName == "Snacks"
select r;
if (res.Count() > 0)
{
ConfigNode node = res.First().resourceValues;
double amount = Double.Parse(node.GetValue("amount"));
if (amount >= demand)
{
node.SetValue("amount", (amount -= demand).ToString());
supplied += demand;
return supplied;
}
else
{
node.SetValue("amount", "0");
supplied += amount;
demand -= amount;
}
}
}
return supplied;
}

private double CalculateSnacksRequired(List<ProtoCrewMember> crew)
{

double extra = 0;
foreach (ProtoCrewMember pc in crew)
{
if (GetRandomChance(pc.courage / 2.0))
extra += snacksPer;
if (GetRandomChance(pc.stupidity / 2.0))
extra -= snacksPer;
if (pc.isBadass && GetRandomChance(.2))
extra -= snacksPer;
}
double demand = extra + crew.Count * snacksPer;
Debug.Log("Snack Demand: " + demand);
return demand;
}

/**
* Removes the calculated number of snacks from the vessel.
* returns the number of snacks that were required, but missing.
* */
public double RemoveSnacks(ProtoVessel pv)
{
double demand = CalculateSnacksRequired(pv.GetVesselCrew());
if (demand <= 0.0)
return 0;
double fed = GetSnackResource(pv.protoPartSnapshots, demand);
if (fed == 0)//unable to feed, no skipping or extra counted
return pv.GetVesselCrew().Count * snacksPer;
return demand - fed;
}

/**
* Removes the calculated number of snacks from the vessel.
* returns the number of snacks that were required, but missing.
* */
public double RemoveSnacks(Vessel v)
{

double demand = CalculateSnacksRequired(v.GetVesselCrew());
if (demand <= 0.0)
return 0;
double fed = GetSnackResource(v.rootPart, demand);
if (fed == 0)//unable to feed, no skipping or extra counted
return v.GetCrewCount() * snacksPer;
return demand - fed;
}
}
}
Loading

0 comments on commit a457b2f

Please sign in to comment.