View difference between Paste ID: 0qmpV8bc and sq1E6FVk
SHOW: | | - or go back to the newest paste.
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")
239+
	self.caption_parts:SetText(string.format("Selected (x%d)",self:GetPartCount(sec)))
240
241
end
242
243
function cooking_ui:OnPartsListItemDbClicked()
244
	self:OnButton_cooking()
245
end
246
247
function cooking_ui:OnListItemClicked()
248
	if self.list_box:GetSize()==0 then return end
249
250
	local item = self.list_box:GetSelectedItem()
251
252
	if not (item) then
253
		self.picture:SetTextureRect(Frect():set(0,0,0,0))
254
		return
255
	end
256
257
	local se_item = alife():object(item.item_id)
258
	if (se_item == nil or not db.actor:object(se_item:section_name())) then
259
		self.list_box:RemoveItem(item)
260
		return
261
	end
262
263
	local w,h = item.width,item.height
264
	if (utils.is_widescreen()) then
265
	w,h = item.width/1.5,item.height/1.2
266
	else
267
	w,h = item.width/1.2,item.height/1.2
268
	end
269
	self.picture:InitTexture("ui\\ui_icon_equipment")
270
	self.picture:SetTextureRect(Frect():set(item.x1,item.y1,item.x2,item.y2))
271
	self.picture:SetWndSize(vector2():set(w,h))
272
273
	if not (self.picture.x) then
274
		local pos = self.picture:GetWndPos()
275
		self.picture.x = pos.x
276
		self.picture.y = pos.y
277
	end
278
279
	self.picture:SetWndPos(vector2():set(self.picture.x-w/2, self.picture.y-h/2))
280-
	self.caption_cooking:SetText("Selected")
280+
281
	self.caption_cooking:SetText(string.format("Selected (x%d)",self:GetFuelCharges(se_item)))
282
283
	if (self.use_parts) then
284
		self.picture_parts:SetTextureRect(Frect():set(0,0,0,0))
285
		self.caption_parts:SetText("")
286
		self:FillPartsList()
287
	end
288
end
289
290
function cooking_ui:OnButton_back_clicked()
291
	alife():create(self.section,db.actor:position(),db.actor:level_vertex_id(),db.actor:game_vertex_id(),db.actor:id())
292
	self:HideDialog()
293
end
294
295
function cooking_ui:OnKeyboard(dik, keyboard_action)
296
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
297
	if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then
298
		if (dik == DIK_keys.DIK_RETURN) then
299
300
		elseif (dik == DIK_keys.DIK_ESCAPE) then
301
			self:OnButton_back_clicked()
302
		end
303
	end
304
	return true
305
end
306
307-
function cooking_ui:GetPartCount(part)
307+
308
function cooking_ui:GetPartCount(part_name)
309
	local cnt = 0
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
	alun_utils.printf("Counted %d %s",cnt,part_name);
320
321
	return cnt
322
end
323
324
function cooking_ui:GetFuelCharges(fuel_item)
325
	-- fuel_name begins with fuel type, and optionally has a 2-8 on the end indicating reduced charges
326
	local fuel_name = fuel_item:section_name()
327
	local last_char = fuel_name:byte(fuel_name:len())
328
	local base_charges = 1
329
	local charge_adjust = 0
330
331
	if(last_char > 49) and (last_char < 57) then
332
		charge_adjust = last_char - 49
333
	end
334
335
	if(alun_utils.startsWith(fuel_name,"kerosene")) then base_charges = 5 end
336
	if(alun_utils.startsWith(fuel_name,"charcoal")) then base_charges = 3 end
337
	if(alun_utils.startsWith(fuel_name,"explo_jerrycan_fuel"))	then base_charges = 8 end
338
	if(alun_utils.startsWith(fuel_name,"explo_balon_gas"))	then base_charges = 8 end
339
340
	return base_charges - charge_adjust
341
end
342
343
function cooking_ui:MakeFuelCharges(fuel_item,charges)
344
	-- fuel_name begins with fuel type, and optionally has a 2-8 on the end indicating reduced charges, or _empty
