Advertisement
Xetrill

ui_main_menu.script

May 26th, 2014
1,551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.99 KB | None | 0 0
  1. -- File:        UI_MAIN_MENU.SCRIPT
  2. -- Description: Load Dialog for STALKER
  3. -- Created:  28.10.2004
  4. -- Lasd edit:   18.01.2006
  5. -- Copyright:   2004 GSC Game World
  6. -- Author:    Serhiy Vynnychenko (narrator@gsc-game.kiev.ua)
  7. -- Version:  0.9
  8.  
  9. ---------------------->> CoP style load mod
  10. -- local press_any_key_on_start = false
  11. -- local sound_volume_eff, sound_volume_music = 1.0, 1.0
  12.  
  13. -- local function toggleReadyMenu()
  14. --  local con = get_console()
  15.  
  16. --  if level.present() and press_any_key_on_start then
  17. --      level.hide_indicators()
  18.  
  19. --      self.start_btn = CUIStatic()
  20. --      self.start_btn:SetWndRect(Frect():set(0, 0, 1024, 768))
  21. --      self.start_btn:InitTexture("ui\\ui_load")
  22. --      self.start_btn:SetText("Ready?")
  23. --      self.start_btn:SetTextAlign(CGameFont.alCenter)
  24. --      self.start_btn:SetTextY(695)
  25. --      self.start_btn:SetFont(GetFontGraffiti19Russian())
  26. --      -- self.alpha = 0
  27. --      self:AttachChild(self.start_btn)
  28. --      self:Show(false)
  29.  
  30. --      sound_volume_eff = con:get_float("snd_volume_eff")
  31. --      con:execute("snd_volume_eff 0.0")
  32. --      sound_volume_music = con:get_float("snd_volume_music")
  33. --      con:execute("snd_volume_music 1.0")
  34.  
  35. --      press_any_key_on_start = false
  36. --  end
  37.  
  38. --  if not press_any_key_on_start and self.start_btn then
  39. --      level.show_indicators()
  40.  
  41. --      console:execute("snd_volume_eff " .. sound_volume_eff)
  42. --      console:execute("snd_volume_music " .. sound_volume_music)
  43. --      sound_volume_eff, sound_volume_music = nil, nil
  44. --      self.start_btn = nil
  45. --  end
  46. -- end
  47. -- local timer = 0
  48.  
  49. -- local function f1() return timer < time_global() end
  50.  
  51. -- local function f2()
  52. --  set_inactivate_input_time(1)
  53. --  get_console():execute("main_menu 1")
  54. --  timer = time_global() + 200
  55. --  level.add_call(f1, f3)
  56. -- end
  57.  
  58. -- local function f3()
  59. --  local console = get_console()
  60. --  console:hide()
  61. --  press_any_key_on_start = true
  62. --  level.show_indicators()
  63. --  console:execute("snd_volume_eff " .. sound_volume_eff)
  64. --  console:execute("snd_volume_music " .. sound_volume_music)
  65. --  sound_volume_eff, sound_volume_music = nil, nil
  66. -- end
  67.  
  68. -- function start()
  69. --  if has_alife_info("mar_intro_start") then
  70. --      timer = time_global() + 500
  71. --      level.add_call(f1, f2)
  72. --  else
  73. --      press_any_key_on_start = true
  74. --  end
  75.  
  76. --  return true
  77. -- end
  78. ----------------------<< CoP style load mod
  79.  
  80. local xs_devmode = false
  81. local xs_state   = {}
  82.  
  83. class "main_menu" (CUIScriptWnd)
  84.  
  85. function main_menu:__init() super()
  86.     self.mbox_mode = 0
  87.     self:InitControls()
  88.     self:InitCallBacks()
  89.     xr_s.on_main_menu_on()
  90. end
  91.  
  92. function main_menu:InitControls()
  93.     self:SetWndRect                 (Frect():set(0,0,1024,768))
  94.  
  95.     local xml                       = CScriptXmlInit()
  96.     xml:ParseFile                   ("ui_mm_main.xml")
  97.  
  98.     xml:InitStatic                  ("background", self)
  99.     self.shniaga                    = xml:InitMMShniaga("shniaga_wnd",self);
  100.  
  101.     self.message_box                = CUIMessageBoxEx()
  102.     self:Register                   (self.message_box, "msg_box")
  103.  
  104.     local _ver = xml:InitStatic     ("static_version",self)
  105.     local mm                        = _G.main_menu.get_main_menu()
  106.     _ver:SetText                    ("v" .. mm:GetGSVer())
  107.  
  108. end
  109.  
  110. function main_menu:Show(f)
  111.     self.shniaga:SetVisibleMagnifier(f)
  112. end
  113.  
  114. function main_menu:InitCallBacks()
  115.     self:AddCallback("btn_novice",       ui_events.BUTTON_CLICKED,                 self.OnButton_new_novice_game,     self)
  116.     self:AddCallback("btn_stalker",      ui_events.BUTTON_CLICKED,                 self.OnButton_new_stalker_game,    self)
  117.     self:AddCallback("btn_veteran",      ui_events.BUTTON_CLICKED,                 self.OnButton_new_veteran_game,    self)
  118.     self:AddCallback("btn_master",       ui_events.BUTTON_CLICKED,                 self.OnButton_new_master_game,     self)
  119.     self:AddCallback("btn_spawn",        ui_events.BUTTON_CLICKED,                 self.OnButton_load_spawn,          self)
  120.     self:AddCallback("btn_options",      ui_events.BUTTON_CLICKED,                 self.OnButton_options_clicked,     self)
  121.     self:AddCallback("btn_load",         ui_events.BUTTON_CLICKED,                 self.OnButton_load_clicked,        self)
  122.     self:AddCallback("btn_save",         ui_events.BUTTON_CLICKED,                 self.OnButton_save_clicked,        self)
  123.     self:AddCallback("btn_multiplayer",  ui_events.BUTTON_CLICKED,                 self.OnButton_multiplayer_clicked, self)
  124.     self:AddCallback("btn_quit",         ui_events.BUTTON_CLICKED,                 self.OnButton_quit_clicked,        self)
  125.     self:AddCallback("btn_quit_to_mm",   ui_events.BUTTON_CLICKED,                 self.OnButton_disconnect_clicked,  self)
  126.     self:AddCallback("btn_ret",          ui_events.BUTTON_CLICKED,                 self.OnButton_return_game,         self)
  127.     self:AddCallback("btn_lastsave",     ui_events.BUTTON_CLICKED,                 self.OnButton_last_save,           self)
  128.     self:AddCallback("btn_credits",      ui_events.BUTTON_CLICKED,                 self.OnButton_credits_clicked,     self)
  129.     self:AddCallback("msg_box",          ui_events.MESSAGE_BOX_OK_CLICKED,         self.OnMsgOk,                      self)
  130.     self:AddCallback("msg_box",          ui_events.MESSAGE_BOX_CANCEL_CLICKED,     self.OnMsgCancel,                  self)
  131.     self:AddCallback("msg_box",          ui_events.MESSAGE_BOX_YES_CLICKED,        self.OnMsgYes,                     self)
  132.     self:AddCallback("msg_box",          ui_events.MESSAGE_BOX_NO_CLICKED,         self.OnMsgNo,                      self)
  133.     self:AddCallback("msg_box",          ui_events.MESSAGE_BOX_QUIT_GAME_CLICKED,  self.OnMessageQuitGame,            self)
  134.     self:AddCallback("msg_box",          ui_events.MESSAGE_BOX_QUIT_WIN_CLICKED,   self.OnMessageQuitWin,             self)
  135. end
  136.  
  137. function main_menu:OnMsgOk()
  138.     self.mbox_mode = 0
  139. end
  140.  
  141. function main_menu:OnMsgCancel()
  142.     self.mbox_mode = 0
  143. end
  144.  
  145. function main_menu:OnMsgYes()
  146.  
  147.     if  self.mbox_mode == 1 then
  148.         self:LoadLastSave()
  149.     end
  150.  
  151.     self.mbox_mode = 0
  152. end
  153.  
  154. function main_menu:OnMsgNo()
  155.     self.mbox_mode = 0
  156. end
  157.  
  158. function main_menu:LoadLastSave()
  159.     local c = get_console()
  160.     c:execute("main_menu off")
  161.     c:execute("load_last_save")
  162. end
  163.  
  164. function main_menu:ContinueLastGame(console)
  165.     local fl = getFS():file_list_open_ex(
  166.         '$game_saves$',
  167.         bit_or(FS.FS_ListFiles, FS.FS_RootOnly),
  168.         '*.sav'
  169.     )
  170.     if fl:Size() <= 0 then
  171.         return
  172.     end
  173.  
  174.     fl:Sort(FS.FS_sort_by_modif_down)
  175.     local name = xs_fs.basename(fl:GetAt(0):NameFull())
  176.     fl = nil
  177.  
  178.     console:execute('main_menu off')
  179.     console:execute('disconnect')
  180.     console:execute(string.format('start server(%s/single/alife/load) client(localhost)', name))
  181. end
  182.  
  183. function main_menu:OnButton_last_save()
  184.     if ( alife() == nil) or (db.actor~=nil and db.actor:alive()==false)  then
  185.         self:LoadLastSave   ()
  186.         return
  187.     end
  188.  
  189.     if ( (db.actor ~= nil) and (db.actor:alive() == false) ) then
  190.         self:LoadLastSave   ()
  191.         return
  192.     end
  193.  
  194.     self.mbox_mode          = 1
  195.     self.message_box:InitMessageBox ("message_box_confirm_load_save")
  196.     self:GetHolder():start_stop_menu(self.message_box, true)
  197. end
  198.  
  199. function main_menu:OnButton_credits_clicked()
  200. --  local console = get_console()
  201. --  console:execute("main_menu off")
  202.     game.start_tutorial("credits_seq")
  203. end
  204.  
  205. function main_menu:OnButton_quit_clicked()
  206.     self.message_box:InitMessageBox("message_box_quit_windows")
  207.     self:GetHolder():start_stop_menu(self.message_box, true)
  208. end
  209.  
  210.  
  211. function main_menu:OnButton_disconnect_clicked()
  212.     self.message_box:InitMessageBox("message_box_quit_game")
  213.  
  214.     if (level.game_id() ~= 1) then
  215.         self.message_box:SetText("ui_mm_disconnect_message")    -- MultiPlayer
  216.     else
  217.         self.message_box:SetText("ui_mm_quit_game_message")     -- SinglePlayer
  218.     end
  219.     self:GetHolder():start_stop_menu(self.message_box, true)
  220. end
  221.  
  222. function main_menu:OnMessageQuitGame()
  223.     local console = get_console()
  224.     console:execute("disconnect")
  225. end
  226.  
  227. function main_menu:OnMessageQuitWin()
  228.     local console = get_console()
  229.     console:execute("quit")
  230. end
  231.  
  232. function main_menu:OnButton_return_game()
  233.     local console = get_console()
  234.     console:execute("main_menu off")
  235.     xr_s.on_main_menu_off()                         --' Distemper 03.2008 --
  236. end
  237.  
  238. function main_menu:OnButton_new_novice_game()
  239.     local console = get_console()
  240.     console:execute("g_game_difficulty gd_novice")
  241.     self:StartGame()
  242. end
  243.  
  244. function main_menu:OnButton_new_stalker_game()
  245.     local console = get_console()
  246.     console:execute("g_game_difficulty gd_stalker")
  247.     self:StartGame()
  248. end
  249.  
  250. function main_menu:OnButton_new_veteran_game()
  251.     local console = get_console()
  252.     console:execute("g_game_difficulty gd_veteran")
  253.     self:StartGame()
  254. end
  255.  
  256. function main_menu:OnButton_new_master_game()
  257.     local console = get_console()
  258.     console:execute("g_game_difficulty gd_master")
  259.     self:StartGame()
  260. end
  261.  
  262. function main_menu:StartGame()
  263.     local console = get_console()
  264.  
  265.     if (alife() ~= nil) then
  266.         console:execute ("disconnect")
  267.     end
  268.     device():pause(false)
  269.     console:execute("start server(all/single/alife/new) client(localhost)")
  270.     console:execute("main_menu off")
  271. end
  272.  
  273. function main_menu:OnButton_load_spawn()
  274.     if self.spawn_dlg == nil then
  275.         self.spawn_dlg = ui_spawn_dialog.spawn_dialog()
  276.         self.spawn_dlg.owner = self
  277.     end
  278.  
  279.     self:GetHolder():start_stop_menu(self.spawn_dlg, true)
  280.     self:GetHolder():start_stop_menu(self, true) --new
  281.     self:Show(false)
  282. end
  283.  
  284. function main_menu:OnButton_save_clicked()
  285.     if self.save_dlg == nil then
  286.         self.save_dlg = ui_save_dialog.save_dialog()
  287.         self.save_dlg.owner = self
  288.     end
  289.  
  290.     self.save_dlg:FillList()
  291.     self:GetHolder():start_stop_menu(self.save_dlg, true)
  292.     self:GetHolder():start_stop_menu(self, true) --new
  293.     self:Show(false)
  294. end
  295.  
  296. function main_menu:OnButton_options_clicked()
  297.     if self.opt_dlg == nil then
  298.         self.opt_dlg = ui_mm_opt_main.options_dialog()
  299.         self.opt_dlg.owner = self
  300.     end
  301.  
  302.     self.opt_dlg:UpdateControls()
  303.     self:GetHolder():start_stop_menu(self.opt_dlg, true)
  304.     self:GetHolder():start_stop_menu(self, true) --new
  305.     self:Show(false)
  306. end
  307.  
  308. function main_menu:OnButton_load_clicked()
  309.     if self.load_dlg ==nil then
  310.         self.load_dlg = ui_load_dialog.load_dialog()
  311.         self.load_dlg.owner = self
  312.     end
  313.  
  314.     self.load_dlg:FillList()
  315.     self:GetHolder():start_stop_menu(self.load_dlg, true)
  316.     self:GetHolder():start_stop_menu(self, true) --new
  317.     self:Show(false)
  318. end
  319.  
  320. function main_menu:OnButton_multiplayer_clicked()
  321.     if self.mp_dlg ==nil then
  322.         self.mp_dlg = ui_mp_main.mp_main()
  323.         self.mp_dlg.owner = self
  324.     end
  325.     self.mp_dlg:UpdateControls()
  326.     self:GetHolder():start_stop_menu(self.mp_dlg, true)
  327.     self:GetHolder():start_stop_menu(self, true) --new
  328.     self.mp_dlg:on_show_script()
  329.     self:Show(false)
  330.  
  331. --  self.mp_dlg:OnBtn_Refresh()
  332.  
  333.     local console           = get_console()
  334.     console:execute         ("check_for_updates 0")
  335.  
  336.     self.mp_dlg:OnRadio_NetChanged()
  337. end
  338.  
  339. function main_menu:Dispatch(cmd, param)  --virtual function
  340.     if cmd == 2 then
  341.         self:OnButton_multiplayer_clicked()
  342.     end
  343.     return true
  344. end
  345.  
  346. ------------------->> CoP style load mod
  347. function main_menu:Update()
  348.     CUIScriptWnd.Update(self)
  349.  
  350.     -- if self.tmr then
  351.     --  self.tmr:stop()
  352.     --  if not self.last_time then self.last_time = self.tmr:time() end
  353.     --  if self.tmr:time() - self.last_time > 7000 then
  354.     --      self.last_time = self.tmr:time()
  355.     --      self.start_btn:SetTextColor(self.alpha,159,142,126)
  356.     --      if self.alpha > 250 then self.step = -2 elseif self.alpha < 60 then self.step = 2 end
  357.     --      self.alpha = self.alpha + self.step
  358.     --  end
  359.     --  self.tmr:start()
  360.     -- end
  361. end
  362. -------------------<< CoP style load mod
  363.  
  364. function main_menu:OnKeyboard(dik, keyboard_action)  --virtual function
  365.     CUIScriptWnd.OnKeyboard(self, dik, keyboard_action)
  366.  
  367.     -- local bind = dik_to_bind(dik)
  368.     local console = get_console()
  369.     local act = db.actor
  370.  
  371.     ------------------->> CoP style load mod
  372.     -- if self.start_btn and dik ~= DIK_keys.DIK_GRAVE then
  373.     --  self:OnButton_return_game()
  374.     --  return true
  375.     -- end
  376.     -------------------<< CoP style load mod
  377.  
  378.     if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
  379.         if dik == DIK_keys.DIK_Q then
  380.             self:OnMessageQuitWin()
  381.  
  382.         elseif dik == DIK_keys.DIK_X then
  383.             xs_devmode = not xs_devmode
  384.             if level.present() and act and act:alive() then
  385.                 xs_game.SendMessage(
  386.                     'Xs',
  387.                     string.format('DevMove %s', xs_devmode and 'Enabled' or 'Disabled'),
  388.                     nil, nil, 4000,
  389.                     [[device\pda\pda_tip]]
  390.                 )
  391.             end
  392.  
  393.         elseif dik == DIK_keys.DIK_T then
  394.             do
  395.                 -- cache Lua globals as upvalues
  396.                 local _G
  397.                     = _G
  398.                 local table, string, io, math, buffer
  399.                     = table, string, io, math, buffer
  400.                 local sprintf
  401.                     = string.format
  402.                 local tostring, select, math_clamp, string_rep, type, pairs, pcall, assert,
  403.                       table_sort, table_remove, table_insert, io_open, rawequal, math_huge
  404.                     = tostring, select, math.clamp, string.rep, type, pairs, pcall, assert,
  405.                       table.sort, table.remove, table.insert, io.open, rawequal, math.huge
  406.                 -- cache CS globals as upvalues
  407.                 local level, sound_object, abort, vector, user_name, get_console
  408.                     = level, sound_object, abort, vector, user_name, get_console
  409.                 local level_present, sound_object_s2d
  410.                     = level.present, sound_object.s2d
  411.                 -- cache Lua lib globals as upvalues
  412.                 local xs_fs, xs_extend
  413.                     = xs_fs, xs_extend
  414.                 local xs_fs_gamePath, xs_extend_export
  415.                     = xs_fs.gamePath, xs_extend.export
  416.  
  417.                 local function beep(n)
  418.                     if not level_present() then
  419.                         return
  420.                     end
  421.                     local s = sound_object(sprintf([[device\pda\%s]], n or 'pda_tip'))
  422.                     s:play_no_feedback(act, sound_object_s2d, 0, vector(), 1.0)
  423.                     s = nil
  424.                 end
  425.  
  426.                 local __marks  = 0
  427.                 local __indent = { 0, '' }
  428.                 local __logfile, e = io_open(
  429.                     xs_fs_gamePath('$logs$', sprintf('Xs_%s.log', user_name():lower())),
  430.                     'wb')
  431.                 if not __logfile then
  432.                     beep('pda_alarm')
  433.                     abort(e)
  434.                 end
  435.  
  436.                 --============================================================--
  437.                 --=== NOTICE ME ================================ NOTICE ME ===--
  438.                 --=== NOTICE ME ================================ NOTICE ME ===--
  439.                 --=== NOTICE ME ================================ NOTICE ME ===--
  440.                 --============================================================--
  441.                 __logfile:setvbuf('no')
  442.                 --============================================================--
  443.                 --============================================================--
  444.  
  445.                 local function print_impl(count, ...)
  446.                     if __logfile and count > 0 then
  447.                         for i = 1, count do
  448.                             __logfile:write(tostring(select(i, ...)))
  449.                         end
  450.                     end
  451.                 end
  452.  
  453.                 local function adjust_indent(num)
  454.                     __indent[1] = math_clamp(num, 0, 100)
  455.                     __indent[2] = string_rep('  ', num)
  456.                 end
  457.  
  458.                 local function indent()
  459.                     adjust_indent(1)
  460.                 end
  461.  
  462.                 local function unindent()
  463.                     adjust_indent(-1)
  464.                 end
  465.  
  466.                 local function print(...)
  467.                     local l = select('#', ...)
  468.                     if l > 0 then
  469.                         print_impl(l, ...)
  470.                     end
  471.                 end
  472.  
  473.                 local function printf(s, ...)
  474.                     if select('#', ...) > 0 then
  475.                         print_impl(2, s:format(...))
  476.                     else
  477.                         print_impl(1, s)
  478.                     end
  479.                 end
  480.  
  481.                 local function println(...)
  482.                     local l = select('#', ...)
  483.                     if l > 0 then
  484.                         if __indent[1] > 0 then
  485.                             print_impl(l + 1, __indent[2], ...)
  486.                         else
  487.                             print_impl(l, ...)
  488.                         end
  489.                     end
  490.                     print_impl(1, "\n")
  491.                 end
  492.  
  493.                 local function print_mark(desc)
  494.                     __marks = __marks + 1
  495.                     if desc then
  496.                         print_impl(5, "MARK #", tostring(__marks), " (", tostring(desc), ")\n")
  497.                     else
  498.                         print_impl(3, "MARK #", tostring(__marks), "\n")
  499.                     end
  500.                     __logfile:flush()
  501.                 end
  502.  
  503.                 local function printfln(s, ...)
  504.                     if select('#', ...) > 0 then
  505.                         if __indent[1] > 0 then
  506.                             print_impl(3, __indent[2], s:format(...), "\n")
  507.                         else
  508.                             print_impl(2, s:format(...), "\n")
  509.                         end
  510.                     else
  511.                         println(s)
  512.                     end
  513.                 end
  514.  
  515.                 local function flush()
  516.                     if __logfile then
  517.                         __logfile:flush()
  518.                     end
  519.                 end
  520.  
  521.                 local function begin_region(title)
  522.                     print_impl(1, "-------------------------------------------------------------------------------\n")
  523.                     println(title, ":")
  524.                     adjust_indent(1)
  525.                 end
  526.  
  527.                 local function end_region()
  528.                     adjust_indent(-1)
  529.                     print_impl(1, "\n")
  530.                     flush()
  531.                 end
  532.  
  533.                 local function exit(exitcode)
  534.                     if __logfile then
  535.                         __logfile:flush()
  536.                         __logfile:close()
  537.                         __logfile = nil
  538.                     end
  539.                     local c = get_console()
  540.                     c:execute("disconnect")
  541.                     c:execute('quit')
  542.                 end
  543.  
  544.                 local function dump(object, max_depth, description)
  545.                     assert(type(object) == 'table',
  546.                            'argument #1 must be of type table')
  547.                     assert(max_depth == nil or type(max_depth) == 'number',
  548.                            'optional argument #2 must be of type number')
  549.                     assert(description == nil or type(description) == 'string',
  550.                            'optional argument #3 must be of type string')
  551.  
  552.                     max_depth = max_depth or math_huge
  553.  
  554.                     local function _dmp(object, depth, k)
  555.                         if depth > max_depth then
  556.                             return
  557.                         end
  558.  
  559.                         local obj_type = type(object)
  560.                         local indent   = string_rep(" ", depth * 2)
  561.  
  562.                         if obj_type == "table" then
  563.                             printfln("%s%s:", indent, tostring(k))
  564.                             for k, v in pairs(object) do
  565.                                 _dmp(v, depth + 1, k)
  566.                             end
  567.                         elseif obj_type == "userdata" then
  568.                             if rawequal(type(object.__tostring), "function") then
  569.                                 printfln("%s%s: userdata `%s'", indent, tostring(k), tostring(object))
  570.                             elseif not rawequal(object.__cls, nil) then
  571.                                 printfln("%s%s: userdata `%s'", indent, tostring(k), object.__cls)
  572.                             else
  573.                                 printfln("%s%s: userdata *omitted*", indent, tostring(k))
  574.                             end
  575.                         elseif obj_type == "string" then
  576.                             printfln("%s%s: `%s'", indent, tostring(k), object)
  577.                         else
  578.                             printfln("%s%s: %s", indent, tostring(k), tostring(object))
  579.                         end
  580.                     end
  581.  
  582.                     _dmp(object, 0, description or "root")
  583.                 end
  584.  
  585.                 local function loadlib(name, rethrow)
  586.                     assert(rawequal(type(name), 'string'),
  587.                            'argument #1 must be of type string')
  588.  
  589.                     name    = name:lower()
  590.                     rethrow = rethrow or true
  591.                     local status, result = pcall(require, name)
  592.                     if status then
  593.                         if _G[name] == nil then
  594.                             _G[name] = result
  595.                         end
  596.                         return result
  597.                     else
  598.                         printfln("  lib `%s' NOT loaded, see error...", name)
  599.                         println("    ", result)
  600.                         if rethrow == true then
  601.                             flush()
  602.                             abort(result)
  603.                         end
  604.                         return {}
  605.                     end
  606.                 end
  607.  
  608.                 local middleclass = require('middleclass')
  609.  
  610.                 local function_profiler = middleclass('function_profiler')
  611.  
  612.                 function function_profiler:__init(prime_contenders)
  613.                     self.contenders = {}
  614.                     self.prime_contenders = prime_contenders or false
  615.  
  616.                     xs_extend_export()
  617.                 end
  618.  
  619.                 function function_profiler:__tostring()
  620.                     if #self.contenders == 0 then
  621.                         return ''
  622.                     end
  623.  
  624.                     local elapsed_total = 0.0
  625.                     local buffer = buffer.new(240 + 80 * #self.contenders)
  626.                     buffer:len(0)
  627.                     buffer:append('--- sorted results (slowest to fastest) ---------------------------------------\n')
  628.                     for i = 1, #self.contenders do
  629.                         buffer:append_format(
  630.                             "%-2d  %-58s  %8.2fms  %-2d%%\n",
  631.                             i,                             -- rank
  632.                             self.contenders[i][1],         -- name
  633.                             self.contenders[i][3] * 0.001, -- elasped time
  634.                             self.contenders[i][4]          -- percentage
  635.                         )
  636.                         elapsed_total = elapsed_total + self.contenders[i][3]
  637.                     end
  638.                     buffer:rep(79, '=')
  639.                     buffer:append_format("\n--  %-58s  %8.2fms  --%%\n", 'total', elapsed_total)
  640.                     return buffer:tostring()
  641.                 end
  642.  
  643.                 function function_profiler:_prep_summery(lap_count, elapsed_total)
  644.                     for i = 1, #self.contenders do
  645.                         self.contenders[i][4] = self.contenders[i][3] / elapsed_total * 100
  646.                     end
  647.                     table_sort(self.contenders, function (lhs, rhs)
  648.                         return lhs[3] > rhs[3] -- slowest to fastest
  649.                     end)
  650.                 end
  651.  
  652.                 function function_profiler:_add_result(contender_index, elapsed_time)
  653.                     self.contenders[contender_index][3] = elapsed_time
  654.                 end
  655.  
  656.                 function function_profiler:run(lap_count)
  657.                     assert(type(lap_count) == 'number' and lap_count > 0,
  658.                            "argument #1 must be a positive number")
  659.                     assert(#self.contenders >= 2, "need at least 2 contenders")
  660.  
  661.                     local elapsed_total = 0.0
  662.  
  663.                     for i = 1, #self.contenders do
  664.                         do
  665.                             local func  = self.contenders[i][2]
  666.  
  667.                             if self.prime_contenders then
  668.                                 func()
  669.                             end
  670.  
  671.                             local timer = profile_timer()
  672.                             timer:start()
  673.                             for i = 1, lap_count do
  674.                                 func()
  675.                             end
  676.                             timer:stop()
  677.  
  678.                             elapsed_total = elapsed_total + timer:time()
  679.  
  680.                             self:_add_result(i, timer:time())
  681.                         end
  682.                     end
  683.  
  684.                     self:_prep_summery(lap_count, elapsed_total)
  685.                 end
  686.  
  687.                 function function_profiler:reset(retain_contenders)
  688.                     if retain_contenders then
  689.                         for i = 1, #self.contenders do
  690.                             while #self.contenders[i] > 2 do
  691.                                 table_remove(self.contenders[i])
  692.                             end
  693.                         end
  694.                     else
  695.                         self.contenders = {}
  696.                     end
  697.                 end
  698.  
  699.                 function function_profiler:add_contenter(name, func)
  700.                     assert(type(name) == 'string',   'argument #1 must be of type string')
  701.                     assert(type(func) == 'function', 'argument #2 must be of type function')
  702.  
  703.                     for i = 1, #self.contenders do
  704.                         if self.contenders[i][1] == name then
  705.                             abort("argument #1 must be a unique string")
  706.                         end
  707.                     end
  708.  
  709.                     table_insert(self.contenders, { name, func })
  710.                 end
  711.  
  712.                 local function replace_global(name, repl)
  713.                     assert(type(_G[name]) == 'function',
  714.                            'argument #1 must be the name of a global function')
  715.                     assert(type(repl) == 'function',
  716.                            'argument #2 must be a function')
  717.                     _G[name] = _G['__' .. name]
  718.                     _G[name] = repl
  719.                 end
  720.  
  721.                 local function restore_global(name)
  722.                     assert(type(_G[name]) == 'function',
  723.                            'argument #1 must be the name of a global function')
  724.                     local backup = '__' .. name
  725.                     if _G[backup] ~= nil then
  726.                         _G[name]   = _G[backup]
  727.                         _G[backup] = nil
  728.                     end
  729.                 end
  730.  
  731.                 ---------------------------------------------------------------
  732.  
  733.                 -- do
  734.                 --  begin_region('marshal CTime')
  735.  
  736.                 --  game.CTime.__size = 4 * 9 -- guess based on ctime.h tm; most likely wrong
  737.  
  738.                 --  function game_CTime___persist(self)
  739.                 --      local Y, M, D, h, m, s, ms
  740.                 --          = self:get(Y, M, D, h, m, s, ms)
  741.                 --      return function ()
  742.                 --          local t = game.CTime()
  743.                 --          t:set(Y, M, D, h, m, s, ms)
  744.                 --          return t
  745.                 --      end
  746.                 --  end
  747.  
  748.                 --  local t = game.CTime()
  749.                 --  printfln('type(t): %s', type(t)); flush()
  750.  
  751.                 --  local m = getmetatable(t)
  752.                 --  m.__persist = game_CTime___persist
  753.                 --  dump(m, 3, 'm')
  754.  
  755.                 --  debug.memdump(t, game.CTime.__size, 't (default)'); flush()
  756.                 --  t:set(2014, 3, 12, 1, 2, 3, 4)
  757.                 --  debug.memdump(t, game.CTime.__size, 't (today)'); flush()
  758.  
  759.                 --  local s = marshal.encode(t)
  760.                 --  debug.memdump(s, #s, 's'); flush()
  761.  
  762.                 --  local x = marshal.decode(s)
  763.                 --  printfln('type(x): %s', type(x)); flush()
  764.  
  765.                 --  end_region()
  766.                 -- end
  767.  
  768.                 ---------------------------------------------------------------
  769.  
  770.                 -- do
  771.                 --  begin_region('container')
  772.  
  773.                 --  local container = _G.container
  774.                 --  dump(container, 2, '_G.container')
  775.  
  776.                 --  do
  777.                 --      local s = container.new_sorted_set('number')
  778.                 --      dump(getmetatable(s), 2, 'getmetatable(s)')
  779.  
  780.                 --      printfln('size: %d', s:size())
  781.                 --      s:insert(10)
  782.                 --      s:insert(20)
  783.                 --      s:insert(30)
  784.                 --      printfln('#len: %d', #s)
  785.                 --      s:insert(10)
  786.                 --      printfln('#len: %d', #s)
  787.  
  788.                 --      println('values:')
  789.                 --      for n in s:iter() do
  790.                 --          printfln('  value: %.0f; contains: %d', n, tonumber(s:contains(n)))
  791.                 --      end
  792.  
  793.                 --      printfln('s:equals(s): %d', s:equals(s))
  794.                 --  end
  795.  
  796.                 --  end_region()
  797.                 -- end
  798.  
  799.                 ---------------------------------------------------------------
  800.  
  801.                 -- do
  802.                 --  begin_region('error handling')
  803.  
  804.                 --  local function err_lvl0()
  805.                 --      error("error 0", 0)
  806.                 --  end
  807.                 --  local function err_lvl1()
  808.                 --      error("error 1", 1)
  809.                 --  end
  810.                 --  local function err_lvl2()
  811.                 --      error("error 2", 2)
  812.                 --  end
  813.                 --  local function err_root()
  814.                 --      err_lvl0()
  815.                 --      err_lvl1()
  816.                 --      err_lvl2()
  817.                 --      println("done")
  818.                 --  end
  819.  
  820.                 --  err_root()
  821.  
  822.                 --  end_region()
  823.                 -- end
  824.  
  825.                 ---------------------------------------------------------------
  826.  
  827.                 do
  828.                     begin_region('traceback')
  829.  
  830.                     replace_global('print', println)
  831.  
  832.                     local u1
  833.                     local invoker = function (p1)
  834.                         u1       = _G.pcall
  835.                         local l1 = "a local"
  836.                         local tb = function (n) print(debug.traceback_ex(n)) end
  837.                         pcall(tb, 0)
  838.                     end
  839.                     invoker("a param")
  840.  
  841.                     restore_global('print')
  842.  
  843.                     end_region()
  844.                 end
  845.  
  846.                 ---------------------------------------------------------------
  847.  
  848.                 -- do
  849.                 --  begin_region("lxsext_c_tests")
  850.  
  851.                 --  replace_global('print', println)
  852.                 --  dofile(xs_fs.gamePath('$game_scripts$', [[lib\lxsext_c_tests.lua]]))
  853.                 --  -- dofile(xs_fs.gamePath('$game_scripts$', [[lib\tests\literals.lua]]))
  854.                 --  -- dofile(xs_fs.gamePath('$game_scripts$', [[lib\tests\strings.lua]]))
  855.                 --  -- dofile(xs_fs.gamePath('$game_scripts$', [[lib\tests\math.lua]]))
  856.                 --  restore_global('print')
  857.  
  858.                 --  end_region()
  859.                 -- end
  860.  
  861.                 ---------------------------------------------------------------
  862.  
  863.                 flush()
  864.                 beep()
  865.             end
  866.         end
  867.  
  868.         if level.present() and act and act:alive() then
  869.             if dik == DIK_keys.DIK_ESCAPE then
  870.                 self:OnButton_return_game()
  871.  
  872.             elseif dik == DIK_keys.DIK_F1 then
  873.                 level.set_time_factor(1)
  874.                 self:OnButton_return_game()
  875.  
  876.             elseif dik == DIK_keys.DIK_F2 then
  877.                 level.set_time_factor(5)
  878.                 self:OnButton_return_game()
  879.  
  880.             elseif dik == DIK_keys.DIK_F3 then
  881.                 level.set_time_factor(480)
  882.                 self:OnButton_return_game()
  883.  
  884.             elseif dik == DIK_keys.DIK_F4 then
  885.                 level.set_time_factor(1440)
  886.                 self:OnButton_return_game()
  887.             end
  888.  
  889.             if xs_devmode then
  890.                 local adjustMoney = xs_game.GiveOrTakeMoney
  891.                 if dik == DIK_keys.DIK_NUMPADENTER then
  892.                     local items = {
  893.                         "dolg_exo_outfit",
  894.                         "exo_outfit",
  895.                         "merc_exo_outfit",
  896.                         "monolith_exo_outfit",
  897.                         "svoboda_exo_outfit",
  898.                     }
  899.                     for i = 1, #items do
  900.                         amk.spawn_item_in_inv(items[i])
  901.                     end
  902.                     act:give_money(500000)
  903.                     xr_sound.set_sound_play(act:id(), "pda_alarm")
  904.                     -- amk.spawn_ammo_in_inv(section, count)
  905.  
  906.                 elseif dik == DIK_keys.DIK_ADD then
  907.                     adjustMoney(1000, act, true, false)
  908.  
  909.                 elseif dik == DIK_keys.DIK_MULTIPLY then
  910.                     adjustMoney(act:money(), act, true, false)
  911.  
  912.                 elseif dik == DIK_keys.DIK_MINUS then
  913.                     adjustMoney(-1000, act, true, false)
  914.  
  915.                 elseif dik == DIK_keys.DIK_DIVIDE then
  916.                     adjustMoney(-(act:money() * 0.5), act, true, false)
  917.  
  918.                 elseif dik == DIK_keys.DIK_NUMPAD0 then
  919.                     local delta = act:money()
  920.                     if xs_state.money == nil then
  921.                         xs_state.money = delta
  922.                         delta = (-delta) - 1
  923.                     else
  924.                         delta = xs_state.money
  925.                         xs_state.money = nil
  926.                     end
  927.                     adjustMoney(delta, act, true, false)
  928.                 end
  929.             end
  930.         else
  931.             if dik == DIK_keys.DIK_L then
  932.                 self:ContinueLastGame(console)
  933.             end
  934.         end
  935.     end
  936.  
  937.     return true
  938. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement