// SPDX-License-Identifier: AGPL-3.0-or-later
package preservation;

/** Source effect and vertical unk18 field needed by Vibhuti, from pinned
 * collider.c. Reuses the verified all-byte ReboundTerrain effect query while
 * leaving earlier terrain and actor behavior unchanged. All256 byte types
 * retain the source initialized vertical offset and defined floor/recursive/
 * half-tile adjustments. Other corrections, dynamic doors and complete player
 * sensors are not implemented by this isolated adapter.
 */
public final class VibhutiTerrain {
    static final class Collision {int effects,up,code;boolean unsupported;}
    private VibhutiTerrain(){}
    static Collision query(StageMap.Room room,int x,int y){return query(room,x,y,false);}
    static Collision query(StageMap.Room room,int x,int y,boolean recursive){
        ReboundTerrain.Collision effects=ReboundTerrain.query(room,x,y,recursive);Collision c=new Collision();c.effects=effects.effects;c.code=effects.code;c.unsupported=effects.unsupported;c.up=-(y&15);
        int fx=15-(x&15),fy=15-(y&15),t=c.code-128,offset=0;
        switch(t){
        case 1:case 2:case 10:case 11:case 24:case 25:if(!recursive){Collision above=query(room,x,y-(y&15)-1,true);if((above.effects&0x8001)==0x8001)c.up+=above.up-1;}break;
        case 0:if((c.effects&1)!=0)c.up=fx+fy-15;break;
        case 3:if((c.effects&1)!=0)c.up=fy-fx;break;
        case 8:case 9:offset=t==8?16:0;if((c.effects&1)!=0)c.up=((fx+offset)>>1)+fy-15;break;
        case 12:case 13:offset=t==12?16:0;if((c.effects&1)!=0)c.up=fy-((offset+fx)>>1);break;
        case 20:case 21:case 22:case 23:offset=(23-t)*16;if((c.effects&1)!=0)c.up=((fx+offset)>>2)+fy-15;break;
        case 26:case 27:case 28:case 29:offset=(29-t)*16;if((c.effects&1)!=0)c.up=fy-(fx+offset)/4;break;
        case 127:case 125:case 122:case 121:case 104:case 102:if(fy<8)c.up+=8;break;
        default:break;
        }return c;
    }
}
