void main() { int len, pos, *del; plist pl = NULL; del = (int*)malloc(sizeof(int)); pl = init_list(); isEmpty_list(pl); insert_item(pl, 1, 1); insert_item(pl, 2, 3); insert_item(pl, 3, 5); insert_item(pl, 4, 7); insert_item(pl, 5, 9); insert_item(pl, 6, 11); display(pl); len = len_list(pl); printf("link list len: %d\n", len); pos = locate_item(pl, 7); printf("num 7 pos: %d\n", pos); delete_item(pl, 3, del); printf("delete pos 3 num: %d\n", *del); display(pl); printf("link list traverse...\n"); pl = traverse_list(pl); display(pl); destroy_list(pl); getch(); }
/** * Open the player shop. */ void shop_open() { _shop_struct *shop_item_tmp; char buf[HUGE_BUF]; /* Need at least one item */ if (shop_gui->shop_items_count < 1) { draw_info("You need to have at least one item to sell in order to open a shop.", COLOR_RED); return; } strcpy(buf, "shop open"); /* Loop through the shop items */ for (shop_item_tmp = shop_gui->shop_items; shop_item_tmp; shop_item_tmp = shop_item_tmp->next) { char tmp_buf[MAX_BUF]; item *shop_item = locate_item(shop_item_tmp->tag); /* If the item could not be found, something is wrong */ if (!shop_item) { draw_info("Unable to locate shop item from tag.", COLOR_RED); clear_shop(0); return; } /* Some checking */ if (shop_item_tmp->price < 1 || shop_item_tmp->price > MAX_PRICE_VALUE) { draw_info_format(COLOR_RED, "The item %s doesn't have a valid price.", shop_item->s_name); return; } else if (shop_item_tmp->nrof < 1 || shop_item_tmp->nrof > MAX_PRICE_VALUE) { draw_info_format(COLOR_RED, "The item %s doesn't have a valid nrof.", shop_item->s_name); return; } /* Store the item tag, nrof and price */ snprintf(tmp_buf, sizeof(tmp_buf), "|%d:%d:%d", shop_item_tmp->tag, shop_item_tmp->nrof, shop_item_tmp->price); /* Append it to the socket string */ strncat(buf, tmp_buf, sizeof(buf) - strlen(buf) - 1); } /* Write the socket string to server */ cs_write_string(buf, strlen(buf)); /* The shop is now open */ shop_gui->shop_state = SHOP_STATE_OPEN; }
/** * Buy a shop item. */ void shop_buy_item() { char buf[MAX_BUF]; item *shop_item = locate_item(shop_gui->selected_tag); _shop_struct *shop_item_tmp; /* Loop through the shop items */ for (shop_item_tmp = shop_gui->shop_items; shop_item_tmp; shop_item_tmp = shop_item_tmp->next) { /* If the tag matches */ if (shop_item_tmp->tag == shop_item->tag) { /* If the nrof is the same, we will want to remove the item from the list */ if (shop_item_tmp->nrof == shop_item->nrof) { shop_remove_item(shop_item->tag); } break; } } /* Sanity check */ if (!shop_item_tmp) { return; } /* If the nrof is not the same, decrease the shop inventory item's nrof */ if (shop_item_tmp->nrof != shop_item->nrof) { shop_item->nrof -= shop_item_tmp->nrof; } /* Store the information in a buffer */ snprintf(buf, sizeof(buf), "shop buy %s %d %d", shop_gui->shop_owner, shop_item->tag, shop_item_tmp->nrof); /* Send the buffer */ cs_write_string(buf, strlen(buf)); }
/** * Put an item to the shop GUI. Called when dropping a dragged item. * @param x X position of the item * @param y Y position of the item * @return 1 if the XY positions were within the shop GUI, 0 otherwise */ int shop_put_item(int x, int y) { /* If we are in the shop GUI */ if (x > cur_widget[SHOP_ID].x1 && x < cur_widget[SHOP_ID].x1 + Bitmaps[BITMAP_SHOP]->bitmap->w && y > cur_widget[SHOP_ID].y1 && y < cur_widget[SHOP_ID].y1 + Bitmaps[BITMAP_SHOP]->bitmap->h) { _shop_struct *shop_item_tmp; item *item_object; int i; /* If we're not opening or the item limit was reached, return */ if (shop_gui->shop_state != SHOP_STATE_NONE || shop_gui->shop_items_count >= SHOP_MAX_ITEMS) { draggingInvItem(DRAG_NONE); itemExamined = 0; return 1; } /* Loop through shop items */ for (shop_item_tmp = shop_gui->shop_items; shop_item_tmp; shop_item_tmp = shop_item_tmp->next) { /* If the item is already in the GUI, return */ if (shop_item_tmp->tag == cpl.win_inv_tag) { draggingInvItem(DRAG_NONE); itemExamined = 0; return 1; } } /* Locate the item */ item_object = locate_item(cpl.win_inv_tag); /* Sanity check, shouldn't happen */ if (!item_object) { draggingInvItem(DRAG_NONE); itemExamined = 0; return 1; } else if (item_object->itype == TYPE_MONEY) { draw_info_format(COLOR_WHITE, "The %s is not allowed to be sold in a player shop.", item_object->s_name); draggingInvItem(DRAG_NONE); itemExamined = 0; return 1; } else if (item_object->locked) { draw_info_format(COLOR_WHITE, "You must unlock the %s before attempting to sell it.", item_object->s_name); draggingInvItem(DRAG_NONE); itemExamined = 0; return 1; } /* Don't allow things from the quickslots. */ for (i = 0; i < MAX_QUICK_SLOTS * MAX_QUICKSLOT_GROUPS; i++) { if (quick_slots[i].tag == cpl.win_inv_tag) { draw_info_format(COLOR_WHITE, "You must first remove %s from your quickslots.", item_object->s_name); draggingInvItem(DRAG_NONE); itemExamined = 0; return 1; } } /* Allocate a new item */ shop_item_tmp = (_shop_struct *) malloc(sizeof(_shop_struct)); /* Initialize values for the object */ shop_item_tmp->nrof = item_object->nrof; shop_item_tmp->price = 0; shop_item_tmp->price_buf[0] = '\0'; shop_item_tmp->tag = cpl.win_inv_tag; shop_item_tmp->next = NULL; /* One more shop item */ shop_gui->shop_items_count++; /* Set the selected tag to the object's tag */ shop_gui->selected_tag = shop_item_tmp->tag; /* Easier if there are no shop items yet */ if (!shop_gui->shop_items) { shop_gui->shop_items = shop_item_tmp; } /* Otherwise we want to append the item to the end of the list */ else { _shop_struct *shop_item_next = shop_gui->shop_items; int i; /* Loop until we find the end of the list */ for (i = 1; i < shop_gui->shop_items_count - 1 && shop_item_next; i++, shop_item_next = shop_item_next->next) { } /* Append it to the end of the list */ shop_item_next->next = shop_item_tmp; } draggingInvItem(DRAG_NONE); itemExamined = 0; shop_gui->current_cursor_pos = 0; return 1; } return 0; }
/** * Show the shop GUI widget. * @param x X position of the widget * @param y Y position of the widget */ void widget_show_shop(int x, int y) { /* Show the main shop bitmap */ sprite_blt(Bitmaps[BITMAP_SHOP], x, y, NULL, NULL); /* Add a close button at top right */ shop_add_close_button(x + Bitmaps[BITMAP_SHOP]->bitmap->w - 10, y + 2); /* Sanity check, because the close button can close the GUI */ if (!shop_gui) { return; } /* Show a title for the shop widget */ if (shop_gui->shop_state == SHOP_STATE_BUYING) { char buf[MAX_BUF]; snprintf(buf, sizeof(buf), "Shop: %s", shop_gui->shop_owner); StringBlt(ScreenSurface, &SystemFont, buf, x + 4, y + 1, COLOR_BLACK, NULL, NULL); StringBlt(ScreenSurface, &SystemFont, buf, x + 5, y + 2, COLOR_WHITE, NULL, NULL); } else { StringBlt(ScreenSurface, &SystemFont, "Shop", x + 4, y + 1, COLOR_BLACK, NULL, NULL); StringBlt(ScreenSurface, &SystemFont, "Shop", x + 5, y + 2, COLOR_WHITE, NULL, NULL); } /* Determine the right kind of buttons and inputs to show */ switch (shop_gui->shop_state) { /* Show "Open" button and Price/Number inputs */ case SHOP_STATE_NONE: if (shop_gui->selected_tag) { shop_add_button(x + 4, y + Bitmaps[BITMAP_SHOP]->bitmap->h - Bitmaps[BITMAP_DIALOG_BUTTON_UP]->bitmap->h - 3, "Remove"); } shop_add_button(x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_DIALOG_BUTTON_UP]->bitmap->w - 4, y + Bitmaps[BITMAP_SHOP]->bitmap->h - Bitmaps[BITMAP_DIALOG_BUTTON_UP]->bitmap->h - 3, "Open"); if (!shop_gui) { return; } StringBlt(ScreenSurface, &SystemFont, "Price:", x + 5, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 56, COLOR_BLACK, NULL, NULL); if (shop_gui->input_type == SHOP_INPUT_TYPE_PRICE) { StringBlt(ScreenSurface, &SystemFont, "Price:", x + 6, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 55, COLOR_HGOLD, NULL, NULL); } else { StringBlt(ScreenSurface, &SystemFont, "Price:", x + 6, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 55, COLOR_WHITE, NULL, NULL); } sprite_blt(Bitmaps[BITMAP_SHOP_INPUT], x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 4, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 57, NULL, NULL); StringBlt(ScreenSurface, &SystemFont, "Number:", x + 5, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 39, COLOR_BLACK, NULL, NULL); if (shop_gui->input_type == SHOP_INPUT_TYPE_NROF) { StringBlt(ScreenSurface, &SystemFont, "Number:", x + 6, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 38, COLOR_HGOLD, NULL, NULL); } else { StringBlt(ScreenSurface, &SystemFont, "Number:", x + 6, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 38, COLOR_WHITE, NULL, NULL); } sprite_blt(Bitmaps[BITMAP_SHOP_INPUT], x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 4, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 40, NULL, NULL); break; /* Shop "Buy" and "Examine" buttons and Number input */ case SHOP_STATE_BUYING: if (shop_gui->selected_tag) { shop_add_button(x + 4, y + Bitmaps[BITMAP_SHOP]->bitmap->h - Bitmaps[BITMAP_DIALOG_BUTTON_UP]->bitmap->h - 3, "Buy"); shop_add_button(x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_DIALOG_BUTTON_UP]->bitmap->w - 4, y + Bitmaps[BITMAP_SHOP]->bitmap->h - Bitmaps[BITMAP_DIALOG_BUTTON_UP]->bitmap->h - 3, "Examine"); } StringBlt(ScreenSurface, &SystemFont, "Number:", x + 5, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 56, COLOR_BLACK, NULL, NULL); if (shop_gui->input_type == SHOP_INPUT_TYPE_NROF) { StringBlt(ScreenSurface, &SystemFont, "Number:", x + 6, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 55, COLOR_HGOLD, NULL, NULL); } else { StringBlt(ScreenSurface, &SystemFont, "Number:", x + 6, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 55, COLOR_WHITE, NULL, NULL); } sprite_blt(Bitmaps[BITMAP_SHOP_INPUT], x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 4, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 57, NULL, NULL); break; } /* If there are items to show */ if (shop_gui->shop_items) { _shop_struct *shop_item_tmp; int item_x = 36, item_y = 18, i = 1, found_selected = 0; int mx, my, mb; char buf[MAX_BUF]; mb = SDL_GetMouseState(&mx, &my) & SDL_BUTTON(SDL_BUTTON_LEFT); /* Loop through the items */ for (shop_item_tmp = shop_gui->shop_items; shop_item_tmp; shop_item_tmp = shop_item_tmp->next) { item *shop_item; /* Item is gone */ if (shop_item_tmp->tag == -1) { continue; } shop_item = locate_item(shop_item_tmp->tag); if (!shop_item) { continue; } /* If we clicked on the item, set the selected tag accordingly */ if (mx > x + item_x && mx < x + item_x + 32 && my > y + item_y && my < y + item_y + 32 && mb && mb_clicked) { shop_gui->selected_tag = shop_item_tmp->tag; switch (shop_gui->shop_state) { case SHOP_STATE_NONE: shop_gui->current_cursor_pos = (int) strlen(shop_item_tmp->price_buf); break; case SHOP_STATE_BUYING: snprintf(buf, sizeof(buf), "%d", shop_item_tmp->nrof); shop_gui->current_cursor_pos = (int) strlen(buf); break; } } /* If this is the selected item, show a marker */ if (shop_item_tmp->tag == shop_gui->selected_tag) { found_selected = 1; sprite_blt(Bitmaps[BITMAP_INVSLOT], x + item_x, y + item_y, NULL, NULL); switch (shop_gui->shop_state) { /* Show "Open" button and Price/Number inputs */ case SHOP_STATE_NONE: snprintf(buf, sizeof(buf), "%s", shop_item_tmp->price_buf); strcpy(buf, shop_show_input(buf, &SystemFont, Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 5, shop_gui->input_type == SHOP_INPUT_TYPE_PRICE ? 1 : 0)); StringBlt(ScreenSurface, &SystemFont, buf, x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 55, COLOR_BLACK, NULL, NULL); StringBlt(ScreenSurface, &SystemFont, buf, x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 1, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 56, COLOR_WHITE, NULL, NULL); snprintf(buf, sizeof(buf), "%d", shop_item_tmp->nrof); strcpy(buf, shop_show_input(buf, &SystemFont, Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 5, shop_gui->input_type == SHOP_INPUT_TYPE_NROF ? 1 : 0)); StringBlt(ScreenSurface, &SystemFont, buf, x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 38, COLOR_BLACK, NULL, NULL); StringBlt(ScreenSurface, &SystemFont, buf, x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 1, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 39, COLOR_WHITE, NULL, NULL); break; case SHOP_STATE_BUYING: snprintf(buf, sizeof(buf), "%d", shop_item_tmp->nrof); strcpy(buf, shop_show_input(buf, &SystemFont, Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 5, shop_gui->input_type == SHOP_INPUT_TYPE_NROF ? 1 : 0)); StringBlt(ScreenSurface, &SystemFont, buf, x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 55, COLOR_BLACK, NULL, NULL); StringBlt(ScreenSurface, &SystemFont, buf, x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w - 1, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 56, COLOR_WHITE, NULL, NULL); break; } if (shop_gui->shop_state != SHOP_STATE_OPEN) { snprintf(buf, sizeof(buf), "Price:%s", shop_int2price(shop_item_tmp->price)); StringBlt(ScreenSurface, &SystemFont, buf, x + Bitmaps[BITMAP_SHOP]->bitmap->w - Bitmaps[BITMAP_SHOP_INPUT]->bitmap->w, y + Bitmaps[BITMAP_SHOP]->bitmap->h - 75, COLOR_WHITE, NULL, NULL); } } blt_inv_item(shop_item, x + item_x, y + item_y, shop_gui->shop_state == SHOP_STATE_NONE ? shop_item_tmp->nrof : shop_item->nrof); /* Adjust the item X position */ item_x += 32; /* If we hit fourth item, adjust the positions */ if (i == 4) { item_x = 36; item_y += 32; i = 0; } i++; } if (shop_gui->selected_tag && !found_selected) { shop_gui->selected_tag = 0; } } }