Guest User

Untitled

a guest
Mar 31st, 2014
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ui_itm_cooking
  2. -- ponney68
  3. --[[
  4. Copyright (C) 2012 Alundaio
  5. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License-]]
  6. --]]
  7. -------------------------------------------------------------------
  8. class "load_item" (CUIListBoxItem)
  9. function load_item:__init(height) super(height)
  10.     self.file_name      = "filename"
  11.  
  12.     self:SetTextColor(GetARGB(255, 170, 170, 170))
  13.  
  14.     self.fn = self:GetTextItem()
  15.     self.fn:SetFont(GetFontLetterica18Russian())
  16.     self.fn:SetEllipsis(true)
  17. end
  18.  
  19. function load_item:__finalize()
  20. end
  21.  
  22. -------------------------------------------------------------------
  23. class "cooking_ui" (CUIScriptWnd)
  24.  
  25. function cooking_ui:__init(owner,section) super()
  26.     self.owner = owner
  27.     self.section = section
  28.  
  29.     self.use_parts = alun_utils.read_from_ini(nil,section,"cooking_use_parts","bool",false)
  30.     self.cooking_type = alun_utils.read_from_ini(nil,section,"cooking_type","string","weapon")
  31.  
  32.     self.cooking_only = alun_utils.parse_list(nil,section,"cooking_only",true)
  33.     self.cooking_refuse = alun_utils.parse_list(nil,section,"cooking_refuse",true)
  34.  
  35.     self.parts_include = alun_utils.parse_list(nil,section,"cooking_parts_include",true)
  36.     self.parts_exclude = alun_utils.parse_list(nil,section,"cooking_parts_exclude",true)
  37.  
  38.     self.use_actor_effects = alun_utils.read_from_ini(nil,section,"cooking_use_actor_effects","bool",false)
  39.  
  40.     self:InitControls()
  41.     self:InitCallBacks()
  42. end
  43.  
  44. function cooking_ui:__finalize()
  45. end
  46.  
  47. function cooking_ui:FillList()
  48.     self.list_box:RemoveAll()
  49.  
  50.     local function fill_list(actor,obj)
  51.         if (obj) and (self.cooking_only and self.cooking_only[obj:section()]) or (self.cooking_refuse == nil) or (self.cooking_refuse[obj:section()] == nil) then
  52.             if (self.cooking_type == "fuel" and alun_utils.item_is_explosive(obj)) then
  53.  
  54.                     self:AddItemToList(obj,self.list_box)
  55.  
  56.             end
  57.         end
  58.     end
  59.  
  60.     db.actor:iterate_inventory(fill_list,db.actor)
  61. end
  62.  
  63. function cooking_ui:FillPartsList()
  64.     self.list_box_parts:RemoveAll()
  65.  
  66.     if self.list_box:GetSize()==0 then return end
  67.  
  68.     local item = self.list_box:GetSelectedItem()
  69.     if not (item) then
  70.         return
  71.     end
  72.  
  73.     local obj = level.object_by_id(item.item_id)
  74.     local function fill_list(actor,itm)
  75.         if (itm and itm:id() ~= item.item_id) then
  76.             if (self.parts_include and self.parts_include[itm:section()]) or (self.parts_exclude == nil and string.find(obj:section(),itm:section())) or (self.parts_exclude and self.parts_exclude[itm:section()] == nil and string.find(obj:section(),itm:section())) then
  77.                 self:AddItemToList(itm,self.list_box_parts,alun_utils.read_from_ini(nil,itm:section(),"float",nil))
  78.             end
  79.         end
  80.     end
  81.  
  82.     db.actor:iterate_inventory(fill_list,db.actor)
  83. end
  84.  
  85. function cooking_ui:InitControls()
  86.     self:SetWndRect         (Frect():set(0,0,1024,768))
  87.  
  88.     self:SetAutoDelete(true)
  89.  
  90.     self.xml                = CScriptXmlInit()
  91.     local ctrl
  92.     self.xml:ParseFile          ("ui_itm_main.xml")
  93.  
  94.     ctrl                    = CUIWindow()
  95.     self.xml:InitWindow         ("itm_cooking:file_item:main",0,ctrl)
  96.  
  97.     self.file_item_main_sz  = vector2():set(ctrl:GetWidth(),ctrl:GetHeight())
  98.  
  99.     self.xml:InitWindow         ("itm_cooking:file_item:fn",0,ctrl)
  100.     self.file_item_fn_sz    = vector2():set(ctrl:GetWidth(),ctrl:GetHeight())
  101.  
  102.     self.xml:InitWindow         ("itm_cooking:file_item:fd",0,ctrl)
  103.     self.file_item_fd_sz    = vector2():set(ctrl:GetWidth(),ctrl:GetHeight())
  104.  
  105.     self.form               = self.xml:InitStatic("itm_cooking:form",self)
  106.     --self.form:SetWndPos(vector2():set(device().width/2-(self.form:GetWidth()+70), device().height/2 - self.form:GetHeight()))
  107.     self.form:SetWndPos(vector2():set(0, 0))
  108.  
  109.     -- Background for forms
  110.     --self.xml:InitStatic("itm_cooking:form:list_background",self.form)
  111.  
  112.     if (self.use_parts) then
  113.         --self.xml:InitStatic("itm_cooking:form:list_parts_background",self.form)
  114.     end
  115.  
  116.     -- Item picture
  117.     self.picture            = self.xml:InitStatic("itm_cooking:form:icon",self.form)
  118.     self.picture_parts      = self.xml:InitStatic("itm_cooking:form:icon_parts",self.form)
  119.  
  120.     -- cooking tool picture
  121.     self.pic = self.xml:InitStatic("itm_cooking:form:icon_tool",self.form)
  122.  
  123.     local inv_grid_width = alun_utils.read_from_ini(ini,self.section,"inv_grid_width","float",0)
  124.     local inv_grid_height = alun_utils.read_from_ini(ini,self.section,"inv_grid_height","float",0)
  125.     local inv_grid_x = alun_utils.read_from_ini(ini,self.section,"inv_grid_x","float",0)
  126.     local inv_grid_y = alun_utils.read_from_ini(ini,self.section,"inv_grid_y","float",0)
  127.  
  128.     local x1 = inv_grid_x*50
  129.     local y1 = inv_grid_y*50
  130.  
  131.     local w = inv_grid_width*50
  132.     local h = inv_grid_height*50
  133.  
  134.     local x2 = x1 + w
  135.     local y2 = y1 + h
  136.  
  137.     local w,h = w,h
  138.     if (utils.is_widescreen()) then
  139.     w,h = w/1.5,h/1.2
  140.     else
  141.     w,h = w/1.3,h/1.3
  142.     end
  143.     self.pic:InitTexture("ui\\ui_icon_equipment")
  144.     self.pic:SetTextureRect(Frect():set(x1,y1,x2,y2))
  145.     self.pic:SetWndSize(vector2():set(w,h))
  146.  
  147.     if not (self.pic.x) then
  148.         local pos = self.pic:GetWndPos()
  149.         local posform = self.form:GetWndPos()
  150.         self.pic.x = pos.x + posform.x
  151.         self.pic.y = pos.y + posform.y
  152.     end
  153.  
  154.     self.pic:SetWndPos(vector2():set(self.pic.x-w/2, self.pic.y-h/2))
  155.  
  156.     -- Caption
  157.     self.caption_parts      = self.xml:InitTextWnd("itm_cooking:form:caption_parts",self.form)
  158.     self.caption_cooking        = self.xml:InitTextWnd("itm_cooking:form:caption_cooking",self.form)
  159.  
  160.     -- List Box
  161.     self.xml:InitFrame          ("itm_cooking:form:list_frame",self.form)
  162.  
  163.     self.list_box           = self.xml:InitListBox("itm_cooking:form:list",self.form)
  164.  
  165.     self.list_box:ShowSelectedItem  (true)
  166.     self:Register           (self.list_box, "list_window")
  167.  
  168.     if (self.use_parts) then
  169.         -- Parts List Box
  170.         self.list_pos = self.list_box:GetWndPos()
  171.  
  172.         self.list_box_parts         = self.xml:InitListBox("itm_cooking:form:list_parts",self.form)
  173.         --self.list_box_parts:SetWndPos(vector2():set(self.list_pos.x+self.list_box:GetWidth()+5, self.list_pos.y))
  174.  
  175.         local frame = self.xml:InitFrame("itm_cooking:form:list_frame_parts",self.form)
  176.         --frame:SetWndPos(vector2():set(self.list_pos.x+self.list_box:GetWidth()+5, self.list_pos.y))
  177.  
  178.         self.list_box_parts:ShowSelectedItem(true)
  179.         self:Register(self.list_box_parts, "list_window_parts")
  180.     else
  181.         --self.form:SetWndSize(vector2():set(self.list_box:GetWidth()+35, self.form:GetHeight()))
  182.     end
  183.     -- Button cooking
  184.     ctrl                    = self.xml:Init3tButton("itm_cooking:form:btn_cooking", self.form)
  185.     self:Register           (ctrl, "button_cooking")
  186.  
  187.     -- Button Cancel
  188.     ctrl = self.xml:Init3tButton    ("itm_cooking:form:btn_cancel", self.form)
  189.     self:Register           (ctrl, "button_back")
  190. end
  191.  
  192. function cooking_ui:InitCallBacks()
  193.     self:AddCallback("button_cooking",      ui_events.BUTTON_CLICKED,         self.OnButton_cooking,            self)
  194.     self:AddCallback("button_back",     ui_events.BUTTON_CLICKED,             self.OnButton_back_clicked,   self)
  195.  
  196.     self:AddCallback("list_window", ui_events.LIST_ITEM_CLICKED,              self.OnListItemClicked,       self)
  197.     self:AddCallback("list_window", ui_events.WINDOW_LBUTTON_DB_CLICK,        self.OnListItemDbClicked,     self)
  198.  
  199.     self:AddCallback("list_window_parts", ui_events.LIST_ITEM_CLICKED,        self.OnPartsListItemClicked,      self)
  200.     self:AddCallback("list_window_parts", ui_events.WINDOW_LBUTTON_DB_CLICK,  self.OnPartsListItemDbClicked,    self)
  201. end
  202.  
  203. function cooking_ui:OnPartsListItemClicked()
  204.     if self.list_box_parts:GetSize()==0 then return end
  205.  
  206.     local item = self.list_box_parts:GetSelectedItem()
  207.     if not (item) then
  208.         self.picture_parts:SetTextureRect(Frect():set(0,0,0,0))
  209.         self.caption_parts:SetText("")
  210.         return
  211.     end
  212.  
  213.     local se_item = item.item_id and alife():object(item.item_id)
  214.     if (se_item == nil or not db.actor:object(se_item:section_name())) then
  215.         self.list_box_parts:RemoveItem(item)
  216.         return
  217.     end
  218.  
  219.     local sec = se_item:section_name()
  220.  
  221.     local w,h = item.width,item.height
  222.     if (utils.is_widescreen()) then
  223.     w,h = item.width/1.5,item.height/1.2
  224.     else
  225.     w,h = item.width/1.2,item.height/1.2
  226.     end
  227.     self.picture_parts:InitTexture("ui\\ui_icon_equipment")
  228.     self.picture_parts:SetTextureRect(Frect():set(item.x1,item.y1,item.x2,item.y2))
  229.     self.picture_parts:SetWndSize(vector2():set(w,h))
  230.  
  231.     if not (self.picture_parts.x) then
  232.         local pos = self.picture_parts:GetWndPos()
  233.         self.picture_parts.x = pos.x
  234.         self.picture_parts.y = pos.y
  235.     end
  236.  
  237.     self.picture_parts:SetWndPos(vector2():set(self.picture_parts.x-w/2, self.picture_parts.y-h/2))
  238.  
  239.     self.caption_parts:SetText("Selected")
  240. end
  241.  
  242. function cooking_ui:OnPartsListItemDbClicked()
  243.     self:OnButton_cooking()
  244. end
  245.  
  246. function cooking_ui:OnListItemClicked()
  247.     if self.list_box:GetSize()==0 then return end
  248.  
  249.     local item = self.list_box:GetSelectedItem()
  250.  
  251.     if not (item) then
  252.         self.picture:SetTextureRect(Frect():set(0,0,0,0))
  253.         return
  254.     end
  255.  
  256.     local se_item = alife():object(item.item_id)
  257.     if (se_item == nil or not db.actor:object(se_item:section_name())) then
  258.         self.list_box:RemoveItem(item)
  259.         return
  260.     end
  261.  
  262.     local w,h = item.width,item.height
  263.     if (utils.is_widescreen()) then
  264.     w,h = item.width/1.5,item.height/1.2
  265.     else
  266.     w,h = item.width/1.2,item.height/1.2
  267.     end
  268.     self.picture:InitTexture("ui\\ui_icon_equipment")
  269.     self.picture:SetTextureRect(Frect():set(item.x1,item.y1,item.x2,item.y2))
  270.     self.picture:SetWndSize(vector2():set(w,h))
  271.  
  272.     if not (self.picture.x) then
  273.         local pos = self.picture:GetWndPos()
  274.         self.picture.x = pos.x
  275.         self.picture.y = pos.y
  276.     end
  277.  
  278.     self.picture:SetWndPos(vector2():set(self.picture.x-w/2, self.picture.y-h/2))
  279.  
  280.     self.caption_cooking:SetText("Selected")
  281.  
  282.     if (self.use_parts) then
  283.         self.picture_parts:SetTextureRect(Frect():set(0,0,0,0))
  284.         self.caption_parts:SetText("")
  285.         self:FillPartsList()
  286.     end
  287. end
  288.  
  289. function cooking_ui:OnButton_back_clicked()
  290.     alife():create(self.section,db.actor:position(),db.actor:level_vertex_id(),db.actor:game_vertex_id(),db.actor:id())
  291.     self:HideDialog()
  292. end
  293.  
  294. function cooking_ui:OnKeyboard(dik, keyboard_action)
  295.     CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
  296.     if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then
  297.         if (dik == DIK_keys.DIK_RETURN) then
  298.  
  299.         elseif (dik == DIK_keys.DIK_ESCAPE) then
  300.             self:OnButton_back_clicked()
  301.         end
  302.     end
  303.     return true
  304. end
  305.  
  306. -- Drengor - multi-cook
  307. function cooking_ui:GetPartCount(part)
  308.     local cnt = 0
  309.     local part_name = part:section_name()
  310.  
  311.     local function countparts(npc, item)
  312.         if item:section() == part_name then
  313.             cnt = cnt + 1
  314.         end
  315.     end
  316.  
  317.     db.actor:iterate_inventory(countparts, nil)
  318.  
  319.     return cnt
  320. end
  321.  
  322. function cooking_ui:GetFuelCharges(fuel_item)
  323.     -- fuel_name begins with fuel type, and optionally has a 2-8 on the end indicating reduced charges
  324.     local fuel_name = fuel_item:section_name()
  325.     local last_char = fuel_name:byte(fuel_name:len())
  326.     local base_charges = 1
  327.     local charge_adjust = 0
  328.  
  329.     if(last_char > 49) and (last_char < 57) then
  330.         charge_adjust = last_char - 49
  331.     end
  332.  
  333.     if(alun_utils.startsWith(fuel_name,"kerosene")) then base_charges = 5 end
  334.     if(alun_utils.startsWith(fuel_name,"charcoal")) then base_charges = 3 end
  335.     if(alun_utils.startsWith(fuel_name,"explo_jerrycan_fuel"))  then base_charges = 8 end
  336.     if(alun_utils.startsWith(fuel_name,"explo_balon_gas"))  then base_charges = 8 end
  337.  
  338.     return base_charges - charge_adjust
  339. end
  340.  
  341. function cooking_ui:MakeFuelCharges(fuel_item,charges)
  342.     -- fuel_name begins with fuel type, and optionally has a 2-8 on the end indicating reduced charges, or _empty
  343.     local fuel_name = fuel_item:section_name()
  344.     local last_char = fuel_name:byte(fuel_name:len())
  345.     local base_charges = 1
  346.  
  347.     if(last_char > 49) and (last_char < 57) then
  348.         fuel_name = fuel_name:sub(1,fuel_name:len()-1)
  349.     end
  350.  
  351.     if(fuel_name == "kerosene") then base_charges = 5 end
  352.     if(fuel_name == "charcoal") then base_charges = 3 end
  353.     if(fuel_name == "explo_jerrycan_fuel") then base_charges = 8 end
  354.     if(fuel_name == "explo_balon_gas") then base_charges = 8 end
  355.  
  356.     if (charges > 0) then
  357.         fuel_name = string.format("%s%d",fuel_name,base_charges + 1 - charges)
  358.     else
  359.         fuel_name = string.format("%s%s",fuel_name,"_empty")
  360.     end
  361.  
  362.     alife():create(fuel_name, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
  363. end
  364.  
  365. function cooking_ui:ConsumeFuel(fuel_item, charges)
  366.     local c = self:GetFuelCharges(fuel_item) - charges
  367.     local fuel_name = fuel_item:section_name()
  368.  
  369.     if(c > 0 or (alun_utils.startsWith(fuel_name,"explo_jerrycan_fuel") or alun_utils.startsWith(fuel_name,"explo_balon_gas"))) then
  370.         self:MakeFuelCharges(fuel_item,c)
  371.     end
  372.  
  373.     alife():release(fuel_item,true)
  374. end
  375.  
  376. function cooking_ui:ConsumePart(part, count)
  377.     local part_name = part:section_name()
  378.     local cnt = 0
  379.  
  380.     local function consumeparts(npc, item)
  381.         if cnt < count and item:section() == part_name then
  382.             alife():release(alife():object(item:id()),true)
  383.             cnt = cnt + 1
  384.         end
  385.     end
  386.  
  387.     db.actor:iterate_inventory(consumeparts, nil)
  388. end
  389.  
  390. function cooking_ui:CreateMeal(part_name,count)
  391.     for i=1,count,1 do
  392.         alife():create(part_name, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
  393.     end
  394. end
  395. -- end Drengor - multi-cook
  396.  
  397. function cooking_ui:OnButton_cooking()
  398.     index = self.list_box_parts:GetSelectedIndex()
  399.     if (index ~= -1) then
  400.         item = self.list_box_parts:GetItemByIndex(index)
  401.         local se_parts = item and item.item_id and alife():object(item.item_id)
  402.         if not (se_parts) then
  403.             return
  404.         end
  405.     end
  406.  
  407.     local index = self.list_box:GetSelectedIndex()
  408.     if index == -1 then return end
  409.     local item  = self.list_box:GetItemByIndex(index)
  410.  
  411.     local obj = item and level.object_by_id(item.item_id)
  412.     local se_obj = item and item.item_id and alife():object(item.item_id)
  413.  
  414.     item = self.list_box_parts:GetItemByIndex(index)
  415.     local se_parts = item and item.item_id and alife():object(item.item_id)
  416.     local sec = se_parts:section_name()
  417.  
  418.     -- Drengor - multi-cook
  419.     local part_count = self:GetPartCount(se_parts)
  420.     local fuel_charges = self:GetFuelCharges(se_obj)
  421.     local cook_count = 1
  422.  
  423.     if(part_count >= fuel_charges) then cook_count = fuel_charges end
  424.     if(fuel_charges > part_count) then cook_count = part_count end
  425.     -- end Drengor - multi-cook
  426.  
  427.     if not (obj) then
  428.         return
  429.     end
  430.  
  431.     if (self.list_box_parts) then
  432.         index = self.list_box_parts:GetSelectedIndex()
  433.         if (index ~= -1) then
  434.             item = self.list_box_parts:GetItemByIndex(index)
  435.             local se_parts = item and item.item_id and alife():object(item.item_id)
  436.             if (se_parts) then
  437.                 local sec = se_parts:section_name()
  438.  
  439.                 -- ponney68 cooking item
  440.                 if self.section =="fieldcooker" then
  441.                 alife():create("fieldcooker", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
  442.                 end
  443.                 if self.section =="wood_stove" then
  444.                 alife():create("wood_stove", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
  445.                 end
  446.                 -- End ponney68
  447.  
  448.                 -- Drengor - multi-cook
  449.                 -- mutant_part_tushkano_meat
  450.                 if(sec=="mutant_part_tushkano_meat")then self:CreateMeal("meat_tushkano",cook_count) end
  451.                 -- mutant_part_flesh_meat
  452.                 if(sec=="mutant_part_flesh_meat")then self:CreateMeal("meat_flesh",cook_count) end
  453.                 -- mutant_part_boar_chop
  454.                 if(sec=="mutant_part_boar_chop")then self:CreateMeal("meat_boar",cook_count) end
  455.                 -- mutant_part_dog_meat
  456.                 if(sec=="mutant_part_dog_meat")then self:CreateMeal("meat_dog",cook_count) end
  457.                 -- mutant_part_psevdodog_meat
  458.                 if(sec=="mutant_part_psevdodog_meat")then self:CreateMeal("meat_pseudodog",cook_count) end
  459.                 -- mutant_part_krovosos_meat
  460.                 if(sec=="mutant_part_krovosos_meat")then self:CreateMeal("meat_bloodsucker",cook_count) end
  461.                 -- mutant_part_snork_hand
  462.                 if(sec=="mutant_part_snork_hand")then self:CreateMeal("meat_snork",cook_count) end
  463.                 -- mutant_part_chimera_meat
  464.                 if(sec=="mutant_part_chimera_meat")then self:CreateMeal("meat_chimera",cook_count) end
  465.                 -- mutant_part_crow_beak
  466.                 if(sec=="mutant_part_crow_beak")then self:CreateMeal("drink_crow",cook_count) end
  467.  
  468.                 self:ConsumeFuel(se_obj,cook_count)
  469.                 self:ConsumePart(se_parts,cook_count)
  470.                 -- end Drengor - multi-cook
  471.             end
  472.         end
  473.     end
  474.  
  475.     self:HideDialog()
  476.  
  477.     if (self.use_actor_effects and actor_effects) then
  478.         actor_effects.use_item(self.section.."_dummy")
  479.         level_weathers.get_weather_manager():forced_weather_change()
  480.     end
  481. end
  482.  
  483. function cooking_ui:AddItemToList(item,listbox)
  484.  
  485.     local _itm          = load_item(self.file_item_main_sz.y)
  486.     local sec = item and item:section()
  487.     local inv_name      = item and game.translate_string(alun_utils.read_from_ini(nil,sec,"inv_name","string","error")) or "none"
  488.  
  489.     _itm:SetWndSize     (self.file_item_main_sz)
  490.  
  491.     _itm.fn:SetWndPos(vector2():set(0,0))
  492.     _itm.fn:SetWndSize  (self.file_item_fn_sz)
  493.     _itm.fn:SetText     (inv_name)
  494.  
  495.     if (item) then
  496.         _itm.item_id = item:id()
  497.  
  498.         local inv_grid_width = alun_utils.read_from_ini(ini,sec,"inv_grid_width","float",0)
  499.         local inv_grid_height = alun_utils.read_from_ini(ini,sec,"inv_grid_height","float",0)
  500.         local inv_grid_x = alun_utils.read_from_ini(ini,sec,"inv_grid_x","float",0)
  501.         local inv_grid_y = alun_utils.read_from_ini(ini,sec,"inv_grid_y","float",0)
  502.  
  503.         _itm.x1 = inv_grid_x*50
  504.         _itm.y1 = inv_grid_y*50
  505.  
  506.         _itm.width = inv_grid_width*50
  507.         _itm.height = inv_grid_height*50
  508.  
  509.         _itm.x2 = _itm.x1 + _itm.width
  510.         _itm.y2 = _itm.y1 + _itm.height
  511.     end
  512.  
  513.     --[[
  514.     _itm.picture = self.xml:InitStatic("itm_cooking:form:picture",self.form)
  515.     _itm.picture:InitTexture("ui\\ui_icon_equipment")
  516.     _itm.picture:SetTextureRect(Frect():set(_itm.x1,_itm.y1,_itm.x2,_itm.y2))
  517.     _itm.picture:SetWndSize(vector2():set(inv_grid_width*50,inv_grid_height*50))
  518.     --]]
  519.  
  520.     listbox:AddExistingItem(_itm)
  521. end
Advertisement
Add Comment
Please, Sign In to add comment