#include "version.h" #include #include "char.h" #include "object.h" #include "lssproto_serv.h" #include "npcutil.h" #include "handletime.h" static BOOL NPC_DengonReadString( int meindex, int id, char *str ); static void NPC_DengonWriteString( int meindex, int talkerindex, int id, char *str ); #define DENGONFILEDIR "./Dengon/" #define DENGONFILELINENUM 1000 #define DENGONFILESTRINGSIZE 256 #define DENGONFILECOUNTERSIZE 11 #define DENGONFILEENTRYSIZE (DENGONFILECOUNTERSIZE+DENGONFILESTRINGSIZE+1) #define MESSAGEINONEWINDOW 7 char NPC_sendbuf[DENGONFILEENTRYSIZE*MESSAGEINONEWINDOW]; BOOL NPC_DengonInit( int meindex ) { char filename[256], tmp[256]; FILE *f; int i,j; int id, maxid; CHAR_setInt( meindex, CHAR_WHICHTYPE, CHAR_TYPEDENGON ); snprintf( filename,sizeof(filename), "%s%d_%d_%d", DENGONFILEDIR, CHAR_getInt( meindex, CHAR_FLOOR ), CHAR_getInt( meindex, CHAR_X ), CHAR_getInt( meindex, CHAR_Y ) ); if( ! (f=fopen( filename, "r" )) ){ f = fopen( filename, "w" ); if( f ){ }else{ print( "ERROR:Can't create Dengonfile %s!\n",filename ); return FALSE; } for( j = 0; j < DENGONFILELINENUM; j++ ){ fwrite( "0000000000:", sizeof(char), DENGONFILECOUNTERSIZE, f); for( i=0;i maxid ) { maxid = id; } } CHAR_setWorkInt( meindex, CHAR_WORKDENGONMAXID, maxid ); fclose(f); return TRUE; } void NPC_DengonWindowTalked( int index, int talker, int seqno, int select, char *data) { char buf[DENGONFILEENTRYSIZE*MESSAGEINONEWINDOW*2]; int id; #define DENGONDISTANCE 3 if(NPC_Util_CharDistance(index,talker) > DENGONDISTANCE ) return; if( seqno == CHAR_WINDOWTYPE_DENGON) { int buttontype = 0; int maxid; switch( select ){ case WINDOW_BUTTONTYPE_NEXT: case WINDOW_BUTTONTYPE_PREV: id = CHAR_getWorkInt( talker, CHAR_WORKSHOPRELEVANT) + MESSAGEINONEWINDOW * (( select == WINDOW_BUTTONTYPE_NEXT) ? 1 : -1); maxid = CHAR_getWorkInt( index, CHAR_WORKDENGONMAXID ); if( id > maxid) id = maxid; else if( id < 2 ) id = 1; buttontype = WINDOW_BUTTONTYPE_OKCANCEL; if( id == maxid ) buttontype |= WINDOW_BUTTONTYPE_PREV; else if( id == 1 ) buttontype |= WINDOW_BUTTONTYPE_NEXT; else { buttontype |= WINDOW_BUTTONTYPE_PREV; buttontype |= WINDOW_BUTTONTYPE_NEXT; } CHAR_setWorkInt( talker, CHAR_WORKSHOPRELEVANT, id); if( NPC_DengonReadString( index, id, NPC_sendbuf ) ){ int fd; fd = getfdFromCharaIndex( talker ); if( fd == -1 )return; lssproto_WN_send(fd, WINDOW_MESSAGETYPE_WIDEMESSAGEANDLINEINPUT, buttontype, CHAR_WINDOWTYPE_DENGON, CHAR_getWorkInt( index, CHAR_WORKOBJINDEX), makeEscapeString( NPC_sendbuf, buf, sizeof(buf))); } break; case WINDOW_BUTTONTYPE_OK: if( strlen( data) == 0 ) break; id = CHAR_getWorkInt(index, CHAR_WORKDENGONMAXID ); id++; NPC_DengonWriteString( index, talker, id, data ); CHAR_setWorkInt(index, CHAR_WORKDENGONMAXID, id ); CHAR_setWorkInt( talker, CHAR_WORKSHOPRELEVANT, id); if( NPC_DengonReadString( index, id, NPC_sendbuf ) ){ int fd; fd = getfdFromCharaIndex( talker ); if( fd == -1 )return; lssproto_WN_send(fd, WINDOW_MESSAGETYPE_WIDEMESSAGEANDLINEINPUT, WINDOW_BUTTONTYPE_OKCANCEL| WINDOW_BUTTONTYPE_PREV, CHAR_WINDOWTYPE_DENGON, CHAR_getWorkInt( index, CHAR_WORKOBJINDEX), makeEscapeString( NPC_sendbuf, buf, sizeof(buf))); } break; default: break; } } } void NPC_DengonLooked( int meindex, int lookedindex ) { int maxid; char buf[DENGONFILEENTRYSIZE*MESSAGEINONEWINDOW*2]; if( NPC_Util_CharDistance( lookedindex, meindex ) > 1) return; maxid = CHAR_getWorkInt( meindex, CHAR_WORKDENGONMAXID ); if( NPC_DengonReadString( meindex, maxid, NPC_sendbuf ) ){ int fd; fd = getfdFromCharaIndex( lookedindex ); if( fd == -1 )return; lssproto_WN_send(fd, WINDOW_MESSAGETYPE_WIDEMESSAGEANDLINEINPUT, WINDOW_BUTTONTYPE_OKCANCEL| WINDOW_BUTTONTYPE_PREV, CHAR_WINDOWTYPE_DENGON, CHAR_getWorkInt( meindex, CHAR_WORKOBJINDEX), makeEscapeString( NPC_sendbuf, buf, sizeof(buf))); CHAR_setWorkInt( lookedindex, CHAR_WORKSHOPRELEVANT, maxid); } } static BOOL NPC_DengonReadString( int meindex, int id, char *str ) { char filename[256]; char readbuf[DENGONFILEENTRYSIZE*MESSAGEINONEWINDOW]; FILE *f; int maxid, i, sendid, len; char *writeptr, *readptr; maxid = CHAR_getWorkInt( meindex, CHAR_WORKDENGONMAXID ); if( id > maxid ) id = maxid; sendid = id - MESSAGEINONEWINDOW + 1; if( sendid < maxid - DENGONFILELINENUM + 1 ){ sendid = maxid - DENGONFILELINENUM + 1; } if( sendid < 1 ) sendid = 1; snprintf( filename,sizeof(filename), "%s%d_%d_%d", DENGONFILEDIR, CHAR_getInt( meindex, CHAR_FLOOR ), CHAR_getInt( meindex, CHAR_X ), CHAR_getInt( meindex, CHAR_Y ) ); writeptr = str; strcpy( writeptr, " 传言板\n"); len = strlen(writeptr); writeptr += len; f = fopen( filename, "r" ); if( !f ) return FALSE; { char buf[DENGONFILEENTRYSIZE]; int wptr = 0; readbuf[0] = '\0'; for( i = 0; i < MESSAGEINONEWINDOW; i ++ ) { fseek( f, ((sendid+i)%DENGONFILELINENUM)*DENGONFILEENTRYSIZE, SEEK_SET ); fread( buf, sizeof(char), DENGONFILEENTRYSIZE , f ); memcpy( &readbuf[wptr], buf,sizeof( buf)); wptr += DENGONFILEENTRYSIZE; } } fclose(f); readptr = readbuf + DENGONFILECOUNTERSIZE; for( i=0;i