Example #1
0
void update_hp(int hp)
{
    if(hp > hp_current) {
        printf_message(COLOR_HP_GOOD, "You gain %d hp.", hp - hp_current);
    } else if(hp < hp_current) {
        printf_message(COLOR_HP_CRIT, "You lose %d hp.", hp_current - hp, hp_current, hp);
    }
    hp_current = hp;
}
Example #2
0
void play_press()
{
   struct disc_status status;
   
   if(!disc_present)
     return;
   
   cd_poll(cd_fd, &status);
   
   switch(status.status_mode) {
    case CDAUDIO_PLAYING:
      print_message("Paused");
      break;
    case CDAUDIO_PAUSED:
      print_message("Resuming");
      break;
    default:
      if(valid_cddb)
	printf_message("Playing track %d: %s", play_track, data.data_track[play_track - 1].track_name);
      else
	printf_message("Playing track %d", play_track);
   }
}
Example #3
0
void adv_release()
{
   if(!disc_present)
     return;

   if(disc_playing) {
      if(play_track + 1 > disc.disc_total_tracks) {
	 print_message("Stopped");
         cd_stop(cd_fd);
      } else {
	 if(valid_cddb)
	   printf_message("Playing track %d: %s", play_track + 1, data.data_track[play_track].track_name);
	 else
	   printf_message("Playing track %d", play_track + 1);
	 
         cd_play(cd_fd, play_track + 1);
      }
   } else {
      if(play_track + 1 <= disc.disc_total_tracks)
         play_track++;
   }
   
   usleep(200000);
}
Example #4
0
void prev_release()
{
   if(!disc_present)
     return;

   if(disc_playing) {
      if(play_track - 1 < disc.disc_first_track) {
	 print_message("Stopped");
         cd_stop(cd_fd);
      } else {
	 if(valid_cddb)
	   printf_message("Playing track %d: %s", play_track - 1, data.data_track[play_track - 2].track_name);
	 else
	   printf_message("Playing track %d", play_track - 1);
	 
         cd_play(cd_fd, play_track - 1);
      }
   } else {
      if(play_track - 1 >= disc.disc_first_track)
         play_track--;
   }
   
   usleep(200000);
}
Example #5
0
void player_act()
{
    switch(get_last_action()) {
        case ACTION_TILL:
            till(x, y, current_map);
            break;
        case ACTION_PICKUP: {
            item* it = get_item(items_at(x, y, current_map), item_count_at(x, y, current_map), PURPOSE_PICKUP, true);
            if(!it)
                break;
            printf_message(COLOR_DEFAULT, "Picked up %d %s", it->count, it->name);
            callback("picked_up", it->script_state);
            take_item(x, y, it, current_map);
            add_item(it);
        } break;
        case ACTION_DROP: {
            item* it = get_item(inventory, item_count, PURPOSE_DROP, false);
            if(!it)
                break;
            if(it->can_equip && equipment[it->slot] == it) {
                equipment[it->slot] = 0;
                printf_message(COLOR_DEFAULT, "You unequip the %s, and drop it on the ground.", it->name);
                callback("removed", it->script_state);
            }
            item* clone = clone_item(it);
            callback("dropped", clone->script_state);
            place_item(x, y, clone, current_map);
            remove_item(it, -1);
        } break;
        case ACTION_APPLY: {
            item* it = get_item(inventory, item_count, PURPOSE_APPLY, false);
            if(!it)
                break;
            callback("apply", it->script_state);
            remove_item(it, 1);
        } break;
        case ACTION_EQUIP: {
            item* it = get_item(inventory, item_count, PURPOSE_EQUIP, false);
            if(!it || it->slot == SLOT_INVALID)
                break;
            callback("equip", it->script_state);
            printf_message(COLOR_DEFAULT, "You equip the %s.", it->name);
            equipment[it->slot] = it;
        break; }
        case ACTION_REMOVE: {
            item* it = get_item(equipment, 3, PURPOSE_REMOVE, false);
            if(!it)
                break;
            callback("remove", it->script_state);
            equipment[it->slot] = 0;
            printf_message(COLOR_DEFAULT, "You unequip the %s.", it->name);
        break; }
        case ACTION_PLANT: {
            if(!can_plant(x, y, current_map, true))
                break;
            item* it = get_item(inventory, item_count, PURPOSE_PLANT, false);
            if(!it)
                break;
            if(spawn_plant(x, y, it->plant_id, current_map)) {
                printf_message(COLOR_DEFAULT, "You plant the %s in the tilled soil.", it->name);
                remove_item(it, 1);
            }
        break; }
        case ACTION_HARVEST: {
            add_item(harvest_plant(x, y, current_map));
        break; }
        case ACTION_HELP:
            show_controls();
            break;
        case ACTION_INVENTORY:
            get_item(inventory, item_count, PURPOSE_NONE, false);
            break;
        case ACTION_WATER:
            water_tile(x, y, current_map);
            break;
        case ACTION_EXAMINE: {
            int mx, my;
            get_last_mouse_position(&mx, &my);
            int xdiff = mx - pres_x;
            int ydiff = my - pres_y;
            if(mx < 1 || my < 1 || mx > 78 || my > 78)
                break;
            examine(x + xdiff, y +ydiff, current_map);
        } break;
    }
    if(ep_current <= 0)
        add_message(COLOR_EP_CRIT, "Out of energy, you fall to the ground.");
}
Example #6
0
void player_move(int direction)
{
    int dx = 0;
    int dy = 0;
    switch(direction) {
        case DIRECTION_NORTH:
            dy = -1;
            break;
        case DIRECTION_SOUTH:
            dy = 1;
            break;
        case DIRECTION_EAST:
            dx = 1;
            break;
        case DIRECTION_WEST:
            dx = -1;
            break;
        case DIRECTION_NORTHEAST:
            dx = 1;
            dy = -1;
            break;
        case DIRECTION_NORTHWEST:
            dx = -1;
            dy = -1;
            break;
        case DIRECTION_SOUTHEAST:
            dx = 1;
            dy = 1;
            break;
        case DIRECTION_SOUTHWEST:
            dx = -1;
            dy = 1;
            break;
        case DIRECTION_UP:
            if(z > 0 && is_up_stairs(x, y, current_map))
                z--;
            return;
        case DIRECTION_DOWN:
            if(z < LEVEL_COUNT - 1 && is_down_stairs(x, y, current_map))
                z++;
            return;
    }
    int res = can_move(x + dx, y + dy, current_map);
    if(res == 1) {
        x += dx;
        y += dy;
        int cost = 1 + get_cost(x, y, current_map);
        if(dx != 0 && dy != 0)
            cost *= 1.5;
        ep_current -= cost;
        if(ep_current <= 0)
            add_message(COLOR_EP_CRIT, "Out of energy, you fall to the ground.");
        else
            describe_ground(x, y, current_map);
    } else if(res == 2) {
        actor* act = get_actor_at(x + dx, y + dy, current_map);
        int astr = str;
        for(int i = 0; i < SLOT_COUNT; ++i)
            if(equipment[i])
                astr += equipment[i]->str;
        int dmg = damage_actor(act, astr);
        char* damage_text = 0;
        if(dmg > 0) {
            int len = snprintf(0, 0, "You hit the %s for %d damage", act->name, dmg);
            damage_text = calloc(len + 1, sizeof(char));
            snprintf(damage_text, len + 1, "You hit the %s for %d damage", act->name, dmg);
        } else {
            int len = snprintf(0, 0, "You miss the %s", act->name);
            damage_text = calloc(len + 1, sizeof(char));
            snprintf(damage_text, len + 1, "You miss the %s", act->name);
        }
        if(act->hp <= 0) {
            printf_message(COLOR_DEFAULT, "You kill the %s!", act->name);
            add_xp(act->xp);
        } else
            add_message(COLOR_DEFAULT, damage_text);
        free(damage_text);
    }
}
Example #7
0
int main()
{
    int listenfd = INVALID_SOCKET;

    if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        return -1;
    }

    struct sockaddr_in sa;
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(8998);
    sa.sin_addr.s_addr = INADDR_ANY;
    //if(inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr.s_addr) != 1) {
    //    fprintf(stderr, "inet_pton maybe failed.\n");
    //    return -2;
    //}

    if(make_socket_reusable(listenfd) == -1)
    {
        printf("[%8d(sfd)] Make socket reusable failed\n", listenfd);
    }
    if(make_socket_non_blocking(listenfd) == -1)
    {
        printf("Make socket nonblock failed\n");
        return -1;
    }
    if(bind(listenfd, (struct sockaddr*)&sa, sizeof(struct sockaddr_in)) == -1)
    {
        perror("bind");
        return -3;
    }

    if(listen(listenfd, SOMAXCONN) == -1)
    {
        perror("listen ");
        return -4;
    }
    printf_address(listenfd, (struct sockaddr*)&sa, sizeof(sa), "Listen on");

    /* use epoll LT */
    int epfd = epoll_create1(0);
    if(epfd == -1) {
        perror("epoll_create1");
        return -5; 
    }

    struct epoll_event ev;
    ev.events = EPOLLIN;
    ev.data.fd = listenfd;

    if(-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev)) {
        perror("epoll_ctl");
        return -1;
    }

    while(1) {
        struct epoll_event events[MAXEVENTS] ;
        int n = epoll_wait(epfd, events, MAXEVENTS, -1);
        for(int i = 0; i < n; i++) {
            uint32_t event = events[i].events;

            if(event & (EPOLLERR | EPOLLHUP)) {
                printf("EPOLLERR | EPOLLHUP\n");

                int fd = events[i].data.fd;
                close(fd);
                conns.erase(fd);

            } else if(event & EPOLLIN) {
                printf("EPOLLIN\n");
                if(events[i].data.fd == listenfd) {
                    //printf("listenfd\n");
                    /* listenfd*/
                    struct sockaddr_in sa_client;
                    socklen_t sa_client_len = sizeof(sa_client);
                    int connfd = accept(listenfd, (struct sockaddr*)&sa_client, &sa_client_len);
                    //if(connfd == -1) {
                    //    perror("accept ");
                    //    continue;
                    //}

                    if(make_socket_non_blocking(connfd) == -1) {
                        printf("Make socket nonblock failed\n");
                        close(connfd);
                        continue;
                    }
                    printf_address(connfd, (struct sockaddr*)&sa_client, sa_client_len, "Accept");

                    /* Register EPOLLIN on connfd */
                    struct epoll_event ev;
                    ev.events = EPOLLIN;
                    ev.data.fd= connfd;
                    if(-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &ev)) {
                        perror("epoll_ctl");
                        close(connfd);
                    }

                } else {

                    //printf("connfd\n");
                    /* connfd */
                    int connfd = events[i].data.fd;
                    Connection& conn = conns[connfd]; /* the [] of map will do insert when not exist */
                    
                    int n = recv(connfd, conn.recv_buff + conn.recv_len, sizeof(conn.recv_buff) - conn.recv_len, 0);
                    if(n == -1) {
                        if(errno == EAGAIN || errno == EWOULDBLOCK) {
                            continue;
                        }else if(errno == EINTR) {
                            n = 0;
                            continue;
                        } else {
                            perror("recv");
                            printf("[%8d]Error, Close the connection.\n", connfd);
                            close(connfd);
                            conns.erase(connfd);
                            continue;
                        }
                    } else if(n == 0) { /* client close the connection */
                        printf("[%8d]Client close the connection.\n", connfd);
                        close(connfd);
                        conns.erase(connfd); /* remove the buffer */
                        continue;
                    }
                    printf_message(connfd, conn.recv_buff + conn.recv_len, n);
                    conn.recv_len += n;


                    /* send the data back to client */
                    /* Register EPOLLOUT */
                    struct epoll_event ev;
                    ev.events = EPOLLIN | EPOLLOUT;
                    ev.data.fd= connfd;
                    if(-1 == epoll_ctl(epfd, EPOLL_CTL_MOD, connfd, &ev)) {
                        perror("epoll_ctl");
                        close(connfd);
                        conns.erase(connfd);
                    }
                }
            } else if(event & EPOLLOUT) {
               printf("EPOLLOUT\n");

                int connfd = events[i].data.fd;
                Connection& conn = conns[connfd]; /* the [] of map will do insert when not exist */
                int left = conn.recv_len - conn.send_len;
                int error_occur = 0;
                while(left > 0) {
                    int n = send(connfd, conn.recv_buff + conn.send_len, left, 0);
                    if(n == -1) {
                        if(errno == EAGAIN || errno == EWOULDBLOCK) {
                            break;
                        } else if(errno == EINTR) {
                            continue;
                        } else {
                            perror("send");
                            error_occur = 1;
                            break;
                        }
                    }
                    left -= n;
                    conn.send_len += n;
                }
                
                /* send finished or error occurs, reset the client_data or free the client_data and  close the connfd */
                if(left == 0 || error_occur ) {
                    if(left == 0) {
                        printf("Send finised\n");
                        conn.recv_len = conn.send_len = 0;
                        /* Unregister EPOLLOUT */
                        struct epoll_event ev;
                        ev.events = EPOLLIN;
                        ev.data.fd = connfd;
                        if(-1 == epoll_ctl(epfd, EPOLL_CTL_MOD, connfd, &ev)) {
                            perror("epoll_ctl");
                            close(connfd);
                            conns.erase(connfd);
                        }
                    } else {
                        printf("Error occurs, close the connection.\n");
                        conns.erase(connfd);
                        close(connfd); /* when close there is a EPOLL_CTL_DEL automatically. */
                    }
                }

            } else {
                printf("Unknown event: 0x%X\n", event);
            }
    
        }
    }
}