void CollideFromPoint(float * mState, float * oState, float * fromPoint, float velMag) { if (stage == 0) { ZRSetPositionTarget(fromPoint); if (Vfunc(6, mState, fromPoint, NULL, 0) < 0.09) { stage = 1; } } if (stage == 1) { Vfunc(9, fromPoint, oState, dir, 0); Vfunc(4, dir, NULL, dir, velMag); ZRSetVelocityTarget(dir); } }
static void CoastToTarget(float* myPos, float* coastTarget, float magnitude) { float temp[3]; if (magnitude > 0.04) magnitude = 0.04; Vfunc(2, (coastTarget), (myPos), (temp), 0); if (mathVecMagnitude((temp), 3) < (mathSquare(mathVecMagnitude((&myPos[3]), 3)) * 50.0 + 0.08)) { ZRSetPositionTarget(coastTarget); } else { mathVecNormalize((temp), 3); Vfunc(4, (temp), NULL, (temp), (magnitude)); if (Vfunc(6, (temp), (&myPos[3]), NULL, 0) > 0.02) ZRSetVelocityTarget(temp); } }
static float Vfunc(int which, float * v1, float * v2, float * vresult, float scalar) { int i; float vtemp[3]; if (which == 2) { // vresult = v1 - v2 Vfunc(4,v2,NULL,vtemp,-1); mathVecAdd(vresult,v1,vtemp,3); return 0; } if (which == 3) { // vresult = v1 / |v1|; if |v1| == 0, returns 0, else 1 memcpy(vresult, v1, sizeof(float)*3); mathVecNormalize(vresult,3); return 0; } if (which == 4) { // vresult = scalar * v1 for (i = 0; i < 3; ++i) vresult[i] = scalar * v1[i]; return 0; } if (which == 5) { // returns dot product: v1 * v2 return mathVecInner(v1,v2,3); } if (which == 6) { // returns distance between v1 and v2 float v3[3]; Vfunc(2,v1,v2,v3,0); // v3 = v1 - v2 return mathVecMagnitude(v3,3); } if (which == 8) { // angle between two vectors float dot = Vfunc(5,v1,v2,NULL,0)/(mathVecMagnitude(v1,3)*mathVecMagnitude(v2,3)); return acos(dot)*180.0/3.14159265; } if (which == 9) { // unit vector pointing from v1 toward v2 float v9[3]; Vfunc(2,v2,v1,v9,0); return Vfunc(3,v9,NULL,vresult,0); } }
void alignToPlusY() { // Performs initial alignment to +Y axis prior to rotateTau() // Requires a "stage" global variable initialized to 0; // In my case, this came before stage 3, hence the assignment to the number 3; float attTarget[3] = {0.0, 1.0, 0.0}; ZRSetAttitudeTarget(attTarget); if (Vfunc(8, att1Target, myState+6, NULL, 0) < 0.75) { stage = 3; // Ties in to stage global variable effective / used in ZRUser(). } }
void ZRUser(float* myState, float* otherState, float time) { float target[3]; float station[4]; float baseAngle; float angleDiff; float baseRadius; float to_opponent[3]; float sun[3] = {0,0,0}; float tolerance=.02; float a1, a2; float s1 = 0; float s2 = 0; //s1 = score 1 second ago. s2 = current score DEBUG(("time: %4.0f, state: %d\n", time, state)); switch (state) { case 0: //Code to initialize the sphere, then search for the panel... state = 1; break; case 1: //Code to move towards panel initialization circle... if (fabs(myState[0] - (getPanelSide() * 0.7)) < 0.1) state = 2; baseAngle = atan2f(myState[2], myState[1]); target_pos[0] = (getPanelSide() * 0.7); target_pos[1] = cosf(baseAngle) * 0.7; target_pos[2] = sinf(baseAngle) * 0.7; ZRSetPositionTarget(target_pos); Vfunc(9, (sun), (myState), (target_att), 0); ZRSetAttitudeTarget(target_att); //Code to find tangent lines... baseAngle = atan2f(-target_pos[2], -target_pos[1]); angleDiff = asinf(0.5 / sqrtf(mathSquare(target_pos[1]) + mathSquare(target_pos[2]))); tangentPoints[0] = baseAngle - angleDiff; tangentPoints[1] = baseAngle + angleDiff; tangentPoints[2] = tangentPoints[0]; break; case 2: if (getPercentChargeRemaining() >= 95) { state = 3; break; } else { // calc vector from current position back toward sun Vfunc(9, (sun), (myState), (target_att), 0); ZRSetPositionTarget(target_pos); ZRSetAttitudeTarget(target_att); } break; case 3: //Code to wait for the opponent to get into their panel's plane Vfunc(9, (myState), (otherState), (to_opponent), 0); if(fabs(otherState[0] - (getPanelSide() * -0.7) > .10)){ state = 5; break; } else { ZRSetAttitudeTarget(to_opponent); if(fabs(otherState[0] - (getPanelSide() * -0.7)) < .005){ state = 4; break; } } break; case 4: ZRSetPositionTarget(target_pos); Vfunc(9, (myState), (otherState), (to_opponent), 0); if (Vfunc(8, (to_opponent), (myState+6), NULL, 0) < 5 && getPercentChargeRemaining() > 0 && fabs(otherState[0]) > .68 && fabs(otherState[0]) < .81) { DEBUG(("time: %4.0f, (BLUE): ZAPPING ++++++++++++++++++++\n",time)); ZRRepel(); } else { if (getPercentChargeRemaining() < 1) { state = 5; break; } Vfunc(9, (myState), (otherState), (to_opponent), 0); DEBUG(("time: %4.0f, (BLUE): angle to opponent: %f\n",time,Vfunc(8, (to_opponent), (myState+3), NULL, 0))); ZRSetAttitudeTarget(to_opponent); } break; case 5: //Code to search for panel... //Check if panel was found. if (isPanelFound()) { getPanelState(panelState); panelState[4] = panelState[0]; baseAngle = atan2f(panelState[2], panelState[1]); baseRadius = sqrtf(mathSquare(panelState[1]) + mathSquare(panelState[2])) - 0.03; panelState[5] = cosf(baseAngle) * baseRadius; panelState[6] = sinf(baseAngle) * baseRadius; state = 6; break; } //Code to find tangent lines... baseAngle = atan2f(-myState[2], -myState[1]); angleDiff = asinf(0.5 / sqrtf(mathSquare(myState[1]) + mathSquare(myState[2]))); tangentPoints[0] = baseAngle - angleDiff; tangentPoints[1] = baseAngle + angleDiff; //Point in the direction of tangentPoints[2] target_att[0] = 0; target_att[1] = cosf(tangentPoints[2]); target_att[2] = sinf(tangentPoints[2]); ZRSetAttitudeTarget(target_att); if(fabs(otherState[0] - (getPanelSide() * -0.7) < .05 && getPercentChargeRemaining() > 0)){ state = 3; break; } tangentPoints[2] += scanTarget * 0.1; if ((scanTarget == 1) && (tangentPoints[2] >= tangentPoints[1])) scanTarget = -1; else if ((scanTarget == -1) && (tangentPoints[2] <= tangentPoints[0])) scanTarget = 1; target_pos[0] = getPanelSide() * 0.7; target_pos[1] = myState[1]; target_pos[2] = myState[2]; ZRSetPositionTarget(target_pos); break; case 6: //Code to move to panel... if (iHavePanel()) { state = 7; scanTarget = 0; break; } ZRSetPositionTarget(&panelState[4]); Vfunc(7, (panelState), NULL, (target_pos), 0); target_pos[0] = 0; Vfunc(3, (target_pos), NULL, (target_pos), 0); ZRSetAttitudeTarget(target_pos); break; case 7: //Code to beeline towards the station... if (s2 - s1 == 0 && s2 > 0){ state = 8; break; } else { s1 = s2; s2 = getOtherCurrentScore(); getStationState(station); Vfunc(7, (station), NULL, (target_att), 0); target_att[1] += cosf(station[3]) * 0.03; target_att[2] += sinf(station[3]) * 0.03; CoastToTarget(myState, target_att); target_att[0] = 0; target_att[1] = cosf(station[3]); target_att[2] = sinf(station[3]); ZRSetAttitudeTarget(target_att); } break; case 8: //Code to beeline towards the station... target[0] = (getPanelSide() * 0.7); target[1] = 0; target[2] = 0; CoastToTarget(myState, target); break; } }
void implicit_2Dy(double *phi, double *xx, double *yy, double nu2, double m21, double gamma2, double h2, double dt, int L, int M, int use_delj_trick){ int ii, jj; double *dy = malloc((M-1) * sizeof(*dy)); double *dfactor = malloc(M * sizeof(*dfactor)); double *yInt = malloc((M-1) * sizeof(*yInt)); double Mfirst, Mlast; double *MInt = malloc((M-1) * sizeof(*MInt)); double *V = malloc(M * sizeof(*V)); double *VInt = malloc((M-1) * sizeof(*VInt)); double *delj = malloc((M-1) * sizeof(*delj)); double *a = malloc(M * sizeof(*a)); double *b = malloc(M * sizeof(*b)); double *c = malloc(M * sizeof(*c)); double *r = malloc(M * sizeof(*r)); double x; compute_dx(yy, M, dy); compute_dfactor(dy, M, dfactor); compute_xInt(yy, M, yInt); for(jj=0; jj < M; jj++) V[jj] = Vfunc(yy[jj], nu2); for(jj=0; jj < M-1; jj++) VInt[jj] = Vfunc(yInt[jj], nu2); tridiag_malloc(M); for(ii=0; ii < L; ii++){ x = xx[ii]; Mfirst = Mfunc2D(yy[0], x, m21, gamma2, h2); Mlast = Mfunc2D(yy[M-1], x, m21, gamma2, h2); for(jj=0; jj < M-1; jj++) MInt[jj] = Mfunc2D(yInt[jj], x, m21, gamma2, h2); compute_delj(dy, MInt, VInt, M, delj, use_delj_trick); compute_abc_nobc(dy, dfactor, delj, MInt, V, dt, M, a, b, c); for(jj = 0; jj < M; jj++) r[jj] = phi[ii*M + jj]/dt; if((ii==0) && (Mfirst <= 0)) b[0] += (0.5/nu2 - Mfirst)*2./dy[0]; if((ii==L-1) && (Mlast >= 0)) b[M-1] += -(-0.5/nu2 - Mlast)*2./dy[M-2]; tridiag_premalloc(a, b, c, r, &phi[ii*M], M); } tridiag_free(); free(dy); free(dfactor); free(yInt); free(MInt); free(V); free(VInt); free(delj); free(a); free(b); free(c); free(r); }
void implicit_2Dx(double *phi, double *xx, double *yy, double nu1, double m12, double gamma1, double h1, double dt, int L, int M, int use_delj_trick){ int ii, jj; double *dx = malloc((L-1) * sizeof(*dx)); double *dfactor = malloc(L * sizeof(*dfactor)); double *xInt = malloc((L-1) * sizeof(*xInt)); double Mfirst, Mlast; double *MInt = malloc((L-1) * sizeof(*MInt)); double *V = malloc(L * sizeof(*V)); double *VInt = malloc((L-1) * sizeof(*VInt)); double *delj = malloc((L-1) * sizeof(*delj)); double *a = malloc(L * sizeof(*a)); double *b = malloc(L * sizeof(*b)); double *c = malloc(L * sizeof(*c)); double *r = malloc(L * sizeof(*r)); double *temp = malloc(L * sizeof(*temp)); double y; compute_dx(xx, L, dx); compute_dfactor(dx, L, dfactor); compute_xInt(xx, L, xInt); for(ii=0; ii < L; ii++) V[ii] = Vfunc(xx[ii], nu1); for(ii=0; ii < L-1; ii++) VInt[ii] = Vfunc(xInt[ii], nu1); tridiag_malloc(L); for(jj=0; jj < M; jj++){ y = yy[jj]; Mfirst = Mfunc2D(xx[0], y, m12, gamma1, h1); Mlast = Mfunc2D(xx[L-1], y, m12, gamma1, h1); for(ii=0; ii < L-1; ii++) MInt[ii] = Mfunc2D(xInt[ii], y, m12, gamma1, h1); compute_delj(dx, MInt, VInt, L, delj, use_delj_trick); compute_abc_nobc(dx, dfactor, delj, MInt, V, dt, L, a, b, c); for(ii = 0; ii < L; ii++) r[ii] = phi[ii*M + jj]/dt; if((jj==0) && (Mfirst <= 0)) b[0] += (0.5/nu1 - Mfirst)*2./dx[0]; if((jj==M-1) && (Mlast >= 0)) b[L-1] += -(-0.5/nu1 - Mlast)*2./dx[L-2]; tridiag_premalloc(a, b, c, r, temp, L); for(ii = 0; ii < L; ii++) phi[ii*M + jj] = temp[ii]; } tridiag_free(); free(dx); free(dfactor); free(xInt); free(MInt); free(V); free(VInt); free(delj); free(a); free(b); free(c); free(r); free(temp); }
//User01: blakeelias Team: Stuy-Naught Project: Kill-Retreat void ZRUser01(float *myState, float *otherState, float time) { #define position procvar #define attitude (procvar+3) #define panel_location (procvar+6) memcpy(vOtherPrev, vOther, sizeof(float)*3); //original value copied to previous value memcpy(vOther, otherState+3, sizeof(float)*3); // this is set to the current value // Wait for them to run out of fuel (constant velocity) if ((Vfunc(6, vOther, vOtherPrev, NULL, 0)) <= 0.01) counter++; else counter = 0; DEBUG(("time: %3.0f, SPH%d: state %i\n", time, getPanelSide() == 1 ? 1 : 2, state)); if (state == 1) { //Zapping // Get between the opponent and the sun memcpy(position, otherState, sizeof(float)*3); mathVecNormalize(position, 3); Vfunc(4, position, NULL, position, 0.1); CoastToTarget(myState, position, 0.04); // Face the opponent Vfunc(9, myState, otherState, attitude, 0); ZRSetAttitudeTarget(attitude); // Zap once we see them if (fabs (Vfunc(8, myState+6, attitude, NULL, 0)) < 6.0) { ZRRepel(); } //DEBUG(("Counter = %i \n", counter)); if(iHavePanel()) { if (counter >= 15 || getPercentFuelRemaining() < 10) state = 6; } else { if (counter >= 15 || time > 100) state = 2; else if (getPercentFuelRemaining() < 20) state = 3; } } if (state == 2) { //Moving to panel circle, they're dead if (isPanelFound()) state = 5; else { attitude[0] = 0; attitude[1] = 1*getPanelSide(); attitude[2] = 0; position[0] = .7*getPanelSide(); position[1] = 0; position[2] = 0; CoastToTarget(myState, position, 0.04); ZRSetAttitudeTarget(attitude); if (Vfunc(6, myState, position, NULL, 0) < 0.03) state = 4; } } if (state == 3) { // Retreating to panel, they are alive if (isPanelFound()) state = 5; else { position[0] = .7*getPanelSide(); position[1] = 0; position[2] = 0; CoastToTarget(myState, position, 0.04); } if (Vfunc(6, position, myState, NULL, 0) < 0.03) state = 4; } if (state == 4) { //Finding panel RotateTarget(myState, position); if (isPanelFound()) state = 5; if (iHavePanel()) state = 6; } if (state == 5) { //Get the panel getPanelState(position); attitude[0] = 0.7*getPanelSide(); attitude[1] = attitude[2] = 0; Vfunc(9, attitude, position, attitude, 0); ZRSetAttitudeTarget(attitude); CoastToTarget(myState, position, 0.04); if (iHavePanel()) state++; } if (state == 6) { //Stop if (counter < 15) state = 1; position[0] = position[1] = position[2] = 0; ZRSetVelocityTarget(position); } }