Advertisement
antogm_

tuto ica 10

Jul 11th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQF 1.94 KB | None | 0 0
  1. /*  tienda_mp.sqf
  2.  *  [true, "objeto"] execVM "tienda_mp.sqf"; -> comprar
  3.  *  [false, "objeto"] execVM "tienda_mp.sqf"; -> vender
  4.  */
  5.  
  6. if (isNil {arr_stock}) then {
  7.     arr_stock = [5, 1, 1, 1, 1, 1];
  8.     publicVariable "arr_stock";
  9. };
  10.  
  11. if (isNil {tienda}) then {
  12.     tienda = [["platano", 100], ["manzana", 200], ["kiwi", 150], ["pera", 160], ["sandia", 190], ["melon", 400]];
  13. };
  14.  
  15. _item = _this select 1;
  16. _comprarOvender = _this select 0;
  17.  
  18. _precio = 0;
  19. _itemvalido = false;
  20. _indc = 0;
  21.  
  22. //Asigna el precio correspondiente a ese objeto y guarda la posición del array en la que se encuentra
  23. {
  24.     if ((_x select 0) == _item)
  25.         then {
  26.             _precio = _x select 1;
  27.             _itemvalido = true;
  28.             _indc = _foreachindex;
  29.         };
  30. }forEach tienda;
  31.  
  32. _stock = arr_stock select _indc;
  33.  
  34. //Si el objeto no existe sale del script
  35. if (!_itemvalido) exitWith {hint "Producto no disponible"};
  36.  
  37. if (_comprarOvender) then {
  38.     //COMPRO
  39.    
  40.     //si no tengo dinero para comprarlo, sale del script
  41.     if (dinero < _precio) exitwith {hint "No tienes suficiente dinero"};
  42.     //si no hay stock, sale del script
  43.     if (_stock <= 0) exitwith {hint format ["No hay stock de %1", _item]};
  44.  
  45.     //resta una unidad al stock de la tienda
  46.     arr_stock set [_indc, (arr_stock select _indc) -1];
  47.     //resta el dinero al comprador
  48.     dinero = dinero - _precio;
  49.     //me añado el item
  50.     missionNamespace setVariable [_item, (missionNamespace getVariable _item) +1];
  51.  
  52.     hint format ["Has comprado un %1 por %2 euros", _item, _precio];
  53.    
  54. } else {
  55.     //VENDO
  56.  
  57.     //Si no tengo el objeto no lo puedo vender / sale de la script
  58.     if (missionNamespace getVariable _item <= 0) exitwith {hint format ["No tienes %1", _item]};
  59.  
  60.     //Me sumo el dinero
  61.     dinero = dinero + _precio;
  62.     //Me resto el item
  63.     missionNamespace setVariable [_item, (missionNamespace getVariable _item) -1];
  64.     //Sumo uno al stock
  65.     arr_stock set [_indc, (arr_stock select _indc) +1];
  66.  
  67.     hint format ["Has vendido un %1 por %2 euros", _item, _precio];
  68. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement