#
# $Id: alc_Messages.py 455 2006-08-04 20:18:42Z Robert The Rebuilder $
#
#    Copyright (C) 2005-2006  Alcugs pyprp Project Team
#    See the file AUTHORS for more info about the team
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    Please see the file COPYING for the full license.
#    Please see the file DISCLAIMER for more details, before doing nothing.
#

import struct
from alcurutypes import *
from alc_hsStream import *

#
# Eventually, the following messages should be implemented here:
#
# 0206 : 9584 : plAnimCmdMsg
# 020A : 70 : plCameraMsg
# 024A : 533 : plTimerCallbackMsg
# 024F : 1166 : plEnableMsg
# 0255 : 2860 : plSoundMsg
# 02E1 : 83 : plLinkToAgeMsg
# 02E8 : 916 : plNotifyMsg
# 02FD : 36 : plResponderEnableMsg
# 0302 : 488 : plOneShotMsg
# 0330 : 311 : plExcludeRegionMsg
# 038E : 515 : plArmatureEffectStateMsg
# 03BA : 4 : plSubWorldMsg
# 045B : 188 : plSimSuppressMsg

class PrpMessage:
    def __init__(self,type=None,version=None):
        if type == None:
            self.msgtype = 0xFFFF
        else:
            self.msgtype = type
        if version == None:
            self.version = 5
        else:
            self.version = version
        self.data = None
        self.Key = plKey(self.version)
        if self.version == 5:
            #UruTypes
            if self.msgtype == 0x02E8:
                self.data = plNotifyMsg(self)
            elif self.msgtype == 0x038E:
                self.data = plArmatureEffectStateMsg(self)
            else:
                raise RuntimeError, "Unsupported message type %04X" % self.type
        elif self.version == 6:
            #Myst 5 types
            raise RuntimeError, "Unsupported Myst 5 message type %04X" % self.type


    def read(self,buf):
        self.data.read(buf)


    def write(self,buf):
        self.data.write(buf)


    def dump(self):
        self.data.dump()


    def getVersion(self):
        return self.version


    def update(self,Key):
        self.Key.update(Key)


    def changePageRaw(self,sid,did,stype,dtype):
        self.Key.changePageRaw(sid,did,stype,dtype)
        if self.data != None:
            self.data.changePageRaw(sid,did,stype,dtype)
        

class plMessage:
    def __init__(self,parent=None):
        if parent == None:
            self.msgtype = 0xFFFF
        else:
            self.msgtype = parent.msgtype
        self.parent = parent


    def read(self, buf):
        pass


    def write(self, buf):
        pass


    def dump(self):
        pass


    def changePageRaw(self,sid,did,stype,dtype):
        pass


class plArmatureEffectStateMsg(plMessage): # 0x038E
    def __init__(self,parent=None):
        plMessage.__init__(self,parent)
        self.groundtype = 0x04 # default to water (so they hear it's unset immediately)


    def read(self,buf):
        self.groundtype = buf.Read16()


    def write(self,buf):
        buf.Write16(self.groundtype)


    def dump(self):
        pass


class plNotifyMsg(plMessage):
    def __init__(self,parent=None):
        plMessage.__init__(self,parent)
        self.byte0 = 0x00
        self.Modifiers = hsTArray([0xA2,0xAE,0x7C,0xCF],self.parent.getVersion(),True)
        self.dword1 = 0x00000000
        self.dword2 = 0x00000000
        self.dword3 = 0x00000840
        self.dword4 = 0x00000000
        self.dword5 = 0x00000000
        self.dword6 = 0x00000000
        self.dword7 = 0x00000000


    def read(self,buf):
        self.byte0 = buf.ReadByte()
        self.Modifiers.read(buf)
        self.dword1 = buf.Read32()
        self.dword2 = buf.Read32()
        self.dword3 = buf.Read32()
        self.dword4 = buf.Read32()
        self.dword5 = buf.Read32()
        self.dword6 = buf.Read32()
        self.dword7 = buf.Read32()


    def write(self,buf):
        buf.WriteByte(self.byte0)
        self.Modifiers.update(self.parent.Key)
        self.Modifiers.write(buf)
        buf.Write32(self.dword1)
        buf.Write32(self.dword2)
        buf.Write32(self.dword3)
        buf.Write32(self.dword4)
        buf.Write32(self.dword5)
        buf.Write32(self.dword6)
        buf.Write32(self.dword7)


    def changePageRaw(self,sid,did,stype,dtype):
        self.Modifiers.changePageRaw(sid,did,stype,dtype)


class plRefMsg(plMessage):
    def __init__(self,parent=None):
        plMessage.__init__(self,parent)
        if(parent == None):
            ver = 5
        else:
            ver = parent.getVersion()
        self.byte30 = 0
        self.reffed = UruObjectRef(ver)
        self.oldRef = UruObjectRef(ver)
    
    
    def read(self,buf):
        plMessage.read(self,buf)
        self.byte30 = buf.ReadByte()
        self.reffed.read(buf)
        self.oldRef.read(buf)
    
    
    def write(self,buf):
        plMessage.write(self,buf)
        buf.WriteByte(self.byte30)
        self.reffed.write(buf)
    
    
    def changePageRaw(self,sid,did,stype,dtype):
        self.reffed.changePageRaw(sid,did,stype,dtype)
        self.oldRef.changePageRaw(sid,did,stype,dtype)


class plGenRefMsg(plRefMsg):
    def __init__(self,parent=None):
        plRefMsg.__init__(self,parent)
        self.byte1 = 0
        self.byte2 = 0
    
    
    def read(self,buf):
        plRefMsg.read(self,buf)
        self.byte1 = buf.ReadByte()
        self.byte2 = buf.ReadByte()
    
    
    def write(self,buf):
        plRefMsg.write(self,buf)
        buf.WriteByte(self.byte1)
        buf.WriteByte(self.byte2)