+- Colobot Forum - International Colobot Community (https://colobot.info/forum)
+-- Forum: [Archive] New forum (2015-2019) (https://colobot.info/forum/forumdisplay.php?fid=76)
+--- Forum: Colobot: Gold Edition Basics (https://colobot.info/forum/forumdisplay.php?fid=58)
+---- Forum: Workshop (https://colobot.info/forum/forumdisplay.php?fid=60)
+---- Thread: CBOT programs and example code (/showthread.php?tid=932)
CBOT programs and example code - melex750 - 06-28-2017
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; }
////////////////////////////////////////////////////////////////////////
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);
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;
}