Advertisement
Guest User

CapturePoints (WiC Style) in SW3

a guest
Jul 19th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.97 KB | None | 0 0
  1. --+------------------+-----------------+
  2. --| EXPANSION MOD    |   03.07.2019    |
  3. --+------------------------------------+
  4. --| Author: Nocalora | Version         |
  5. --+------------------+     0.6.2X      |
  6. --| Module           |      RC19       |
  7. --+------------------+-----------------+
  8. --| Gameplay Element Core              |
  9. --+------------------------------------+
  10. -- Object-Oriented-Programming approach
  11. -- Create a new Capture Point
  12. --
  13. -- cp1 = CP_CreateCapturePoint(
  14. --          1 {Vector3(-100, 0, -100), Vector3(100, 0, -100), Vector3(100, 0, 100), Vector3(-100, 0, 100)},
  15. --          2 CP_Config,
  16. --          3 {PLAYER_TEAM});
  17. --
  18. --ToDo:
  19. --      - Add AttachOnCaptureEvent(function, args) + sub-point variant
  20. --      - Add AttachOnLoseEvent(function, args) + sub-point variant
  21. --
  22. --      END GOAL: Optimize and make as few calculations as possible.
  23. --
  24. function CP_CreateCapturePoint(sName, tPositions, tConfig, tSides, fOnCapture)
  25.     LOG("[CP]: Creating new Capture Point: "..sName);
  26.     --This Object
  27.     local self = {};
  28.    
  29.     -- Sets --
  30.     self.Name               = sName;            -- Name of the Capture Point
  31.     self.Positions          = tPositions;       -- Positions of the Capture Points (Vector3 Table)
  32.     self.Config             = tConfig;          -- Configuration of this Capture Point set
  33.     self.Sides              = tSides;           -- Sides which can interact with this Capture Point set
  34.     self.OnCapture          = fOnCapture;       -- Function to exec on Capture Event (complete CP)
  35.     self.Amount             = getn(tPositions); -- Amount of Positions in a Control Point
  36.    
  37.     self.Points                 = {};           -- Data involving the ownership of the Points
  38.     self.PointsConnection       = {};           -- The Connection NavPoint and associated Lines | [1] is reserved for central NavPoint for the control point
  39.     --self.Points[].Lines       = {};           -- Lines associated with the Point
  40.     --self.Points[].NavPoints   = {};           -- NavPoints associated with the Point | [5] is reserved for the central NavPoint for the sub-point
  41.     --self.Points[].Ships       = {};           -- Ships inside the Point
  42.     --self.Points[].ShipsIDs    = {};           -- Associated IDs and their Ships
  43.     --self.Points[].Ownership   = {TEAM, GROUP};-- Owning Faction
  44.     --self.Points[].OwnershipVal= VALUE;        -- Ownership Percentage
  45.    
  46.     self.FullOwnership      = FALSE;            -- If all points are owned by one side
  47.     self.FullOwnershipSide  = nil;              -- Full Ownership Side
  48.     self.DoUpdates          = TRUE;             -- If autoexec should remain active
  49.    
  50.    
  51.     -- Gets --
  52.    
  53.     -- Get Positions of the Capture Points
  54.     function self:GetPositions()        return self.Positions; end;
  55.     function self:GetConfig()           return self.Config; end;
  56.     function self:GetSides()            return self.Sides; end;
  57.     function self:GetOnCapture()        return self.OnCapture(); end;
  58.     function self:GetOnLoose()          return self.OnLoose(); end;
  59.     function self:GetAmount()           return self.Amount; end;
  60.     function self:GetPoints()           return self.Points; end;
  61.     function self:HasFullownership()    return self.FullOwnership; end;
  62.     function self:GetFullownershipSide() return self.FullOwnershipSide; end;
  63.  
  64.     -- Create the Capture "Points" --
  65.     local tThisPoint, iThisX , iThusY, iThisZ;
  66.     LOG("[CP]: Creating Points for Capture Point");
  67.     for i = 1, self.Amount, 1 do
  68.         LOG("[CP]: Creating Points... ["..i.."/"..self.Amount.."]");
  69.         tThisPoint = self.Positions[i];
  70.         iThisX = tThisPoint.x;
  71.         iThisY = tThisPoint.y;
  72.         iThisZ = tThisPoint.z;
  73.        
  74.         -- Save the points in the class.
  75.         self.Points[i] = tThisPoint;
  76.         LOG("[CP]:      at: "..iThisX.." ".. iThisY.." "..iThisZ);
  77.        
  78.         -- Prepare the Tables
  79.         self.Points[i].NavPoints    = {};
  80.         self.Points[i].Lines        = {};
  81.         self.Points[i].Connection   = {};
  82.         self.Points[i].Ships        = {};
  83.         self.Points[i].ShipsIDs     = {};
  84.         self.Points[i].Ownership    = {};
  85.         self.Points[i].Ownership.Team = nil;
  86.         self.Points[i].Ownership.Group = nil;
  87.         self.Points[i].OwnershipVal = 0;
  88.         self.Points[i].FortifyVal   = 0;
  89.         self.Points[i].FortifyStage = 0;
  90.         self.Points[i].FortifyRebuiltVal = 0;
  91.         self.Points[i].FortifyFlight = nil;
  92.     end;
  93.    
  94.     --DrawPoints(string sOwnerColor)
  95.     function self:DrawPoints(sOwnerColor)
  96.         LOG("[CP]: DrawPoints()");
  97.         local vec3Pos, np1, np2, np3, np4, npm, npl1, npl2, npl3, npl4;
  98.         for i = 1, self.Amount, 1 do
  99.             local NavPoints, Lines = {}, {};
  100.             vec3Pos = self.Positions[i];
  101.             np1 = CreateNavPoint(" Point: "..i, Vector3(vec3Pos.x + 100, 0, vec3Pos.z), "nav_void.dds");
  102.             np2 = CreateNavPoint(" ", Vector3(vec3Pos.x, 0, vec3Pos.z + 100), "nav_void.dds");
  103.             np3 = CreateNavPoint(" ", Vector3(vec3Pos.x - 100, 0, vec3Pos.z), "nav_void.dds");
  104.             np4 = CreateNavPoint(" ", Vector3(vec3Pos.x, 0, vec3Pos.z - 100), "nav_void.dds");
  105.             npm = CreateNavPoint(" ", Vector3(vec3Pos.x, 0, vec3Pos.z), "nav_void.dds");
  106.            
  107.             --CreateLine(npSrc, npDest, vec3RGB, iLineWidth, bAnimated, iAnimSpeed, iArrowMode
  108.             -- iArrowMode | 0 = No Arrow, 1 = Arrow at Dest, 2 = Arrow at Source, 3 = Arrow at Src AND Dest
  109.             npl1 = CreateLine(np1, np2, self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  110.             npl2 = CreateLine(np2, np3, self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  111.             npl3 = CreateLine(np3, np4, self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  112.             npl4 = CreateLine(np4, np1, self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  113.            
  114.             tinsert(NavPoints, np1);
  115.             tinsert(NavPoints, np2);
  116.             tinsert(NavPoints, np3);
  117.             tinsert(NavPoints, np4);
  118.             tinsert(NavPoints, npm);
  119.             tinsert(Lines, npl1);
  120.             tinsert(Lines, npl2);
  121.             tinsert(Lines, npl3);
  122.             tinsert(Lines, npl4);
  123.            
  124.             LOG("[CP]: DrawPoints(): Adding NavPoints and Lines for Point: ["..i.."/"..self.Amount.."]");
  125.             self.Points[i].NavPoints = NavPoints;
  126.             self.Points[i].Lines = Lines;
  127.         end;
  128.     end;
  129.    
  130.     --DeletePoint(int iPositionIndex)
  131.     function self:DeletePoint(iPositionIndex)
  132.         for j = 1, 5, 1 do
  133.             if (self.Points[iPositionIndex].Lines[j]) then self.Points[iPositionIndex].Lines[j]:Delete(); end;
  134.             if (self.Points[iPositionIndex].NavPoints[j]) then self.Points[iPositionIndex].NavPoints[j]:Delete(); end;
  135.         end;
  136.     end;
  137.    
  138.     --DrawPointConnections(RGB sOwnerColor)
  139.     function self:DrawPointConnections(sOwnerColor)
  140.         LOG("[CP]: DrawPointConnections()");
  141.         local vec3Center, npc, npcl = Vector3(0, 0, 0);
  142.         for i = 1, self.Amount, 1 do
  143.             vec3Center.x = vec3Center.x + self.Positions[i].x;
  144.             vec3Center.z = vec3Center.z + self.Positions[i].z;
  145.         end;
  146.         vec3Center.x = (vec3Center.x / self.Amount);
  147.         vec3Center.z = (vec3Center.z / self.Amount);
  148.        
  149.         npc = CreateNavPoint(self.sName, Vector3(vec3Center.x, 0, vec3Center.z), "nav_void.dds");
  150.         self.PointsConnection[1] = npc;
  151.         for i = 1, self.Amount, 1 do
  152.             npcl = CreateLine(npc, self.Points[i].NavPoints[5], self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  153.             self.PointsConnection[1 + i] = npcl;
  154.         end;
  155.     end;
  156.    
  157.     --DrawPointConnection(RGB sOwnerColor, int iPositionIndex)
  158.     function self:DrawPointConnection(sOwnerColor, iPositionIndex)
  159.         LOG("[CP]: DrawPointConnection() for Point: "..iPositionIndex.." with sOwnerColor: "..sOwnerColor);
  160.         local npcp_mid = self.PointsConnection[1];
  161.         local npsubpoint_mid = self.Points[iPositionIndex].NavPoints[5];
  162.         local rgbcolor = self.Config["tColors"][sOwnerColor];
  163.         local linecenterline = CreateLine(npcp_mid, npsubpoint_mid, rgbcolor, .2, TRUE, .2, 0);
  164.         self.PointsConnection[1 + iPositionIndex] = linecenterline;
  165.     end;
  166.    
  167.     --RedrawPointConnection(RGB sOwnerColor, int iPositionIndex)
  168.     function self:RedrawPointConnection(sOwnerColor, iPositionIndex)
  169.         LOG("[CP]: RedrawPointConnection() for Point: "..iPositionIndex.." with sOwnerColor: "..sOwnerColor);
  170.         self.PointsConnection[1 + iPositionIndex]:Delete(); -- Delete old Connection Point
  171.         self:DrawPointConnection(sOwnerColor, iPositionIndex); -- Create new Connection Point
  172.     end;
  173.    
  174.     --DeletePointConnections(int iPositionIndex)
  175.     function self:DeletePointConnections(tself, iPositionIndex)
  176.         tself.PointsConnection[1]:Delete(); -- Delete NavPoint
  177.         for j = 1, tself.Amount, 1 do
  178.             tself.PointsConnection[1 + j]:Delete(); -- Delete Line
  179.         end;
  180.     end;
  181.    
  182.     --RedrawPoint(RGB sOwnerColor, int iPositionIndex): Redraws a specific Point
  183.     function self:RedrawPoint(sOwnerColor, iPositionIndex)
  184.         -- Delete old Point
  185.         LOG("[CP]: RedrawPoint(): Removing Lines and NavPoints for Point: ["..iPositionIndex.."/"..self.Amount.."]");
  186.         self:DeletePoint(iPositionIndex);
  187.         -- Create new Point
  188.         self:DrawPoint(sOwnerColor, iPositionIndex);
  189.     end;
  190.    
  191.     --DrawPoint(RGB OwnerColor, int iPositionIndex): Draws a specific Point
  192.     function self:DrawPoint(sOwnerColor, iPositionIndex)
  193.         LOG("[CP]: DrawPoint(): for "..iPositionIndex.." with sOwner: "..sOwnerColor);
  194.         local NavPoints, Lines = {}, {};
  195.         vec3Pos = self.Positions[iPositionIndex];
  196.         np1 = CreateNavPoint(" Point: "..iPositionIndex, Vector3(vec3Pos.x + 100, 0, vec3Pos.z), "nav_void.dds");
  197.         np2 = CreateNavPoint(" ", Vector3(vec3Pos.x, 0, vec3Pos.z + 100), "nav_void.dds");
  198.         np3 = CreateNavPoint(" ", Vector3(vec3Pos.x - 100, 0, vec3Pos.z), "nav_void.dds");
  199.         np4 = CreateNavPoint(" ", Vector3(vec3Pos.x, 0, vec3Pos.z - 100), "nav_void.dds");
  200.         npm = CreateNavPoint(" ", Vector3(vec3Pos.x, 0, vec3Pos.z), "nav_void.dds");
  201.        
  202.         --CreateLine(npSrc, npDest, vec3RGB, iLineWidth, bAnimated, iAnimSpeed, iArrowMode
  203.         -- iArrowMode | 0 = No Arrow, 1 = Arrow at Dest, 2 = Arrow at Source, 3 = Arrow at Src AND Dest
  204.         npl1 = CreateLine(np1, np2, self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  205.         npl2 = CreateLine(np2, np3, self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  206.         npl3 = CreateLine(np3, np4, self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  207.         npl4 = CreateLine(np4, np1, self.Config["tColors"][sOwnerColor], .2, TRUE, .2, 0);
  208.        
  209.         tinsert(NavPoints, np1);
  210.         tinsert(NavPoints, np2);
  211.         tinsert(NavPoints, np3);
  212.         tinsert(NavPoints, np4);
  213.         tinsert(NavPoints, npm);
  214.         tinsert(Lines, npl1);
  215.         tinsert(Lines, npl2);
  216.         tinsert(Lines, npl3);
  217.         tinsert(Lines, npl4);
  218.        
  219.         LOG("[CP]: DrawPoint(): Adding NavPoints and Lines for Point: ["..iPositionIndex.."/"..self.Amount.."]");
  220.         self.Points[iPositionIndex].NavPoints = NavPoints;
  221.         self.Points[iPositionIndex].Lines = Lines;
  222.     end;
  223.    
  224.     --HasShipsInsidePoint(int iPositionIndex)
  225.     function self:HasShipsInsidePoint(iPositionIndex)
  226.         LOG("[CP]: HasShipsInsidePoint() of "..iPositionIndex);
  227.         if (getn(self.Points[iPositionIndex].Ships) >= 1) then
  228.             LOG("[CP]: HasShipsInsidePoint() of "..iPositionIndex.." returns TRUE");
  229.             for i = 1, getn(self.Points[iPositionIndex].Ships), 1 do
  230.                 LOG("[CP]:      "..self.Points[iPositionIndex].Ships[i]:GetName());
  231.             end;
  232.             return TRUE;
  233.         end;
  234.         LOG("[CP]: HasShipsInsidePoint() of "..iPositionIndex.." returns FALSE");
  235.         return FALSE;
  236.     end;
  237.    
  238.     --GetFirstSideOfPoint(int iPositionIndex)
  239.     function self:GetFirstSideOfPoint(iPositionIndex)
  240.         return self.Points[iPositionIndex].Ships[1]:GetPilot():GetSide();
  241.     end;
  242.    
  243.     --GetFirstGroupOfPoint(int iPositionIndex)
  244.     function self:GetFirstGroupOfPoint(iPositionIndex)
  245.         return self.Points[iPositionIndex].Ships[1]:GetPilot():GetGroup();
  246.     end;
  247.    
  248.     --HasMultipleSidesInPoint(int iPositionIndex) (Will return either a TRUE or FALSE depending if the Point only consists of one claiming Side or multiple)
  249.     function self:HasMultipleSidesInPoint(iPositionIndex)
  250.         LOG("[CP]: HasMultipleSidesInPoint() of "..iPositionIndex);
  251.         local latestSide, curSide = nil, nil;
  252.         for i = 1, getn(self.Points[iPositionIndex].Ships), 1 do
  253.             curSide = self.Points[iPositionIndex].Ships[i]:GetPilot():GetSide();
  254.            
  255.             if (latestSide == nil) then
  256.                 latestSide = curSide;
  257.                 break;
  258.             end;
  259.            
  260.             if (latestSide ~= curSide) then
  261.                 LOG("[CP]: HasMultipleSidesInPoint() of "..iPositionIndex.." returns TRUE");
  262.                 return TRUE;
  263.             end;
  264.         end;
  265.        
  266.         LOG("[CP]: HasMultipleSidesInPoint() of "..iPositionIndex.." returns FALSE");
  267.         return FALSE;
  268.     end;
  269.    
  270.     --UpdateOwnership(int iPositionIndex)
  271.     --Cases:
  272.     --      - One Side inside Point     : Point "Ownership" percentage goes up for that faction.
  273.     --      - Two sides inside Point    : Point "Ownership" percentage halts.
  274.     --      - No side inside Point      : Point "Ownership" percentage goes down.
  275.     function self:UpdatePointOwnership(iPositionIndex)
  276.         LOG("[CP]: UpdatePointOwnership()");
  277.        
  278.         -- Check if the sub-point even has ships in it
  279.         if (self:HasShipsInsidePoint(iPositionIndex) == TRUE) then
  280.             if (self:HasMultipleSidesInPoint(iPositionIndex) == TRUE) then -- Multiple sides (HALT PERCENTAGE)
  281.                 -- Do nothing
  282.                 LOG("[CP]: UpdatePointOwnership(): Do Nothing in "..iPositionIndex);
  283.                
  284.             else -- Only one side inside sub-point (INCREASE PERCENTAGE)
  285.                 if (self.Points[iPositionIndex].Ownership.Team == self:GetFirstSideOfPoint(iPositionIndex)) then -- The ownership matches with the current residing side. (INCREASE PERCENTAGE)
  286.                     self:IncreasePointOwnership(iPositionIndex);
  287.                 else -- The ownership doesnt match with the current residing. (DECREASE PERCENTAGE)
  288.                     self:DecreasePointOwnership(iPositionIndex);
  289.                     if (self.Points[iPositionIndex].OwnershipVal == 0) then -- Percentage is 0, update side ownership with new owner
  290.                         self.Points[iPositionIndex].Ownership.Team = self:GetFirstSideOfPoint(iPositionIndex);
  291.                         self.Points[iPositionIndex].Ownership.Group = self:GetFirstGroupOfPoint(iPositionIndex);
  292.                         self:UpdateColors(iPositionIndex); -- Update the Colors
  293.                     end;
  294.                 end;
  295.             end;
  296.         else -- No sides inside sub-point (LOWER PERCENTAGE)
  297.             if (self.Points[iPositionIndex].OwnershipVal == 100) then -- Ownership Percentage is 100, do not decrease.
  298.            
  299.             elseif (self.Points[iPositionIndex].OwnershipVal == 0) then --  Ownership Percentage is zero
  300.                
  301.                
  302.             else
  303.                 self:DecreasePointOwnership(iPositionIndex);
  304.                 if (self.Points[iPositionIndex].OwnershipVal == 0) then -- Ownership Percentage just reachd zero
  305.                     self.Points[iPositionIndex].Ownership.Team = "Unowned";
  306.                     self:UpdateColors(iPositionIndex);
  307.                 end;
  308.             end;
  309.            
  310.         end;
  311.     end;
  312.    
  313.     --Event: On Complete Control of Capture Point
  314.     function self:EventOnCompleteControl()
  315.         LOG("[CP]: EventOnCompleteControl()");
  316.         if (self.FullOwnership == TRUE) then
  317.             LOG("[CP]: EventOnCompleteControl(): Complete ownership from a side | Executing OnCapture, passing Side as Arg");
  318.             ExecFunc(self.OnCapture, {0, self.FullOwnershipSide});
  319.         end;
  320.     end;
  321.    
  322.     --IncreasePointOwnership(int iPositionIndex)
  323.     function self:IncreasePointOwnership(iPositionIndex)
  324.         if (self.Points[iPositionIndex].OwnershipVal < 100) then
  325.             local iIncrement = (self.Config["iCaptureSpeedperUnit"] * getn(self.Points[iPositionIndex].Ships));
  326.             if (getn(self.Points[iPositionIndex].Ships) == 0) then iIncrement = self.Config["iCaptureSpeedperUnit"]; end;
  327.            
  328.             self.Points[iPositionIndex].OwnershipVal = self.Points[iPositionIndex].OwnershipVal + iIncrement;
  329.             LOG("[CP]: IncreasePointOwnership(): Increasing % | Residing Side Matches | in "..iPositionIndex.." | current "..self.Points[iPositionIndex].OwnershipVal);
  330.             if (self.Points[iPositionIndex].Ownership.Team == PLAYER_TEAM) then OutputToScreenLog(self.Points[iPositionIndex].OwnershipVal.."% Captured", 1); end;
  331.         else -- percentage is 100
  332.             LOG("[CP]: IncreasePointOwnership(): Increasing % | % = 100 | Residing Side Matches | in "..iPositionIndex);
  333.         end;
  334.     end;
  335.    
  336.     --DecreasePointOwnership(int iPositionIndex)
  337.     function self:DecreasePointOwnership(iPositionIndex)
  338.         if (self.Points[iPositionIndex].OwnershipVal >= 1) then
  339.             local iDecrement = (self.Config["iCaptureSpeedperUnit"] * getn(self.Points[iPositionIndex].Ships));
  340.             if (getn(self.Points[iPositionIndex].Ships) == 0) then iDecrement = self.Config["iCaptureSpeedperUnit"]; end;
  341.            
  342.             self.Points[iPositionIndex].OwnershipVal = self.Points[iPositionIndex].OwnershipVal - iDecrement;
  343.             LOG("[CP]: DecreasePointOwnership(): Lowering % | in "..iPositionIndex.." | current "..self.Points[iPositionIndex].OwnershipVal);
  344.         else -- percentage is 0
  345.             LOG("[CP]: DecreasePointOwnership():  Lowering % | % = 0 | in "..iPositionIndex);
  346.         end;
  347.     end;
  348.    
  349.     --UpdateColors(int iPositionIndex) (Refreshes the Colors according to ownership of individual Capture sub-point)
  350.     function self:UpdateColors(iPositionIndex)
  351.         LOG("[CP]: UpdateColors() with position: "..iPositionIndex);
  352.        
  353.         local sideOwner = self.Points[iPositionIndex].Ownership.Team;
  354.         if (sideOwner == "Unowned") then
  355.             self:RedrawPoint("Unowned", iPositionIndex);
  356.             self:RedrawPointConnection("Unowned", iPositionIndex);
  357.         elseif (sideOwner == PLAYER_TEAM) then
  358.             self:RedrawPoint("Player", iPositionIndex);
  359.             self:RedrawPointConnection("Player", iPositionIndex);
  360.         elseif (GetTeamRelation(sideOwner, PLAYER_TEAM) == RELATION_FRIEND) then
  361.             self:RedrawPoint("Allied", iPositionIndex);
  362.             self:RedrawPointConnection("Allied", iPositionIndex);
  363.         elseif (GetTeamRelation(sideOwner, PLAYER_TEAM) == RELATION_WAR) then
  364.             self:RedrawPoint("War", iPositionIndex);
  365.             self:RedrawPointConnection("War", iPositionIndex);
  366.         elseif (GetTeamRelation(sideOwner, PLAYER_TEAM) == RELATION_NEUTRAL) then
  367.             self:RedrawPoint("Neutral", iPositionIndex);
  368.             self:RedrawPointConnection("Neutral", iPositionIndex);
  369.         end;
  370.     end;
  371.    
  372.     --Update()
  373.     function self:Update(tself)
  374.         LOG("[CP]: Update(): ");
  375.        
  376.         -- Check for full Ownership
  377.         local sideP1Owner, bSameOwner = tself.Points[1].Ownership.Team, TRUE;
  378.         for i = 1, tself.Amount, 1 do -- go through each Capture Point sub-point.
  379.             tself:UpdatePointOwnership(i);
  380.             tself:EventOnCompleteControl(i);
  381.            
  382.             if ((tself.Points[i].Ownership.Team == sideP1Owner) and (sideP1Owner ~= nil)) then --same owner and or one is Unowned
  383.                
  384.             else --different owner
  385.                 LOG("[CP]: UpdatePointOwnership(): different owners in "..i);
  386.                 bSameOwner = FALSE;
  387.             end;
  388.         end;
  389.         if (bSameOwner == TRUE) then  
  390.             tself.FullOwnership = TRUE;
  391.         end;
  392.     end;
  393.    
  394.     --Add/Remove Flight: ID Approach
  395.     function self:AddFlightToPoint(iPositionIndex, flight)
  396.         LOG("[CP]: AddFlightToPoint added flight of Leader: "..flight:GetLeader():GetShip():GetName().." id: "..flight.id);
  397.         local flightPilots = flight:GetPilotCount();
  398.         for i = 1, flightPilots, 1 do
  399.             self:AddShipToPoint(iPositionIndex, flight:GetPilotByNumber(i):GetShip());
  400.         end;
  401.     end;
  402.     function self:RemoveFlightFromPoint(iPositionIndex, flight)
  403.         LOG("[CP]: RemoveFlightFromPoint removed flight of Leader: "..flight:GetLeader():GetShip():GetName().." id: "..flight.id);
  404.         local flightPilots = flight:GetPilotCount();
  405.         for i = 1, flightPilots, 1 do
  406.             self:RemoveShipFromPoint(iPositionIndex, flight:GetPilotByNumber(i):GetShip());
  407.         end;
  408.     end;
  409.    
  410.     --Add/Remove Ship: ID Approach
  411.     --AddShipToPoint(int iPositionIndex, ship shipCarcass)
  412.     function self:AddShipToPoint(iPositionIndex, shipCarcass)
  413.         LOG("[CP]: AddShipToPoint added ship: "..shipCarcass:GetName().." id: "..shipCarcass.id);
  414.         tinsert(self.Points[iPositionIndex].Ships, shipCarcass);
  415.         self.Points[iPositionIndex].ShipsIDs[tostring(shipCarcass.id)] = getn(self.Points[iPositionIndex].Ships); -- The id now can be used to get the index of the ship.
  416.         LOG("[CP]:      Added Index: "..getn(self.Points[iPositionIndex].Ships)..", ID: "..shipCarcass.id);
  417.     end;
  418.    
  419.     --RemoveShipFromPoint(int iPositionIndex, ship shipCarcass)
  420.     function self:RemoveShipFromPoint(iPositionIndex, shipCarcass)
  421.         LOG("[CP]: RemoveShipFromPoint removed ship: "..shipCarcass:GetName().." id: "..shipCarcass.id);
  422.         local shipIndex = self.Points[iPositionIndex].ShipsIDs[tostring(shipCarcass.id)];
  423.         if (shipIndex) then
  424.             LOG("[CP]:      Removed Index: "..shipIndex.."/"..getn(self.Points[iPositionIndex].Ships)..", ID: "..shipCarcass.id);
  425.             tremove(self.Points[iPositionIndex].Ships, shipIndex);
  426.             tremove(self.Points[iPositionIndex].ShipsIDs, shipCarcass.id);
  427.         else
  428.             LOG("[CP]:      Error while: Removing Index: ?/"..getn(self.Points[iPositionIndex].Ships)..", ID: "..shipCarcass.id);
  429.         end;
  430.     end;
  431.    
  432.     --OnShipEnter Event (Receives the Position Index of the Point)
  433.     function self:EventOnSideEnter(tself, iPositionIndex)
  434.         LOG("[CP]: Event: OnShipEnter() fired on PositionIndex "..iPositionIndex);
  435.         local TriggerContext        = GetTriggerContext();
  436.         local triggeringPilot       = TriggerContext:GetPilot();
  437.         local triggeringShip        = triggeringPilot:GetShip();
  438.         local triggeringPointVec    = tself.Positions[iPositionIndex];
  439.        
  440.         if (tself:IsShipInside(iPositionIndex, triggeringShip) == FALSE) then
  441.             tself:AddFlightToPoint(iPositionIndex, triggeringPilot:GetFlight());
  442.             TU_CreateOnLeaveShipTrigger(triggeringPointVec, 100, triggeringShip, tself.EventOnSideLeave, {0, tself, iPositionIndex}); -- Attach OnLeave event for entered Ship
  443.         end;
  444.     end;
  445.    
  446.     --OnShipLeave Event (Receives the Position Index of the Point)
  447.     function self:EventOnSideLeave(tself, iPositionIndex)
  448.         LOG("[CP]: Event: OnShipLeave() fired on PositionIndex "..iPositionIndex);
  449.         local TriggerContext        = GetTriggerContext();
  450.         local triggeringTargetObj   = TriggerContext:GetTargetObject();
  451.         local triggeringPilot       = triggeringTargetObj:GetPilot();
  452.         local triggeringShip        = triggeringPilot:GetShip();
  453.        
  454.         tself:RemoveFlightFromPoint(iPositionIndex, triggeringPilot:GetFlight());
  455.         TU_CreateOnEnterTrigger(tself.Positions[iPositionIndex], 100, tself.Sides, tself.EventOnSideEnter, {0, tself, iPositionIndex}); -- ReAttach OnEnter event for side
  456.     end;
  457.    
  458.     --IsShipInside(int iPositionIndex, ship shipCarcass) (Returns wether a ship is inside the sub-point or not)
  459.     function self:IsShipInside(iPositionIndex, shipCarcass)
  460.         local shipCurship;
  461.         for i = 1, getn(self.Points[iPositionIndex].Ships), 1 do
  462.             shipCurship = self.Points[iPositionIndex].Ships[i];
  463.             if (shipCarcass.id == shipCurship.id) then
  464.                 return TRUE;
  465.             end;
  466.         end;
  467.         return FALSE;
  468.     end;
  469.    
  470.     --ToggleCaptureable(bool isCaptureable)
  471.     function self:ToggleCaptureable(bCapturable)
  472.         LOG("[CP]: ToggleCaptureable()");
  473.         if (bCapturable == TRUE) then
  474.             local vec3Pos;
  475.             for i = 1, self.Amount, 1 do
  476.                 vec3Pos = self.Positions[i];
  477.                 LOG("[CP]:      Created Trigger for Position ["..i.."/"..self.Amount.."]");
  478.                 -- Create OnEnter Trigger to make Capture possible
  479.                 TU_CreateOnEnterTrigger(vec3Pos, 100, self.Sides, self.EventOnSideEnter, {0, self, i});
  480.             end;
  481.            
  482.         end;
  483.     end;
  484.    
  485.     -- self:UpdateCondition()
  486.     function self:UpdateCondition()
  487.         return TRUE;
  488.     end;
  489.    
  490.     --Delete()
  491.     function self:Delete()
  492.    
  493.         for i = 1, self.Amount, 1 do
  494.             self:DeletePointConnections(self, i);
  495.             self:DeletePoint(i);
  496.             self.Points[i].Lines        = nil;
  497.             self.Points[i].NavPoints    = nil;
  498.             self.Points[i].Ships        = nil;
  499.             self.Points[i].ShipsIDs     = nil;
  500.             self.Points[i].Ownership    = nil;
  501.             self.Points[i].OwnershipVal = nil;
  502.         end;
  503.        
  504.         self.Name               = nil;
  505.         self.Positions          = nil;
  506.         self.Config             = nil;
  507.         self.Sides              = nil;
  508.         self.OnCapture          = nil; 
  509.         self.Amount             = nil;
  510.         self.Points             = nil;
  511.         self.PointsConnection   = nil;
  512.         self.FullOwnership      = nil;
  513.         self.FullOwnershipSide  = nil;
  514.         self.DoUpdates          = nil;
  515.         self                    = nil;
  516.     end;
  517.    
  518.     -- Draw the Points
  519.     self:DrawPoints("Unowned");
  520.     self:ToggleCaptureable(TRUE);
  521.     self:DrawPointConnections("Unowned", TRUE);
  522.    
  523.     -- Activate the Update Function
  524.     ExecEvery(self.Config["iUpdateTime"], self.Update, {0, self},
  525.     function()
  526.         if (%self.Name == nil) then
  527.             return FALSE;
  528.         else
  529.             return %self:UpdateCondition();
  530.         end;
  531.     end);
  532.    
  533.     LOG("[CP]: Returning Capture Point");
  534.     return self;
  535. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement