Advertisement
Guest User

civdos city production selection v1.4

a guest
Jun 5th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.89 KB | None | 0 0
  1. //v 1.3 https://pastebin.com/JLUStSk3
  2.  
  3. //CIVDOS: AI CITY BUILDING LOGIC (pseudocode) v 1.4
  4.  
  5. //I think in strange parts with DX DX should be always 0 (if we want sensible game logic), so I deleted it in such cases.
  6. //see v 1.3 for strange DX parts
  7.  
  8. //per-continent strategies (same numbers as unit roles)
  9. #define P_SETTLE    0
  10. #define P_ATTACK    1
  11. #define P_DEFEND    2
  12. #define P_TRANSPORT 5
  13.  
  14. //unit roles
  15. #define UR_SETTLER    0
  16. #define UR_ATTACK     1
  17. #define UR_DEFENCE    2
  18. #define UR_SEA_ATTACK 3
  19. #define UR_TRANSPORT  5
  20.  
  21. //unit domain
  22. #define UD_LAND  0
  23. #define UD_AIR   1
  24. #define UD_FLEET 2
  25.  
  26. //types of units
  27. #define U_SETTLERS  0
  28. #define U_MILITIA   1
  29. #define U_FIGHTER  14
  30. #define U_BOMBER   15
  31. #define U_NUCLEAR  25
  32. #define U_DIPLOMAT 26
  33.  
  34. //city improvements (bit flags)
  35. #define BB_PALACE         0x1
  36. #define BB_BARRACKS       0x2
  37. #define BB_GRANARY        0x4
  38. #define BB_MARKETPLACE   0x10
  39. #define BB_LIBRARY       0x20
  40. #define BB_FACTORY     0x4000
  41. #define BB_MFG_PLANT   0x8000
  42. #define BB_POWER_PLANT    0x4 //second word
  43. #define BB_HYDRO_PLANT    0x8
  44. #define BB_NUCLEAR_PLANT 0x10
  45.  
  46. //city improvements (palace is 1)
  47. #define B_MARKETPLACE    5
  48. #define B_LIBRARY        6
  49. #define B_COURTHOUSE     7
  50. #define B_BANK          10
  51. #define B_UNIVERSITY    12
  52. #define B_FACTORY       15
  53. #define B_POWER_PLANT   19
  54. #define B_HYDRO_PLANT   20
  55. #define B_NUCLEAR_PLANT 21
  56.  
  57. //wonders of the world (Pyramids is 1)
  58. #define W_HOOVER_DAM        15
  59. #define W_MANHATTAN_PROJECT 17
  60. #define W_APOLLO_PROGRAM    19
  61.  
  62. //civilization advances
  63. #define T_NUCLEAR_FISSION 0x1b
  64. #define T_EXPLOSIVES      0x38
  65.  
  66. //governments
  67. #define G_DESPOTISM 1
  68.  
  69. //start sequence: 55 8b ec 81 ec 4a 01 57 56...
  70. //start address: 0xd9a1 in unpacked civ.exe (ver .05 en)
  71.  
  72. FUN_prod_menu_and_ai_prod_selection(u8 civ, u8 city_id)
  73. {
  74.     //1ade:0421
  75.     current_list = 0; //2 - city improvements
  76.     advisers_drawn = 0;
  77.    
  78.     FUN_city_count_everything(city_id,-1); //darkpanda: CityProcess
  79.     //it calculates various variables for city_id city
  80.     //this function also can draw city screen, but it doesn't do it here ('cause -1 param)
  81.    
  82.     LAB_CREATE_LIST:
  83.    
  84.     //omitted: create string: "What should we build in City Name?"
  85.    
  86.     list_entries = 1; //total menu items to select
  87.     list_prod[0] = 0;
  88.     if (current_list != 2)
  89.     {
  90.         for (counter = 0; counter < 28; counter++)
  91.         {
  92.             if (check_tech(civ,unit_tech[counter]))
  93.             {
  94.                 if (check_tech(civ,unit_obsolete_tech[counter]) == 0)
  95.                 {
  96.                     if (city_flags[city_id]&0x2
  97.                     || unit_domain[counter] != UD_FLEET)
  98.                     {
  99.                         if (counter == U_NUCLEAR)
  100.                         {
  101.                             if (wonder_city[W_MANHATTAN_PROJECT] == -1)
  102.                                 continue;
  103.                             if (check_tech(civ,T_NUCLEAR_FISSION) == 0)
  104.                                 continue;
  105.                         }
  106.                        
  107.                         //omitted: add to string unit string: i.e. "Militia (5 turns, A/D/M:1/1/1)"
  108.                        
  109.                         list_prod[list_entries] = counter;
  110.                         list_entries++;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116.    
  117.     //1ade:05f4
  118.     list_entries_units = list_entries;
  119.     string_pos_units = get_string_length(current_string);
  120.     hoover_dam_eff = 0;
  121.     if (check_wonder_eff(civ,W_HOOVER_DAM))
  122.     {
  123.         if (get_continent(city_x[wonder_city[W_HOOVER_DAM]],city_y[wonder_city[W_HOOVER_DAM]]) ==
  124.         get_continent(city_x[city_id],city_y[city_id])
  125.         {
  126.             hoover_dam_eff = 1;
  127.         }
  128.     }
  129.    
  130.     //1ade:066b
  131.     if ((debug_flag&0x4)!=0) //you can change debug flags is .SVE using JCivED
  132.     {
  133.         for (counter = 1; counter <= 24; counter++)
  134.         {
  135.             if (counter > 21) //SS parts
  136.             {
  137.                 if (wonder_city[W_APOLLO_PROGRAM] == -1)
  138.                     continue;
  139.                 if ((spaceship_flags&(1<<civ))==0) //civ already launched SS
  140.                     continue;
  141.             }
  142.            
  143.             if (check_tech(civ,city_improvement_tech[counter] == 0)
  144.                 continue;
  145.            
  146.             //1ade:06c8 city already has this improvement: I rewrote it pretty loosely...
  147.             if (counter-1 < 16)
  148.             {
  149.                 if ((city_buildings1[city_id]&(1<<(counter-1))) != 0)
  150.                     continue;
  151.             }
  152.             else
  153.             {
  154.                 if ((city_buildings2[city_id]&(1<<(counter-1-16))) != 0)
  155.                     continue;
  156.             }
  157.            
  158.             //1ade:06fb
  159.             if (counter == B_FACTORY)
  160.             {
  161.                 if (city_buildings1[city_id]&BB_MFG_PLANT)
  162.                 {
  163.                     continue;
  164.                 }
  165.             }
  166.             if (counter == B_BANK)
  167.             {
  168.                 if ((city_buildings1[city_id]&BB_MARKETPLACE)==0)
  169.                 {
  170.                     if (check_tech(civ,city_improvement_tech[B_MARKETPLACE]))
  171.                         continue;
  172.                 }
  173.             }
  174.             if (counter == B_UNIVERSITY)
  175.             {
  176.                 if ((city_buildings1[city_id]&BB_LIBRARY)==0)
  177.                 {
  178.                     if (check_tech(civ,city_improvement_tech[B_LIBRARY]))
  179.                         continue;
  180.                 }
  181.             }
  182.             if (counter == B_HYDRO_PLANT)
  183.             {
  184.                 if ((city_flags[city_id]&0x80)==0) //city doesn't have hydropower
  185.                     continue;
  186.             }
  187.             if (counter == B_POWER_PLANT)
  188.             {
  189.                 if ((city_buildings1[city_id]&BB_FACTORY)==0)
  190.                     continue;
  191.             }
  192.             if (counter == B_POWER_PLANT
  193.             || counter == B_HYDRO_PLANT
  194.             || counter == B_NUCLEAR_PLANT)
  195.             {
  196.                 if (hoover_dam_eff)
  197.                     continue;
  198.                 if ((city_buildings2[city_id]&BB_POWER_PLANT) != 0
  199.                 || (city_buildings2[city_id]&BB_HYDRO_PLANT) != 0
  200.                 || (city_buildings2[city_id]&BB_NUCLEAR_PLANT) != 0)
  201.                     continue;
  202.             }
  203.            
  204.             //omitted: add city improvement string, i.e. "Barracks (20 turns)"
  205.            
  206.             list_prod[list_entries] = -counter; //1 -> -1 is palace 2 -> -2 is barracks etc.
  207.             list_entries++;
  208.         }
  209.     }
  210.    
  211.     //1ade:082f
  212.     for (counter = 25; counter <= 45; counter++)
  213.     {
  214.         if (check_tech(civ,city_improvement_tech[counter])==0)
  215.             continue;
  216.         if (wonder_city[counter-24] == -1 //wonder was never built
  217.         || wonder_city[counter-24] == 127) //wonder was destroyed
  218.             continue;
  219.         if (check_tech(civ,wonder_tech_obsolete[counter-24])
  220.         {
  221.             //omitted: add "*" to string
  222.         }
  223.        
  224.         //omitted: add wonder string, i.e. "Pyramids (100 turns)"
  225.        
  226.         list_prod[list_entries] = -counter;
  227.         list_entries++;
  228.     }
  229.    
  230.     //1ade:08dd
  231.     if (civ == player_civ)
  232.     {
  233.         if ((city_flags[city_id]&0x10)==0) //not autobuild
  234.         {
  235.             if (list_entries >= 30)
  236.             {
  237.                 if (current_list == 0)
  238.                 {
  239.                     current_list = 1;
  240.                     goto LAB_CREATE_LIST; //repeat everything? why?..
  241.                 }
  242.                
  243.                 //omitted: replace part of "current_string" after "string_pos_units" with string "More..." (1)
  244.                
  245.                 list_prod[list_entries_units] = 99; //flag to switch to the next list
  246.             }
  247.         }
  248.     }
  249.    
  250.     if (current_list == 2)
  251.     {
  252.         //omitted: add string "More..." (2)
  253.        
  254.         list_prod[list_entries] = 98; //flag to switch to previous list
  255.     }
  256.     //end of: list creation (position -> what to build) and creation of text for this list--------------------------------------------------------------
  257.    
  258.     //1ade:094f
  259.     cont = get_continent(city_x[city_id],city_y[city_id]);
  260.     cont_pol = continent_policy[civ][cont];
  261.     pol = 0;
  262.     if (cont_pol == P_ATTACK
  263.     || cont_pol == P_DEFEND)
  264.     {
  265.         pol = 1;
  266.     }
  267.     if (gl_ai_handicap != 0 //'human player dominance': 1st place in civ powergraph, > 4 cities and 0 nuclear bombs, and turn > 200
  268.     && cont_pol == P_TRANSPORT)
  269.     {
  270.         pol = 1;
  271.     }
  272.    
  273.     if (government[civ] <= G_DESPOTISM)
  274.     {
  275.         settlers_food = 1;
  276.     }
  277.     else
  278.         settlers_food = 2;
  279.     s16 res_food = city_food_prod-settlers_food*settler_counter-2*city_size[city_id]; //city_food_prod is a global var
  280.    
  281.     //1ade:09dd
  282.     counter = 0;
  283.     do //types of units
  284.     {
  285.         stack_fee6[counter] = 1; //modifer: it's 1 for every unit type, so it's possible to just use '1' instead?
  286.         unit_counter_on_continent[counter] = 0; //stack -0x44
  287.         counter++;
  288.     }
  289.     while (counter < 28);
  290.    
  291.     //1ade:0a01
  292.     local_civ_un_counter = civ_unit_counter[civ]; //old, normal value (without settlers and nuclears)
  293.     civ_unit_counter[civ] = 0;
  294.    
  295.     city_count = 0;
  296.     do //all cities
  297.     {
  298.         cit_cont = get_continent(city_x[city_count],city_y[city_count]);
  299.         if (city_flags[city_count] != -1 //a city exists
  300.         && city_civ[city_count] == civ //it's belong to us
  301.         && cit_cont == cont) //a city is on the same continent as our city
  302.         {
  303.             if (city_production[city_count] >= 0) //unit in production
  304.             {
  305.                 if (city_count != city_id) //not our city itself
  306.                 {
  307.                     unit_in_prod_on_cont[city_production[city_count]]++; //local array
  308.                 }
  309.             }
  310.             for (i = 0; i < 2; i++) //special fields for unit storage...
  311.             {
  312.                 alt_un = city_alt_unit_field[city_count][i]
  313.                 if (alt_un != 0xFF)
  314.                 {
  315.                     civ_units_counter[civ]++; //global array
  316.                     unit_counter_on_continent[alt_un&0x3F]++; //local array
  317.                 }
  318.             }
  319.         }
  320.         city_count++;
  321.     }
  322.     while (city_count < 128);
  323.    
  324.     //1ade:0ab2
  325.     counter = 0;
  326.     do //all units
  327.     {
  328.         if (unit_type[counter] != -1) //unit exists
  329.         {
  330.             civ_units_counter[civ]++;
  331.             un_cont = get_continent(unit_x[counter], unit_y[counter]);
  332.             if (un_cont == cont)
  333.             {
  334.                 unit_counter_on_continent[unit_type[counter]]++;
  335.             }
  336.         }
  337.     }
  338.     while (counter < 128);
  339.    
  340.     //1ade:0b0b
  341.     if (civ_units_counter[civ] == 0)
  342.     {
  343.         if (civ != player_civ)
  344.         {
  345.             if (government[civ] <= G_DESPOTISM)
  346.             {
  347.                 return 1; //militia //actually does not work for first city (probably because settler is not disbanded yet)
  348.             }
  349.         }
  350.     }
  351.    
  352.     //1ade:0b2c
  353.     if (civ == 0) //barbarians
  354.     {
  355.         if (first_discoverer_civ[T_EXPLOSIVES] != 0) //explosives was discovered by somebody before...
  356.         {
  357.             return 7; //knights
  358.         }
  359.         return 3; //legion
  360.     }
  361.    
  362.     //1ade:0b4a
  363.     def_un_desire = 0;
  364.     counter = find_unit_id_in_xy(city_x[city_id],city_y[city_id]);
  365.     if (counter != -1) //there's some unit in the city
  366.     {
  367.         def_units = count_units_of_this_type_in_stack(counter,civ,UR_DEFENCE);
  368.         var_a = city_size[city_id]/4+pol; //pol can be 0 or 1
  369.         if (var_a > def_units)
  370.             def_un_desire = 1; //we need more def units...
  371.     }
  372.     else
  373.     {
  374.         def_un_desire = 2; //we NEED def unit!!
  375.     }
  376.    
  377.     //1ade:0baf
  378.     best_unit_desire = 999; //the less is better
  379.     best_bldng_desire = 999;
  380.     best_desire = 999;
  381.     best_unit_production = -1;
  382.     best_bldng_production = -1; //stack -0x4a
  383.     counter = 1;
  384.    
  385.     //1ade:0f57
  386.     for (counter = 0; counter < list_entries; counter++)
  387.     {
  388.         prod_type = list_prod[counter];
  389.        
  390.         if (prod_type >= 0 //unit, not city improvement
  391.         && prod_type < U_DIPLOMAT) //but not diplomat or caravan
  392.         {
  393.             if (prod_type == U_CARRIER)
  394.             {
  395.                 continue; //AI never builds carriers...
  396.             }
  397.            
  398.             if (prod_type == U_SETTLERS)
  399.             {
  400.                 settl_food = 2;
  401.                 if (government[civ] <= G_DESPOTISM)
  402.                     settl_food = 1;
  403.                 if (settl_food >= res_food) //city produces not enough food to support new settler...
  404.                 {
  405.                     if (units_per_type[civ][U_SETTLERS] != 0
  406.                     || unit_in_prod_on_cont[U_SETTLERS] > 1)
  407.                     {
  408.                         continue;
  409.                     }
  410.                 }
  411.             }
  412.            
  413.             //1ade:0fd2
  414.             if (prod_type == U_MILITIA)
  415.             {
  416.                 if (government[civ] > G_DESPOTISM) //AI never builds militias if its govt's not Despotism...
  417.                 {
  418.                     continue;
  419.                 }
  420.             }
  421.            
  422.             if (prod_type == U_NUCLEAR)
  423.             {
  424.                 if (units_per_type[civ][U_NUCLEAR] >= 4 //AI never builds nukes if it already has 4 of them
  425.                 || units_in_production[civ][U_NUCLEAR] >= 2) //or if >= 2 nukes in production
  426.                 {
  427.                     continue;
  428.                 }
  429.             }
  430.            
  431.             //1ade:1009
  432.             if (prod_type == U_FIGHTER)
  433.             {
  434.                 if (units_per_type[civ][prod_type]+units_in_production[civ][prod_type] >=
  435.                 (units_per_type[player_civ][U_BOMBER])>>1)
  436.                 {
  437.                     continue;
  438.                 }
  439.                 if ((diplomacy_status[civ][player_civ]&2) != 0) //peace with player
  440.                 {
  441.                     continue;
  442.                 }
  443.             }
  444.            
  445.             //1ade:1054
  446.             if (unit_domain[prod_type] == UD_LAND)
  447.             {
  448.                 curr_desire = (stack_fee6[prod_type]<<1)+unit_counter_on_continent[prod_type]; //stack_fee6[prod_type] is always 1
  449.                 //more units of this type (esp. in production) -> less desire to build another one
  450.             }
  451.             else
  452.             {
  453.                 if (defence_per_continent[civ][cont] < 8) //not enough defence on continent -> don't build any boats or aircraft...
  454.                 {
  455.                     continue;
  456.                 }
  457.                
  458.                 var_des = (units_in_production[civ][prod_type]<<1)+units_per_type[civ][prod_type]; //var_des: DX -> CX
  459.                 //more units of this type (esp. in production) -> less desire to build another one     
  460.                 curr_desire = var_des*defence_per_continent[civ][cont]/(civ_unit_counter[civ]+1); //AX*CX
  461.                 //strange... more defence on continent -> less desire to build non-land unit?..
  462.                 //more units in total -> more desire to build non-land unit...
  463.             }
  464.            
  465.             if (def_un_desire != 0)
  466.             {
  467.                 if (unit_role[prod_type] != UR_DEFENCE) //we want to build defence units ONLY...
  468.                 {
  469.                     continue;
  470.                 }
  471.                
  472.                 curr_desire = 2/def_un_desire; //the less is better
  473.             }
  474.            
  475.             if (unit_role[prod_type] != UR_DEFENCE && unit_role[prod_type] != UR_SETTLER)
  476.             {
  477.                 curr_desire = (curr_desire<<1); //if it's not a defender or settler, we need it less...
  478.                 if (cont_pol != unit_role[prod_type]) //unit role is not our continent role...
  479.                 {
  480.                     curr_desire = (curr_desire<<1); //we need it less...
  481.                 }
  482.             }
  483.            
  484.             //1ade:1113
  485.             if (unit_role[prod_type] == UR_TRANSPORT)
  486.             {
  487.                 if (cont_pol != P_TRANSPORT) //AI doesn't build any transports if continent role is not 'transport'
  488.                     continue;
  489.                 curr_desire = ((3*curr_desire)>>1);
  490.             }
  491.            
  492.             //1ade:113c
  493.             if (prod_type == U_SETTLERS)
  494.             {
  495.                 granary_factor = 6; //CX->AX
  496.                 if ((city_buildings1[city_id]&BB_GRANARY)!=0) //city has a granary
  497.                     granary_factor = 4; //we want to build settlers more
  498.                 granary_factor -= get_leader_civilized_attitude(civ,civ_0_or_1_same_color[civ]); //+1->-1 for civilized, -1->+1 for militaristic
  499.                 //civilized leaders want to build settlers more... BUT 'expansionism' attitude is not matter here...
  500.             }
  501.             else
  502.                 granary_factor = 10; //AX
  503.            
  504.             granary_factor *= (unit_shield_rows_cost[prod_type]+2); //AX
  505.             granary_factor *= curr_desire; //AX->CX
  506.            
  507.             unit_coolness = (unit_attack[prod_type]+unit_defence[prod_type])*(unit_tot_moves[prod_type]+1);
  508.             granary_factor /= unit_coolness; //we want to build strongest and fastest units...
  509.             curr_desire = granary_factor;
  510.            
  511.             //1ade:11a8
  512.             if (unit_role[prod_type] == UR_SEA_ATTACK)
  513.             {
  514.                 if (cont_pol != P_SETTLE)
  515.                 {
  516.                     curr_desire *= 4; //we want it much less (we need military units or transports!)
  517.                 }
  518.                 else
  519.                 {
  520.                     curr_desire *= 2; //we want it less
  521.                 }
  522.             }
  523.            
  524.             if (unit_domain[prod_type] == UD_AIR)
  525.             {
  526.                 curr_desire = (curr_desire<<1);
  527.             }
  528.            
  529.             //1ade:11d9
  530.             if (civ_unit_counter[civ] > 120) //too many units...
  531.             {
  532.                 curr_desire = (curr_desire<<2);
  533.             }
  534.            
  535.             if (cont_pol == P_SETTLE)
  536.             {
  537.                 //continent policy is not attack/defend or transport -> so we don't units THIS much...
  538.                 if (city_tot_shields <= city_supp_units)
  539.                 //so, if there's too many supported units (in comparison with city shield production)...
  540.                 {
  541.                     curr_desire = (curr_desire<<1); //we want to build another unit less...
  542.                 }
  543.             }
  544.            
  545.             if (civ_has_contact_or_peace_with_somebody == 0)  //i.e. no contacts with other civs...
  546.             //if diplomacy flags 'contact' or 'peace' is not 0 with some other civ at the beginning of civ turn => then it's 1
  547.             {
  548.                 curr_desire = (curr_desire<<1);
  549.             }
  550.            
  551.             //1ade:1206
  552.             if (curr_desire < best_desire) //better
  553.             {
  554.                 best_common_production = counter; // Stack[-0x146]
  555.                 best_desire = curr_desire;
  556.             }
  557.         } //normal unit
  558.         else //city improvements, diplomat, caravan
  559.         {
  560.             //1ade:0f79
  561.             curr_desire = 999;
  562.             building_pol = 0;
  563.             temp_var_ax = -prod_type; //city improvement number... -1 palace->1, -2 barracks->2 etc. diplomat: -26, caravan: -27, //NEG AX
  564.             temp_var_ax += 27; // SUB AX, -27 //palace: 28 nuclear plant: 48 SS structural: 49
  565.             //diplomat: 1, caravan: 0
  566.             if (temp_var_ax <= 49) //caravan, diplomat or any city improvement, except SS component, SS module and wonders
  567.             {
  568.                 //ADD        AX,AX //2 bytes per entry...
  569.                 //XCHG       AX,BX //AX <-> BX for what purpose?..
  570.                 //JMP        word ptr CS:[BX + 0xe71] //what the heck?
  571.                 //maybe "switch" in original code?
  572.                
  573.                 switch (temp_var_ax)
  574.                 {
  575.                     case 31: //temple 0x0bcf
  576.                     if ((city_flags[city_id]&1) != 0) //city disorder
  577.                         curr_desire = -4; //stack 0xfecc
  578.                     break;
  579.                    
  580.                     case 34: //courthouse 0x0bea
  581.                     curr_desire = -((city_corruption<<1)-12);
  582.                     break;
  583.                    
  584.                     case 38: //cathedral 0x0bfb
  585.                     if ((city_flags[city_id]&1) != 0) //city disorder
  586.                         curr_desire = -2;
  587.                     break;
  588.                    
  589.                     case 41: //colosseum 0x0c16
  590.                     if ((city_flags[city_id]&1) != 0) //city disorder
  591.                         curr_desire = -1;
  592.                     break;
  593.                    
  594.                     case 29: //barracks  0x0c31
  595.                     curr_desire = 5;
  596.                     building_pol = 1; //stack 0xfece
  597.                     break;
  598.                    
  599.                     case 30: //granary 0x0c40
  600.                     if (city_tot_shields >= 3)
  601.                         curr_desire = 4;
  602.                     else
  603.                         curr_desire = 8;
  604.                     break;
  605.                    
  606.                     case 42: //factory
  607.                     case 47: //hydro plant 0x0c51
  608.                     curr_desire = -(((city_tot_shields)>>1)-13);
  609.                     break;
  610.                    
  611.                     case 46: //power plant 0x0c5e
  612.                     curr_desire = -(city_tot_shields/5-12);
  613.                     break;
  614.                    
  615.                     case 43: //mfg plant
  616.                     case 45: //rec center
  617.                     case 48: //nuclear plant 0x0c69
  618.                     AX = city_tot_shields;
  619.                     AX = AX>>3;
  620.                     curr_desire = -(AX-12);
  621.                     break;
  622.                    
  623.                     case 32: //marketplace 0x0c7d
  624.                     curr_desire = -(((city_tot_trade)>>1)-10);
  625.                     if ((city_flags[city_id]&1) != 0) //city disorder
  626.                     {
  627.                         curr_desire = -3;
  628.                     }
  629.                     break;
  630.                    
  631.                     case 33: //library
  632.                     case 37: //bank 0x0ca9
  633.                     curr_desire = -(city_tot_trade/3-10);
  634.                    
  635.                     break;
  636.                    
  637.                     case 39: //university 0x0cb8
  638.                     AX = city_tot_trade;
  639.                     AX = AX>>2;
  640.                     curr_desire = -(AX-10);
  641.                     break;
  642.                    
  643.                     case 35: //city walls 0x0ccb
  644.                     curr_desire = -(city_size[city_id]/2-10);
  645.                     if (city_buildings1[city_id]&BB_PALACE)
  646.                         curr_desire = 4;
  647.                     break;
  648.                    
  649.                     case 36: //aqueduct 0x0cf8
  650.                     AX = 10-(res_food>>1);
  651.                     if (AX <= city_size[city_id])
  652.                     {
  653.                         curr_desire = -(city_size[city_id]+(res_food>>1)-14);
  654.                     }
  655.                     break;
  656.                    
  657.                     case 49: //SS structural 0x0d27
  658.                     if ((spaceship_flags&((1<<civ)+(1<<player_civ)))==0) //Our spaceship not launched AND human spaceship not launched too.
  659.                     {
  660.                         if (spaceship_flags&(1<<8)) //score completed: post game (&0x100)
  661.                         {
  662.                             curr_desire = 2; //but why AI want it _more_ in this case?..
  663.                         }
  664.                         else
  665.                         {
  666.                             curr_desire = 4;
  667.                         }
  668.                     }
  669.                     break;
  670.                    
  671.                     case 0: //caravan 0x0d5a
  672.                     if (trade_cities[city_id][2] == -1)
  673.                     {
  674.                         curr_desire = -((city_tot_trade>>1)-10); //I have no idea what's in DX at the moment...
  675.                         if (trade_cities[city_id][0] != -1)
  676.                         {
  677.                             curr_desire += 2;
  678.                         }
  679.                         if (trade_cities[city_id][1] != -1)
  680.                         {
  681.                             curr_desire += 2;
  682.                         }
  683.                         if (curr_desire < 3)
  684.                         {
  685.                             curr_desire = 3;
  686.                         }
  687.                     }
  688.                     if (player_civ == civ)
  689.                     {
  690.                         if (city_flags[city_id]&0x10) //auto build
  691.                         {
  692.                             curr_desire = 999; //human player should NOT build caravans in autobuild!
  693.                         }
  694.                     }
  695.                     break;
  696.                    
  697.                     case 1: //diplomat 0xdcd
  698.                     if (player_civ != 0) //human doesn't play for Barbarians...
  699.                     {
  700.                         if ((unknown_array_civ_x_civ[civ][player_civ]&2)==0) //always?..
  701.                         //array: 2 bytes per civ, civ x civ = 2*8*8 = 128 bytes total
  702.                         //2nd byte is 0xfd, if in the past there was sneak attack by human player on AI or sneak attack by AI on the human player
  703.                         //but 1st byte never changes and it's always 0 (if it does not change in overlays...)
  704.                         {
  705.                             if (units_in_production[civ][diplomat] <= 0)
  706.                             {
  707.                                 counter = random(5); //random block of 16 tech (bit flags, 16*5 = 80 tech max per civ)
  708.                                 if ((player_civ_near_me&(1<<civ))!=0)
  709.                                 //it's 1, if human player has > 1 cities on some of our AI civ continents
  710.                                 //and AI civ defence on this continent is not 0
  711.                                 //and there can be some other conditions - it's set in big routine which determines AI policy, it's pretty complicated...
  712.                                 {
  713.                                     di_var = tech_list[civ][counter];
  714.                                     ax_var = tech_list[player_civ][counter];
  715.                                     ax_var |= di_var;
  716.                                     if (ax_var != dx_var) //player civ has some new techs in this 16 tech chunk...
  717.                                     {
  718.                                         ax_var = number_of_tech[civ]-number_of_tech[player_civ]+10;
  719.                                         curr_desire = normalize_minmax(ax_var,5,10);
  720.                                     }
  721.                                 }
  722.                             }
  723.                         }
  724.                     }
  725.                     break;
  726.                    
  727.                     case 40: //mass transit
  728.                     case 44: //sdi defence
  729.                     default: //2..28 - military units (not used) and palace
  730.                     //do nothing
  731.                 }
  732.             }
  733.            
  734.             //1ade:0ed5
  735.             if (curr_desire >= 400)
  736.             {
  737.                 curr_desire = 0x3fff; //whut?
  738.             }
  739.             else
  740.             {
  741.                 if (cont_pol != building_pol) //building_pol usually 0, 1 (attack) for barracks
  742.                 {
  743.                     base_des = 14; //we need attack/defence units or transports, so we don't want to build city improvements...
  744.                 }
  745.                 else
  746.                     base_des = 10;
  747.                
  748.                 curr_desire *= base_des;
  749.                 curr_desire = 3*curr_desire/(get_leader_civilized_attitude(civ,civ_0_or_1_same_color[civ])+3);
  750.                 // /4 if civilized -> want to build city improvements
  751.                 // /2 if militaristic -> don't want to build city improvements
  752.                 // /3 otherwise
  753.             }
  754.            
  755.             //1ade:0f22
  756.             if ((city_buildings1[city_id]&BB_BARRACKS)!=0) //if city has barracks...
  757.             {
  758.                 curr_desire += (curr_desire>>2); //you better to build units and not city improvements?..
  759.             }
  760.            
  761.             if (curr_desire < best_desire) //better
  762.             {
  763.                 best_common_production = counter;
  764.                 best_desire = curr_desire;
  765.             }
  766.         }
  767.        
  768.         if (prod_type < 0 //city improvement
  769.         || prod_type >= U_DIPLOMAT) //diplomat or caravan
  770.         {
  771.             if (curr_desire < best_bldng_desire)
  772.             {
  773.                 best_bldng_desire = curr_desire;
  774.                 best_bldng_production = counter;
  775.             }
  776.         }
  777.         else //normal unit
  778.         {
  779.             if (curr_desire < best_unit_desire)
  780.             {
  781.                 best_unit_desire = curr_desire;
  782.                 best_unit_production = counter;
  783.             }
  784.         }
  785.     } //loop: select best production
  786.    
  787.     //1ade:1268
  788.     civ_unit_counter[civ] = local_civ_un_counter; //restore previous value (without settlers and nuclear bombs)
  789.     if ((human_civs_bits&(1<<civ)) == 0) //ai
  790.     {
  791.         if (city_production[city] == -1) //palace
  792.         {
  793.             return -1; //continue palace construction?.. probably AI decides to change capital/select capital if NONE in some different function!
  794.             //but definetely not in this function...
  795.         }
  796.         return list_prod[best_common_production];
  797.     }
  798.    
  799.     //1ade:12a5
  800.     if ((city_flags[city]&x0x10)!=0) //AUTO
  801.     {
  802.         if (best_bldng_production != -1 //some new buildings was selected for AUTO
  803.         && best_bldng_desire <= 500) //and desire to build it is high enough...
  804.         {
  805.             return list_prod[best_bldng_production];
  806.         }
  807.         else
  808.             return 99; //flag: no good new production for AUTO was found. Do not change production. Go to city screen, if needed...
  809.     }
  810.    
  811.     //1ade:12cd
  812.     if ((game_settings&0x1)!=0) //instant advice
  813.     {
  814.         if (advisers_drawn == 0)
  815.         {
  816.             if (best_unit_production != -1) //military advisor
  817.             {
  818.                 glob_0xaa[16] = 2; //?? m.b. total number of lists/messages??
  819.                 if (list_prod[best_unit_production] > 0) //unit
  820.                 {
  821.                     //omitted: use name from units name list...
  822.                 }
  823.                 else
  824.                 {
  825.                     //omitted: use name from city improvements name list - never happens
  826.                 }
  827.                
  828.                 //omitted: create string for military advisor message (using produce.txt)
  829.                
  830.                 help_message_on_screen_flag = 1;
  831.                
  832.                 draw_message_B(current_string,8,160); //draw military advisor advice
  833.             }
  834.            
  835.             if (best_bldng_production != -1) //domestic advisor
  836.             {
  837.                 curr_desire = list_prod[best_bldng_production];
  838.                 if (list_prod[best_bldng_production] > 0) //unit: diplomat or caravan
  839.                 {
  840.                     //omitted: use name from units name list...
  841.                 }
  842.                 else
  843.                 {
  844.                     //omitted: use name from city improvements name list...
  845.                 }
  846.                
  847.                 //omitted: create string for domestic advisor message (using produce.txt)
  848.                
  849.                 draw_message_B(current_string,164,160); //draw domestic advisor advice
  850.             }
  851.             advisers_drawn = 1;
  852.             glob_0xaa[16] = 1; //??
  853.            
  854.             goto LAB_CREATE_LIST;
  855.         }
  856.     } //instant advice
  857.    
  858.     //1ade:1438
  859.     if (list_entries > 20 || current_list != 0)
  860.     {
  861.         glob_0xaa[16] = 2; //??
  862.     }
  863.    
  864.     //1ade:144e
  865.     menu_on_screen_flag = 1;
  866.     SI = draw_message_B(current_string,80,8); //draw menu (what you can build) in X: 80 Y: 8
  867.     current_desire = stack_de[SI]; //stack -0xde, never used elsewhere... should be selected menu item?
  868.     if (current_desire == 99) //go to the next list...
  869.     {
  870.         current_list = 2; //city improvements
  871.         goto LAB_CREATE_LIST;
  872.     }
  873.     if (current_desire == 98) //go to previous list...
  874.     {
  875.         current_list = 1; //units
  876.         goto LAB_CREATE_LIST;
  877.     }
  878.     glob_0xaa[16] = 1; //??
  879.    
  880.     //1ade:148c
  881.     if (civilopedia_help_flag != 0)
  882.     {
  883.         FUN_black_screen_mb();
  884.         FUN_clear_screen_mb_B(); //some_graph_driver_func_1(0,0,200,320,0,0,glob_0xaa);
  885.         cpedia_from_city_screen_flag = 1;
  886.         if (current_desire >= 0) //unit
  887.         {
  888.             cpedia_type = 2;
  889.         }
  890.         else //city improvement
  891.         {
  892.             cpedia_type = 1;
  893.             current_desire = -current_desire; //city improvements have negative values
  894.         }
  895.         overlay_08_2A06(current_desire,cpedia_type); //civilopedia help
  896.         clear_all_screen_mb(); //some_graph_driver_func_2(0,0,glob_0xaa,200,320,unkn_graph_var) - clear screen?
  897.         update_screen_mb();
  898.         to_other_screen_from_city_screen_flag = 1;
  899.         goto LAB_CREATE_LIST;
  900.     }
  901.    
  902.     return current_desire;
  903. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement