Pmap array

  • QuanTech
    30th Nov 2016 Member 0 Permalink

    Hi. I've tried searching through the source code for a long time... and I can't find where the pmap[] array is created. Could you please tell me where in the code it is first created? Thanks.

    EDIT: oh and where are the IDs and types stored in pmap[][]?

    Edited once by QuanTech. Last: 30th Nov 2016
  • LBPHacker
    30th Nov 2016 Developer 0 Permalink

    It's a member of class Simulation. It's created whenever Simulation is created. Only IDs are stored in Simulation::pmap, types are stored in Simulation::parts. In fact, Simulation::pmap is a huge 2-dimensional array of IDs.

    Edited once by LBPHacker. Last: 30th Nov 2016
  • QuanTech
    30th Nov 2016 Member 0 Permalink


    r=pmap[y+ry][x+rx];
    if((r&0xFF)==PT_BLAH)...

    So if pmap only contains IDs, why does r&0xFF = type?

  • mark2222
    1st Dec 2016 Member 0 Permalink

    @QuanTech (View Post)

     

    pmap contains both IDs and types. See https://github.com/simtr/The-Powder-Toy/blob/master/src/simulation/Simulation.cpp#L4877:

    pmap[y][x] = t|(i<<8);

    Here (in the BeforeSim function), a loop is run to fill up pmap using the data in parts. t is type and i is ID. It uses bit-magic to store both type and ID into the same int to save memory. Specifically, the lowest 8 bits are used to store the type, while the higher bits are used to store the ID. In fact, it is for this reason that there cannot be more than 256 (2^8) types in TPT.

     

    Thus, to get the type, you extract out only the lowermost 8 bits by using the AND operator to mask out everything else (0xFF is "11111111" in binary):

    t = pmap[y][x] & 0xFF;

    To get the ID, you shift the entire number right by 8 bits, destroying the lowest 8 bits:

    i = pmap[y][x] >> 8;
  • QuanTech
    1st Dec 2016 Member 0 Permalink

    @mark2222 ah ok. LBPHacker told me about this before, but right now he's saying it only contains IDs :P

    Edited once by QuanTech. Last: 1st Dec 2016