int main(int argc, char **argv) { t_env *env; t_map *map; if (argc != 2) return (usage()); if (!(env = (t_env *)ft_memalloc(sizeof(t_env)))) return (-1); mlx_env_init(&(*env)); if (!(map = (t_map *)ft_memalloc(sizeof(t_map)))) return (-1); map = getmap(map, argv[1]); env->map = &(*map); printmap(map, env); mlx_loop(env->mlx); return (0); }
int getvalue(cons_t *next) { switch (next->type) { case L_K: if (next->car != NULL) { return eval(next->car); } else { return eval(next->cdr); } case ARG: return g_arga[g_argl][next->ivalue]; case INT: return next->ivalue; case STR: return g_qa[getmap(next->cvalue)].value; default: return 0; } }
int main(int argc, char* argv[]) { int i; char cpu[10] = {0}; char ip[30] = {0}; char op[100] = {0}; ipgross = getmap(); if (argc == 1) { for(i = 0; i < ipgross; i++) { printf("%s ", map[i].cpu); } printf("\n"); } else { strcpy(cpu, argv[1]); cputoip(cpu, ip); strcat(op, sendcpucommand); strcat(op, " "); strcat(op, cpu); system(op); } return 0; }
void sdl_end() { extern t_sdl t; SDL_EnableKeyRepeat(0, 0); SDL_FreeSurface(t.mur); SDL_FreeSurface(t.echelle); SDL_FreeSurface(t.key_img); SDL_FreeSurface(t.end); SDL_FreeSurface(t.monstre); SDL_FreeSurface(t.coeur); for (t.i = 0 ; t.i < 4 ; t.i++) SDL_FreeSurface(t.mario[t.i]); if (t.flag == 2 && t.niveau == 4) win(1); if (t.flag == 2 && t.niveau != 4) { t.niveau++; getmap(t.lvl[t.niveau]); } if (t.flag == 3) win(2); SDL_Quit(); }
/* Next move: Divide the map into 4 parts to decide the next move. Moving direction is clockwise.*/ struct data next(int map[H][W],struct data x[], int n){ int row, col,prow, pcol, choice,cnt=0,cntflag[3]={0,0,0}; struct data empty={-1,-1,-1},tmpmove[4]; row=x[n-1].row; col=x[n-1].col; /* The center- Special case*/ if(row==H/2 && col ==(W-1)/2){ if(n==1){ switch(choice=rand_int(4)){ case 0: tmpmove[0].row=row; tmpmove[0].col=col+1; tmpmove[0].value=map[row][col+1]; break; case 1: tmpmove[0].row=row+1; tmpmove[0].col=col; tmpmove[0].value=map[row+1][col]; break; case 2: tmpmove[0].row=row; tmpmove[0].col=col-1; tmpmove[0].value=map[row][col-1]; break; case 3: tmpmove[0].row=row-1; tmpmove[0].col=col; tmpmove[0].value=map[row-1][col+1]; break; default:break; } return tmpmove[0]; } else{ prow=x[n-2].row; /* previous row*/ pcol=x[n-2].col; /* previous column*/ if(prow==row){ if(rand_int(2)){ tmpmove[0].row=row; tmpmove[0].col=col+1; tmpmove[0].value=map[row][col+1]; } else{ tmpmove[0].row=row-1; tmpmove[0].col=col; tmpmove[0].value=map[row-1][col]; } } else{ if(rand_int(2)){ tmpmove[0].row=row; tmpmove[0].col=col+1; tmpmove[0].value=map[row+1][col+1]; } else{ tmpmove[0].row=row+1; tmpmove[0].col=col; tmpmove[0].value=map[row+1][col]; } } return tmpmove[0]; } } /* Part 1: Top left->right*/ if((row<3 && col<W-3) ||(row>=3 && row<H-3 && col==(W-1)/2 )){ if(getmap(map,row, col+1)!=-1 && !flag[map[row][col+1]]){ tmpmove[cnt].row=row; tmpmove[cnt].col=col+1; tmpmove[cnt].value=map[row][col+1]; cnt++; cntflag[0]=1; } if(getmap(map,row+1, col)!=-1 && !flag[map[row+1][col]]){ tmpmove[cnt].row=row+1; tmpmove[cnt].col=col; tmpmove[cnt].value=map[row+1][col]; cnt++; cntflag[1]=1; } if(getmap(map,row-1, col)!=-1 && !flag[map[row-1][col]]){ tmpmove[cnt].row=row-1; tmpmove[cnt].col=col; tmpmove[cnt].value=map[row-1][col]; cnt++; cntflag[2]=1; } if(cnt==0) return empty; if(cnt==1) return tmpmove[0]; if(cnt==2){ if(cntflag[0]){ if(rand_int(RANGE)<1) return tmpmove[0]; else return tmpmove[1]; } else{ if(rand_int(2)) return tmpmove[0]; else return tmpmove[1]; } } if(cnt==3){ choice=rand_int(2*RANGE+1); if(!choice) return tmpmove[0]; else if(choice<=RANGE) return tmpmove[1]; else return tmpmove[2]; } } /* Part 2: Right Top->Bottom*/ if(col>=W-3 && row <H-3){ if(getmap(map,row+1, col)!=-1 && !flag[map[row+1][col]]){ tmpmove[cnt].row=row+1; tmpmove[cnt].col=col; tmpmove[cnt].value=map[row+1][col]; cntflag[0]=1; cnt++; } if(getmap(map,row, col+1)!=-1 && !flag[map[row][col+1]]){ tmpmove[cnt].row=row; tmpmove[cnt].col=col+1; tmpmove[cnt].value=map[row][col+1]; cntflag[1]=1; cnt++; } if(getmap(map,row, col-1)!=-1 && !flag[map[row][col-1]]){ tmpmove[cnt].row=row; tmpmove[cnt].col=col-1; tmpmove[cnt].value=map[row][col-1]; cntflag[2]=1; cnt++; } if(cnt==0) return empty; if(cnt==1) return tmpmove[0]; if(cnt==2){ if(cntflag[0]){ if(rand_int(RANGE)<1) return tmpmove[0]; else return tmpmove[1]; } else{ if(rand_int(2)) return tmpmove[0]; else return tmpmove[1]; } } if(cnt==3){ choice=rand_int(2*RANGE+1); if(!choice) return tmpmove[0]; else if(choice<=RANGE) return tmpmove[1]; else return tmpmove[2]; } } /* Part 3: Bottom: Left->Right*/ if(row>=H-3 && col>=3){ if(getmap(map,row, col-1)!=-1 && !flag[map[row][col-1]]){ tmpmove[cnt].row=row; tmpmove[cnt].col=col-1; tmpmove[cnt].value=map[row][col-1]; cntflag[0]=1; cnt++; } if(getmap(map,row+1, col)!=-1 && !flag[map[row+1][col]]){ tmpmove[cnt].row=row+1; tmpmove[cnt].col=col; tmpmove[cnt].value=map[row+1][col]; cntflag[1]=1; cnt++; } if(getmap(map,row-1, col)!=-1 && !flag[map[row-1][col]]){ tmpmove[cnt].row=row-1; tmpmove[cnt].col=col; tmpmove[cnt].value=map[row-1][col]; cntflag[2]=1; cnt++; } if(cnt==0) return empty; if(cnt==1) return tmpmove[0]; if(cnt==2){ if(cntflag[0]){ if(rand_int(RANGE)<1) return tmpmove[0]; else return tmpmove[1]; } else{ if(rand_int(2)) return tmpmove[0]; else return tmpmove[1]; } } if(cnt==3){ choice=rand_int(2*RANGE+1); if(!choice) return tmpmove[0]; else if(choice<=RANGE) return tmpmove[1]; else return tmpmove[2]; } } /* Part 4: Left Bottom->Top*/ if((col<3 && row>=3) ||(row==H/2 && col>=3 && col<W-3)){ if(getmap(map,row-1, col)!=-1 && !flag[map[row-1][col]]){ tmpmove[cnt].row=row-1; tmpmove[cnt].col=col; tmpmove[cnt].value=map[row-1][col]; cntflag[0]=1; cnt++; } if(getmap(map,row, col+1)!=-1 && !flag[map[row][col+1]]){ tmpmove[cnt].row=row; tmpmove[cnt].col=col+1; tmpmove[cnt].value=map[row][col+1]; cntflag[1]=1; cnt++; } if(getmap(map,row, col-1)!=-1 && !flag[map[row][col-1]]){ tmpmove[cnt].row=row; tmpmove[cnt].col=col-1; tmpmove[cnt].value=map[row][col-1]; cntflag[2]=1; cnt++; } if(cnt==0) return empty; if(cnt==1) return tmpmove[0]; if(cnt==2){ if(cntflag[0]){ if(!rand_int(RANGE)) return tmpmove[0]; else return tmpmove[1]; } else{ if(rand_int(2)) return tmpmove[0]; else return tmpmove[1]; } } if(cnt==3){ choice=rand_int(2*RANGE+1); if(!choice) return tmpmove[0]; else if(choice<=RANGE) return tmpmove[1]; else return tmpmove[2]; } } }
int main(){ //if( argc != 2) // { cout << "requires localport" << endl; return(-1); } //int localport = atoi(argv[1]); int localport = 1234; cout << "Hello!" << endl; dataholder players("playerlist.txt"); dataholder highs("highscores.txt"); string gamemap[15]; getmap(gamemap); cout << "Current map:\n"; for(int i = 0; i < 15; i++) cout << gamemap[i] << endl; cout << "\n\n"; int mainsocket = socket(AF_INET, SOCK_DGRAM, 0); if (mainsocket < 0){ perror("socket creation"); exit(1); } sockaddr_in localinfo; //localinfo.sin_len = sizeof(localinfo); localinfo.sin_family = AF_INET; localinfo.sin_addr.s_addr = htonl(INADDR_ANY); localinfo.sin_port = htons(localport); int r = bind(mainsocket, (sockaddr *) &localinfo, sizeof(localinfo)); if (r<0){ perror("bind"); exit(1); } printf("ready\n"); cout << "running on port " << localport << endl; char buffer[20000]; int tokcount; string tok1; string tok2; string tok3; string tok4; string tosend; string strbuff; int a; string findret; while(true){ tosend.clear(); tokcount = 1; //struct sockadder_in incoming_info; //unsigned int socklen = sizeof(incoming); struct sockaddr_in incoming; unsigned int socklen = sizeof(incoming); r = recvfrom( mainsocket, &buffer, sizeof(buffer)-1, 0, (sockaddr *) &incoming, &socklen); printf("from %s port %d, %d characters: %s\n", inet_ntoa(incoming.sin_addr), ntohs(incoming.sin_port), r, buffer); buffer[r] = 0; cout << "Received: " << buffer << endl; if( r<0 ){ perror("recvfrom"); exit(1); } buffer[r] = 0; strbuff = buffer; // change to loop when adding data structures a = totoken(strbuff,tok1); if (a >= 0){ tokcount++; a = totoken(strbuff,tok2); if (a >= 0){ tokcount++; a = totoken(strbuff,tok3); if( a >= 0){ tokcount++; a = totoken(strbuff,tok4); if( a >=0) tokcount++; } } } cout << "tokcount-> " << tokcount << endl; if (tokcount == 2){ // FINP, DELP, FINM, FINH, WINNER cout << "tok1: " << tok1 << " tok2: " << tok2 << endl; string comp = "WIN"; if(tok1 == "WINNER" || tok1 == "WIN" || tok1 == comp) cout << "They are the same\n"; if( tok1 == "FINP" ){ a = players.find(tok2,findret); if( a == -1) tosend = "could not find"; else{ tosend = "PLR:"; tosend += findret; } } else if(tok1 == "WINNER" || tok1 == "WIN" || tok1 == comp){ r = recvfrom( mainsocket, &buffer, sizeof(buffer)-1, 0, (sockaddr *) &incoming, &socklen); buffer[r] = 0; cout << "ABOUT TO ADD: " << buffer << " TO HIGHSCORES\n"; highs.highadd(buffer); cout << "Added " << buffer << " to highscores\n"; tosend = "good"; } else if (tok1 == "DELP"){ a = players.find(tok2,findret); if( a == -1) tosend = "ERR:invalid gamertag"; else{ players.take(a-1,a+3); tosend = "good\n";} } else if (tok1 == "FINM"){ tosend = "good"; } else if( tok1 == "FINH"){ a = highs.find(tok2,findret); if( a == -1) tosend = "cound not find"; else{ tosend = "SCO:"; tosend += findret; } } else { tosend = "ERR: invalid first token"; } } else if (tokcount == 3){// ADDM cout << "tok1: " << tok1 << " tok2: " << tok2 << " tok3: " << tok3<< endl; if( tok1 == "ADDM"){ tosend = "good"; } else tosend = "ERR: invalid first token"; } else if (tokcount == 4){// ADDH, ADDP, U cout << "tok1: " << tok1 << " tok2: " << tok2 << " tok3: " << tok3 << " tok4: " << tok4 << endl; if(tok1 == "U"){ players.find(tok2, findret); tosend += tok2; tosend += ':'; tosend += tok3; tosend += ':'; tosend += tok4; cout << "Comparing tosend and findret\n"; cout << "Tosend: " << tosend << " findret: " << findret << endl; if(tosend == findret) tosend = '1'; else tosend = '0'; } else if (tok1 == "ADDP"){ a = players.find(tok3,findret); if(a == -1){ players.add(4,tok2,tok3,tok4); tosend = "good"; } else tosend = "ERR:gamertag taken"; } else if( tok1 == "ADDH"){ tosend = "good"; } } else { if (tok1 == "off") tosend = "off"; else if (tok1 == "save"){ players.writeout(); //finish later highs.writeout(); } else if (tok1 == "MAP"){ for( int i = 0; i < 15; i++){ tosend += gamemap[i]; tosend += '!'; } } else tosend = "ERR: invalid message structure";} cout << "---REPONSE---\n" << tosend << endl << endl; r = sendto(mainsocket, tosend.c_str(), strlen(tosend.c_str()), 0, (sockaddr *) &incoming, sizeof(incoming)); if (r<0){ perror("sendto"); exit(1); } if( tok1 == "off" ) break; } return 0; }
void *malloc(int64_t size) { void *retval = NULL; if(size <= 64) { if(pool_64b_firstfree == NULL) { if(addmap(phymem_get_page(), POOL_64B + pool_64b_top , MM_READWRITE) == 0) { return NULL; } for(int i = (4096/64)-1; i > 0; i--) { *(uint64_t*)(POOL_64B + pool_64b_top + (i * 64)) = pool_64b_firstfree; pool_64b_firstfree = POOL_64B + pool_64b_top + (i * 64); } retval = (void*)(POOL_64B + pool_64b_top); pool_64b_top += 4096; } else { retval = (void*)pool_64b_firstfree; // The first free should now be the next element in the linked list pool_64b_firstfree = *(uint64_t*)pool_64b_firstfree; } } else if(size <= 512) { if(pool_512b_firstfree == NULL) { if(addmap(phymem_get_page(), POOL_512B + pool_512b_top , MM_READWRITE) == 0) { return NULL; } for(int i = (4096/512)-1; i > 0; i--) { *(uint64_t*)(POOL_512B + pool_512b_top + (i * 512)) = pool_512b_firstfree; pool_512b_firstfree = POOL_512B + pool_512b_top + (i * 512); } retval = (void*)(POOL_512B + pool_512b_top); pool_512b_top += 4096; } else { retval = (void*)pool_512b_firstfree; // The first free should now be the next element in the linked list pool_512b_firstfree = *(uint64_t*)pool_512b_firstfree; } } // If we're bigger than 512b well allocate a full 4k page we // don't check a linked list we just allocate the space and // adjust the top address else if(size <= 4 * 1024) { if(addmap(phymem_get_page(), POOL_4K + pool_4k_top , MM_READWRITE) == 0) { return NULL; } retval = (void*)(POOL_4K + pool_4k_top); pool_4k_top += 4 * 1024; } else if(size <= 64 * 1024) { // Map enough pages for the region for(int i = 0; i < 64/4; i++) { uint64_t pagetemp = phymem_get_page(); if(addmap(pagetemp, POOL_64K + pool_64k_top + (i * 4096), MM_READWRITE) == 0) { // If we can't map then free up the pages we succedded in mapping i--; for(;i > 0;i--) { phymem_mark_free( getmap( POOL_64K + pool_64k_top + (i * 4096) )); removemap( POOL_64K + pool_64k_top + (i * 4096) ); } // Then return a null pointer return NULL; } } retval = (void*)(POOL_64K + pool_64k_top); pool_64k_top += 64 * 1024; } else if(size <= 1 * 1024 * 1024) { // Map enough pages for the region for(int i = 0; i < (1*1024)/4; i++) { if(addmap(phymem_get_page(), POOL_1M + pool_1M_top + (i * 4096), MM_READWRITE) == 0) { // If we can't map then free up the pages we succedded in mapping i--; for(;i > 0;i--) { phymem_mark_free( getmap( POOL_1M + pool_1M_top + (i * 4096) )); removemap( POOL_1M + pool_1M_top + (i * 4096) ); } // Then return a null pointer return NULL; } } retval = (void*)(POOL_1M + pool_1M_top); pool_1M_top += (1*1024) * 1024; } else if(size <= 16 * 1024 * 1024) { // Map enough pages for the region for(int i = 0; i < (16*1024)/4; i++) { uint64_t temppage = phymem_get_page(); //dprintf("%x\n",temppage); if(addmap(temppage, POOL_16M + pool_16M_top + (i * 4096), MM_READWRITE) == 0) { // If we can't map then free up the pages we succedded in mapping i--; for(;i > 0;i--) { phymem_mark_free( getmap( POOL_16M + pool_16M_top + (i * 4096) )); removemap( POOL_16M + pool_16M_top + (i * 4096) ); } // Then return a null pointer return NULL; } } retval = (void*)(POOL_16M + pool_16M_top); pool_16M_top += (16*1024) * 1024; } else if(size <= 64 * 1024 * 1024) { // Map enough pages for the region for(int i = 0; i < (64*1024)/4; i++) { if(addmap(phymem_get_page(), POOL_64M + pool_64M_top + (i * 4096), MM_READWRITE) == 0) { // If we can't map then free up the pages we succedded in mapping i--; for(;i > 0;i--) { phymem_mark_free( getmap( POOL_64M + pool_64M_top + (i * 4096) )); removemap( POOL_64M + pool_64M_top + (i * 4096) ); } // Then return a null pointer return NULL; } } retval = (void*)(POOL_64M + pool_64M_top); pool_64M_top += (64*1024) * 1024; } else if(size <= 128 * 1024 * 1024) { // Map enough pages for the region for(int i = 0; i < (128*1024)/4; i++) { if(addmap(phymem_get_page(), POOL_128M + pool_128M_top + (i * 4096), MM_READWRITE) == 0) { // If we can't map then free up the pages we succedded in mapping i--; for(;i > 0;i--) { phymem_mark_free( getmap( POOL_128M + pool_128M_top + (i * 4096) )); removemap( POOL_128M + pool_128M_top + (i * 4096) ); } // Then return a null pointer return NULL; } } retval = (void*)(POOL_128M + pool_128M_top); pool_128M_top += (128*1024) * 1024; } else if(size <= 512 * 1024 * 1024) { // Map enough pages for the region for(int i = 0; i < (512*1024)/4; i++) { if(addmap(phymem_get_page(), POOL_512M + pool_512M_top + (i * 4096), MM_READWRITE) == 0) { // If we can't map then free up the pages we succedded in mapping i--; for(;i > 0;i--) { phymem_mark_free( getmap( POOL_512M + pool_512M_top + (i * 4096) )); removemap( POOL_512M + pool_512M_top + (i * 4096) ); } // Then return a null pointer return NULL; } } retval = (void*)(POOL_512M + pool_512M_top); pool_512M_top += (512*1024) * 1024; } else if(size <= 1 * 1024 * 1024 * 1024) { // Map enough pages for the region for(int i = 0; i < (1024*1024)/4; i++) { if(addmap(phymem_get_page(), POOL_1G + pool_1G_top + (i * 4096), MM_READWRITE) == 0) { // If we can't map then free up the pages we succedded in mapping i--; for(;i > 0;i--) { phymem_mark_free( getmap( POOL_1G + pool_1G_top + (i * 4096) )); removemap( POOL_1G + pool_1G_top + (i * 4096) ); } // Then return a null pointer return NULL; } } retval = (void*)(POOL_1G + pool_1G_top); pool_1G_top += (1024*1024) * 1024; } return retval; }
void free(void *ptr) { if((uint64_t)ptr < POOL_64B || (uint64_t)ptr > POOL_MAX) { // Invalid pointer to free } /* 64b and 512b pools will be used a lot, * we won't try and free physical pages for these * since there will be multiple pool elements in a page ************/ else if((uint64_t)ptr < POOL_512B) // 64 byte ptr to free { // Use the freed memory to point to the next free memory *(uint64_t*)ptr = pool_64b_firstfree; pool_64b_firstfree = (uint64_t)ptr; } else if((uint64_t)ptr < POOL_4K) // 512 byte { // Use the freed memory to point to the next free memory *(uint64_t*)ptr = pool_512b_firstfree; pool_512b_firstfree = (uint64_t)ptr; } /* Below 4k we weren't page aligned, now we can free the physical pages */ else if((uint64_t)ptr < POOL_64K) // 4K { phymem_mark_free( getmap( (uint64_t)ptr ) ); removemap( (uint64_t) ptr ); } else if((uint64_t)ptr < POOL_1M) // 64k { for(int i = 0; i < (64/4); i++) { phymem_mark_free( getmap( (uint64_t)(ptr + (4096 * i) ) )); removemap( (uint64_t) ptr + (4096 * i) ); } } else if((uint64_t)ptr < POOL_16M) // 1M { for(int i = 0; i < (1024/4); i++) { phymem_mark_free( getmap( (uint64_t)(ptr + (4096 * i) ) )); removemap( (uint64_t) ptr + (4096 * i) ); } } else if((uint64_t)ptr < POOL_64M) // 16M { for(int i = 0; i < ((16*1024)/4); i++) { phymem_mark_free( getmap( (uint64_t)(ptr + (4096 * i) ) )); removemap( (uint64_t) ptr + (4096 * i) ); } } else if((uint64_t)ptr < POOL_128M) // 64M { for(int i = 0; i < ((64*1024)/4); i++) { phymem_mark_free( getmap( (uint64_t)(ptr + (4096 * i) ) )); removemap( (uint64_t) ptr + (4096 * i) ); } } else if((uint64_t)ptr < POOL_512M) // 128M { for(int i = 0; i < ((128*1024)/4); i++) { phymem_mark_free( getmap( (uint64_t)(ptr + (4096 * i) ) )); removemap( (uint64_t) ptr + (4096 * i) ); } } else if((uint64_t)ptr < POOL_1G) // 512M { for(int i = 0; i < ((512*1024)/4); i++) { phymem_mark_free( getmap( (uint64_t)(ptr + (4096 * i) ) )); removemap( (uint64_t) ptr + (4096 * i) ); } } else // 1G { for(int i = 0; i < ((1024*1024)/4); i++) { phymem_mark_free( getmap( (uint64_t)(ptr + (4096 * i) ) )); removemap( (uint64_t) ptr + (4096 * i) ); } } }