public void calcDistFromCockpit() {
   if (cockpit == null) return;
   distFromCockpit = -1;
   if (cockpit == body) distFromCockpit = 0;
   for (JointEdge j = body.getJointList(); j != null; j = j.next) {
     Body otherBody = j.other;
     if (otherBody.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) otherBody.getUserData();
       if (data.cockpit == cockpit && data.distFromCockpit != -1) {
         data.calcDistFromCockpit();
         if (data.distFromCockpit != -1 && data.distFromCockpit < distFromCockpit)
           distFromCockpit = data.distFromCockpit;
       }
     }
   }
   if (distFromCockpit == -1) setCockpit(null);
 }
 public double requestResource(String name, double amount, boolean acceptPartial) {
   if (cockpit != null && cockpit != body) {
     if (cockpit.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) cockpit.getUserData();
       return data.requestResource(name, amount, acceptPartial);
     }
   }
   if (acceptPartial) {
     double collected = removeResource(name, amount);
     int size = subparts.size();
     for (int a = 0; a < size && collected < amount; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         collected += data.removeResource(name, amount);
       }
     }
     return collected;
   } else {
     if (getResource(name).amount >= amount) return removeResource(name, amount);
     int size = subparts.size();
     for (int a = 0; a < size; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         if (data.getResource(name).amount >= amount) return data.removeResource(name, amount);
       }
     }
     return 0;
   }
 }
 public double depositResource(String name, double amount, boolean acceptPartial) {
   if (cockpit != null && cockpit != body) {
     if (cockpit.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) cockpit.getUserData();
       return data.depositResource(name, amount, acceptPartial);
     }
   }
   if (acceptPartial) {
     double collected = addResource(name, amount, true);
     int size = subparts.size();
     for (int a = 0; a < size && collected < amount; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         collected += data.addResource(name, amount - collected, true);
       }
     }
     return collected;
   } else {
     double deposited = addResource(name, amount, false);
     if (deposited != 0) return amount;
     int size = subparts.size();
     for (int a = 0; a < size; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         if (data.allowedResource(name)) {
           deposited = data.addResource(name, amount, false);
           if (deposited != 0) return amount;
         }
       }
     }
     return 0;
   }
 }