﻿ CBOT programs and example code

 CBOT programs and example code melex750 TrackedGrabber Posts: 43 Threads: 10 Joined: Sep 2015 Reputation: 21 #1 06-28-2017, 10:38 AM Spoiler: QuickSort recursive sorting algorithm Code:```extern void object::TestQuickSort() {     int array[] = {4,1,9,2,7,5,8,6,9,0,5,3,7,2,8,0,4,6,1,3};     QuickSort(array, 0, 19);     message("" + array); }   void QuickSort(int[] a, int l, int r) {     if (r <= l) return;     float v = a[r];     int i = l-1, j = r;     int p = l-1, q = r;     while (true) {         while (a[++i] < v) ;         while (v < a[--j] && !(j == l)) ;         if (i >= j) break;         Swap(a, i, j);         if (a[i] == v) Swap(a, ++p, i);         if (a[j] == v) Swap(a, j, --q);     }     Swap(a, i, r); j = i-1; ++i;     int k = l; while (k < p) Swap(a, k++, j--);     k = r - 1; while (k > q) Swap(a, i++, k--);     QuickSort(a, l, j); QuickSort(a, i, r); } void Swap(int[] a, int l, int r) { float f = a[l]; a[l] = a[r]; a[r] = f; }``` Screenshot Better teamwork between bots with: Spoiler: Reserved object list Code:```//////////////////////////////////////////////////////////////////////// extern void object::TestReservedObject() {     ResObjPtr ptr = ReserveRadar(PowerCell, 0, 360, 0, 50);     // object is reserved until ptr's destructor is called     if (ptr != null)     {         object cell = ptr.obj;         goto(cell.position);         grab(InFront);     }          GoRecharge(this); } /* ReserveRadar() and ReserveSearch() do not find objects already in reserved list. ReserveRadar() and ReserveSearch() ignore some objects in certain areas see ReservedObjects::IsAtBuilding(). You may use ForceReserveObject() to create a pointer for those specific objects. */ //////////////////////////////////////////////////////////////////////// public void GoRecharge(object thisBot) {     object powerstation = radar(PowerStation,0,360,0,100,1,thisBot.team);     if ( powerstation == null ) return; // not found     ResObjPtr resptr = TryReserveObject(powerstation);     while ( resptr == null ) // is already reserved     {         while ( powerstation != null )         {             if ( Goto(space(powerstation.position)) ) break; // find a place to wait         }         while ( resptr == null )         {             if ( powerstation == null ) // if it was destroyed find another             {                 powerstation = radar(PowerStation,0,360,0,100,1,thisBot.team);                 if ( powerstation == null ) return; // not found                 break;             }             resptr = TryReserveObject(powerstation); // try again             if ( resptr == null ) wait(1);         }     }     while ( !Goto(powerstation.position) );     if ( thisBot.load != null && thisBot.load.category == PowerCell )     {         while ( thisBot.load.energyLevel < 1.0 ) wait(0.1);     }     if ( thisBot.energyCell.category == PowerCell )     {         while ( thisBot.energyCell.energyLevel < 1.0 ) wait(0.1);     }     while ( !Goto(space()) ); } //////////////////////////////////////////////////////////////////////// bool Goto(point pos) {     errmode(0);     int err = goto(pos);     errmode(1);     return (err == 0); } //////////////////////////////////////////////////////////////////////// public class ResObjPtr {     void ResObjPtr(object o, ObjectList p)     {         this.obj = o;         this.ptr = p;     }     void ~ResObjPtr()     {         if (ptr != null) ptr.obj = null; // set list item to null     }     ObjectList ptr = null;     object obj = null; } //////////////////////////////////////////////////////////////////////// public ResObjPtr ReserveRadar(int[] types,                               float angle  = 0,                               float focus  = 360,                               float min    = 0,                               float max    = 1000,                               float sense  = 1,                               int   filter = FilterNone) {     return new ReservedObjects.Radar(types, angle, focus, min, max, sense, filter); } //////////////////////////////////////////////////////////////////////// public ResObjPtr ReserveRadar(int   type,                               float angle  = 0,                               float focus  = 360,                               float min    = 0,                               float max    = 1000,                               float sense  = 1,                               int   filter = FilterNone) {     return new ReservedObjects.Radar(type, angle, focus, min, max, sense, filter); } //////////////////////////////////////////////////////////////////////// public ResObjPtr ReserveSearch(int[] types,                                point pos,                                float min    = 0,                                float max    = 1000,                                float sense  = 1,                                int   filter = FilterNone) {     return new ReservedObjects.Search(types, pos, min, max, sense, filter); } //////////////////////////////////////////////////////////////////////// public ResObjPtr ReserveSearch(int   type,                                point pos,                                float min    = 0,                                float max    = 1000,                                float sense  = 1,                                int   filter = FilterNone) {     return new ReservedObjects.Search(type, pos, min, max, sense, filter); } //////////////////////////////////////////////////////////////////////// public ResObjPtr TryReserveObject(object item) {     if (item == null) return null;     return new ReservedObjects.check(item); } //////////////////////////////////////////////////////////////////////// public ResObjPtr ForceReserveObject(object item) {     if (item == null) return null;     return new ReservedObjects.forceReserved(item); } //////////////////////////////////////////////////////////////////////// public class ObjectList {     void ObjectList(object o, ObjectList n = null)     {         this.obj = o;         this.next = n;     }     object obj = null;     ObjectList next = null; } //////////////////////////////////////////////////////////////////////// public class ReservedObjects {     ////////////////////////////////////////////////////////     synchronized     ResObjPtr Radar(int[] types, float angle, float focus,                    float min, float max, float sense, int filter)     {         return check(radarall(types, angle, focus, min, max, sense, filter));     }     ////////////////////////////////////////////////////////     synchronized     ResObjPtr Radar(int type, float angle, float focus,                    float min, float max, float sense, int filter)     {         return check(radarall(type, angle, focus, min, max, sense, filter));     }     ////////////////////////////////////////////////////////     synchronized     ResObjPtr Search(int[] types, point pos, float min,                     float max, float sense, int filter)     {         return check(searchall(types, pos, min, max, sense, filter));     }     ////////////////////////////////////////////////////////     synchronized     ResObjPtr Search(int type, point pos, float min,                     float max, float sense, int filter)     {         return check(searchall(type, pos, min, max, sense, filter));     }     ////////////////////////////////////////////////////////     synchronized ResObjPtr check(object[] itemList)     {         ResObjPtr ptr = null; int i = 0, count = sizeof(itemList);         while (i < count && null == (ptr = check(itemList[i++])));         return ptr;     }     ////////////////////////////////////////////////////////     synchronized ResObjPtr check(object item, bool forced = false)     {         if (item == null) return null;         if (!forced && IsAtBuilding(item) ) return null; // ignore this one         ObjectList p = this.m_list;         ObjectList prv = null;         while ( p != null )         {             if (p.obj == null) { p = Remove(prv, p); continue; }             else if (p.obj == item) return null; // already in the list ?             prv = p;             p = p.next;         }         this.m_list = new ObjectList(item, this.m_list);         return new ResObjPtr(item, this.m_list);     }     ////////////////////////////////////////////////////////     synchronized bool IsAtBuilding(object item)     {         switch (item.category)         {             case PowerCell:             {                 return (null != search(PowerPlant, item.position, 0, 4));             }             case TitaniumOre:             {                 return (null != search(Converter, item.position, 0, 4));             }             case Titanium:             {                 int[] types = {Converter,BotFactory,PowerPlant};                 return (null != search(types, item.position,0,4));             }             default:         }         return false;     }     ////////////////////////////////////////////////////////     synchronized void UnReserve(object item)     {         if (item == null || this.m_list == null) return;         ObjectList p = this.m_list, prv = null;         while ( p != null )         {             if (p.obj == null || p.obj == item)             {                 p = Remove(prv, p);                 continue;             }             prv = p;             p = p.next;         }     }     ////////////////////////////////////////////////////////     synchronized ResObjPtr forceReserved(object item)     {         if (item == null) return null;         UnReserve(item);         return check(item, true);     }     ////////////////////////////////////////////////////////     synchronized ObjectList Remove(ObjectList prv, ObjectList p)     {         if (prv == null) return (this.m_list = p.next);         else return (prv.next = p.next);     }     private static ObjectList m_list = null; } ////////////////////////////////////////////////////////////////////////``` « Next Oldest | Next Newest »