Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yet another Resource Collecting Program
#1
I decided to play around with classes and the result is here. It's very basic program but may be a good start for someone who wants to extend it. Too bad there's no inheritance in CBot. :)

Program that uses this class looks like that:
   
Load this class on the very first Grabber you've created and before any program that uses it.
Not recommended for Winged Grabbers. Optimal for Legged Grabbers and Free Game mode.
To suppress the messages just change  bool verbose true; to false.

PHP Code:
// Resources

extern void object::classResources() {
    
// !! do not edit this program, create a new one instead  !!
    // !! load this class only on one grabber, use everywhere !!
    // usage:
    // Resources res(this, Titanium);
    // Resources res(this, PowerCell);
    // Resources res(this, NuclearCell);
}

public class 
Resources {
    
int resourceproductunit;
    
int pause 15;
    private 
object self null;
    
bool verbose true;
    
    
void Resources(object oint p) {
        
self o;
        
product p;
        
configure();
        
init();
        
produce();
    }
    
    
void configure() {
        switch(
product) {
            case 
Titanium:
             
   resource TitaniumOre;
             
   unit Converter;
             
   break;
            case 
PowerCell:
             
   resource Titanium;
             
   unit PowerPlant;
             
   pause 12;
             
   break;
            case 
NuclearCell:
             
   resource UraniumOre;
             
   unit NuclearPlant;
             
   pause 35;
             
   break;
        }
    }
    
    
void init() {
        
selfRecharge();
        
selfRepair();
        goto(
space(self.position));
        
errmode(0);
        
drop();
        
errmode(1);
    }
    
    
void produce() {
        while(
true) {
            
pursue(resource);
            
grab();
            
process();
            goto(
space(self.position));
            
drop();
            
selfRecharge();
        }
    }
    
    
void selfRecharge() {
        
// if power cell half empty, do recharge
        
if(self.energyCell.energyLevel 0.5) {
            if(!
pursue(PowerStation))
             
   return;
            while(
self.energyCell.energyLevel 1)
             
   wait(1);
        }
    }
    
    
void selfRepair() {
        
// if shiled level below 0.5, do recharge
        
if(self.shieldLevel 0.5) {
            if(!
pursue(RepairCenter))
             
   return;
            while(
self.shieldLevel 1)
             
   wait(1);
        }
    }
    
    
void process() {
        
object item;
        
pursue(unit);
        
errmode(0);
        if(
drop() != 0) {
            
report("cannot drop, something in place, trying to remove");
            goto(
space(self.position));
            
drop();
            
pursue(unit);
            
grab();
            return;
        }
        
errmode(1);
        
move(-2.5);
        
wait(pause);
        
move(2.5);
        
grab();
    }
    
    
bool pursue(int cat) {
        
// go to target
        
object target radar(cat);
        if(
target != null) {
            
report(target.category " available, moving");
            
errmode(0);
            if(goto(
target.position) != 0) {
                goto(
space(target.position));
                
report("place occupied, waiting");
                while(goto(
target.position) != 0)
                 
   wait(10);
            }
            
errmode(1);
            
report(target.category " reached");
            return 
true;
        } else {
            
report(target.category " not available");
            return 
false;
        }
    }
    
    
void report(string msg) {
        if(
verbose)
         
   message(self.category ": " msg);
    }

#2
I like the OOPish way in which you wrote your program! About inheritance, take a look at this: https://github.com/colobot/colobot/issues/540
[Image: XvN5CTW.png] [Image: UYXyyMS.png]
#3
(05-20-2016, 12:00 PM)Simbax Wrote: About inheritance, take a look at this
Cool. I never expected this to be implemented so soon.


Forum Jump:


Users browsing this thread: 1 Guest(s)