Пример #1
0
void g1_trike_class::think()
{


	if (!alive())
	{
		unoccupy_location();
		request_remove();
		return;
	}

	if (attack_target.valid())
	{
		request_think();

		dest_x = attack_target->x;
		dest_y = attack_target->y;
		stagger=0;

		path_cos = dest_x - x; //cheat and calculate path from current position
		path_sin = dest_y - y;
		path_tan_phi = attack_target->h - h;
		stagger = 0;

		path_pos = 0;

		path_len = (float)sqrt(path_cos*path_cos + path_sin*path_sin);
		i4_float dist_w = 1.0f/path_len;
		path_cos *= dist_w;
		path_sin *= dist_w;
		path_tan_phi *= dist_w;

		i4_float dist=0, dtheta=0, dx=0, dy=0,dz=0;
		speed*=1.3f;
		suggest_move(dist, dtheta, dx, dy, 0);
		g1_object_class * blocking=0;
		check_move(dx,dy,dz,blocking);
		move(dx,dy,dz);

		const i4_float KILL_RADIUS=0.5;
		//The trike is a self-destructing attacker
		//if we've run into the guy
		if (x > dest_x-KILL_RADIUS  &&  y > dest_y-KILL_RADIUS  &&
			x < dest_x+KILL_RADIUS  &&  y < dest_y+KILL_RADIUS)
		{
			int damage=g1_trike_def.get_damage_map()->get_damage_for(attack_target.get()->id);

			//hurt the dude
			attack_target->damage(this,damage,i4_3d_vector(dx,dy,0));

			//throw some gibs
			g1_shrapnel_class * shrapnel = NULL;
			shrapnel = (g1_shrapnel_class *)g1_create_object(shrapnel_type);
			if (shrapnel)
			{
				i4_float rx,ry,rh;
				rx = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2f;
				ry = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2f;
				rh = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2f;
				shrapnel->setup(x+rx,y+ry,h + rh,6,i4_T);
			}
			//unoccupy_location();
			//request_remove();
			//health=0;//be shure that we aren't still alive for the next tick
			this->damage(this,health,i4_3d_vector(-dx,-dy,0));
			return;
		}
		return;
	}
	else if (next_path.valid())
	{
		//Need a new target because our's just blew away without our help...
		if (speed>(defaults->speed+defaults->accel))
		{
			speed-=defaults->accel;
		}                           //slow down again.
		g1_path_object_class * next=(g1_path_object_class *)next_path.get();
		if (dest_x!=next->x || dest_y != next->y)
		{
			return_to_path(); //Maybe called a bit too often, but shoudn't hurt.
		}
	}
	g1_map_piece_class::think();
}
Пример #2
0
/*
 * This handles the game for each client.
 */
void *game_runner(void *client_data_pointer)
{
    /* Get client and details */
    client_nfo client = *(client_nfo*)client_data_pointer;
    int socket_id = client.socket_id;

    int status = 1, client_status = 1, move = 0, ai_move = 0, read_size;
    uint32_t move_nbo, ai_move_nbo;
    c4_t board;

    /* To ensure memory is freed at the end */
    pthread_detach(pthread_self());
    
    /* Set-up the game */
    srand(time(NULL));
    init_empty(board, NO_PRINT);

    /* log that client is connected */
    append_log(STATUS_CONNECTED, 0, &client, STATUS_CONNECTED, 0);

    /* Receive move from client */
    while((read_size = recv(socket_id, &move_nbo, sizeof(uint32_t), 0)) > 0)
    {
        if(status < 0) break;
        
        /* Get move from (client move in network byte order)*/
        move = ntohl(move_nbo);

        /* Make client move */
        status = human_play(board, move, NO_PRINT);
        client_status = status; /* needed for log */

        /* Make AI move if game is not over */
        if(status > 0)
        {
            ai_move = suggest_move(board, RED);
            status = ai_play(board, ai_move, NO_PRINT);
        }
        /* log the moves */
        append_log(status, ai_move, &client, client_status, move);

        /* If game is over, the old ai_move will be sent
           to client again but game will be terminated just after the
           client move is made in client program, so the ai move
           won't be made
        */

        /* Send the AI move to client */
        ai_move_nbo = htonl(ai_move);
        if(send(socket_id, &ai_move_nbo, sizeof(uint32_t), 0) < 0)
        {
            perror("send error");
            break;
        }
    }

    if(read_size == 0) {
        /* Client has been disconnected from server */
        if (status > 0) status = STATUS_ABNORMAL;
    }
    else if(read_size == -1)
    { 
        perror("recv error");
        if (status > 0) status = STATUS_ABNORMAL;
    }
    /* log any abnormal end of game */
    if(status == STATUS_ABNORMAL)
        append_log(STATUS_ABNORMAL, 0, &client, STATUS_ABNORMAL, 0);
    
    free(client_data_pointer);
    return 0;
}
void *thread_game(void *threadInfo)
{   
    struct readThreadParams *thisTheadInfo = (struct readThreadParams *)threadInfo;
    FILE *fptr = thisTheadInfo->filep;
    int sock = thisTheadInfo->sock;
    int status,n;
    //connect 4 variables
    c4_t board;
    int clientMove, serverMove;
    //uint16 used so all systems can play!
    uint16_t clientMoveNewtorked, serverMoveNetworked;



    printLog(fptr,thisTheadInfo,"client connected" ,-1,CLIENT);
 
    //connect 4 actions here
    srand(RSEED);
    //create a new board
    init_empty(board);
    /* main loop does two moves each iteration, one from the human
     * playing, and then one from the computer program (or game server)
     */
    while (1) {

        //first we want the clients move
        if( (n = recv(sock, &clientMove, sizeof(uint16_t), 0)) != sizeof(uint16_t) )
        {

            printf("socket closed\n");
            status = STATUS_ABNORMAL;
            printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT);
            break;
        }
        clientMoveNewtorked = htons(clientMove);
        //do move
        if (do_move(board, (int)clientMoveNewtorked, YELLOW)!=1) {
            printf("Panic\n");
            status = STATUS_ABNORMAL;
            printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT);
            break;
        } 
        //if move possible we write to log
        printLog(fptr, thisTheadInfo, "client's move", (int)clientMoveNewtorked,CLIENT);

        // print_config(board);
        if (winner_found(board) == YELLOW) {
            /* rats, the person beat us! */
            printf("Petty human wins\n");
            status = STATUS_USER_WON;
            printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT);
            break;
        }

        /* was that the last possible move? */
         if (!move_possible(board)) {
             /* yes, looks like it was */
             printf("An honourable draw\n");
             status = STATUS_DRAW;
             printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT);
             break;
         }
         /* otherwise, look for a move from the computer */
         serverMove = suggest_move(board, RED);

         serverMoveNetworked = htons(serverMove);
         /* then play the move */
         // printf(" I play in column %d\n", serverMove);
         if(send(sock, &serverMoveNetworked,sizeof(uint16_t),0) != sizeof(uint16_t))
         {
            printf("error\n");
            status = STATUS_ABNORMAL;
            printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT);
            break;
         }
         if (do_move(board, serverMove, RED)!=1) {
             printf("Panic\n");
             status = STATUS_ABNORMAL;
             printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT);
             break;
         }
         
         printLog(fptr, thisTheadInfo, "server's move", serverMove,SERVER);

         // print_config(board);
         /* and did we win??? */
         if (winner_found(board) == RED) {
             /* yes!!! */
             printf("Server wins again!\n");
             status = STATUS_AI_WON;
             printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT);
             break;
             
         }
         /* otherwise, the game goes on */
        


    }
     

    return NULL;
}