Advertisement
King_ZZ

MapBuyableObjects_drawParts.lua

Nov 18th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.48 KB | None | 0 0
  1. --
  2. --
  3. --      MapBuayableObjetcs_drawPart
  4. --@date: 02/10/2012
  5. --@Author: Bluebaby210      www.mod-portal.com
  6. --@Version: v1.0
  7. --
  8. --darf in jede Map eingebaut werden, MapBuayableObjetcs.zip darf nicht neu hochgeladen werden
  9. --
  10. --Keine Änderung ohne meine Erlaubnis
  11. --No modification without my permission
  12. --
  13.  
  14. masterTriggerAllowed = 0;
  15.  
  16.  
  17. ObjectBuy = {};
  18. ObjectBuy.drawParts = {};
  19. ObjectBuy.drawPartsIcon = {};
  20. ObjectBuy.drawPartsMaster = {};
  21.  
  22. function ObjectBuy:loadMap(name)
  23.  
  24.     self.mapName = g_currentMission.missionInfo.mapId
  25.    
  26.     local dir = getUserProfileAppPath() .. "savegame"..g_currentMission.missionInfo.savegameIndex;
  27.     createFolder(dir);
  28.     self.saveXMLFile = dir .. "/MapBuyableObjects.xml";
  29.     if g_currentMission.missionStats.playTime == 0 then
  30.         self.firstRun = false;
  31.     else
  32.         self.firstRun = true;
  33.     end;
  34.    
  35.     self.triggerUpdate = true;
  36. end;
  37. function ObjectBuy:deleteMap()
  38.  
  39.     if table.getn(ObjectBuy.drawParts) ~= 0 then
  40.         for k=1,table.getn(ObjectBuy.drawParts) do
  41.             ObjectBuy.drawParts[k] = nil;
  42.         end;
  43.     end;
  44.     if table.getn(ObjectBuy.drawPartsIcon) ~= 0 then
  45.         for k=1,table.getn(ObjectBuy.drawPartsIcon) do
  46.             ObjectBuy.drawPartsIcon[k] = nil;
  47.         end;
  48.     end;
  49.     if table.getn(ObjectBuy.drawPartsMaster) ~= 0 then
  50.         for k=1,table.getn(ObjectBuy.drawPartsMaster) do
  51.             ObjectBuy.drawPartsMaster[k] = nil;
  52.         end;
  53.     end;
  54.  
  55. end;
  56.  
  57. function ObjectBuy:mouseEvent(posX, posY, isDown, isUp, button)
  58. end;
  59.  
  60. function ObjectBuy:keyEvent(unicode, sym, modifier, isDown)
  61. end;
  62.  
  63. function ObjectBuy:readStream(streamId, connection)
  64.     self.triggerUpdate = streamReadBool(streamId);
  65.     self.firstRun = streamReadBool(streamId);
  66. end;
  67.  
  68. function ObjectBuy:writeStream(streamId, connection)
  69.     streamWriteBool(streamId, self.triggerUpdate);
  70.     streamWriteBool(streamId, self.firstRun);
  71. end;
  72.  
  73. function ObjectBuy:update(dt)
  74.  
  75.     if g_server ~= nil then
  76.         if self.triggerUpdate then
  77.             for i=1, table.getn(ObjectBuy.drawParts) do
  78.                 ObjectBuy.drawParts[i]:visibility(false, false, 3);
  79.             end;
  80.             self.triggerUpdate = false;
  81.         end;
  82.         if self.firstRun then
  83.             ObjectBuy:loadBuyableObjectFromXML();
  84.             self.firstRun = false;         
  85.         end;
  86.     end;
  87.  
  88. end;
  89.  
  90. function ObjectBuy:draw()
  91.    
  92.     if table.getn(ObjectBuy.drawParts) ~= 0 then
  93.         for i=1, table.getn(ObjectBuy.drawParts) do
  94.             ObjectBuy.drawParts[i]:draw();
  95.         end;
  96.     end;
  97.     if table.getn(ObjectBuy.drawPartsIcon) ~= 0 then
  98.         for i=1, table.getn(ObjectBuy.drawPartsIcon) do
  99.             ObjectBuy.drawPartsIcon[i]:draw();
  100.         end;
  101.     end;
  102.     if table.getn(ObjectBuy.drawPartsMaster) ~= 0 then
  103.         for i=1, table.getn(ObjectBuy.drawPartsMaster) do
  104.             ObjectBuy.drawPartsMaster[i]:draw();
  105.         end;
  106.     end;
  107.  
  108. end;
  109.  
  110. local CareerScreenSaveSelectedGame = CareerScreen.saveSelectedGame;
  111. CareerScreen.saveSelectedGame = function(self)
  112.     CareerScreenSaveSelectedGame(self);
  113.     ObjectBuy:saveBuyableObjectToXML();
  114. end;
  115.  
  116. function ObjectBuy:loadBuyableObjectFromXML()
  117.  
  118.  
  119.     if fileExists(self.saveXMLFile) then
  120.         local xmlFile = loadXMLFile("MapBuyableObjects", self.saveXMLFile);
  121.         local a = Utils.getNoNil(getXMLString(xmlFile, "BuyableObjects#mapIdentifier"), nil);
  122.             if (a == self.mapName) then
  123.                 local c = Utils.getNoNil(getXMLInt(xmlFile, "BuyableObjects#withMapBuyableObjects"), 1);
  124.                 masterTriggerAllowed = c;
  125.                 if c == 1 then
  126.                     for i=1, table.getn(self.drawParts) do
  127.                             local key = ("BuyableObjects."..ObjectBuy.drawParts[i].buyableText.."");
  128.                             ObjectBuy.drawParts[i].newNumber = Utils.getNoNil(getXMLInt(xmlFile, key.."#BuyObject_Buy"),0);
  129.                             ObjectBuy.drawParts[i].newNum = Utils.getNoNil(getXMLInt(xmlFile, key.."#BuyObject_Delete"),1);
  130.                             ObjectBuy.drawParts[i].newTri = Utils.getNoNil(getXMLInt(xmlFile, key.."#Trigger_Buy"),0);
  131.                             ObjectBuy.drawParts[i].newDelTri = Utils.getNoNil(getXMLInt(xmlFile, key.."#Trigger_Delete"),1);
  132.                             ObjectBuy.drawParts[i].newTransObj = Utils.getNoNil(getXMLInt(xmlFile, key.."#Trans_Objects"),0);
  133.                             --savegame laden, wenn savegame nummer ist wie gekauft/nicht da, dann einmal visibilitiy aufrufen um Event zu senden.                  
  134.                             if (ObjectBuy.drawParts[i].newNumber == 1 or ObjectBuy.drawParts[i].newNumber == 3) and (ObjectBuy.drawParts[i].newNum == 0 or ObjectBuy.drawParts[i].newNum == 3) and (ObjectBuy.drawParts[i].newTri == 1 or ObjectBuy.drawParts[i].newTri == 3) and (ObjectBuy.drawParts[i].newDelTri == 0 or ObjectBuy.drawParts[i].newDelTri == 3) and (ObjectBuy.drawParts[i].newTransObj == 1 or ObjectBuy.drawParts[i].newTransObj == 3) then
  135.                                 ObjectBuy.drawParts[i].hasPressedBuy = 1;
  136.                                 ObjectBuy.drawParts[i].saveGameLoad = true;
  137.                                 ObjectBuy.drawParts[i].payedPrice = true;
  138.                                 ObjectBuy.drawParts[i]:visibility(true, false, 1);
  139.                                 ObjectBuy.drawParts[i].infoToOthers = false;
  140.                             end;
  141.                     end;
  142.  
  143.                     for i=1, table.getn(self.drawPartsIcon) do
  144.                             local key = ("BuyableObjects."..ObjectBuy.drawPartsIcon[i].iconText.."");
  145.                             ObjectBuy.drawPartsIcon[i].newNumberIcon = Utils.getNoNil(getXMLInt(xmlFile, key.."#BuyObject_Buy"),0);
  146.                             ObjectBuy.drawPartsIcon[i].newNumIcon = Utils.getNoNil(getXMLInt(xmlFile, key.."#BuyObject_Delete"),1);
  147.                             ObjectBuy.drawPartsIcon[i].newTriIcon = Utils.getNoNil(getXMLInt(xmlFile, key.."#Trigger_Buy"),0);
  148.                             ObjectBuy.drawPartsIcon[i].newDelTriIcon = Utils.getNoNil(getXMLInt(xmlFile, key.."#Trigger_Delete"),1);
  149.                             ObjectBuy.drawPartsIcon[i].newTransIcon = Utils.getNoNil(getXMLInt(xmlFile, key.."#Trans_Objects"),0);
  150.  
  151.                             --savegame laden, wenn savegame nummer ist wie gekauft/nicht da, dann einmal visibilitiy aufrufen um Event zu senden.                  
  152.                             if (ObjectBuy.drawPartsIcon[i].newNumberIcon == 1 or ObjectBuy.drawPartsIcon[i].newNumberIcon == 3) and (ObjectBuy.drawPartsIcon[i].newNumIcon == 0 or ObjectBuy.drawPartsIcon[i].newNumIcon == 3) and (ObjectBuy.drawPartsIcon[i].newTriIcon == 1 or ObjectBuy.drawPartsIcon[i].newTriIcon == 3) and (ObjectBuy.drawPartsIcon[i].newDelTriIcon == 0 or ObjectBuy.drawPartsIcon[i].newDelTriIcon == 3) and (ObjectBuy.drawPartsIcon[i].newTransIcon == 1 or ObjectBuy.drawPartsIcon[i].newTransIcon == 3) then
  153.                                 ObjectBuy.drawPartsIcon[i]:visibilityIcon(true);
  154.                             end;
  155.                     end;
  156.                     print("Map Buyable Objects, Savegame states was loaded.");
  157.                    
  158.                     if table.getn(ObjectBuy.drawPartsMaster) ~= 0 then
  159.                             ObjectBuy.drawPartsMaster[1]:withoutMBO(false)
  160.                     end;
  161.  
  162.                 elseif c == 2 then
  163.                     for i=1, table.getn(self.drawParts) do
  164.                         ObjectBuy.drawParts[i].hasPressedBuy = 1;
  165.                         ObjectBuy.drawParts[i].saveGameLoad = true;
  166.                         ObjectBuy.drawParts[i].payedPrice = true;
  167.                         ObjectBuy.drawParts[i]:visibility(true, false, 1);
  168.                         ObjectBuy.drawParts[i].infoToOthers = false;
  169.                     end;
  170.                     for i=1, table.getn(self.drawPartsIcon) do
  171.                         ObjectBuy.drawPartsIcon[i]:visibilityIcon(true);
  172.                     end;
  173.                     print("Play without Map Buyable Objects, all objects are there.");
  174.                 end;
  175.                
  176.                 local b = Utils.getNoNil(getXMLInt(xmlFile, "BuyableObjects#numberOfObjects"), 0);
  177.                     if b > table.getn(self.drawParts) then
  178.                         ObjectBuy:saveBuyableObjectToXML()
  179.                         print("MapBuyableObject: Object deleted from map, Old saves loaded and saved new.");
  180.                     elseif b < table.getn(self.drawParts) then
  181.                         ObjectBuy:saveBuyableObjectToXML()
  182.                         print("MapBuyableObject: New Object added to map, saves loaded. New Object saved in Savegame XML.");
  183.                     end;
  184.             else
  185.                 ObjectBuy:saveBuyableObjectToXML()
  186.                 print("Warning: MapBuyableObjects can't load ", self.saveXMLFile, ": Name of the current map does not match the match in the savegame.");
  187.             end;
  188.             delete(xmlFile);
  189.     else
  190.         ObjectBuy:saveBuyableObjectToXML()
  191.         print("Warning: MapBuyableObjects can't load ", self.saveXMLFile, ": Playing time is not 0, but Savegame XML does not exist.");
  192.     end;
  193.  
  194. end;
  195.  
  196. function ObjectBuy:saveBuyableObjectToXML()
  197.  
  198.     local existDir = io.open (self.saveXMLFile, "w");  
  199.     if existDir == nil then
  200.         createXMLFile("MapBuyableObjects", self.saveXMLFile, "saveObject");
  201.     end;
  202.    
  203.     local xmlFile = io.open (self.saveXMLFile, "w");
  204.     if xmlFile ~= nil then
  205.         xmlFile:write('<?xml version="1.0" encoding="utf-8" standalone="no" ?>\n');
  206.         xmlFile:write('\n');
  207.         xmlFile:write('<BuyableObjects  mapIdentifier="'..self.mapName..'"  numberOfObjects="'..table.getn(ObjectBuy.drawParts)..'"  withMapBuyableObjects="'..masterTriggerAllowed..'">\n');
  208.         for i=1, table.getn(ObjectBuy.drawParts) do
  209.             xmlFile:write('    <'..ObjectBuy.drawParts[i].buyableText..'  BuyObject_Buy="'..ObjectBuy.drawParts[i].toBuy..'"  BuyObject_Delete="'..ObjectBuy.drawParts[i].toDelete..'"  Trigger_Buy="'..ObjectBuy.drawParts[i].toTrigger..'"  Trigger_Delete="'..ObjectBuy.drawParts[i].toDeleteTrigger..'"  Trans_Objects="'..ObjectBuy.drawParts[i].toTrans..'"/> \n');
  210.         end;       
  211.         xmlFile:write('     <!--BuyObject_buy:0=Objekt noch unsichtbar,nicht gekauft. 1=Objekt wurde gekauft,Sichtbar. 3=Kein Objekt angegeben./--> \n');
  212.         xmlFile:write('     <!--BuyObject_Delete: 0=Objekt wurde durch kauf unsichtbar gestellt. 1=Objekt noch sichtbar. 3=Kein Objekt angegeben./--> \n');
  213.         xmlFile:write('     <!--Trigger_Buy: 0=Trigger wurde noch nicht gekauft. 1=Trigger gekauft. 3=Kein Trigger angegeben./--> \n');
  214.         xmlFile:write('     <!--Trigger_Delete: 0=Trigger wurde durch kauf ohne Funktion. 1=Trigger noch da. 3=Kein Trigger angegeben./--> \n');
  215.         xmlFile:write('     <!--Trans_Objects: 0=Object noch orginal. 1=Object in neuer Position. 3=Kein Object angegeben./--> \n');
  216.         xmlFile:write("</BuyableObjects>");
  217.  
  218.         xmlFile:close();
  219.     end;   
  220.  
  221. end;
  222.  
  223. addModEventListener(ObjectBuy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement