Пример #1
0
int Filesys::fssynch() {
    // Write FAT to Sdisk.
    std::string buffer;
    std::ostringstream outstream, outstream2;
    for (int i = 0; i < getNumberOfBlocks(); i++) {
        outstream << std::setfill('0') << std::setw(5)<< fat[i] << " ";
    }
    buffer = outstream.str();
    // Block before writing
    std::vector<std::string> blocks = block(buffer, getBlocksize());
    //std::cout << "blocks size: " << blocks.size() << '\n';
    for (int i = 1; i <= blocks.size(); i++) {
        //std::cout << blocks[i-1] << '\n';
        putBlock(i, blocks[i - 1]);
    }

    // Now write root to disk
    for (int i = 0; i < rootSize; i++) {
        outstream2 << fileName[i] << " " << std::setfill('0') 
                   << std::setw(5) << firstBlock[i] << " ";
    }
    buffer = outstream2.str();
    //std::cout << "Root buffer: " << buffer << '\n';
    blocks.clear();
    blocks = block(buffer, getBlocksize());
    //std::cout << "blocks size: " << blocks.size() << '\n';
    for (int i = 0; i < blocks.size(); i++) {
        putBlock(i, blocks[i]);
    }
}
Пример #2
0
int main(int argc, char *argv[])
{

  int n, blocks, i;
  while(scanf("%d%d", &n, &blocks) != EOF)
  {
    for(i = 0; i < blocks; i ++)
    {
      double x, y;
      scanf("%lf%lf", &x, &y);
      putBlock(x, y);
    }
    for(i = 0; i < n; i ++)
    {
      putBall();
    }
    listNode *tmp = head;
    double R2 = 0;
    printf("X\t  Y\t  R\n");
    while (tmp) {
      if(tmp->isblock)
      {
        tmp = tmp->next;
        continue; 
      }
      printf("%.3lf\t %.3lf\t %.3lf\t\n",tmp->ball.x, tmp->ball.y, tmp->ball.r);
      R2 += tmp->ball.r * tmp->ball.r;
      tmp = tmp->next;
    }
    printf("\nsum of R^2: \t%lf\n", R2);
    freeBox();
  }

  return 0;
}
Пример #3
0
int Filesys::addBlock(std::string file, std::string block) {
    int allocate;
    int fileBlock = getFirstBlock(file);
    if (fileBlock == -1) { // No available blocks; fs full.
        throw std::invalid_argument("Cannot add block. File doesn't exist.");
        return 0;
    } else if (fileBlock == 0) { // No blocks in file, OK to add.
        allocate = fat[0];
        if (allocate == 0) { // Disk is full.
            throw std::invalid_argument("Disk is full, cannot add block.");
            return -1;
        } else {
            fat[0] = fat[fat[0]];
            fat[allocate] = 0;
            // Update root
            for (int i = 0; i < fileName.size(); i++) {
                if (fileName[i] == file) {
                    firstBlock[i] = allocate;
                }
            }
        }
    } else { // File already has blocks. 
        allocate = fat[0];
        fat[0] = fat[fat[0]];
        fat[allocate] = 0;
        // Follow links to change previous last block.
        while (fat[fileBlock] != 0) {
            fileBlock = fat[fileBlock];
        }
        fat[fileBlock] = allocate;
    }
    fssynch();
    putBlock(allocate, block);
    return allocate;
}
Пример #4
0
void Game::askGodToDo(Map & map, int & wait)
	{
		unsigned int x, y;
		int tBdstr = _god.makeStep((*this),x,y);	//Ile zostanie zniszczonych
		if(tBdstr>0)
		{
			_printer.printMessage(GOD_DECIDE_TO_DO,cout);
			int result = putBlock(x,y,_map,PS_GOD_BLOCK);
			prepareWeights(_map);
			list<ElementData *> * path = testWeight(_map);
			if(path->size() > 0)
			{
				if(_map(x,y,DIM_WHO_TAB) != path->back()->operator [](DIM_WHO_TAB))
				{
					_destroyedBlocks += destroyElements(path);
					_printer.printMessage(GOD_SAY_OMG,cout);
					_printer.makeBoomConsole();
				}
				else
					_printer.printMessage(GOD_BLESS_YOU, cout);
				wait++;	
			}
			delete path;
		}
	}
Пример #5
0
void erase(MLan *mlan, uchar *serial)
{
    uchar buffer[DATA_SIZE];

    assert(mlan);
    assert(serial);

    memset(buffer, 0x00, sizeof(buffer));
    putBlock(mlan, serial, buffer, START_PAGE, 16, 512);
}
Пример #6
0
int Filesys::writeBlock(std::string file, int blockNumber, std::string buffer) {
    if (checkBlock(file, blockNumber)) { // Block is in file.
        putBlock(blockNumber, buffer);
        fssynch();
        return 1;
    } else { // Block not in file.
        throw std::invalid_argument("Cannot write: block permission error.");
        return 0;
    }
}
Пример #7
0
void putFile(MLan *mlan, uchar *serial, char *file)
{
    int fd=-1;
    struct stat st;
    uchar buffer[DATA_SIZE];
    int size;
    int i=0;

    assert(mlan);
    assert(serial);
    assert(file);

    memset(buffer, 0x00, sizeof(buffer));

    if(stat(file, &st)<0) {
        perror(file);
        exit(-1);
    }
    size=st.st_size;
    if(size>(DATA_SIZE-START_DATA)) {
        fprintf(stderr, "You're too big for me (%d bytes)\n", size);
        exit(-1);
    }

    /* LSB first */
    buffer[0]=(size&0xFF);
    buffer[1]=(size>>8);

    fd=open(file, O_RDONLY, 0);
    if(fd<0) {
        perror(file);
        exit(-1);
    }
    /* Read starting at the fourth byte */
    if( read(fd, buffer+START_DATA, size) != size) {
        fprintf(stderr, "Did not read enough data.\n");
        exit(-1);
    }

    /* Calculate the CRC */
    mlan->DOWCRC=buffer[0];
    mlan->dowcrc(mlan, buffer[1]);
    for(i=0; i<size; i++) {
        mlan->dowcrc(mlan, buffer[i+START_DATA]);
    }

    buffer[2]=mlan->DOWCRC;

    putBlock(mlan, serial, buffer, START_PAGE, 16, size);
}
Пример #8
0
int Game::askUserToDo(Map & map, int & wait)
	{
		unsigned int x,y;
		do
		{
			_printer.printMessage(USER_ASKED_TO_DO,cout);	//Prosba o wpisanie danych
			_user.makeStep(x,y);							//uzytkownik wpisuje dane
		}while(x>map.getWidth()-1 || y>map.getHeight()-1);
		//Wstawianie bloku
		int result = putBlock(x,y,map);
		//reakcja na wstepny test czy w ogole sie da polozyc blok
		if(result == GAME_BUSY_BLOCK)
		{	
			_printer.printMessage(GAME_BUSY_BLOCK,cout);
			wait++;
		}
		else if(result == GAME_IN_THE_AIR_BLOCK)			//Jesli w powietrzu kamien
		{
			_printer.makeBoomConsole();
			_printer.printMessage(GAME_IN_THE_AIR_BLOCK, cout);	//To prosba, aby nie klasc w powietrzu kamieni
			bool destroyedBlock = false;
			for(int i= y; i>=0 && !destroyedBlock; i--)		//Sprawdzenie czy spadl kamien na jakis inny
			{
				if(map(x,i,DIM_WEIGHT_TAB)!=EMPTY)		//Jesli spadl to trzeba o tym poinformowac i usunac kamien
				{
					_destroyedBlocks++;
					_printer.printMessage(GAME_SAY_YOU_DESTROYED, cout);
					clearBlock(x,i,map);
					destroyedBlock = true;
					wait ++;
				}//Szukanie czy mozna rozbic jakis blok
			}
			_usedBlocks++;
			_destroyedBlocks++;
			wait++;
		}
		if(result == 0)
		{	
			_usedBlocks++;
		}
		return result;
	}
