public double CalculateWork() { if (paused) { return(0); } double hours = 0; var built = builtStuff.required; var cost = buildCost.required; if (state == State.Building) { for (int i = built.Count; i-- > 0;) { var res = built[i]; hours += res.kerbalHours * res.amount; } } else if (state == State.Canceling) { for (int i = built.Count; i-- > 0;) { var res = built[i]; var cres = ELBuildWindow.FindResource(cost, res.name); hours += res.kerbalHours * (cres.amount - res.amount); } } return(hours); }
int CountResources(List <BuildResource> built, List <BuildResource> cost) { int count = 0; foreach (var bres in built) { var cres = ELBuildWindow.FindResource(cost, bres.name); if (cres.amount - bres.amount > 0) { count++; } } return(count); }
void SetPadMass() { double mass = 0; if (builtStuff != null && buildCost != null) { var built = builtStuff.required; var cost = buildCost.required; foreach (var bres in built) { var cres = ELBuildWindow.FindResource(cost, bres.name); mass += (cres.amount - bres.amount) * bres.density; } } builder.SetCraftMass(mass); }
private void DoWork_Cancel(double kerbalHours) { var built = builtStuff.required; var cost = buildCost.required; for (int i = built.Count; i-- > 0;) { built[i].deltaAmount = 0; } bool did_work; int count; do { count = CountResources(built, cost); if (kerbalHours == 0) { break; } if (count == 0) { break; } did_work = false; foreach (var bres in built) { var cres = ELBuildWindow.FindResource(cost, bres.name); double remaining = cres.amount - bres.amount; if (remaining <= 0) { continue; } double work = kerbalHours / count--; double amount = work / bres.kerbalHours; double base_amount = Math.Abs(amount); if (amount > remaining) { amount = remaining; } kerbalHours -= work; did_work = true; // do only the work required to process the actual amount // of returned or disposed resource double dkH = work * amount / base_amount; if (dkH > work) { dkH = work; } work -= dkH; // return any unused kerbal-hours to the pool kerbalHours += work; bres.amount += amount; //Debug.Log("remove delta: "+amount); bres.deltaAmount += amount; double capacity = padResources.ResourceCapacity(bres.name) - padResources.ResourceAmount(bres.name); if (amount > capacity) { amount = capacity; } if (amount <= 0) { continue; } padResources.TransferResource(bres.name, amount); } } while (did_work); // recount resources that still need to be taken from the build count = CountResources(built, cost); if (count == 0) { state = State.Planning; KACalarmID = ""; builtStuff = null; // ensure pad mass gets reset } SetPadMass(); }
private void DoWork_Cancel(double kerbalHours) { var built = builtStuff.required; var cost = buildCost.required; bool did_work; int count; do { count = 0; foreach (var bres in built) { var cres = ELBuildWindow.FindResource(cost, bres.name); if (cres.amount - bres.amount > 0) { count++; } } if (count == 0) { break; } double work = kerbalHours / count; did_work = false; count = 0; foreach (var bres in built) { var cres = ELBuildWindow.FindResource(cost, bres.name); double remaining = cres.amount - bres.amount; if (remaining <= 0) { continue; } double amount = work / bres.kerbalHours; double base_amount = Math.Abs(amount); if (amount > remaining) { amount = remaining; } count++; did_work = true; // do only the work required to process the actual amount // of returned or disposed resource kerbalHours -= work * amount / base_amount; bres.amount += amount; //Debug.Log("remove delta: "+amount); bres.deltaAmount = amount; double capacity = padResources.ResourceCapacity(bres.name) - padResources.ResourceAmount(bres.name); if (amount > capacity) { amount = capacity; } if (amount <= 0) { continue; } padResources.TransferResource(bres.name, amount); } } while (did_work); SetPadMass(); if (count == 0) { state = State.Planning; KACalarmID = ""; } }