345
	local fuel_name = fuel_item:section_name()
346
	local last_char = fuel_name:byte(fuel_name:len())
347
	local base_charges = 1
348
349
	if(last_char > 49) and (last_char < 57) then
350
		fuel_name = fuel_name:sub(1,fuel_name:len()-1)
351
	end
352
353
	if(fuel_name == "kerosene") then base_charges = 5 end
354
	if(fuel_name == "charcoal") then base_charges = 3 end
355
	if(fuel_name == "explo_jerrycan_fuel") then base_charges = 8 end
356
	if(fuel_name == "explo_balon_gas") then base_charges = 8 end
357
358
	if (charges > 0) then
359
		fuel_name = string.format("%s%d",fuel_name,base_charges + 1 - charges)
360
	else
361
		fuel_name = string.format("%s%s",fuel_name,"_empty")
362
	end
363
364
	alife():create(fuel_name, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
365
end
366
367
function cooking_ui:ConsumeFuel(fuel_item, charges)
368
	local c = self:GetFuelCharges(fuel_item) - charges
369
	local fuel_name = fuel_item:section_name()
370
371
	if(c > 0 or (alun_utils.startsWith(fuel_name,"explo_jerrycan_fuel") or alun_utils.startsWith(fuel_name,"explo_balon_gas"))) then
372
		self:MakeFuelCharges(fuel_item,c)
373
	end
374
375
	alife():release(fuel_item,true)
376
end
377
378
function cooking_ui:ConsumePart(part, count)
379
	local part_name = part:section_name()
380
	local cnt = 0
381
382
	local function consumeparts(npc, item)
383
		if cnt < count and item:section() == part_name then
384
			alife():release(alife():object(item:id()),true)
385
			cnt = cnt + 1
386
		end
387
	end
388
389
	db.actor:iterate_inventory(consumeparts, nil)
390
end
391
392
function cooking_ui:CreateMeal(part_name,count)
393
	for i=1,count,1 do
394
		alife():create(part_name, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
395
	end
396
end
397
-- end Drengor - multi-cook
398
399
function cooking_ui:OnButton_cooking()
400
	index = self.list_box_parts:GetSelectedIndex()
401
	if (index ~= -1) then
402
		item = self.list_box_parts:GetItemByIndex(index)
403
		local se_parts = item and item.item_id and alife():object(item.item_id)
404
		if not (se_parts) then
405
			return
406
		end
407
	end
408
409
	local index = self.list_box:GetSelectedIndex()
410
 	if index == -1 then return end
411
	local item  = self.list_box:GetItemByIndex(index)
412
413
	local obj = item and level.object_by_id(item.item_id)
414-
	item = self.list_box_parts:GetItemByIndex(index)
414+
415-
	local se_parts = item and item.item_id and alife():object(item.item_id)
415+
416-
	local sec = se_parts:section_name()
416+
417
		return
418-
	-- Drengor - multi-cook
418+
419-
	local part_count = self:GetPartCount(se_parts)
419+
420-
	local fuel_charges = self:GetFuelCharges(se_obj)
420+
421-
	local cook_count = 1
421+
422
			index = self.list_box_parts:GetSelectedIndex()
423-
	if(part_count >= fuel_charges) then cook_count = fuel_charges end
423+
424-
	if(fuel_charges > part_count) then cook_count = part_count end
424+
425-
	-- end Drengor - multi-cook
425+
			local sec = se_parts:section_name()
426
427
			if (se_parts) then
428
429
				-- Drengor - multi-cook
430
				local part_count = self:GetPartCount(sec)
431
				local fuel_charges = self:GetFuelCharges(se_obj)
432-
		index = self.list_box_parts:GetSelectedIndex()
432+
				local cook_count = part_count
433
434
				if(part_count >= fuel_charges) then cook_count = fuel_charges end
435
				-- end Drengor - multi-cook
436
437-
				local sec = se_parts:section_name()
437+
438
				if self.section =="fieldcooker" then
439
				alife():create("fieldcooker", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
440
				end
441
				if self.section =="wood_stove" then
442
				alife():create("wood_stove", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
443
				end
444
				-- End ponney68
445
446
				-- Drengor - multi-cook
447
				-- mutant_part_tushkano_meat
448
				if(sec=="mutant_part_tushkano_meat")then self:CreateMeal("meat_tushkano",cook_count) end
449
				-- mutant_part_flesh_meat
450
				if(sec=="mutant_part_flesh_meat")then self:CreateMeal("meat_flesh",cook_count) end
451
				-- mutant_part_boar_chop
452
				if(sec=="mutant_part_boar_chop")then self:CreateMeal("meat_boar",cook_count) end
453
				-- mutant_part_dog_meat
454
				if(sec=="mutant_part_dog_meat")then self:CreateMeal("meat_dog",cook_count) end
455
				-- mutant_part_psevdodog_meat
456
				if(sec=="mutant_part_psevdodog_meat")then self:CreateMeal("meat_pseudodog",cook_count) end
457
				-- mutant_part_krovosos_meat
458
				if(sec=="mutant_part_krovosos_meat")then self:CreateMeal("meat_bloodsucker",cook_count) end
459
				-- mutant_part_snork_hand
460
				if(sec=="mutant_part_snork_hand")then self:CreateMeal("meat_snork",cook_count) end
461
				-- mutant_part_chimera_meat
462
				if(sec=="mutant_part_chimera_meat")then self:CreateMeal("meat_chimera",cook_count) end
463
				-- mutant_part_crow_beak
464
				if(sec=="mutant_part_crow_beak")then self:CreateMeal("drink_crow",cook_count) end
465
466
				self:ConsumeFuel(se_obj,cook_count)
467
				self:ConsumePart(se_parts,cook_count)
468
				-- end Drengor - multi-cook
469
			end
470
		end
471
	end
472
473
	self:HideDialog()
474
475
	if (self.use_actor_effects and actor_effects) then
476
		actor_effects.use_item(self.section.."_dummy")
477
		level_weathers.get_weather_manager():forced_weather_change()
478
	end
479
end
480
481
function cooking_ui:AddItemToList(item,listbox)
482
483
	local _itm			= load_item(self.file_item_main_sz.y)
484
	local sec = item and item:section()
485
	local inv_name 		= item and game.translate_string(alun_utils.read_from_ini(nil,sec,"inv_name","string","error")) or "none"
486
487
	_itm:SetWndSize		(self.file_item_main_sz)
488
489
	_itm.fn:SetWndPos(vector2():set(0,0))
490
	_itm.fn:SetWndSize	(self.file_item_fn_sz)
491
	_itm.fn:SetText		(inv_name)
492
493
	if (item) then
494
		_itm.item_id = item:id()
495
496
		local inv_grid_width = alun_utils.read_from_ini(ini,sec,"inv_grid_width","float",0)
497
		local inv_grid_height = alun_utils.read_from_ini(ini,sec,"inv_grid_height","float",0)
498
		local inv_grid_x = alun_utils.read_from_ini(ini,sec,"inv_grid_x","float",0)
499
		local inv_grid_y = alun_utils.read_from_ini(ini,sec,"inv_grid_y","float",0)
500
501
		_itm.x1 = inv_grid_x*50
502
		_itm.y1 = inv_grid_y*50
503
504
		_itm.width = inv_grid_width*50
505
		_itm.height = inv_grid_height*50
506
507
		_itm.x2 = _itm.x1 + _itm.width
508
		_itm.y2 = _itm.y1 + _itm.height
509
	end
510
511
	--[[
512
	_itm.picture = self.xml:InitStatic("itm_cooking:form:picture",self.form)
513
	_itm.picture:InitTexture("ui\\ui_icon_equipment")
514
	_itm.picture:SetTextureRect(Frect():set(_itm.x1,_itm.y1,_itm.x2,_itm.y2))
515
	_itm.picture:SetWndSize(vector2():set(inv_grid_width*50,inv_grid_height*50))
516
	--]]
517
518
	listbox:AddExistingItem(_itm)
519
end