int main(int argc, char **argv) { GOC_IStream *is = NULL; GOC_OStream *os = NULL; int c; // unsigned char *pBuff; // unsigned int nBuff; char *line; if ( argc < 2 ) { GOC_ERROR("No input file name"); return -1; } os = goc_memOStreamOpen(); GOC_INFO("--- GOC_FileIStream test"); is = goc_fileIStreamOpen(argv[1]); // nBuff = goc_isAvailable(is); // pBuff = malloc(nBuff); GOC_BINFO("Available: %d\n", goc_isAvailable(is)); // nBuff = 0; while ( (c = goc_isReadChar(is)) > 0 ) { goc_osWriteByte(os, (unsigned char)c); // pBuff[nBuff++] = c; putchar(c); } goc_isClose(is); GOC_INFO("--- GOC_MemIStream test\n"); is = goc_memIStreamAttach(goc_memOStreamGet(os), goc_memOStreamSize(os)); // nBuff = goc_isAvailable(is); GOC_BINFO("Available: %d\n", goc_isAvailable(is)); while ( (c = goc_isReadChar(is)) > 0 ) putchar(c); goc_isClose(is); GOC_INFO("--- GOC_FileIStream readLine test"); is = goc_fileIStreamOpen(argv[1]); while ( (line = goc_isReadLine(is)) ) { printf("%s\n", line); } goc_isClose(is); return 0; }
static void generateFillUp(int code) { int x; int y; GOC_BINFO("Filling up with %d", code); for ( x = 0; x < context->configuration.maxx; x++ ) { for ( y = 0; y < context->configuration.maxy; y++ ) { goc_maparawSetPoint(context->mapa, x, y, code); } } }
void generateBeat(Parameters* params, int ibeat) { // wyznaczanie wielko¶ci pojedynczej ramki d¼wieku w bajtach, jest to // liczba bajtów na jedn± próbê // * liczba próbek dla jednej ramki // liczba bajtów na jedn± próbê to // liczba bajtów na kana³ // * liczba kana³ów // liczba próbek dla jednej ramki // liczba próbek na 1 sekundê (tzw. rate) // * liczba sekund w minucie (60) // / tempo int nsamples = SAMPLE_RATE * 60 / params->tempo / (params->inter+1); int nFrame = SAMPLE_BITS / 8 * SAMPLE_CHANNELS * nsamples; // warto¶æ próbki int sample; // moment w czasie w ramce float moment; // licznik int i; params->pBeat[ibeat].nFrame = nFrame; params->pBeat[ibeat].pFrame = malloc(nFrame); GOC_BINFO("Generate beat %d with params: duration: %.2fs freq: %.2fHz power: %.2f frames: %d", ibeat, params->tdur, params->pBeat[ibeat].freq, params->pBeat[ibeat].power, params->pBeat[ibeat].nFrame); // wyznaczenie w próbce danych na czas trwania ticku w danym momencie // Jedna sekunda i/SAMPLE_RATE to moment w czasie od 0..1 sec for ( i=0; i<nsamples; i++ ) { // moment [sec] w ramce, któr± budujemy moment = (float)i/SAMPLE_RATE; if ( moment > params->tdur ) break; // cisza // i/SAMPLE_RATE to moment dla przebiegu fali o danej czêstotliwo¶ci // w stosunku do pe³nej jednosekundowej próbki, w którym // zostaje pobrana próbka sample = (int)(params->pBeat[ibeat].power*MAX_SHORT*sin(2*M_PI*params->pBeat[ibeat].freq*((float)i/SAMPLE_RATE))); // DLA 16 bitowego dwukana³owego params->pBeat[ibeat].pFrame[i<<2] = params->pBeat[ibeat].pFrame[(i<<2)+2] = sample & 0xFF; params->pBeat[ibeat].pFrame[(i<<2)+1] = params->pBeat[ibeat].pFrame[(i<<2)+3] = (sample>>8) & 0xFF; } }
// TODO: Random start point // TODO: Generate around start point in selected radius with kind of gauss propability or // kond of selected propability static void rainGeneration(GOC_HANDLER mapa, int numStartPoints) { int landMass = context.configuration.minLandMass + goc_random(context.configuration.maxLandMass-context.configuration.minLandMass); stChange point; point.x = 0; point.y = 0; point.v = 0; GOC_BINFO("Raind landmass %d", landMass); while ( landMass-- ) { int x = point.x = goc_random(context.configuration.maxx); int y = point.y = goc_random(context.configuration.maxy); int v = goc_maparawGetPoint(mapa, point.x, point.y); if ( v >= 9 ) continue; goc_maparawSetPoint(mapa, point.x, point.y, v+1); } }
static void generateLandMass( int code, // genrowany kod int *pAllowedCode, // dozwolona tablica kodow do zjadania int nAllowedCode) { stChange** pBiomeSet = NULL; int nBiomeSet = 0; int landMass = context->configuration.minLandMass + goc_random(context->configuration.maxLandMass-context->configuration.minLandMass); stChange* point = NULL; GOC_BINFO("Generate land %d with landmass size %d", code, landMass); point = randomLandMassStartPoint(code, pAllowedCode, nAllowedCode, 20); if ( point == NULL ) { GOC_INFO("Start point of landmass not found"); return; } pBiomeSet = goc_tableAdd(pBiomeSet, &nBiomeSet, sizeof(void*)); pBiomeSet[nBiomeSet-1] = point; goc_maparawSetPoint(context->mapa, point->x, point->y, point->v); while ( landMass ) { if ( ! nBiomeSet ) { break; } int r = goc_random(nBiomeSet); point = pBiomeSet[r]; int x = point->x; int y = point->y; // czy ma zginac - TODO: MARGIN if ( ( (x+1 >= context->configuration.maxx) || (!isInCodeSet(goc_maparawGetPoint(context->mapa, x+1, y), pAllowedCode, nAllowedCode)) ) && ( (y+1 >= context->configuration.maxy) || (!isInCodeSet(goc_maparawGetPoint(context->mapa, x, y+1), pAllowedCode, nAllowedCode)) ) && ( (y-1 <= 0) || (!isInCodeSet(goc_maparawGetPoint(context->mapa, x, y-1), pAllowedCode, nAllowedCode)) ) && ( (x-1 <= 0) || (!isInCodeSet(goc_maparawGetPoint(context->mapa, x-1, y), pAllowedCode, nAllowedCode)) ) ) { free(pBiomeSet[r]); pBiomeSet = goc_tableRemove(pBiomeSet, &nBiomeSet, sizeof(void*), r); continue; } // znajdz, jakie miejsce moze zarazic { // RANDOM ONLY in 4 base direction int r = goc_random(4); switch ( r ) { case 0: x++; break; case 1: x--; break; case 2: y++; break; case 3: y--; break; } } // wykluczenie if (( x >= context->configuration.maxx ) || ( y >= context->configuration.maxy ) || ( x < 0 ) || ( y < 0 )) { continue; } if ( !isInCodeSet( goc_maparawGetPoint(context->mapa, x, y), pAllowedCode, nAllowedCode) ) { continue; } // wykonaj zarazenie { stChange* newpoint = mallocPoint(x, y, point->v); pBiomeSet = goc_tableAdd(pBiomeSet, &nBiomeSet, sizeof(void*)); pBiomeSet[nBiomeSet-1] = newpoint; goc_maparawSetPoint(context->mapa, x, y, point->v); landMass--; // goc_maskPaintPoint(maska, x, y); // TODO: metoda paintAreaPoint, do rysowania punktu wskazanego z danych } } pBiomeSet = goc_tableClear(pBiomeSet, &nBiomeSet); }
static int hotkeyNextTurnDrops( GOC_HANDLER u, GOC_StMessage* msg) { // TODO: Setting max and min landmass for forest/hills/land seperate GOC_BINFO("Turn: %3d", nBiomeTurn); if ( nBiomeTurn <= 0 ) { // fill up with sea generateFillUp(6); } else if ( nBiomeTurn <= biome.lands ) { int *allowed = malloc(sizeof(int)*1); allowed[0] = 6; generateLandMass(1, allowed, 1); free(allowed); } else if ( nBiomeTurn <= biome.lands + biome.trees ) { int *allowed = malloc(sizeof(int)*1); allowed[0] = 1; generateLandMass(2, allowed, 1); free(allowed); } else if ( nBiomeTurn <= biome.lands + biome.trees + biome.hills ) { int *allowed = malloc(sizeof(int)*1); allowed[0] = 1; generateLandMass(4, allowed, 1); free(allowed); } else if ( nBiomeTurn <= biome.lands + biome.trees + biome.hills + biome.desert ) { int *allowed = malloc(sizeof(int)*1); allowed[0] = 1; generateLandMass(5, allowed, 1); free(allowed); } else if ( nBiomeTurn <= biome.lands + biome.trees + biome.hills + biome.desert + biome.swamp) { int *allowed = malloc(sizeof(int)*1); allowed[0] = 1; generateLandMass(7, allowed, 1); free(allowed); } else { GOC_INFO("This is the end"); // I think it's the end return GOC_ERR_OK; } nBiomeTurn++; GOC_MSG_PAINT( msgPaint ); goc_systemSendMsg(context->mapa, msgPaint); goc_gotoxy(0, 0); goc_textallcolor(GOC_WHITE); printf("%03d", nBiomeTurn); goc_gotoxy(0, 0); fflush(stdout); return GOC_ERR_OK; }
int main(int argc, char** argv) { // zak³ada siê, ¿e ramka zawiera próbkê, w której // jest d¼wiêk beat-u i cisza do nastêpnego beatu // // // ramka //char *pFrame; // licznik int i; // goc_termInit(); printf(GOC_STRING_WHITE); // Procedury inicjuj±ce GOC_DEBUG("Initializing libao"); ao_initialize(); GOC_DEBUG("Initializing libao Device"); aodriverid = ao_default_driver_id(); if ( aodriverid == -1 ) return -1; openOutputDevice(); params = allocParameters(); if ( argc > 1 ) { if ( interpretArgs(argc, argv, params) < 0 ) endProgram(-1); } GOC_DEBUG("Allocating buffer"); generateBeat(params, BEAT_BASE); generateBeat(params, BEAT_ACCENT); generateBeat(params, BEAT_INTER); generateBeat(params, BEAT_QUIET); // pFrame = malloc(nFrame); // memset(pFrame, 0, nFrame); GOC_BINFO("Tempo: %d Rate: %d Channels: %d Bits: %d", params->tempo, SAMPLE_RATE, SAMPLE_CHANNELS, SAMPLE_BITS); // GOC_BDEBUG("Allocated %d at 0x%X", nFrame, (unsigned int)pFrame); if ( params->flags & FLAG_PRINTCOUNT ) { goc_clearscreen(); goc_gotoxy(1,1); } printf("Playing ... \n"); if ( params->pattern ) printf("Use pattern: %s\n", params->pattern); fflush(stdout); { // to nie do koñca jest liczba ramek - to jest liczba // podstawowych beatów jakie maj± zostaæ zagrane - nie // s± liczone beat-y po¶rednie int nFrames; int imeasure = 0; int iinter; int patternPosition = 0; int patternLen = 0; char *prevString = goc_stringCopy(NULL, ""); char playedPattern = '.'; if ( params->pattern ) { patternLen = strlen(params->pattern); } if ( params->tacts ) // na podstawie liczby podanych taktów nFrames = params->tacts * params->measure; else // na podstawie podanego czasu nFrames = params->tempo * params->pdur / 60; NEXTPATTERN(); // goc_saveXY(); for (i=0; i<nFrames; i++) { if ( imeasure ) { if ( params->flags & FLAG_PRINTCOUNT ) { goc_gotoxy(1, 5); printf("%s%s%s%d%s", GOC_STRING_YELLOW, prevString, GOC_STRING_BWHITE, imeasure + 1, GOC_STRING_YELLOW); prevString = goc_stringAddInt(prevString, imeasure+1); prevString = goc_stringAdd(prevString, " .. "); fflush(stdout); } if ( playedPattern == '.' ) ao_play(aodev, params->pBeat[BEAT_BASE].pFrame, params->pBeat[BEAT_BASE].nFrame); else ao_play(aodev, params->pBeat[BEAT_QUIET].pFrame, params->pBeat[BEAT_QUIET].nFrame); NEXTPATTERN(); } else { if ( params->flags & FLAG_PRINTCOUNT ) { goc_gotoxy(1, 5); printf("%s%s", GOC_STRING_YELLOW, prevString); goc_gotoxy(1, 5); printf("%s%d%s", GOC_STRING_BWHITE, imeasure + 1, GOC_STRING_YELLOW); prevString = goc_stringFree(prevString); prevString = goc_stringAddInt(prevString, imeasure+1); prevString = goc_stringAdd(prevString, " .. "); fflush(stdout); } if ( playedPattern == '.' ) ao_play(aodev, params->pBeat[BEAT_ACCENT].pFrame, params->pBeat[BEAT_ACCENT].nFrame); else ao_play(aodev, params->pBeat[BEAT_QUIET].pFrame, params->pBeat[BEAT_QUIET].nFrame); NEXTPATTERN(); } if ( (iinter = params->inter) ) { while ( iinter-- ) { if ( params->flags & FLAG_PRINTCOUNT ) { goc_gotoxy(1, 5); printf("%s%s%si%s", GOC_STRING_YELLOW, prevString, GOC_STRING_WHITE, GOC_STRING_YELLOW); prevString = goc_stringAdd(prevString, "i .. "); fflush(stdout); } if ( playedPattern == '.' ) ao_play(aodev, params->pBeat[BEAT_INTER].pFrame, params->pBeat[BEAT_INTER].nFrame); else ao_play(aodev, params->pBeat[BEAT_QUIET].pFrame, params->pBeat[BEAT_QUIET].nFrame); NEXTPATTERN(); } } imeasure++; imeasure %= params->measure; } if ( params->flags & FLAG_PRINTCOUNT ) { goc_gotoxy(1, 5); printf("%s%s", GOC_STRING_YELLOW, prevString); } prevString = goc_stringFree(prevString); } printf("%s\nFinish\n", GOC_STRING_WHITE); if ( params->tacts ) { GOC_BINFO("Played %d frames in %d tacts", i, params->tacts); } else { GOC_BINFO("Played %d frames in %d seconds", i, params->pdur); } endProgram(0); return 0; }
static int hotkeyNextTurnBactery( GOC_HANDLER u, GOC_MSG m, void* pBuf, unsigned int nBuf) { int randomAtOneTurn = 100; int nKill = 0; int nBurn = 0; int nInvade = 0; GOC_BINFO("Turn %d - randoms(%d)", nBiomeTurn, randomAtOneTurn); nBiomeTurn++; while ( randomAtOneTurn-- ) { if ( ! nBiomeSet ) { break; } int r = goc_random(nBiomeSet); stChange* point = pBiomeSet[r]; int x = point->x; int y = point->y; // czy ma zginac if ( ( (x+1 >= context.configuration.maxx) || (goc_maparawGetPoint(context.mapa, x+1, y) != 0) ) && ( (y+1 >= context.configuration.maxy) || (goc_maparawGetPoint(context.mapa, x, y+1) != 0 )) && ( (y-1 <= 0) || (goc_maparawGetPoint(context.mapa, x, y-1) != 0) ) && ( (x-1 <= 0) || (goc_maparawGetPoint(context.mapa, x-1, y) != 0) ) ) { nKill++; free(pBiomeSet[r]); pBiomeSet = goc_tableRemove(pBiomeSet, &nBiomeSet, sizeof(void*), r); continue; } // znajdz, jakie miejsce moze zarazic { // RANDOM ONLY in 4 base direction int r = goc_random(4); switch ( r ) { case 0: x++; break; case 1: x--; break; case 2: y++; break; case 3: y--; break; } } // wykluczenie if (( x >= context.configuration.maxx ) || ( y >= context.configuration.maxy ) || ( x < 0 ) || ( y < 0 )) { nBurn++; continue; } if ( goc_maparawGetPoint(context.mapa, x, y) != 0 ) { nBurn++; continue; } // wykonaj zarazenie { nInvade++; stChange* newpoint = mallocPoint(x, y, point->v); pBiomeSet = goc_tableAdd(pBiomeSet, &nBiomeSet, sizeof(void*)); pBiomeSet[nBiomeSet-1] = newpoint; goc_maparawSetPoint(context.mapa, x, y, point->v); // goc_maskPaintPoint(maska, x, y); // TODO: metoda paintAreaPoint, do rysowania punktu wskazanego z danych } } GOC_BINFO("Dies: %3d Burns: %3d Invades: %3d", nKill, nBurn, nInvade); goc_systemSendMsg(context.mapa, GOC_MSG_PAINT, 0, 0); return GOC_ERR_OK; }
// TODO : random landMass from min to max // TODO : putting next landMass level for height static void civGeneration(GOC_HANDLER mapa, int numStartPoints, int minLM, int maxLM, int MINLEVEL, int NEWLEVEL) { // int MINLEVEL=0; // int NEWLEVEL=1; int type = 0; stChange point; point.x = 0; point.y = 0; point.v = 0; stChange** pSet = NULL; int nSet = 0; int landMass; int svStartPoints = numStartPoints; int createdLandMass; GOC_BINFO("Start civ generation system [numStartPoint: %d, minLandMass: %d, maxLandMass: %d, level: %d]", numStartPoints, minLM, maxLM, NEWLEVEL); if ( MINLEVEL >= 10 ) return; if ( numStartPoints <= 0 ) return; if ( maxLM <= 0 ) return; while (numStartPoints--) { GOC_BDEBUG("Number of start points %d", numStartPoints); // inicjowanie do kolejnej iteracji tworzenia masy ladowej type = 0; landMass = minLM + goc_random(maxLM-minLM); createdLandMass = 0; GOC_BINFO("Generated landmass: %d", landMass); if ( nSet ) { GOC_DEBUG("Clear set"); int i; for ( i=0; i<nSet; i++ ) free(pSet[i]); pSet = goc_tableClear(pSet, &nSet); } while ( landMass ) { GOC_BDEBUG("Landmass to generate %d", landMass); // random new point if ( randomPoint(&point, type, MINLEVEL, mapa) ) { GOC_BDEBUG("Setting a point (%d,%d,%d)", point.x, point.y, NEWLEVEL); pSet = goc_tableAdd(pSet, &nSet, sizeof(void*)); pSet[nSet-1] = mallocPoint(point.x, point.y, NEWLEVEL); goc_maparawSetPoint(mapa, point.x, point.y, NEWLEVEL); createdLandMass++; } landMass--; while ( nSet ) { // random point from set int randp = goc_random(nSet); int x, y; type = 1; // neighbour // check a point has any free neighbour x = pSet[randp]->x; y = pSet[randp]->y; if ( ( (x+1 < context.configuration.maxx) && (goc_maparawGetPoint(mapa, x+1, y) <= MINLEVEL) ) || ( (y+1 < context.configuration.maxy) && (goc_maparawGetPoint(mapa, x, y+1) <= MINLEVEL )) || ( (y > 0) && (goc_maparawGetPoint(mapa, x, y-1) <= MINLEVEL) ) || ( (x > 0) && (goc_maparawGetPoint(mapa, x-1, y) <= MINLEVEL) ) ) { point.x = x; point.y = y; point.v = pSet[randp]->v; GOC_BDEBUG("Find point in set (%d,%d,%d)", point.x, point.y, point.v); break; } else { GOC_DEBUG("Remove point from set"); free(pSet[randp]); pSet = goc_tableRemove(pSet, &nSet, sizeof(void*), randp); } } // check, if there are any points in set if ( !nSet ) break; } GOC_BINFO("Created landmass: %d", createdLandMass); } civGeneration( mapa, svStartPoints, //-svStartPoints/20, minLM-minLM*precentageCut[MINLEVEL]/10, maxLM-maxLM*precentageCut[MINLEVEL]/10, MINLEVEL+1, NEWLEVEL+1); /* pStartPoint = randomStartPoints(&nStartPoint, numStartPoints); for (i=0; i<nStartPoint; i++) { goc_maparawSetPoint(mapa, pStartPoint[i]->x, pStartPoint[i]->y, 1); } */ }
int main(int argc, char **argv) { GOC_IStream *wordStream = NULL; GOC_IStream *fileStream = NULL; GOC_Properties *props = NULL; GOC_Iterator *propIt = NULL; char *line; char *pos = NULL; if ( argc < 3 ) { GOC_BINFO("Usage: %s wordconvert.properties filetochange", argv[0]); GOC_INFO("\tApplication changes word in file to another"); return 0; } wordStream = goc_fileIStreamOpen(argv[1]); if ( wordStream == NULL ) { GOC_BERROR("Cannot open file %s", argv[1]); return -1; } fileStream = goc_fileIStreamOpen(argv[2]); if ( fileStream == NULL ) { GOC_BERROR("Cannot open file %s", argv[2]); return -1; } props = goc_propertiesAlloc(); props = goc_propertiesLoad(props, wordStream); while ( line = goc_isReadLine(fileStream) ) { char *bline = NULL; char *last = line; pos = NULL; while ( pos = findNextWordBegin(line, pos) ) { char *word = getWord( pos ); char *v = goc_propertiesGetValue( props, word ); *pos = 0; bline = goc_stringAdd(bline, last); if ( v != NULL ) { bline = goc_stringAdd(bline, v); } else { bline = goc_stringAdd(bline, word); } pos += strlen(word); word = goc_stringFree(word); last = pos; } bline = goc_stringAdd(bline, last); puts(bline); line = goc_stringFree(line); bline = goc_stringFree(bline); } free(propIt); goc_isClose(wordStream); goc_isClose(fileStream); return 0; }