/************************************************************************** Marks all techs which are requirements for goal in pplayer->research->inventions[goal].required_techs. Works recursive. **************************************************************************/ static void build_required_techs_helper(struct player *pplayer, Tech_type_id tech, Tech_type_id goal) { /* The is_tech_a_req_for_goal condition is true if the tech is * already marked */ if (!player_invention_reachable(pplayer, tech, FALSE) || player_invention_state(pplayer, tech) == TECH_KNOWN || is_tech_a_req_for_goal(pplayer, tech, goal)) { return; } /* Mark the tech as required for the goal */ BV_SET(player_research_get(pplayer)->inventions[goal].required_techs, tech); if (advance_required(tech, AR_ONE) == goal || advance_required(tech, AR_TWO) == goal) { log_fatal("tech \"%s\": requires itself", advance_name_by_player(pplayer, goal)); exit(EXIT_FAILURE); } build_required_techs_helper(pplayer, advance_required(tech, AR_ONE), goal); build_required_techs_helper(pplayer, advance_required(tech, AR_TWO), goal); }
/************************************************************************** Returns TRUE iff the given tech is ever reachable by the given player by checking tech tree limitations. If allow_prereqs is TRUE check if the player can ever reach this tech. pplayer may be NULL in which case a simplified result is returned (used by the client). **************************************************************************/ bool player_invention_reachable(const struct player *pplayer, const Tech_type_id tech, bool allow_prereqs) { Tech_type_id root; if (!valid_advance_by_number(tech)) { return FALSE; } root = advance_required(tech, AR_ROOT); if (A_NONE != root) { if (root == tech) { /* This tech requires itself; it can only be reached by special means * (init_techs, lua script, ...). * If you already know it, you can "reach" it; if not, not. (This case * is needed for descendants of this tech.) */ return TECH_KNOWN == player_invention_state(pplayer, tech); } else if (allow_prereqs) { /* Recursive check if the player can ever reach this tech (root tech * and both requirements). */ return (player_invention_reachable(pplayer, root, TRUE) && player_invention_reachable(pplayer, advance_required(tech, AR_ONE), allow_prereqs) && player_invention_reachable(pplayer, advance_required(tech, AR_TWO), allow_prereqs)); } else if (TECH_KNOWN != player_invention_state(pplayer, root) || !player_invention_reachable(pplayer, advance_required(tech, AR_ONE), allow_prereqs) || !player_invention_reachable(pplayer, advance_required(tech, AR_TWO), allow_prereqs)) { /* This tech requires knowledge of another tech (root tech or recursive * a root tech of a requirement) before being available. Prevents * sharing of untransferable techs. */ return FALSE; } } return TRUE; }
/**************************************************************************** Give technologies to players with EFT_TECH_PARASITE (traditionally from the Great Library). ****************************************************************************/ void do_tech_parasite_effect(struct player *pplayer) { int mod; struct effect_list *plist = effect_list_new(); /* Note that two EFT_TECH_PARASITE effects will combine into a single, * much worse effect. */ if ((mod = get_player_bonus_effects(plist, pplayer, EFT_TECH_PARASITE)) > 0) { char buf[512]; buf[0] = '\0'; effect_list_iterate(plist, peffect) { if (buf[0] != '\0') { sz_strlcat(buf, ", "); } get_effect_req_text(peffect, buf, sizeof(buf)); } effect_list_iterate_end; advance_index_iterate(A_FIRST, i) { if (player_invention_reachable(pplayer, i) && player_invention_state(pplayer, i) != TECH_KNOWN) { int num_players = 0; players_iterate(aplayer) { if (player_invention_state(aplayer, i) == TECH_KNOWN) { num_players++; } } players_iterate_end; if (num_players >= mod) { notify_player(pplayer, NULL, E_TECH_GAIN, FTC_SERVER_INFO, NULL, _("%s acquired from %s!"), advance_name_for_player(pplayer, i), buf); notify_embassies(pplayer, NULL, NULL, E_TECH_GAIN, FTC_SERVER_INFO, NULL, _("The %s have acquired %s from %s."), nation_plural_for_player(pplayer), advance_name_for_player(pplayer, i), buf); do_free_cost(pplayer, i); found_new_tech(pplayer, i, FALSE, TRUE); script_signal_emit("tech_researched", 3, API_TYPE_TECH_TYPE, advance_by_number(i), API_TYPE_PLAYER, pplayer, API_TYPE_STRING, "stolen"); break; } } } advance_index_iterate_end;
/************************************************************************** Mark as TECH_PREREQS_KNOWN each tech which is available, not known and which has all requirements fullfiled. If there is no such a tech mark A_FUTURE as researchable. Recalculate research->num_known_tech_with_flag Should always be called after player_invention_set() **************************************************************************/ void player_research_update(struct player *pplayer) { enum tech_flag_id flag; int researchable = 0; struct player_research *research = player_research_get(pplayer); /* This is set when the game starts, but not everybody finds out * right away. */ player_invention_set(pplayer, A_NONE, TECH_KNOWN); advance_index_iterate(A_FIRST, i) { if (!player_invention_reachable(pplayer, i, FALSE)) { player_invention_set(pplayer, i, TECH_UNKNOWN); } else { if (player_invention_state(pplayer, i) == TECH_PREREQS_KNOWN) { player_invention_set(pplayer, i, TECH_UNKNOWN); } if (player_invention_state(pplayer, i) == TECH_UNKNOWN && player_invention_state(pplayer, advance_required(i, AR_ONE)) == TECH_KNOWN && player_invention_state(pplayer, advance_required(i, AR_TWO)) == TECH_KNOWN) { player_invention_set(pplayer, i, TECH_PREREQS_KNOWN); researchable++; } } build_required_techs(pplayer, i); } advance_index_iterate_end; #ifdef DEBUG_TECH advance_index_iterate(A_FIRST, i) { char buf[advance_count() + 1]; advance_index_iterate(A_NONE, j) { if (BV_ISSET(research->inventions[i].required_techs, j)) { buf[j] = '1'; } else { buf[j] = '0'; } } advance_index_iterate_end; buf[advance_count()] = '\0'; log_debug("%s: [%3d] %-25s => %s", player_name(pplayer), i, advance_rule_name(advance_by_number(i)), tech_state_name(player_invention_state(pplayer, i))); log_debug("%s: [%3d] %s", player_name(pplayer), i, buf); } advance_index_iterate_end;
PyObject* get_techs(int level) { PyObject* list = PyList_New(0); int num, i; advance_index_iterate(A_FIRST, i) { if (player_invention_reachable(client.conn.playing, i, FALSE) && TECH_KNOWN != player_invention_state(client.conn.playing, i) && (level > (num = num_unknown_techs_for_goal(client.conn.playing, i)) /*|| player_research_get(client.conn.playing) == research->tech_goal*/)) { PyList_Append(list, Py_BuildValue("isi", i, advance_name_translation(advance_by_number(i)), num)); } } advance_index_iterate_end; return list; }
/************************************************************************** ... **************************************************************************/ SDL_Color * get_tech_color(Tech_type_id tech_id) { if (player_invention_reachable(client.conn.playing, tech_id)) { switch (player_invention_state(client.conn.playing, tech_id)) { case TECH_UNKNOWN: return get_game_colorRGB(COLOR_REQTREE_UNKNOWN); case TECH_KNOWN: return get_game_colorRGB(COLOR_REQTREE_KNOWN); case TECH_PREREQS_KNOWN: return get_game_colorRGB(COLOR_REQTREE_PREREQS_KNOWN); default: return get_game_colorRGB(COLOR_REQTREE_BACKGROUND); } } return get_game_colorRGB(COLOR_REQTREE_UNREACHABLE); }
/************************************************************************** ... **************************************************************************/ static int spy_steal_popup(struct widget *pWidget) { struct city *pVcity = pWidget->data.city; int id = MAX_ID - pWidget->ID; struct player *pVictim = NULL; struct CONTAINER *pCont; struct widget *pBuf = NULL; struct widget *pWindow; SDL_String16 *pStr; SDL_Surface *pSurf; int max_col, max_row, col, i, count = 0; SDL_Rect area; popdown_diplomat_dialog(); if(pVcity) { pVictim = city_owner(pVcity); } if (pDiplomat_Dlg || !pVictim) { return 1; } count = 0; advance_index_iterate(A_FIRST, i) { if (player_invention_reachable(client.conn.playing, i, FALSE) && TECH_KNOWN == player_invention_state(pVictim, i) && (TECH_UNKNOWN == player_invention_state(client.conn.playing, i) || TECH_PREREQS_KNOWN == player_invention_state(client.conn.playing, i))) { count++; } } advance_index_iterate_end; if(!count) { /* if there is no known tech to steal then send steal order at Spy's Discretion */ int target_id = pVcity->id; request_diplomat_action(DIPLOMAT_STEAL, id, target_id, advance_count()); return -1; } pCont = fc_calloc(1, sizeof(struct CONTAINER)); pCont->id0 = pVcity->id; pCont->id1 = id;/* spy id */ pDiplomat_Dlg = fc_calloc(1, sizeof(struct diplomat_dialog)); pDiplomat_Dlg->diplomat_id = id; pDiplomat_Dlg->diplomat_target_id = pVcity->id; pDiplomat_Dlg->pdialog = fc_calloc(1, sizeof(struct ADVANCED_DLG)); pStr = create_str16_from_char(_("Select Advance to Steal"), adj_font(12)); pStr->style |= TTF_STYLE_BOLD; pWindow = create_window_skeleton(NULL, pStr, 0); pWindow->action = spy_steal_dlg_window_callback; set_wstate(pWindow , FC_WS_NORMAL); add_to_gui_list(ID_DIPLOMAT_DLG_WINDOW, pWindow); pDiplomat_Dlg->pdialog->pEndWidgetList = pWindow; area = pWindow->area; area.w = MAX(area.w, adj_size(8)); /* ------------------ */ /* exit button */ pBuf = create_themeicon(pTheme->Small_CANCEL_Icon, pWindow->dst, WF_WIDGET_HAS_INFO_LABEL | WF_RESTORE_BACKGROUND); pBuf->info_label = create_str16_from_char(_("Close Dialog (Esc)"), adj_font(12)); area.w += pBuf->size.w + adj_size(10); pBuf->action = exit_spy_steal_dlg_callback; set_wstate(pBuf, FC_WS_NORMAL); pBuf->key = SDLK_ESCAPE; add_to_gui_list(ID_TERRAIN_ADV_DLG_EXIT_BUTTON, pBuf); /* ------------------------- */ count++; /* count + at Spy's Discretion */ /* max col - 104 is steal tech widget width */ max_col = (Main.screen->w - (pWindow->size.w - pWindow->area.w) - adj_size(2)) / adj_size(104); /* max row - 204 is steal tech widget height */ max_row = (Main.screen->h - (pWindow->size.h - pWindow->area.h)) / adj_size(204); /* make space on screen for scrollbar */ if (max_col * max_row < count) { max_col--; } if (count < max_col + 1) { col = count; } else { if (count < max_col + adj_size(3)) { col = max_col - adj_size(2); } else { if (count < max_col + adj_size(5)) { col = max_col - 1; } else { col = max_col; } } } pStr = create_string16(NULL, 0, adj_font(10)); pStr->style |= (TTF_STYLE_BOLD | SF_CENTER); count = 0; advance_index_iterate(A_FIRST, i) { if (player_invention_reachable(client.conn.playing, i, FALSE) && TECH_KNOWN == player_invention_state(pVictim, i) && (TECH_UNKNOWN == player_invention_state(client.conn.playing, i) || TECH_PREREQS_KNOWN == player_invention_state(client.conn.playing, i))) { count++; copy_chars_to_string16(pStr, advance_name_translation(advance_by_number(i))); pSurf = create_sellect_tech_icon(pStr, i, FULL_MODE); pBuf = create_icon2(pSurf, pWindow->dst, WF_FREE_THEME | WF_RESTORE_BACKGROUND); set_wstate(pBuf, FC_WS_NORMAL); pBuf->action = spy_steal_callback; pBuf->data.cont = pCont; add_to_gui_list(MAX_ID - i, pBuf); if (count > (col * max_row)) { set_wflag(pBuf, WF_HIDDEN); } } } advance_index_iterate_end; /* get spy tech */ i = advance_number(unit_type(game_unit_by_number(id))->require_advance); copy_chars_to_string16(pStr, _("At Spy's Discretion")); pSurf = create_sellect_tech_icon(pStr, i, FULL_MODE); pBuf = create_icon2(pSurf, pWindow->dst, (WF_FREE_THEME | WF_RESTORE_BACKGROUND| WF_FREE_DATA)); set_wstate(pBuf, FC_WS_NORMAL); pBuf->action = spy_steal_callback; pBuf->data.cont = pCont; add_to_gui_list(MAX_ID - advance_count(), pBuf); count++; /* --------------------------------------------------------- */ FREESTRING16(pStr); pDiplomat_Dlg->pdialog->pBeginWidgetList = pBuf; pDiplomat_Dlg->pdialog->pBeginActiveWidgetList = pDiplomat_Dlg->pdialog->pBeginWidgetList; pDiplomat_Dlg->pdialog->pEndActiveWidgetList = pDiplomat_Dlg->pdialog->pEndWidgetList->prev->prev; /* -------------------------------------------------------------- */ i = 0; if (count > col) { count = (count + (col - 1)) / col; if (count > max_row) { pDiplomat_Dlg->pdialog->pActiveWidgetList = pDiplomat_Dlg->pdialog->pEndActiveWidgetList; count = max_row; i = create_vertical_scrollbar(pDiplomat_Dlg->pdialog, col, count, TRUE, TRUE); } } else { count = 1; } area.w = MAX(area.w, (col * pBuf->size.w + adj_size(2) + i)); area.h = count * pBuf->size.h + adj_size(2); /* alloca window theme and win background buffer */ pSurf = theme_get_background(theme, BACKGROUND_SPYSTEALDLG); if (resize_window(pWindow, pSurf, NULL, (pWindow->size.w - pWindow->area.w) + area.w, (pWindow->size.h - pWindow->area.h) + area.h)) { FREESURFACE(pSurf); } area = pWindow->area; widget_set_position(pWindow, (Main.screen->w - pWindow->size.w) / 2, (Main.screen->h - pWindow->size.h) / 2); /* exit button */ pBuf = pWindow->prev; pBuf->size.x = area.x + area.w - pBuf->size.w - 1; pBuf->size.y = pWindow->size.y + adj_size(2); setup_vertical_widgets_position(col, area.x + 1, area.y, 0, 0, pDiplomat_Dlg->pdialog->pBeginActiveWidgetList, pDiplomat_Dlg->pdialog->pEndActiveWidgetList); if(pDiplomat_Dlg->pdialog->pScroll) { setup_vertical_scrollbar_area(pDiplomat_Dlg->pdialog->pScroll, area.x + area.w, area.y, area.h, TRUE); } redraw_group(pDiplomat_Dlg->pdialog->pBeginWidgetList, pWindow, FALSE); widget_mark_dirty(pWindow); return -1; }