
#define copyright "Copyright 2005 Luke-Jr"
#define compileme \
if gcc "${0}" -o "${0/.c/}" -lm; then ./"${0/.c/}" | tee "${0/.c/}.out"; fi; \
exit

#include <stdio.h>
#include <math.h>

static int numberWinding = 6;
static int scale = 85;
static float ox = 127.5, oy = 73.612160;
// -42.5, 73.612160

void pf_init() {
	printf("ePoint *WP, *SWP;\n");
}

void pf_begin(float x, float y) {
	printf("WP = grid->Insert(eCoord(%.1f, %.6f) * sizeMultiplier);\n", x, y);
}

void pf_drawto(float x, float y) {
	printf("WP = grid->DrawLine(WP, eCoord(%.3f, %.6f) * sizeMultiplier, tNEW(gWallRim) (grid), 0);\n", x, y);
}

void pf_savestate(float x, float y) {
	printf("SWP = WP; // Save state\n", x, y);
}

void pf_restorestate(float x, float y) {
	printf("WP = SWP; // Restore state\n", x, y);
}

void pf_end(float x, float y) {
	; //nothing
}

void pf_precomment() {
	printf("/* ");
}

void pf_postcomment() {
	printf(" */\n");
}

void pf_seperator() {
	printf("\n");
}

void forward(float distance, int i, float *x, float *y)
{
    float tetha;
        tetha = M_PI*2*(numberWinding - i - 1)/numberWinding;	// copied from src/engine/eAxis.cpp
        *x += (cosf(tetha)) * distance;
        *y += (sinf(tetha)) * distance;
}

void turn(int left, int *cd) {
	*cd += left % 6;
	while (cd < 0)
		*cd += 6;
}

void pf_spawn(float x, float y, int direct) {
	float dx, dy;
	dx=dy=0;
	forward(1., direct, &dx, &dy);	// note: this is *1*, NOT *scale*
	printf("NewSpawnPoint(eCoord(%.1f, %.6f),eCoord(%.1f,%.6f));\n", x, y, dx, dy);
}

int main() {
	int cd, scd, j, i;
	float x, y, sx, sy;
	
	pf_precomment(); printf("------- START SPAWN CODE -------"); pf_postcomment();
	
	for (j = 0; j < 2; ++j) {
		cd=0;x=ox;y=oy;
		turn(-1, &cd);								// turn right
		for (i = 0; i < numberWinding; ++i) {
			forward(scale*(4.5-j*2.5)/3, cd, &x, &y);	// jump 2/3
			scd = cd;
			turn(-1-j, &scd);
			pf_spawn(x, y, scd);						// spawn
			forward(scale*(1.5+j*2.5)/3, cd, &x, &y);	// jump 1/3
			turn(-1, &cd);								// turn right
		}
		pf_seperator();
	}
	
	pf_precomment(); printf("------- START GRID CODE -------"); pf_postcomment();
	
	pf_init();
	pf_seperator();
	
	/* Outer grid */
	pf_precomment(); printf("Outer grid"); pf_postcomment();
	
	cd=0;x=ox;y=oy;
	pf_begin(x, y);
	pf_seperator();
	
	for (j = 0; j < numberWinding; ++j) {
		for (i = 0; i < numberWinding / 2; ++i) {	// 3 times:
			forward(scale, cd, &x, &y);					// forward 1
			pf_drawto(x, y);
			turn(-1, &cd);								// turn right
		}
		sx=x; sy=y; scd=cd;							// save state
		pf_savestate(x, y);
		forward(scale/3, cd, &x, &y);				// forward 1/3
		pf_drawto(x, y);
		x=sx; y=sy; cd=scd;							// restore state
		pf_restorestate(x, y);
		turn(2, &cd);								// turn 2 left
		pf_seperator();
	}
	pf_end(ox, oy);
	
	/* Now, the inner grid =D */
	pf_precomment(); printf("Inner grid"); pf_postcomment();
	cd=0;x=ox;y=oy;
	/* Locate inner start point */
	turn(-2, &cd);								// turn 2 right
	forward(scale, cd, &x, &y);					// jump 1
	turn(1, &cd);								// turn left
	forward(scale*2/3, cd, &x, &y);				// jump 2/3
	for (j = 0; j < numberWinding; ++j) {
		pf_seperator();
		pf_begin(x, y);								// begin
		forward(scale/3, cd, &x, &y);				// forward 1/3
		pf_drawto(x, y);
		sx=x; sy=y; scd=cd;							// save state
		pf_savestate(x, y);
		turn(1, &cd);								// turn left
		forward(scale/3, cd, &x, &y);				// forward 1/3
		pf_drawto(x, y);
		x=sx; y=sy; cd=scd;							// restore state
		pf_restorestate(x, y);
		turn(-1, &cd);								// turn right
		forward(scale/3, cd, &x, &y);				// forward 1/3
		pf_drawto(x, y);
		pf_end(x, y);
		forward(scale/3, cd, &x, &y);				// jump 1/3
		//155,168
		//170,145
		//198,145
	}
}


