Method: Api::ActionRawUnitCommand#_encode

Defined in:
lib/sc2ai/protocol/raw_pb.rb

#_encode(buff) ⇒ Object



17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
# File 'lib/sc2ai/protocol/raw_pb.rb', line 17786

def _encode(buff)
  val = @ability_id
  if has_ability_id?
    buff << 0x08

    loop do
      byte = val & 0x7F

      val >>= 7
      # This drops the top bits,
      # Otherwise, with a signed right shift,
      # we get infinity one bits at the top
      val &= (1 << 57) - 1

      byte |= 0x80 if val != 0
      buff << byte
      break if val == 0
    end
  end

  val = @target_world_space_pos
  if val
    buff << 0x12

    # Save the buffer size before appending the submessage
    current_len = buff.bytesize

    # Write a single dummy byte to later store encoded length
    buff << 42 # "*"
    val._encode(buff)

    # Calculate the submessage's size
    submessage_size = buff.bytesize - current_len - 1

    # Hope the size fits in one byte
    byte = submessage_size & 0x7F
    submessage_size >>= 7
    byte |= 0x80 if submessage_size > 0
    buff.setbyte(current_len, byte)

    # If the sub message was bigger
    if submessage_size > 0
      current_len += 1

      # compute how much we need to shift
      encoded_int_len = 0
      remaining_size = submessage_size
      while remaining_size != 0
        remaining_size >>= 7
        encoded_int_len += 1
      end

      # Make space in the string with dummy bytes
      buff.bytesplice(current_len, 0, "*********", 0, encoded_int_len)

      # Overwrite the dummy bytes with the encoded length
      while submessage_size != 0
        byte = submessage_size & 0x7F
        submessage_size >>= 7
        byte |= 0x80 if submessage_size > 0
        buff.setbyte(current_len, byte)
        current_len += 1
      end
    end

    buff
  end

  val = @target_unit_tag
  if val != 0
    buff << 0x18

    loop do
      byte = val & 0x7F
      val >>= 7
      byte |= 0x80 if val > 0
      buff << byte
      break if val == 0
    end
  end

  list = @unit_tags
  if list.size > 0
    list.each do |item|
      val = item
      if val != 0
        buff << 0x20

        loop do
          byte = val & 0x7F
          val >>= 7
          byte |= 0x80 if val > 0
          buff << byte
          break if val == 0
        end
      end
    end
  end

  if has_queue_command?
    val = @queue_command
    buff << 0x28

    if val == true
      buff << 1
    elsif val == false
      buff << 0
    end
  end
  buff << @_unknown_fields if @_unknown_fields
  buff
end