Пример #9
0
ulong
writeBlock(uchar *b, int type)
{
	uchar *cb, *ob;
	int n;
	PaqBlock bh;
	uchar buf[BlockSize];
	ulong offset;

	offset = Boffset(out);

	bh.magic = BlockMagic;
	bh.size = blocksize;	
	bh.type = type;
	bh.encoding = NoEnc;
	bh.adler32 = adler32(0, b, blocksize);
	ob = b;

	if(!uflag) {
		cb = emallocz(blocksize);
		n = deflateblock(cb, blocksize, b, blocksize, 6, 0);
		if(n > 0 && n < blocksize) {
			bh.encoding = DeflateEnc;
			bh.size = n;
			ob = cb;
		}	
	}

	putBlock(buf, &bh);
	outWrite(buf, sizeof(buf));
	outWrite(ob, bh.size);
	
	if(ob != b)
		free(ob);
	return offset;
}
Пример #10
0
void GfxMgr::putScreen() {
	putBlock(0, 0, GFX_WIDTH - 1, GFX_HEIGHT - 1);
}
Пример #11
0
bool waitEvent(SDL_Event event, SDL_Window* window, int* selectedItem, Player* player, block** map, client_network out){ //Gére les différents évenement
	bool quit = false;
	int mouseX,mouseY;

	//block air = {NONE, SKY};
	//block cave = {NONE, CAVE};
	//block dirt = {DIRT, SKY, true};
	//block stone = {STONE, CAVE, true};
	//block wood = {WOOD, CAVE};
	//block iron = {IRON, CAVE, true};
	if(SDL_WaitEventTimeout(&event, 50)){
		switch (event.type) {
			case SDL_WINDOWEVENT:
				if(event.window.event == SDL_WINDOWEVENT_CLOSE){
					quit = true;
					printf("Close window\n");
				}
				break;
			case SDL_MOUSEBUTTONUP:
				if(event.button.button == SDL_BUTTON_LEFT){
					SDL_GetMouseState(&mouseX,&mouseY);
					putBlock(player, mouseX/24 + player->position[0] - NB_LIGNE/2, mouseY/24 + player->position[1] - NB_COLONNE/2, *selectedItem, map, out);
					SDL_UpdateWindowSurface(window);
				}
				else if(event.button.button == SDL_BUTTON_RIGHT){
					SDL_GetMouseState(&mouseX,&mouseY);
					breakBlock(player, mouseX/24 + player->position[0] - NB_LIGNE/2, mouseY/24 + player->position[1] - NB_COLONNE/2, map, out);
					SDL_UpdateWindowSurface(window);
				}
				break;
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym){
					case SDLK_ESCAPE:
						quit = true;
						printf("Close window\n");
						break;
					case SDLK_LEFT:
					    move(player, LEFT, map, out);
					    break;
					case SDLK_RIGHT:
					    move(player, RIGHT, map, out);
					    break;
					case SDLK_UP:
					    move(player, TOP, map, out);
					    break;
					/*case SDLK_DOWN:
					    move(player, BOT, map, out);
					    break;*/
					case '&':
						*selectedItem = 0;
						break;
					case 233:
						*selectedItem = 1;
						break;
					case '"':
						*selectedItem = 2;
						break;
					case '\'':
						*selectedItem = 3;
						break;
				}
				break;
			case SDL_MOUSEWHEEL :
				if(event.wheel.y == -1){
					*selectedItem = (*selectedItem + 1) % 4;
				}
				else if (event.wheel.y == 1){
					*selectedItem = (*selectedItem - 1) ;
					if(*selectedItem < 0){
						*selectedItem = 3;
					}
				}
				SDL_UpdateWindowSurface(window);
				break;
		}
	}

	return quit;
}
Пример #12
0
int playGame()
{
	int tempx = current.getPosX(); //Temp variables for position
	int tempy = current.getPosY();

	if (keyboard_counter > 0 && keypressed()) //Keyboard Delay
	{
		if (key[KEY_LEFT]) //Move to the left
		{
			if (noCollision(current, tempx - 1, tempy)) //Checks collisions with the left movement
			{
				tempx--; //If there is not collision move the piece
				current.setPosX(tempx);
			}
		}
			
		if (key[KEY_RIGHT])
		{
			if (noCollision(current, tempx + 1, tempy))
			{
				tempx++;
				current.setPosX(tempx);
			}
		}
		
		if (key[KEY_DOWN])
		{
			if (noCollision(current, tempx, tempy + 1))
			{
				tempy++;
				current.setPosY(tempy);
			}
		}
		
		if (key[KEY_UP]) //Rotate the piece to the right
		{	
			/*Create a temp piece for the request rotation*/

			cPiece temp;
				
			temp.type = current.type;
			temp.rotation = rotateBlock(1, current.rotation);
				
			cleanPiece(temp);
			changePiece(temp, temp.type, temp.rotation);
			temp.setPosX(current.getPosX());
			temp.setPosY(current.getPosY());
				
			if (noCollision(temp, temp.getPosX(), temp.getPosY())) //Checks collision for the temp piece
			{
				//If there is not collision, the real falling(current) piece rotates to the right
				current.rotation = rotateBlock(1, current.rotation);
				cleanPiece(current);
				changePiece(current, current.type, current.rotation);
			}
		}

		if (key[KEY_T]) //Up the volume of the music
		{
			volume[0]+= 5;
			if (volume[0] > 255){
				volume[0] = 255;}

			options[4].d2 = volume[0];
			adjust_sample(sound[0], volume[0], 128, 1000, TRUE);
			adjust_sample(sound[1], volume[0], 128, 1000, TRUE);
		}

		if (key[KEY_G]) //Down the volume of the music
		{
			volume[0]-= 5;
			if (volume[0] < 0){
				volume[0] = 0;}

			options[4].d2 = volume[0];
			adjust_sample(sound[0], volume[0], 128, 1000, TRUE);
			adjust_sample(sound[1], volume[0], 128, 1000, TRUE);
		}

		if (key[KEY_Y]) //Up the volume of the sound effects
		{
			volume[1]+= 5;
			if (volume[1] > 255){
				volume[1] = 255;}

			options[6].d2 = volume[1];
			adjust_sample(sound[2], volume[1], 128, 1000, TRUE);
			adjust_sample(sound[3], volume[1], 128, 1000, TRUE);
		}

		if (key[KEY_H]) //Down the volume of the sound effects
		{
			volume[1]-= 5;
			if (volume[1] < 0){
				volume[1] = 0;}

			options[6].d2 = volume[1];
			adjust_sample(sound[2], volume[1], 128, 1000, TRUE);
			adjust_sample(sound[3], volume[1], 128, 1000, TRUE);
		}

	keyboard_counter--;
	}

    fpscount++;
	
	if (ticks > speed) 
	{
		tempx = current.getPosX();
		tempy = current.getPosY();
		
		if (noCollision(current, tempx, tempy + 1)) //Checks collision for the falling
		{
			tempy++;
			current.setPosY(tempy);
		}
		
		else
		{
			play_sample(sound[2], volume[1], 128, 1000, FALSE);
			putBlock(current, tempx, tempy); //Put block on the board

			possibleRows(); //Check if a row is fill

			if (gameOver()) //Checks if the game is over
			{
				quitgame = 1;
			}
			
			else
				createBlock(current, next); //If the game it's not over create new pieces

		}

		ticks = 0;

	}

	return quitgame;
}