Some little help with condensing code please

Discussion about Mods for Prison Architect

Moderator: NBJeff

murgh
level2
level2
Posts: 232
Joined: Sat Jan 30, 2016 11:52 am

Some little help with condensing code please

Postby murgh » Wed Mar 16, 2016 3:51 pm

Ok here is the deal. In fact, all I want is to know from an object that is xPos+1 away from my object a value stored in xTimes. Doesn't sound too hard, does it?
So why the bleep did I come up with this piece of crap:

Code: Select all

local xTimes=0;

function FindPager()
   --check for my booth and my pager partner.
   this.SubType = 1;  -- show the 'No Pager' display until a pager panel is found
   local myPageri=0;
   local myPageru=0;
   local myBooth = this.GetNearbyObjects('ControlBooth',1);  -- am I placed in a small booth perhaps?
   for name, distance in pairs (myBooth ) do
      if name.Id.i==this.CarrierId.i and name.Id.u==this.CarrierId.u then
         if name.Slot1.i==this.Id.i and name.Slot1.u==this.Id.u then -- if I'm in slot1 then slot2 is my pager partner
            myPageri=name.Slot2.i;
            myPageru=name.Slot2.u;
         end
      end
   end
   if myPageri==0 then -- I guess I wasn't in a small booth, gotta take the long route...
      local myBooth = this.GetNearbyObjects('ControlBoothMedium',1);  -- am I placed in a medium booth perhaps?
      for name, distance in pairs (myBooth ) do
         if name.Id.i==this.CarrierId.i and name.Id.u==this.CarrierId.u then
            if name.Slot0.i==this.Id.i and name.Slot0.u==this.Id.u then -- if I'm in slot0 then slot2 is my pager partner
               myPageri=name.Slot2.i;
               myPageru=name.Slot2.u;
            end
            if name.Slot1.i==this.Id.i and name.Slot1.u==this.Id.u then -- if I'm in slot1 then slot3 is my pager partner
               myPageri=name.Slot3.i;
               myPageru=name.Slot3.u;
            end            
         end
      end
      if myPageri==0 then -- I guess I wasn't in a medium booth, gotta take the even longer route...
         local myBooth = this.GetNearbyObjects('ControlBoothLarge',2);  -- am I placed in a large booth perhaps? Then I must check 2 tiles, because Slot0 is 2 tiles away from center.
         for name, distance in pairs (myBooth ) do
            if name.Id.i==this.CarrierId.i and name.Id.u==this.CarrierId.u then
               if name.Slot0.i==this.Id.i and name.Slot0.u==this.Id.u then -- if I'm in slot0 then slot3 is my pager partner
                  myPageri=name.Slot3.i;
                  myPageru=name.Slot3.u;
               end
               if name.Slot1.i==this.Id.i and name.Slot1.u==this.Id.u then -- if I'm in slot1 then slot4 is my pager partner
                  myPageri=name.Slot4.i;
                  myPageru=name.Slot4.u;
               end
               if name.Slot2.i==this.Id.i and name.Slot2.u==this.Id.u then -- if I'm in slot2 then slot5 is my pager partner
                  myPageri=name.Slot5.i;
                  myPageru=name.Slot5.u;
               end
            end
         end
      end
   end
   
   --if myPageri==0 then my pager partner hasn't arrived yet or has been removed or something like that.
   
   if myPageri~=0 then -- when I'm in a booth it's about time to find my pager partners xTimes value so I know how many staff I should spawn
      local xPager = this.GetNearbyObjects('Pagerx10',1);
      for pager, distance in pairs ( xPager ) do
         if pager.Id.i==myPageri and pager.Id.u==myPageru then
            xTimes = pager.xTimes;
            this.SubType = 0; -- show the 'Guard' display
         end
      end
      local xPager = this.GetNearbyObjects('Pagerx20',1);
      for pager, distance in pairs ( xPager ) do
         if pager.Id.i==myPageri and pager.Id.u==myPageru then
            xTimes = pager.xTimes;
            this.SubType = 0; -- show the 'Guard' display
         end
      end
      local xPager = this.GetNearbyObjects('Pagerx50',1);
      for pager, distance in pairs ( xPager ) do
         if pager.Id.i==myPageri and pager.Id.u==myPageru then
            xTimes = pager.xTimes;
            this.SubType = 0; -- show the 'Guard' display
         end
      end
      local xPager = this.GetNearbyObjects('Pagerx100',1);
      for pager, distance in pairs ( xPager ) do
         if pager.Id.i==myPageri and pager.Id.u==myPageru then
            xTimes = pager.xTimes;
            this.SubType = 0; -- show the 'Guard' display
         end
      end         
   end -- else still show SubType 1 sprite and wait for a partner until next update()...
end


I mean, RLY!? Do I need this bunch of code to get value xTimes from a thing next to me? I think there has to be a quicker way than scanning A) in which item I am loaded, B ) checking the slot number where I am, C) find a value in a corresponding partner slotnumber, D) find that object with that id, then E) read xTimes from it. OMG.

OK, the above code seems to be working fine without any problems, but come on... does it HAVE to be this long? HELP! :twisted: :evil:


edit: pff why not just spawn a generic pager and find its values... what was I thinking :roll:
Anyway I'll leave the code in this topic, perhaps somebody else finds a good use for it.

Meanwhile this replaced the 20 pages of code above and does what I needed:

Code: Select all

function FindPager()
   --check for my booth and my pager partner.
   this.SubType = 1;  -- show the 'No Pager' display until a pager panel is found
   local myPager = this.GetNearbyObjects('Pager',1);
   for thatpagers, distance in pairs (myPager) do
      if thatpagers.Pos.x-0.50000==this.Pos.x and thatpagers.Loaded then -- is this thing right next to me and loaded into the booth as well?
         xTimes = thatpagers.xTimes; -- tell me how many staff I should spawn
         this.SubType = 0; -- show the 'Guard' display and hope the player connected a switch to me, otherwise they start whining about things not working :s
         break;
      end
   end -- else still show SubType 1 sprite and wait for a partner until next update()...
end

8)

Return to “Modding”

Who is online

Users browsing this forum: No registered users and 10 guests