~ubuntu-branches/debian/experimental/mudlet/experimental

« back to all changes in this revision

Viewing changes to .pc/freebsd_ifdef/src/TLuaInterpreter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Craig Small
  • Date: 2011-10-19 06:38:33 UTC
  • Revision ID: james.westby@ubuntu.com-20111019063833-0jlhszb532n3d24a
Tags: 2.0-rc11-2
* Conditionally compiles against lua or luajit Closes: #644649
* Changed ifdef so it is conditional on GLIBC and not Linux Closes:
  #644592

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008-2011 by Heiko Koehn                                     *
 
3
 *   KoehnHeiko@googlemail.com                                             *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
 ***************************************************************************/
 
20
 
 
21
 
 
22
#include <QDebug>
 
23
#include <QDir>
 
24
#include <QString>
 
25
#include <QRegExp>
 
26
#include <QNetworkAccessManager>
 
27
#include <QDesktopServices>
 
28
#include "TLuaInterpreter.h"
 
29
#include "TForkedProcess.h"
 
30
#include "TTrigger.h"
 
31
#include "HostManager.h"
 
32
#include "mudlet.h"
 
33
#include "TDebug.h"
 
34
#include <list>
 
35
#include <string>
 
36
#include "TEvent.h"
 
37
#include "dlgMapper.h"
 
38
 
 
39
 
 
40
 
 
41
//#ifdef Q_OS_WIN32
 
42
//    #include "lua_yajl.c"
 
43
//#else
 
44
    #include "lua_yajl1.c"
 
45
//#endif
 
46
 
 
47
extern "C"
 
48
{
 
49
    #include <lua.h>
 
50
    #include <lualib.h>
 
51
    #include <lauxlib.h>
 
52
}
 
53
#include <phonon>
 
54
 
 
55
extern QStringList gSysErrors;
 
56
 
 
57
using namespace std;
 
58
 
 
59
map<lua_State*, Host*> TLuaInterpreter::luaInterpreterMap;
 
60
 
 
61
TLuaInterpreter::TLuaInterpreter( Host * pH, int id )
 
62
:mpHost( pH )
 
63
,mHostID( id )
 
64
,purgeTimer(this)
 
65
{
 
66
    pGlobalLua = 0;
 
67
 
 
68
    connect(this,SIGNAL(signalEchoMessage(int, QString)), this,SLOT(slotEchoMessage(int,QString)));//,Qt::DirectConnection);
 
69
    connect(this,SIGNAL(signalNewCommand(int,QString)), this,SLOT(slotNewCommand(int,QString)));//,Qt::QueuedConnection);
 
70
 
 
71
    connect(this,SIGNAL(signalOpenUserWindow(int,QString)), this,SLOT(slotOpenUserWindow(int,QString)));
 
72
    connect(this,SIGNAL(signalEchoUserWindow(int,QString,QString)), this,SLOT(slotEchoUserWindow(int,QString,QString)));
 
73
    connect(this,SIGNAL(signalEnableTimer(int,QString)),this,SLOT(slotEnableTimer(int,QString)));
 
74
    connect(this,SIGNAL(signalDisableTimer(int,QString)),this,SLOT(slotDisableTimer(int,QString)));
 
75
    connect(this,SIGNAL(signalClearUserWindow(int,QString)),this,SLOT(slotClearUserWindow(int,QString)));
 
76
 
 
77
    connect(this, SIGNAL(signalSelect(int, QString, int)), this, SLOT(slotSelect(int,QString,int)));
 
78
    connect(this, SIGNAL(signalSelectSection(int, int,int)), this, SLOT(slotSelectSection(int,int,int)));
 
79
    connect(this, SIGNAL(signalTempTimer(int, double,QString,QString)), this, SLOT(slotTempTimer(int,double,QString,QString)));
 
80
 
 
81
    connect(this, SIGNAL(signalReplace(int, QString)), this, SLOT(slotReplace(int,QString)));
 
82
    connect(this, SIGNAL(signalSetFgColor(int, int,int,int)), this, SLOT(slotSetFgColor(int,int,int,int)));
 
83
    connect(this, SIGNAL(signalSetBgColor(int, int,int,int)), this, SLOT(slotSetBgColor(int,int,int,int)));
 
84
    connect(&purgeTimer, SIGNAL(timeout()), this, SLOT(slotPurge()));
 
85
 
 
86
    mpFileDownloader = new QNetworkAccessManager( this );
 
87
    connect(mpFileDownloader, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
 
88
 
 
89
    initLuaGlobals();
 
90
 
 
91
    purgeTimer.start(2000);
 
92
}
 
93
 
 
94
lua_State * TLuaInterpreter::getLuaExecutionUnit( int unit )
 
95
{
 
96
    switch( unit )
 
97
    {
 
98
        case 1:
 
99
            return pGlobalLua;
 
100
        case 2:
 
101
            return pGlobalLua;
 
102
        case 3:
 
103
            return pGlobalLua;
 
104
        case 4:
 
105
            return pGlobalLua;
 
106
        case 5:
 
107
            return pGlobalLua;
 
108
    };
 
109
    return 0;
 
110
}
 
111
 
 
112
void TLuaInterpreter::replyFinished(QNetworkReply * reply )
 
113
{
 
114
    if( ! downloadMap.contains(reply) ) return;
 
115
    QString name = downloadMap[reply];
 
116
    QFile file(name);
 
117
    file.open( QFile::WriteOnly );
 
118
    file.write( reply->readAll() );
 
119
    file.flush();
 
120
    file.close();
 
121
 
 
122
    TEvent * e = new TEvent;
 
123
    if( reply->error() == 0 )
 
124
    {
 
125
        e->mArgumentList << "sysDownloadDone";
 
126
        e->mArgumentTypeList << ARGUMENT_TYPE_STRING;
 
127
        e->mArgumentList << name;
 
128
        e->mArgumentTypeList << ARGUMENT_TYPE_STRING;
 
129
    }
 
130
    else
 
131
    {
 
132
        e->mArgumentList << "sysDownloadError";
 
133
        e->mArgumentTypeList << ARGUMENT_TYPE_STRING;
 
134
        e->mArgumentList << reply->errorString();
 
135
        e->mArgumentTypeList << ARGUMENT_TYPE_STRING;
 
136
    }
 
137
 
 
138
    mpHost->raiseEvent( e );
 
139
}
 
140
 
 
141
void TLuaInterpreter::slotDeleteSender() {
 
142
    objectsToDelete.append(sender());
 
143
}
 
144
 
 
145
void TLuaInterpreter::slotPurge() {
 
146
    while (!objectsToDelete.isEmpty()) {
 
147
        delete objectsToDelete.takeFirst();
 
148
    }
 
149
}
 
150
 
 
151
 
 
152
int TLuaInterpreter::Wait( lua_State *L )
 
153
{
 
154
  int n = lua_gettop( L );
 
155
  if(n!=1)
 
156
  {
 
157
      lua_pushstring( L, "Wait: wrong number of arguments" );
 
158
      lua_error( L );
 
159
      return 1;
 
160
  }
 
161
 
 
162
  int luaSleepMsec;
 
163
  if( ! lua_isnumber( L, 1 ) )
 
164
  {
 
165
      lua_pushstring( L, "Wait: wrong argument type" );
 
166
      lua_error( L );
 
167
      return 1;
 
168
  }
 
169
  else
 
170
  {
 
171
      luaSleepMsec = lua_tointeger( L, 1 );
 
172
  }
 
173
  msleep( luaSleepMsec );//FIXME thread::sleep()
 
174
  return 0;
 
175
}
 
176
 
 
177
int TLuaInterpreter::denyCurrentSend( lua_State * L )
 
178
{
 
179
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
180
    pHost->mAllowToSendCommand = false;
 
181
    return 0;
 
182
}
 
183
 
 
184
int TLuaInterpreter::raiseEvent( lua_State * L )
 
185
{
 
186
    TEvent * pE = new TEvent;
 
187
 
 
188
    int n = lua_gettop( L );
 
189
    for( int i=1; i<=n; i++)
 
190
    {
 
191
        if( lua_isnumber( L, i ) )
 
192
        {
 
193
            pE->mArgumentList.append( QString::number(lua_tonumber( L, i ) ) );
 
194
            pE->mArgumentTypeList.append( ARGUMENT_TYPE_NUMBER );
 
195
        }
 
196
        else if( lua_isstring( L, i ) )
 
197
        {
 
198
            pE->mArgumentList.append( QString(lua_tostring( L, i )) );
 
199
            pE->mArgumentTypeList.append( ARGUMENT_TYPE_STRING );
 
200
        }
 
201
    }
 
202
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
203
    pHost->raiseEvent( pE );
 
204
    return 0;
 
205
}
 
206
 
 
207
int TLuaInterpreter::resetProfile( lua_State * L )
 
208
{
 
209
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
210
    pHost->mResetProfile = true;
 
211
    return 0;
 
212
}
 
213
 
 
214
 
 
215
 
 
216
// cursorPositionInLine = select( text ) if not found -1
 
217
int TLuaInterpreter::select( lua_State * L )
 
218
{
 
219
    int s = 1;
 
220
    int n = lua_gettop( L );
 
221
    string a1;
 
222
    if( n > 2 )
 
223
    {
 
224
        if( ! lua_isstring( L, s ) )
 
225
        {
 
226
            lua_pushstring( L, "select: wrong argument type" );
 
227
          lua_error( L );
 
228
          return 1;
 
229
        }
 
230
        else
 
231
        {
 
232
            a1 = lua_tostring( L, s );
 
233
            s++;
 
234
        }
 
235
    }
 
236
    string luaSendText="";
 
237
    if( ! lua_isstring( L, s ) )
 
238
    {
 
239
        lua_pushstring( L, "select: wrong argument type" );
 
240
        lua_error( L );
 
241
        return 1;
 
242
    }
 
243
    else
 
244
    {
 
245
        luaSendText = lua_tostring( L, s );
 
246
        s++;
 
247
    }
 
248
    int luaNumOfMatch;
 
249
    if( ! lua_isnumber( L, s ) )
 
250
    {
 
251
        lua_pushstring( L, "select: wrong argument type" );
 
252
        lua_error( L );
 
253
        return 1;
 
254
    }
 
255
    else
 
256
    {
 
257
        luaNumOfMatch = lua_tointeger( L, s );
 
258
    }
 
259
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
260
    if( n == 2 )
 
261
    {
 
262
        int pos = pHost->mpConsole->select( QString( luaSendText.c_str() ), luaNumOfMatch );
 
263
        lua_pushnumber( L, pos );
 
264
        return 1;
 
265
    }
 
266
    else
 
267
    {
 
268
        QString _name(a1.c_str());
 
269
        int pos = mudlet::self()->selectString( pHost, _name, QString( luaSendText.c_str() ), luaNumOfMatch );
 
270
        lua_pushnumber( L, pos );
 
271
        return 1;
 
272
    }
 
273
}
 
274
 
 
275
int TLuaInterpreter::selectCurrentLine( lua_State * L )
 
276
{
 
277
    string luaSendText="";
 
278
    if( lua_gettop( L ) == 0 )
 
279
    {
 
280
        luaSendText = "main";
 
281
    }
 
282
    else
 
283
    {
 
284
        if( ! lua_isstring( L, 1 ) )
 
285
        {
 
286
            lua_pushstring( L, "selectCurrentLine: wrong argument type" );
 
287
            lua_error( L );
 
288
            return 1;
 
289
        }
 
290
        else
 
291
        {
 
292
            luaSendText = lua_tostring( L, 1 );
 
293
        }
 
294
    }
 
295
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
296
    pHost->mpConsole->selectCurrentLine( luaSendText );
 
297
    return 0;
 
298
}
 
299
 
 
300
int TLuaInterpreter::isAnsiFgColor( lua_State * L )
 
301
{
 
302
    int ansiFg;
 
303
 
 
304
    std::string console = "main";
 
305
 
 
306
    if( ! lua_isnumber( L, 1 ) )
 
307
    {
 
308
        lua_pushstring( L, "isAnsiFgColor: wrong argument type" );
 
309
        lua_error( L );
 
310
        return 1;
 
311
    }
 
312
    else
 
313
    {
 
314
        ansiFg = lua_tointeger( L, 1 );
 
315
    }
 
316
 
 
317
    std::list<int> result;
 
318
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
319
    result = pHost->mpConsole->getFgColor( console );
 
320
    typedef std::list<int>::iterator IT;
 
321
    IT it=result.begin();
 
322
    if( result.size() < 3 ) return 0;
 
323
    if( ansiFg < 0 ) return 0;
 
324
    if( ansiFg > 16 ) return 0;
 
325
 
 
326
 
 
327
    QColor c;
 
328
    switch( ansiFg )
 
329
    {
 
330
        case 0: c = pHost->mFgColor;  break;
 
331
        case 1: c = pHost->mLightBlack; break;
 
332
        case 2: c = pHost->mBlack; break;
 
333
        case 3: c = pHost->mLightRed; break;
 
334
        case 4: c = pHost->mRed; break;
 
335
        case 5: c = pHost->mLightGreen; break;
 
336
        case 6: c = pHost->mGreen; break;
 
337
        case 7: c = pHost->mLightYellow; break;
 
338
        case 8: c = pHost->mYellow; break;
 
339
        case 9: c = pHost->mLightBlue; break;
 
340
        case 10: c = pHost->mBlue; break;
 
341
        case 11: c = pHost->mLightMagenta; break;
 
342
        case 12: c = pHost->mMagenta; break;
 
343
        case 13: c = pHost->mLightCyan; break;
 
344
        case 14: c = pHost->mCyan; break;
 
345
        case 15: c = pHost->mLightWhite; break;
 
346
        case 16: c = pHost->mWhite; break;
 
347
    }
 
348
 
 
349
    int val = *it;
 
350
    if( val == c.red() )
 
351
    {
 
352
        it++;
 
353
        val = *it;
 
354
        if( val == c.green() )
 
355
        {
 
356
            it++;
 
357
            val = *it;
 
358
            if( val == c.blue() )
 
359
            {
 
360
                lua_pushboolean( L, 1 );
 
361
                return 1;
 
362
            }
 
363
        }
 
364
    }
 
365
 
 
366
    lua_pushboolean( L, 0 );
 
367
    return 1;
 
368
}
 
369
 
 
370
int TLuaInterpreter::isAnsiBgColor( lua_State * L )
 
371
{
 
372
    int ansiFg;
 
373
 
 
374
    std::string console = "main";
 
375
 
 
376
    if( ! lua_isnumber( L, 1 ) )
 
377
    {
 
378
        lua_pushstring( L, "isAnsiBgColor: wrong argument type" );
 
379
        lua_error( L );
 
380
        return 1;
 
381
    }
 
382
    else
 
383
    {
 
384
        ansiFg = lua_tointeger( L, 1 );
 
385
    }
 
386
 
 
387
    std::list<int> result;
 
388
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
389
    if( ! pHost ) return 0;
 
390
    result = pHost->mpConsole->getBgColor( console );
 
391
    typedef std::list<int>::iterator IT;
 
392
    IT it=result.begin();
 
393
    if( result.size() < 3 ) return 0;
 
394
    if( ansiFg < 0 ) return 0;
 
395
    if( ansiFg > 16 ) return 0;
 
396
 
 
397
 
 
398
    QColor c;
 
399
    switch( ansiFg )
 
400
    {
 
401
        case 0: c = pHost->mBgColor;  break;
 
402
        case 1: c = pHost->mLightBlack; break;
 
403
        case 2: c = pHost->mBlack; break;
 
404
        case 3: c = pHost->mLightRed; break;
 
405
        case 4: c = pHost->mRed; break;
 
406
        case 5: c = pHost->mLightGreen; break;
 
407
        case 6: c = pHost->mGreen; break;
 
408
        case 7: c = pHost->mLightYellow; break;
 
409
        case 8: c = pHost->mYellow; break;
 
410
        case 9: c = pHost->mLightBlue; break;
 
411
        case 10: c = pHost->mBlue; break;
 
412
        case 11: c = pHost->mLightMagenta; break;
 
413
        case 12: c = pHost->mMagenta; break;
 
414
        case 13: c = pHost->mLightCyan; break;
 
415
        case 14: c = pHost->mCyan; break;
 
416
        case 15: c = pHost->mLightWhite; break;
 
417
        case 16: c = pHost->mWhite; break;
 
418
    }
 
419
 
 
420
    int val = *it;
 
421
    if( val == c.red() )
 
422
    {
 
423
        it++;
 
424
        val = *it;
 
425
        if( val == c.green() )
 
426
        {
 
427
            it++;
 
428
            val = *it;
 
429
            if( val == c.blue() )
 
430
            {
 
431
                lua_pushboolean( L, 1 );
 
432
                return 1;
 
433
            }
 
434
        }
 
435
    }
 
436
 
 
437
    lua_pushboolean( L, 0 );
 
438
    return 1;
 
439
}
 
440
 
 
441
int TLuaInterpreter::getFgColor( lua_State * L )
 
442
{
 
443
    string luaSendText="";
 
444
    if( lua_gettop( L ) == 0 )
 
445
    {
 
446
        luaSendText = "main";
 
447
    }
 
448
    else
 
449
    {
 
450
        if( ! lua_isstring( L, 1 ) )
 
451
        {
 
452
            lua_pushstring( L, "getFgColor: wrong argument type" );
 
453
            lua_error( L );
 
454
            return 1;
 
455
        }
 
456
        else
 
457
        {
 
458
            luaSendText = lua_tostring( L, 1 );
 
459
        }
 
460
    }
 
461
    QString _name(luaSendText.c_str());
 
462
    std::list<int> result;
 
463
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
464
    result = pHost->mpConsole->getFgColor( luaSendText );
 
465
    typedef std::list<int>::iterator IT;
 
466
    for( IT it=result.begin(); it!=result.end(); it++ )
 
467
    {
 
468
        int pos = *it;
 
469
        lua_pushnumber( L, pos );
 
470
    }
 
471
    return result.size();
 
472
}
 
473
 
 
474
int TLuaInterpreter::getBgColor( lua_State * L )
 
475
{
 
476
    string luaSendText="";
 
477
    if( lua_gettop( L ) == 0 )
 
478
    {
 
479
        luaSendText = "main";
 
480
    }
 
481
    else
 
482
    {
 
483
        if( ! lua_isstring( L, 1 ) )
 
484
        {
 
485
            lua_pushstring( L, "getBgColor: wrong argument type" );
 
486
            lua_error( L );
 
487
            return 1;
 
488
        }
 
489
        else
 
490
        {
 
491
            luaSendText = lua_tostring( L, 1 );
 
492
        }
 
493
    }
 
494
 
 
495
    std::list<int> result;
 
496
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
497
    result = pHost->mpConsole->getBgColor( luaSendText );
 
498
    typedef std::list<int>::iterator IT;
 
499
    for( IT it=result.begin(); it!=result.end(); it++ )
 
500
    {
 
501
        int pos = *it;
 
502
        lua_pushnumber( L, pos );
 
503
    }
 
504
    return result.size();
 
505
}
 
506
 
 
507
int TLuaInterpreter::wrapLine( lua_State * L )
 
508
{
 
509
    int s = 1;
 
510
    int n = lua_gettop( L );
 
511
    string a1 = "main";
 
512
    if( n > 1 )
 
513
    {
 
514
        if( ! lua_isstring( L, s ) )
 
515
        {
 
516
            lua_pushstring( L, "wrapLine: wrong argument type" );
 
517
          lua_error( L );
 
518
          return 1;
 
519
        }
 
520
        else
 
521
        {
 
522
            a1 = lua_tostring( L, s );
 
523
            s++;
 
524
        }
 
525
    }
 
526
 
 
527
    int luaNumOfMatch;
 
528
    if( ! lua_isnumber( L, s ) )
 
529
    {
 
530
        lua_pushstring( L, "wrapLine: wrong argument type" );
 
531
        lua_error( L );
 
532
        return 1;
 
533
    }
 
534
    else
 
535
    {
 
536
        luaNumOfMatch = lua_tointeger( L, s );
 
537
    }
 
538
 
 
539
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
540
    pHost->mpConsole->luaWrapLine( a1, luaNumOfMatch );
 
541
    return 0;
 
542
}
 
543
 
 
544
 
 
545
 
 
546
int TLuaInterpreter::spawn( lua_State * L )
 
547
{
 
548
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
549
    return TForkedProcess::startProcess(pHost->getLuaInterpreter(), L);
 
550
}
 
551
 
 
552
 
 
553
 
 
554
// cursorPositionInLine = selectCaptureGroup( groupNumber ) if not found -1
 
555
int TLuaInterpreter::selectCaptureGroup( lua_State * L )
 
556
{
 
557
    int luaNumOfMatch;
 
558
    if( ! lua_isnumber( L, 1 ) )
 
559
    {
 
560
        lua_pushstring( L, "selectCaptureGroup: wrong argument type" );
 
561
        lua_error( L );
 
562
        return 1;
 
563
    }
 
564
    else
 
565
    {
 
566
        luaNumOfMatch = lua_tointeger( L, 1 );
 
567
    }
 
568
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
569
    if( luaNumOfMatch < 1 )
 
570
    {
 
571
        lua_pushnumber( L, -1 );
 
572
        return 1;
 
573
    }
 
574
    luaNumOfMatch--; //we want capture groups to start with 1 instead of 0
 
575
    if( luaNumOfMatch < static_cast<int>(pHost->getLuaInterpreter()->mCaptureGroupList.size()) )
 
576
    {
 
577
        TLuaInterpreter * pL = pHost->getLuaInterpreter();
 
578
        std::list<std::string>::iterator its = pL->mCaptureGroupList.begin();
 
579
        std::list<int>::iterator iti = pL->mCaptureGroupPosList.begin();
 
580
 
 
581
        for( int i=0; iti!=pL->mCaptureGroupPosList.end(); ++iti,++i )
 
582
        {
 
583
            if( i >= luaNumOfMatch ) break;
 
584
        }
 
585
        for( int i=0; its!=pL->mCaptureGroupList.end(); ++its,++i)
 
586
        {
 
587
            if( i >= luaNumOfMatch ) break;
 
588
        }
 
589
 
 
590
        int begin = *iti;
 
591
        std::string & s = *its;
 
592
        int length = s.size();
 
593
        //cout << "selectSection("<<begin<<", "<<length<<")"<<endl;
 
594
        if( mudlet::debugMode ) {TDebug(QColor(Qt::white),QColor(Qt::red))<<"selectCaptureGroup("<<begin<<", "<<length<<")\n">>0;}
 
595
        int pos = pHost->mpConsole->selectSection( begin, length );
 
596
        lua_pushnumber( L, pos );
 
597
    }
 
598
    else
 
599
    {
 
600
        lua_pushnumber( L, -1 );
 
601
    }
 
602
    return 1;
 
603
}
 
604
 
 
605
// luaTable result[line_number, content] = getLines( from_cursorPos, to_cursorPos )
 
606
int TLuaInterpreter::getLines( lua_State * L )
 
607
{
 
608
    int luaFrom;
 
609
    if( ! lua_isnumber( L, 1 ) )
 
610
    {
 
611
        lua_pushstring( L, "getLines: wrong argument type" );
 
612
        lua_error( L );
 
613
        return 1;
 
614
    }
 
615
    else
 
616
    {
 
617
        luaFrom = lua_tointeger( L, 1 );
 
618
    }
 
619
 
 
620
    int luaTo;
 
621
    if( ! lua_isnumber( L, 2 ) )
 
622
    {
 
623
        lua_pushstring( L, "getLines: wrong argument type" );
 
624
        lua_error( L );
 
625
        return 1;
 
626
    }
 
627
    else
 
628
    {
 
629
        luaTo=lua_tointeger( L, 2 );
 
630
    }
 
631
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
632
    QStringList strList = pHost->mpConsole->getLines( luaFrom, luaTo );
 
633
 
 
634
    lua_newtable(L);
 
635
    for( int i=0; i<strList.size(); i++ )
 
636
    {
 
637
        lua_pushnumber( L, i+1 );
 
638
        lua_pushstring( L, strList[i].toLatin1().data() );
 
639
        lua_settable(L, -3);
 
640
    }
 
641
    return 1;
 
642
}
 
643
 
 
644
// luaTable result[line_number, content] = getLines( from_cursorPos, to_cursorPos )
 
645
int TLuaInterpreter::getBufferTable( lua_State * L )
 
646
{
 
647
    int luaFrom;
 
648
    if( ! lua_isnumber( L, 1 ) )
 
649
    {
 
650
        lua_pushstring( L, "getBufferTable: wrong argument type" );
 
651
        lua_error( L );
 
652
        return 1;
 
653
    }
 
654
    else
 
655
    {
 
656
        luaFrom = lua_tointeger( L, 1 );
 
657
    }
 
658
 
 
659
    int luaTo;
 
660
    if( ! lua_isnumber( L, 2 ) )
 
661
    {
 
662
        lua_pushstring( L, "getBufferTable: wrong argument type" );
 
663
        lua_error( L );
 
664
        return 1;
 
665
    }
 
666
    else
 
667
    {
 
668
        luaTo=lua_tointeger( L, 2 );
 
669
    }
 
670
    /*Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
671
    QStringList strList = pHost->getBufferTable( luaFrom, luaTo );
 
672
    if( mudlet::debugMode ) qDebug()<<"TLuaInterpreter::getBufferTable() strList="<<strList;
 
673
    lua_newtable(L);
 
674
    for( int i=0; i<strList.size(); i++ )
 
675
    {
 
676
        lua_pushnumber( L, i+1 );
 
677
        lua_pushstring( L, strList[i].toLatin1().data() );
 
678
        lua_settable(L, -3);
 
679
    } */
 
680
    return 0;
 
681
}
 
682
 
 
683
int TLuaInterpreter::loadRawFile( lua_State * L )
 
684
{
 
685
    string luaSendText="";
 
686
    if( ! lua_isstring( L, 1 ) )
 
687
    {
 
688
        lua_pushstring( L, "loadRawFile: wrong argument type" );
 
689
        lua_error( L );
 
690
        return 1;
 
691
    }
 
692
    else
 
693
    {
 
694
        luaSendText = lua_tostring( L, 1 );
 
695
    }
 
696
 
 
697
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
698
    pHost->mpConsole->loadRawFile( luaSendText );
 
699
    return 0;
 
700
}
 
701
 
 
702
int TLuaInterpreter::getCurrentLine( lua_State * L )
 
703
{
 
704
    string luaSendText="";
 
705
    if( lua_gettop( L ) == 0 )
 
706
    {
 
707
        luaSendText = "main";
 
708
    }
 
709
    else
 
710
    {
 
711
        if( ! lua_isstring( L, 1 ) )
 
712
        {
 
713
            lua_pushstring( L, "getCurrentLine: wrong argument type" );
 
714
            lua_error( L );
 
715
            return 1;
 
716
        }
 
717
        else
 
718
        {
 
719
            luaSendText = lua_tostring( L, 1 );
 
720
        }
 
721
    }
 
722
 
 
723
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
724
    QString line = pHost->mpConsole->getCurrentLine( luaSendText );
 
725
    lua_pushstring( L, line.toLatin1().data() );
 
726
    return 1;
 
727
}
 
728
 
 
729
int TLuaInterpreter::setMiniConsoleFontSize( lua_State * L )
 
730
{
 
731
    string luaSendText="";
 
732
    if( ! lua_isstring( L, 1 ) )
 
733
    {
 
734
        lua_pushstring( L, "setMiniConsoleFontSize: wrong argument type" );
 
735
        lua_error( L );
 
736
        return 1;
 
737
    }
 
738
    else
 
739
    {
 
740
        luaSendText = lua_tostring( L, 1 );
 
741
    }
 
742
    int luaNumOfMatch;
 
743
    if( ! lua_isnumber( L, 2 ) )
 
744
    {
 
745
        lua_pushstring( L, "setMiniConsoleFontSize: wrong argument type" );
 
746
        lua_error( L );
 
747
        return 1;
 
748
    }
 
749
    else
 
750
    {
 
751
        luaNumOfMatch = lua_tointeger( L, 2 );
 
752
    }
 
753
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
754
    pHost->mpConsole->setMiniConsoleFontSize( luaSendText, luaNumOfMatch );
 
755
    return 0;
 
756
}
 
757
 
 
758
// returns current y position of the user cursor
 
759
int TLuaInterpreter::getLineNumber( lua_State * L )
 
760
{
 
761
 
 
762
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
763
    int lineNumber = pHost->mpConsole->getLineNumber();
 
764
    lua_pushnumber( L, lineNumber );
 
765
    return 1;
 
766
}
 
767
 
 
768
int TLuaInterpreter::centerview( lua_State * L )
 
769
{
 
770
    int roomid;
 
771
    if( lua_isnumber( L, 1 ) || lua_isstring( L, 1 ) )
 
772
    {
 
773
        roomid = lua_tointeger( L, 1 );
 
774
    }
 
775
    else
 
776
    {
 
777
        lua_pushstring( L, "centerview: need a valid room ID" );
 
778
        lua_error( L );
 
779
        return 1;
 
780
    }
 
781
 
 
782
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
783
    if( pHost->mpMap && pHost->mpMap->rooms.contains( roomid ) )
 
784
    {
 
785
        pHost->mpMap->mRoomId = roomid;
 
786
        pHost->mpMap->mNewMove = true;
 
787
        if( pHost->mpMap->mpM )
 
788
        {
 
789
            pHost->mpMap->mpM->update();
 
790
        }
 
791
        if( pHost->mpMap->mpM )
 
792
        {
 
793
            pHost->mpMap->mpMapper->mp2dMap->update();
 
794
        }
 
795
 
 
796
    }
 
797
 
 
798
    return 0;
 
799
}
 
800
 
 
801
int TLuaInterpreter::copy( lua_State * L )
 
802
{
 
803
    string luaWindowName="";
 
804
    if( lua_isstring( L, 1 ) )
 
805
    {
 
806
        luaWindowName = lua_tostring( L, 1 );
 
807
    }
 
808
    else
 
809
        luaWindowName = "main";
 
810
 
 
811
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
812
    QString windowName(luaWindowName.c_str());
 
813
    if( luaWindowName == "main" )
 
814
        pHost->mpConsole->copy();
 
815
    else
 
816
       mudlet::self()->copy( pHost, windowName );
 
817
    return 0;
 
818
}
 
819
int TLuaInterpreter::cut( lua_State * L )
 
820
{
 
821
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
822
    pHost->mpConsole->cut();
 
823
    return 0;
 
824
}
 
825
int TLuaInterpreter::paste( lua_State * L )
 
826
{
 
827
    string luaWindowName="";
 
828
    if( lua_isstring( L, 1 ) )
 
829
    {
 
830
        luaWindowName = lua_tostring( L, 1 );
 
831
    }
 
832
    else
 
833
        luaWindowName = "main";
 
834
 
 
835
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
836
    QString windowName(luaWindowName.c_str());
 
837
    if( luaWindowName == "main" )
 
838
        pHost->mpConsole->paste();
 
839
    else
 
840
       mudlet::self()->pasteWindow( pHost, windowName );
 
841
    return 0;
 
842
}
 
843
 
 
844
 
 
845
int TLuaInterpreter::feedTriggers( lua_State * L )
 
846
{
 
847
    string luaWindowName="";
 
848
    if( lua_isstring( L, 1 ) )
 
849
    {
 
850
        luaWindowName = lua_tostring( L, 1 );
 
851
    }
 
852
    else
 
853
    {
 
854
        lua_pushstring( L, "feedTriggers: wrong argument type" );
 
855
        lua_error( L );
 
856
        return 1;
 
857
    }
 
858
 
 
859
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
860
    pHost->mpConsole->printOnDisplay( luaWindowName );
 
861
    return 0;
 
862
}
 
863
 
 
864
 
 
865
int TLuaInterpreter::isPrompt( lua_State *L )
 
866
{
 
867
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
868
    int userCursorY = pHost->mpConsole->getLineNumber();
 
869
    if( userCursorY < pHost->mpConsole->buffer.promptBuffer.size() && userCursorY >= 0 )
 
870
    {
 
871
        lua_pushboolean( L, pHost->mpConsole->buffer.promptBuffer.at( userCursorY ) );
 
872
        return 1;
 
873
    }
 
874
    else
 
875
    {
 
876
        if( pHost->mpConsole->mTriggerEngineMode && pHost->mpConsole->mIsPromptLine )
 
877
            lua_pushboolean( L, true );
 
878
        else
 
879
            lua_pushboolean( L, false );
 
880
        return 1;
 
881
    }
 
882
}
 
883
 
 
884
int TLuaInterpreter::setWindowWrap( lua_State * L )
 
885
{
 
886
    string luaSendText="";
 
887
    if( ! lua_isstring( L, 1 ) )
 
888
    {
 
889
        lua_pushstring( L, "setWindowWrap: wrong argument type" );
 
890
        lua_error( L );
 
891
        return 1;
 
892
    }
 
893
    else
 
894
    {
 
895
        luaSendText = lua_tostring( L, 1 );
 
896
    }
 
897
    int luaFrom;
 
898
    if( ! lua_isnumber( L, 2 ) )
 
899
    {
 
900
        lua_pushstring( L, "setWindowWrap: wrong argument type" );
 
901
        lua_error( L );
 
902
        return 1;
 
903
    }
 
904
    else
 
905
    {
 
906
        luaFrom = lua_tointeger( L, 2 );
 
907
    }
 
908
 
 
909
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
910
    QString name = luaSendText.c_str();
 
911
    if( name == "main" )
 
912
        pHost->mpConsole->setWrapAt( luaFrom );
 
913
    else
 
914
        mudlet::self()->setWindowWrap( pHost, name, luaFrom );
 
915
    return 0;
 
916
}
 
917
 
 
918
int TLuaInterpreter::setWindowWrapIndent( lua_State * L )
 
919
{
 
920
    string luaSendText="";
 
921
    if( ! lua_isstring( L, 1 ) )
 
922
    {
 
923
        lua_pushstring( L, "setWindowWrapIndent: wrong argument type" );
 
924
        lua_error( L );
 
925
        return 1;
 
926
    }
 
927
    else
 
928
    {
 
929
        luaSendText = lua_tostring( L, 1 );
 
930
    }
 
931
    int luaFrom;
 
932
    if( ! lua_isnumber( L, 2 ) )
 
933
    {
 
934
        lua_pushstring( L, "setWindowWrapIndent: wrong argument type" );
 
935
        lua_error( L );
 
936
        return 1;
 
937
    }
 
938
    else
 
939
    {
 
940
        luaFrom = lua_tointeger( L, 2 );
 
941
    }
 
942
 
 
943
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
944
    QString name = luaSendText.c_str();
 
945
    mudlet::self()->setWindowWrapIndent( pHost, name, luaFrom );
 
946
    return 0;
 
947
}
 
948
 
 
949
int TLuaInterpreter::getLineCount( lua_State * L )
 
950
{
 
951
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
952
    if( lua_isstring( L, 1 ) )
 
953
    {
 
954
        string window = lua_tostring( L, 1 );
 
955
        QString _window = window.c_str();
 
956
        lua_pushnumber( L, mudlet::self()->getLastLineNumber( pHost, _window ) + 1 );
 
957
        return 1;
 
958
    }
 
959
    else
 
960
    {
 
961
        int lineNumber = pHost->mpConsole->getLineCount();
 
962
        lua_pushnumber( L, lineNumber );
 
963
        return 1;
 
964
    }
 
965
    return 0;
 
966
}
 
967
 
 
968
int TLuaInterpreter::getColumnNumber( lua_State * L )
 
969
{
 
970
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
971
    int lineNumber = pHost->mpConsole->getColumnNumber();
 
972
    lua_pushnumber( L, lineNumber );
 
973
    return 1;
 
974
}
 
975
 
 
976
int TLuaInterpreter::getStopWatchTime( lua_State * L )
 
977
{
 
978
    int watchID;
 
979
    if( ! lua_isnumber( L, 1 ) )
 
980
    {
 
981
        lua_pushstring( L, "getStopWatchTime: wrong argument type" );
 
982
        lua_error( L );
 
983
        return 1;
 
984
    }
 
985
    else
 
986
    {
 
987
        watchID = lua_tointeger( L, 1 );
 
988
    }
 
989
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
990
    double time = pHost->getStopWatchTime( watchID );
 
991
    lua_pushnumber( L, time );
 
992
    return 1;
 
993
}
 
994
 
 
995
int TLuaInterpreter::createStopWatch( lua_State * L )
 
996
{
 
997
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
998
    double watchID = pHost->createStopWatch();
 
999
    lua_pushnumber( L, watchID );
 
1000
    return 1;
 
1001
}
 
1002
 
 
1003
int TLuaInterpreter::stopStopWatch( lua_State * L )
 
1004
{
 
1005
    int watchID;
 
1006
    if( ! lua_isnumber( L, 1 ) )
 
1007
    {
 
1008
        lua_pushstring( L, "stopStopWatch: wrong argument type" );
 
1009
        lua_error( L );
 
1010
        return 1;
 
1011
    }
 
1012
    else
 
1013
    {
 
1014
        watchID = lua_tointeger( L, 1 );
 
1015
    }
 
1016
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1017
    double time = pHost->stopStopWatch( watchID );
 
1018
    lua_pushnumber( L, time );
 
1019
    return 1;
 
1020
}
 
1021
 
 
1022
int TLuaInterpreter::startStopWatch( lua_State * L )
 
1023
{
 
1024
    int watchID;
 
1025
    if( ! lua_isnumber( L, 1 ) )
 
1026
    {
 
1027
        lua_pushstring( L, "startStopWatch: wrong argument type" );
 
1028
        lua_error( L );
 
1029
        return 1;
 
1030
    }
 
1031
    else
 
1032
    {
 
1033
        watchID = lua_tointeger( L, 1 );
 
1034
    }
 
1035
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1036
    bool b = pHost->startStopWatch( watchID );
 
1037
    lua_pushboolean( L, b );
 
1038
    return 1;
 
1039
}
 
1040
 
 
1041
int TLuaInterpreter::resetStopWatch( lua_State * L )
 
1042
{
 
1043
    int watchID;
 
1044
    if( ! lua_isnumber( L, 1 ) )
 
1045
    {
 
1046
        lua_pushstring( L, "resetStopWatch: wrong argument type" );
 
1047
        lua_error( L );
 
1048
        return 1;
 
1049
    }
 
1050
    else
 
1051
    {
 
1052
        watchID = lua_tointeger( L, 1 );
 
1053
    }
 
1054
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1055
    bool b = pHost->resetStopWatch( watchID );
 
1056
    lua_pushboolean( L, b );
 
1057
    return 1;
 
1058
}
 
1059
 
 
1060
 
 
1061
 
 
1062
 
 
1063
// cusorPositionInLine = selectSection( from_cursorPos, to_cursorPos ) -1 on not found
 
1064
int TLuaInterpreter::selectSection( lua_State * L )
 
1065
{
 
1066
    int s = 1;
 
1067
    int n = lua_gettop( L );
 
1068
    string a1;
 
1069
    if( n > 2 )
 
1070
    {
 
1071
        if( ! lua_isstring( L, s ) )
 
1072
        {
 
1073
            lua_pushstring( L, "selectSection: wrong argument type" );
 
1074
          lua_error( L );
 
1075
          return 1;
 
1076
        }
 
1077
        else
 
1078
        {
 
1079
            a1 = lua_tostring( L, s );
 
1080
            s++;
 
1081
        }
 
1082
    }
 
1083
    int luaFrom;
 
1084
    if( ! lua_isnumber( L, s ) )
 
1085
    {
 
1086
        lua_pushstring( L, "selectSection: wrong argument type" );
 
1087
        lua_error( L );
 
1088
        return 1;
 
1089
    }
 
1090
    else
 
1091
    {
 
1092
        luaFrom = lua_tointeger( L, s );
 
1093
        s++;
 
1094
    }
 
1095
 
 
1096
    int luaTo;
 
1097
    if( ! lua_isnumber( L, s ) )
 
1098
    {
 
1099
        lua_pushstring( L, "selectSection: wrong argument type" );
 
1100
        lua_error( L );
 
1101
        return 1;
 
1102
    }
 
1103
    else
 
1104
    {
 
1105
        luaTo=lua_tointeger( L, s );
 
1106
    }
 
1107
 
 
1108
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1109
 
 
1110
    bool ret;
 
1111
    if( n > 2 )
 
1112
    {
 
1113
        QString _name = a1.c_str();
 
1114
        ret = mudlet::self()->selectSection( pHost, _name, luaFrom, luaTo );
 
1115
    }
 
1116
    else
 
1117
    {
 
1118
        ret = pHost->mpConsole->selectSection( luaFrom, luaTo );
 
1119
    }
 
1120
    lua_pushboolean( L, ret );
 
1121
    return 1;
 
1122
}
 
1123
 
 
1124
 
 
1125
int TLuaInterpreter::moveCursor( lua_State * L )
 
1126
{
 
1127
    int s = 1;
 
1128
    int n = lua_gettop( L );
 
1129
    string a1;
 
1130
    if( n > 2 )
 
1131
    {
 
1132
        if( ! lua_isstring( L, s ) )
 
1133
        {
 
1134
            lua_pushstring( L, "moveCursor: wrong argument type" );
 
1135
          lua_error( L );
 
1136
          return 1;
 
1137
        }
 
1138
        else
 
1139
        {
 
1140
            a1 = lua_tostring( L, s );
 
1141
            s++;
 
1142
        }
 
1143
    }
 
1144
    int luaFrom;
 
1145
    if( ! lua_isnumber( L, s ) )
 
1146
    {
 
1147
        lua_pushstring( L, "moveCursor: wrong argument type" );
 
1148
        lua_error( L );
 
1149
        return 1;
 
1150
    }
 
1151
    else
 
1152
    {
 
1153
        luaFrom = lua_tointeger( L, s );
 
1154
        s++;
 
1155
    }
 
1156
 
 
1157
    int luaTo;
 
1158
    if( ! lua_isnumber( L, s ) )
 
1159
    {
 
1160
        lua_pushstring( L, "moveCursor: wrong argument type" );
 
1161
        lua_error( L );
 
1162
        return 1;
 
1163
    }
 
1164
    else
 
1165
    {
 
1166
        luaTo=lua_tointeger( L, s );
 
1167
    }
 
1168
 
 
1169
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1170
 
 
1171
    if( a1 == "main" || n < 3 )
 
1172
        lua_pushboolean( L, pHost->mpConsole->moveCursor( luaFrom, luaTo ) );
 
1173
    else
 
1174
    {
 
1175
        QString windowName = a1.c_str();
 
1176
        lua_pushboolean( L, mudlet::self()->moveCursor( pHost, windowName, luaFrom, luaTo ) );
 
1177
    }
 
1178
    return 1;
 
1179
}
 
1180
 
 
1181
int TLuaInterpreter::setConsoleBufferSize( lua_State * L )
 
1182
{
 
1183
    int s = 1;
 
1184
    int n = lua_gettop( L );
 
1185
    string a1;
 
1186
    if( n > 2 )
 
1187
    {
 
1188
        if( ! lua_isstring( L, s ) )
 
1189
        {
 
1190
            lua_pushstring( L, "setConsoleBufferSize: wrong argument type" );
 
1191
          lua_error( L );
 
1192
          return 1;
 
1193
        }
 
1194
        else
 
1195
        {
 
1196
            a1 = lua_tostring( L, s );
 
1197
            s++;
 
1198
        }
 
1199
    }
 
1200
    int luaFrom;
 
1201
    if( ! lua_isnumber( L, s ) )
 
1202
    {
 
1203
        lua_pushstring( L, "setConsoleBufferSize: wrong argument type" );
 
1204
        lua_error( L );
 
1205
        return 1;
 
1206
    }
 
1207
    else
 
1208
    {
 
1209
        luaFrom = lua_tointeger( L, s );
 
1210
        s++;
 
1211
    }
 
1212
 
 
1213
    int luaTo;
 
1214
    if( ! lua_isnumber( L, s ) )
 
1215
    {
 
1216
        lua_pushstring( L, "setConsoleBufferSize: wrong argument type" );
 
1217
        lua_error( L );
 
1218
        return 1;
 
1219
    }
 
1220
    else
 
1221
    {
 
1222
        luaTo=lua_tointeger( L, s );
 
1223
    }
 
1224
 
 
1225
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1226
 
 
1227
    if( a1 == "main" || n < 3 )
 
1228
    {
 
1229
        pHost->mpConsole->buffer.setBufferSize( luaFrom, luaTo );
 
1230
    }
 
1231
    else
 
1232
    {
 
1233
        QString windowName = a1.c_str();
 
1234
        mudlet::self()->setConsoleBufferSize( pHost, windowName, luaFrom, luaTo );
 
1235
    }
 
1236
    return 0;
 
1237
}
 
1238
 
 
1239
int TLuaInterpreter::getBufferLine( lua_State * L )
 
1240
{
 
1241
    int luaLine;
 
1242
    if( ! lua_isnumber( L, 1 ) )
 
1243
    {
 
1244
        lua_pushstring( L, "getBufferLine: wrong argument type" );
 
1245
        lua_error( L );
 
1246
        return 1;
 
1247
    }
 
1248
    else
 
1249
    {
 
1250
        luaLine = lua_tointeger( L, 1 );
 
1251
    }
 
1252
 
 
1253
    /*Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1254
    QString line = pHost->getBufferLine( luaLine );
 
1255
    if( mudlet::debugMode ) qDebug()<<"TLuaInterpreter::getBufferLine() line="<<line;
 
1256
    lua_pushstring( L, line.toLatin1().data() );*/
 
1257
    return 0;
 
1258
}
 
1259
 
 
1260
// replace( sessionID, replace_with )
 
1261
int TLuaInterpreter::replace( lua_State * L )
 
1262
{
 
1263
    string a1 = "";
 
1264
    string a2 = "";
 
1265
    int n = lua_gettop( L );
 
1266
    int s = 1;
 
1267
    if( ! lua_isstring( L, s ) )
 
1268
    {
 
1269
        lua_pushstring( L, "replace: wrong argument type" );
 
1270
        lua_error( L );
 
1271
        return 1;
 
1272
    }
 
1273
    else
 
1274
    {
 
1275
        a1 = lua_tostring( L, s );
 
1276
        s++;
 
1277
    }
 
1278
 
 
1279
    QString _name( a1.c_str() );
 
1280
    string luaSendText="";
 
1281
    if( n > 1 )
 
1282
    {
 
1283
        if( ! lua_isstring( L, s ) )
 
1284
        {
 
1285
            lua_pushstring( L, "replace: wrong argument type" );
 
1286
            lua_error( L );
 
1287
            return 1;
 
1288
        }
 
1289
        else
 
1290
        {
 
1291
            a2 = lua_tostring( L, s );
 
1292
        }
 
1293
    }
 
1294
 
 
1295
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1296
    if( n == 1 )
 
1297
        pHost->mpConsole->replace( QString(a1.c_str()) );
 
1298
    else
 
1299
        mudlet::self()->replace( pHost, _name, QString(a2.c_str()) );
 
1300
    return 0;
 
1301
}
 
1302
 
 
1303
int TLuaInterpreter::deleteLine( lua_State * L )
 
1304
{
 
1305
    string name="";
 
1306
    if( lua_gettop( L ) == 1 )
 
1307
    {
 
1308
        if( ! lua_isstring( L, 1 ) )
 
1309
        {
 
1310
            lua_pushstring( L, "deleteLine: wrong argument type" );
 
1311
            lua_error( L );
 
1312
            return 1;
 
1313
        }
 
1314
        else
 
1315
        {
 
1316
            name = lua_tostring( L, 1 );
 
1317
        }
 
1318
    }
 
1319
 
 
1320
    QString _name( name.c_str() );
 
1321
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1322
 
 
1323
    if( name == "" )
 
1324
        pHost->mpConsole->skipLine();
 
1325
    else
 
1326
        mudlet::self()->deleteLine( pHost, _name );
 
1327
    return 0;
 
1328
}
 
1329
 
 
1330
int TLuaInterpreter::saveMap( lua_State * L )
 
1331
{
 
1332
    string location="";
 
1333
    if( lua_gettop( L ) == 1 )
 
1334
    {
 
1335
        if( ! lua_isstring( L, 1 ) )
 
1336
        {
 
1337
            lua_pushstring( L, "saveMap: where do you want to save to?" );
 
1338
            lua_error( L );
 
1339
            return 1;
 
1340
        }
 
1341
        else
 
1342
        {
 
1343
            location = lua_tostring( L, 1 );
 
1344
        }
 
1345
    }
 
1346
 
 
1347
    QString _location( location.c_str() );
 
1348
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1349
 
 
1350
    bool error = pHost->mpConsole->saveMap(_location);
 
1351
    lua_pushboolean( L, error );
 
1352
    return 1;
 
1353
}
 
1354
 
 
1355
// enableTimer( sess, timer_name )
 
1356
int TLuaInterpreter::enableTimer( lua_State *L )
 
1357
{
 
1358
    string luaSendText="";
 
1359
    if( ! lua_isstring( L, 1 ) )
 
1360
    {
 
1361
        lua_pushstring( L, "enableTimer: wrong argument type" );
 
1362
        lua_error( L );
 
1363
        return 1;
 
1364
    }
 
1365
    else
 
1366
    {
 
1367
        luaSendText = lua_tostring( L, 1 );
 
1368
    }
 
1369
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1370
    QString text(luaSendText.c_str());
 
1371
    bool error = pHost->getTimerUnit()->enableTimer( text );
 
1372
    lua_pushboolean( L, error );
 
1373
    return 1;
 
1374
}
 
1375
 
 
1376
// disableTimer( session, timer_name )
 
1377
int TLuaInterpreter::disableTimer( lua_State *L )
 
1378
{
 
1379
    string luaSendText="";
 
1380
    if( ! lua_isstring( L, 1 ) )
 
1381
    {
 
1382
        lua_pushstring( L, "disableTimer: wrong argument type" );
 
1383
        lua_error( L );
 
1384
        return 1;
 
1385
    }
 
1386
    else
 
1387
    {
 
1388
        luaSendText = lua_tostring( L, 1 );
 
1389
    }
 
1390
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1391
    QString text(luaSendText.c_str());
 
1392
    bool error = pHost->getTimerUnit()->disableTimer( text );
 
1393
    lua_pushboolean( L, error );
 
1394
    return 1;
 
1395
}
 
1396
 
 
1397
int TLuaInterpreter::enableKey( lua_State *L )
 
1398
{
 
1399
    string luaSendText="";
 
1400
    if( ! lua_isstring( L, 1 ) )
 
1401
    {
 
1402
        lua_pushstring( L, "enableKey: wrong argument type" );
 
1403
        lua_error( L );
 
1404
        return 1;
 
1405
    }
 
1406
    else
 
1407
    {
 
1408
        luaSendText = lua_tostring( L, 1 );
 
1409
    }
 
1410
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1411
    QString text(luaSendText.c_str());
 
1412
    bool error = pHost->getKeyUnit()->enableKey( text );
 
1413
    lua_pushboolean( L, error );
 
1414
    return 1;
 
1415
}
 
1416
 
 
1417
// disableTimer( session, timer_name )
 
1418
int TLuaInterpreter::disableKey( lua_State *L )
 
1419
{
 
1420
    string luaSendText="";
 
1421
    if( ! lua_isstring( L, 1 ) )
 
1422
    {
 
1423
        lua_pushstring( L, "disableKey: wrong argument type" );
 
1424
        lua_error( L );
 
1425
        return 1;
 
1426
    }
 
1427
    else
 
1428
    {
 
1429
        luaSendText = lua_tostring( L, 1 );
 
1430
    }
 
1431
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1432
    QString text(luaSendText.c_str());
 
1433
    bool error = pHost->getKeyUnit()->disableKey( text );
 
1434
    lua_pushboolean( L, error );
 
1435
    return 1;
 
1436
}
 
1437
 
 
1438
int TLuaInterpreter::enableAlias( lua_State *L )
 
1439
{
 
1440
    string luaSendText="";
 
1441
    if( ! lua_isstring( L, 1 ) )
 
1442
    {
 
1443
        lua_pushstring( L, "enableAlias: wrong argument type" );
 
1444
        lua_error( L );
 
1445
        return 1;
 
1446
    }
 
1447
    else
 
1448
    {
 
1449
        luaSendText = lua_tostring( L, 1 );
 
1450
    }
 
1451
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1452
    QString text(luaSendText.c_str());
 
1453
    bool error = pHost->getAliasUnit()->enableAlias( text );
 
1454
    lua_pushboolean( L, error );
 
1455
    return 1;
 
1456
}
 
1457
 
 
1458
// disableTimer( session, timer_name )
 
1459
int TLuaInterpreter::disableAlias( lua_State *L )
 
1460
{
 
1461
    string luaSendText="";
 
1462
    if( ! lua_isstring( L, 1 ) )
 
1463
    {
 
1464
        lua_pushstring( L, "disableAlias: wrong argument type" );
 
1465
        lua_error( L );
 
1466
        return 1;
 
1467
    }
 
1468
    else
 
1469
    {
 
1470
        luaSendText = lua_tostring( L, 1 );
 
1471
    }
 
1472
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1473
    QString text(luaSendText.c_str());
 
1474
    bool error = pHost->getAliasUnit()->disableAlias( text );
 
1475
    lua_pushboolean( L, error );
 
1476
    return 1;
 
1477
}
 
1478
 
 
1479
int TLuaInterpreter::killAlias( lua_State *L )
 
1480
{
 
1481
    string luaSendText="";
 
1482
    if( ! lua_isstring( L, 1 ) )
 
1483
    {
 
1484
        lua_pushstring( L, "killAlias: wrong argument type" );
 
1485
        lua_error( L );
 
1486
        return 1;
 
1487
    }
 
1488
    else
 
1489
    {
 
1490
        luaSendText = lua_tostring( L, 1 );
 
1491
    }
 
1492
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1493
    QString text(luaSendText.c_str());
 
1494
    lua_pushboolean( L, pHost->getAliasUnit()->killAlias( text ) );
 
1495
    return 1;
 
1496
}
 
1497
 
 
1498
// enableTimer( sess, timer_name )
 
1499
int TLuaInterpreter::enableTrigger( lua_State *L )
 
1500
{
 
1501
    string luaSendText="";
 
1502
    if( ! lua_isstring( L, 1 ) )
 
1503
    {
 
1504
        lua_pushstring( L, "enableTrigger: wrong argument type" );
 
1505
        lua_error( L );
 
1506
        return 1;
 
1507
    }
 
1508
    else
 
1509
    {
 
1510
        luaSendText = lua_tostring( L, 1 );
 
1511
    }
 
1512
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1513
    QString text(luaSendText.c_str());
 
1514
    bool error = pHost->getTriggerUnit()->enableTrigger( text );
 
1515
    lua_pushboolean( L, error );
 
1516
    return 1;
 
1517
}
 
1518
 
 
1519
// disableTimer( session, timer_name )
 
1520
int TLuaInterpreter::disableTrigger( lua_State *L )
 
1521
{
 
1522
    string luaSendText="";
 
1523
    if( ! lua_isstring( L, 1 ) )
 
1524
    {
 
1525
        lua_pushstring( L, "disableTrigger: wrong argument type" );
 
1526
        lua_error( L );
 
1527
        return 1;
 
1528
    }
 
1529
    else
 
1530
    {
 
1531
        luaSendText = lua_tostring( L, 1 );
 
1532
    }
 
1533
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1534
    QString text(luaSendText.c_str());
 
1535
    bool error = pHost->getTriggerUnit()->disableTrigger( text );
 
1536
    lua_pushboolean( L, error );
 
1537
    return 1;
 
1538
}
 
1539
 
 
1540
 
 
1541
int TLuaInterpreter::killTimer( lua_State *L )
 
1542
{
 
1543
    string luaSendText="";
 
1544
    if( ! lua_isstring( L, 1 ) )
 
1545
    {
 
1546
        lua_pushstring( L, "killTimer: killTimer requires a string ID" );
 
1547
        lua_error( L );
 
1548
        return 1;
 
1549
    }
 
1550
    else
 
1551
    {
 
1552
        luaSendText = lua_tostring( L, 1 );
 
1553
    }
 
1554
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1555
    QString text(luaSendText.c_str());
 
1556
    //pHost->disableTimer( text );
 
1557
    lua_pushboolean( L, pHost->killTimer( text ) );
 
1558
    return 1;
 
1559
}
 
1560
 
 
1561
int TLuaInterpreter::killTrigger( lua_State *L )
 
1562
{
 
1563
    string luaSendText="";
 
1564
    if( ! lua_isstring( L, 1 ) )
 
1565
    {
 
1566
        lua_pushstring( L, "killTrigger: wrong argument type" );
 
1567
        lua_error( L );
 
1568
        return 1;
 
1569
    }
 
1570
    else
 
1571
    {
 
1572
        luaSendText = lua_tostring( L, 1 );
 
1573
    }
 
1574
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1575
    QString text(luaSendText.c_str());
 
1576
    lua_pushboolean( L, pHost->killTrigger( text ) );
 
1577
    return 1;
 
1578
}
 
1579
 
 
1580
// openUserWindow( session, string window_name )
 
1581
int TLuaInterpreter::openUserWindow( lua_State *L )
 
1582
{
 
1583
    string luaSendText="";
 
1584
    if( ! lua_isstring( L, 1 ) )
 
1585
    {
 
1586
        lua_pushstring( L, "openUserWindow: wrong argument type" );
 
1587
        lua_error( L );
 
1588
        return 1;
 
1589
    }
 
1590
    else
 
1591
    {
 
1592
        luaSendText = lua_tostring( L, 1 );
 
1593
    }
 
1594
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1595
    QString text(luaSendText.c_str());
 
1596
    mudlet::self()->openWindow( pHost, text );
 
1597
    return 0;
 
1598
}
 
1599
 
 
1600
int TLuaInterpreter::createMiniConsole( lua_State *L )
 
1601
{
 
1602
    string luaSendText="";
 
1603
    if( ! lua_isstring( L, 1 ) )
 
1604
    {
 
1605
        lua_pushstring( L, "createMiniConsole: wrong argument type" );
 
1606
        lua_error( L );
 
1607
        return 1;
 
1608
    }
 
1609
    else
 
1610
    {
 
1611
        luaSendText = lua_tostring( L, 1 );
 
1612
    }
 
1613
    int x,y,width,height;
 
1614
    if( ! lua_isnumber( L, 2 ) )
 
1615
    {
 
1616
        lua_pushstring( L, "createMiniConsole: wrong argument type" );
 
1617
        lua_error( L );
 
1618
        return 1;
 
1619
    }
 
1620
    else
 
1621
    {
 
1622
        x = lua_tonumber( L, 2 );
 
1623
    }
 
1624
    if( ! lua_isnumber( L, 3 ) )
 
1625
    {
 
1626
        lua_pushstring( L, "createMiniConsole: wrong argument type" );
 
1627
        lua_error( L );
 
1628
        return 1;
 
1629
    }
 
1630
    else
 
1631
    {
 
1632
        y = lua_tonumber( L, 3 );
 
1633
    }
 
1634
    if( ! lua_isnumber( L, 4 ) )
 
1635
    {
 
1636
        lua_pushstring( L, "createMiniConsole: wrong argument type" );
 
1637
        lua_error( L );
 
1638
        return 1;
 
1639
    }
 
1640
    else
 
1641
    {
 
1642
        width = lua_tonumber( L, 4 );
 
1643
    }
 
1644
    if( ! lua_isnumber( L, 5 ) )
 
1645
    {
 
1646
        lua_pushstring( L, "createMiniConsole: wrong argument type" );
 
1647
        lua_error( L );
 
1648
        return 1;
 
1649
    }
 
1650
    else
 
1651
    {
 
1652
        height = lua_tonumber( L, 5 );
 
1653
    }
 
1654
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1655
    QString name(luaSendText.c_str());
 
1656
    lua_pushboolean( L, mudlet::self()->createMiniConsole( pHost, name, x, y, width, height ) );
 
1657
    return 1;
 
1658
}
 
1659
 
 
1660
int TLuaInterpreter::createLabel( lua_State *L )
 
1661
{
 
1662
    string luaSendText="";
 
1663
    if( ! lua_isstring( L, 1 ) )
 
1664
    {
 
1665
        lua_pushstring( L, "createLabel: wrong argument type" );
 
1666
        lua_error( L );
 
1667
        return 1;
 
1668
    }
 
1669
    else
 
1670
    {
 
1671
        luaSendText = lua_tostring( L, 1 );
 
1672
    }
 
1673
    int x,y,width,height;
 
1674
    bool fillBackground=false;
 
1675
    if( ! lua_isnumber( L, 2 ) )
 
1676
    {
 
1677
        lua_pushstring( L, "createLabel: wrong argument type" );
 
1678
        lua_error( L );
 
1679
        return 1;
 
1680
    }
 
1681
    else
 
1682
    {
 
1683
        x = lua_tonumber( L, 2 );
 
1684
    }
 
1685
    if( ! lua_isnumber( L, 3 ) )
 
1686
    {
 
1687
        lua_pushstring( L, "createLabel: wrong argument type" );
 
1688
        lua_error( L );
 
1689
        return 1;
 
1690
    }
 
1691
    else
 
1692
    {
 
1693
        y = lua_tonumber( L, 3 );
 
1694
    }
 
1695
    if( ! lua_isnumber( L, 4 ) )
 
1696
    {
 
1697
        lua_pushstring( L, "createLabel: wrong argument type" );
 
1698
        lua_error( L );
 
1699
        return 1;
 
1700
    }
 
1701
    else
 
1702
    {
 
1703
        width = lua_tonumber( L, 4 );
 
1704
    }
 
1705
    if( ! lua_isnumber( L, 5 ) )
 
1706
    {
 
1707
        lua_pushstring( L, "createLabel: wrong argument type" );
 
1708
        lua_error( L );
 
1709
        return 1;
 
1710
    }
 
1711
    else
 
1712
    {
 
1713
        height = lua_tonumber( L, 5 );
 
1714
    }
 
1715
    if( ! lua_isnumber( L, 6 ) )
 
1716
    {
 
1717
        lua_pushstring( L, "createLabel: wrong argument type" );
 
1718
        lua_error( L );
 
1719
        return 1;
 
1720
    }
 
1721
    else
 
1722
    {
 
1723
        fillBackground = lua_toboolean( L, 6 );
 
1724
    }
 
1725
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1726
    QString name(luaSendText.c_str());
 
1727
    lua_pushboolean( L, mudlet::self()->createLabel( pHost, name, x, y, width, height, fillBackground ) );
 
1728
    return 1;
 
1729
}
 
1730
 
 
1731
int TLuaInterpreter::createMapper( lua_State *L )
 
1732
{
 
1733
    int x,y,width,height;
 
1734
    if( ! lua_isnumber( L, 1 ) )
 
1735
    {
 
1736
        lua_pushstring( L, "createMapper: wrong argument type" );
 
1737
        lua_error( L );
 
1738
        return 1;
 
1739
    }
 
1740
    else
 
1741
    {
 
1742
        x = lua_tonumber( L, 1 );
 
1743
    }
 
1744
    if( ! lua_isnumber( L, 2 ) )
 
1745
    {
 
1746
        lua_pushstring( L, "createMapper: wrong argument type" );
 
1747
        lua_error( L );
 
1748
        return 1;
 
1749
    }
 
1750
    else
 
1751
    {
 
1752
        y = lua_tonumber( L, 2 );
 
1753
    }
 
1754
    if( ! lua_isnumber( L, 3 ) )
 
1755
    {
 
1756
        lua_pushstring( L, "createMapper: wrong argument type" );
 
1757
        lua_error( L );
 
1758
        return 1;
 
1759
    }
 
1760
    else
 
1761
    {
 
1762
        width = lua_tonumber( L, 3 );
 
1763
    }
 
1764
    if( ! lua_isnumber( L, 4 ) )
 
1765
    {
 
1766
        lua_pushstring( L, "createMapper: wrong argument type" );
 
1767
        lua_error( L );
 
1768
        return 1;
 
1769
    }
 
1770
    else
 
1771
    {
 
1772
        height = lua_tonumber( L, 4 );
 
1773
    }
 
1774
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1775
    pHost->mpConsole->createMapper( x, y, width, height );
 
1776
    return 0;
 
1777
}
 
1778
 
 
1779
 
 
1780
int TLuaInterpreter::createButton( lua_State *L )
 
1781
{
 
1782
    string luaSendText="";
 
1783
    if( ! lua_isstring( L, 1 ) )
 
1784
    {
 
1785
        lua_pushstring( L, "createButton: wrong argument type" );
 
1786
        lua_error( L );
 
1787
        return 1;
 
1788
    }
 
1789
    else
 
1790
    {
 
1791
        luaSendText = lua_tostring( L, 1 );
 
1792
    }
 
1793
    int x,y,width,height;
 
1794
    bool fillBackground=false;
 
1795
    if( ! lua_isnumber( L, 2 ) )
 
1796
    {
 
1797
        lua_pushstring( L, "createButton: wrong argument type" );
 
1798
        lua_error( L );
 
1799
        return 1;
 
1800
    }
 
1801
    else
 
1802
    {
 
1803
        x = lua_tonumber( L, 2 );
 
1804
    }
 
1805
    if( ! lua_isnumber( L, 3 ) )
 
1806
    {
 
1807
        lua_pushstring( L, "createButton: wrong argument type" );
 
1808
        lua_error( L );
 
1809
        return 1;
 
1810
    }
 
1811
    else
 
1812
    {
 
1813
        y = lua_tonumber( L, 3 );
 
1814
    }
 
1815
    if( ! lua_isnumber( L, 4 ) )
 
1816
    {
 
1817
        lua_pushstring( L, "createButton: wrong argument type" );
 
1818
        lua_error( L );
 
1819
        return 1;
 
1820
    }
 
1821
    else
 
1822
    {
 
1823
        width = lua_tonumber( L, 4 );
 
1824
    }
 
1825
    if( ! lua_isnumber( L, 5 ) )
 
1826
    {
 
1827
        lua_pushstring( L, "createButton: wrong argument type" );
 
1828
        lua_error( L );
 
1829
        return 1;
 
1830
    }
 
1831
    else
 
1832
    {
 
1833
        height = lua_tonumber( L, 5 );
 
1834
    }
 
1835
    if( ! lua_isnumber( L, 6 ) )
 
1836
    {
 
1837
        lua_pushstring( L, "createButton: wrong argument type" );
 
1838
        lua_error( L );
 
1839
        return 1;
 
1840
    }
 
1841
    else
 
1842
    {
 
1843
        fillBackground = lua_toboolean( L, 6 );
 
1844
    }
 
1845
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1846
    QString name(luaSendText.c_str());
 
1847
    //TODO FIXME
 
1848
    mudlet::self()->createLabel( pHost, name, x, y, width, height, fillBackground );
 
1849
    return 0;
 
1850
}
 
1851
 
 
1852
 
 
1853
int TLuaInterpreter::createBuffer( lua_State *L )
 
1854
{
 
1855
    string luaSendText="";
 
1856
    if( ! lua_isstring( L, 1 ) )
 
1857
    {
 
1858
        lua_pushstring( L, "createBuffer: wrong argument type" );
 
1859
        lua_error( L );
 
1860
        return 1;
 
1861
    }
 
1862
    else
 
1863
    {
 
1864
        luaSendText = lua_tostring( L, 1 );
 
1865
    }
 
1866
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1867
    QString text(luaSendText.c_str());
 
1868
    mudlet::self()->createBuffer( pHost, text );
 
1869
    return 0;
 
1870
}
 
1871
 
 
1872
int TLuaInterpreter::clearUserWindow( lua_State *L )
 
1873
{
 
1874
    string luaSendText="";
 
1875
    if( ! lua_isstring( L, 1 ) )
 
1876
    {
 
1877
        lua_pushstring( L, "clearUserWindow: wrong argument type" );
 
1878
        lua_error( L );
 
1879
        return 1;
 
1880
    }
 
1881
    else
 
1882
    {
 
1883
        luaSendText = lua_tostring( L, 1 );
 
1884
    }
 
1885
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1886
    QString text(luaSendText.c_str());
 
1887
    mudlet::self()->clearWindow( pHost, text );
 
1888
 
 
1889
    return 0;
 
1890
}
 
1891
 
 
1892
int TLuaInterpreter::closeUserWindow( lua_State *L )
 
1893
{
 
1894
    string luaSendText="";
 
1895
    if( ! lua_isstring( L, 1 ) )
 
1896
    {
 
1897
        lua_pushstring( L, "closeUserWindow: wrong argument type" );
 
1898
        lua_error( L );
 
1899
        return 1;
 
1900
    }
 
1901
    else
 
1902
    {
 
1903
        luaSendText = lua_tostring( L, 1 );
 
1904
    }
 
1905
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1906
    QString text(luaSendText.c_str());
 
1907
    mudlet::self()->hideWindow( pHost, text );
 
1908
 
 
1909
    return 0;
 
1910
}
 
1911
 
 
1912
int TLuaInterpreter::hideUserWindow( lua_State *L )
 
1913
{
 
1914
    string luaSendText="";
 
1915
    if( ! lua_isstring( L, 1 ) )
 
1916
    {
 
1917
        lua_pushstring( L, "hideUserWindow: wrong argument type" );
 
1918
        lua_error( L );
 
1919
        return 1;
 
1920
    }
 
1921
    else
 
1922
    {
 
1923
        luaSendText = lua_tostring( L, 1 );
 
1924
    }
 
1925
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1926
    QString text(luaSendText.c_str());
 
1927
    //mudlet::self()->hideWindow( pHost, text );
 
1928
    pHost->mpConsole->hideWindow( text );
 
1929
 
 
1930
    return 0;
 
1931
}
 
1932
 
 
1933
int TLuaInterpreter::setBorderTop( lua_State *L )
 
1934
{
 
1935
    int x1;
 
1936
    if( ! lua_isnumber( L, 1 ) )
 
1937
    {
 
1938
        lua_pushstring( L, "setBorderTop: wrong argument type" );
 
1939
        lua_error( L );
 
1940
        return 1;
 
1941
    }
 
1942
    else
 
1943
    {
 
1944
        x1 = lua_tonumber( L, 1 );
 
1945
    }
 
1946
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1947
    pHost->mBorderTopHeight = x1;
 
1948
    int x,y;
 
1949
    x = pHost->mpConsole->width();
 
1950
    y = pHost->mpConsole->height();
 
1951
    QSize s = QSize(x,y);
 
1952
    QResizeEvent event(s, s);
 
1953
    QApplication::sendEvent( pHost->mpConsole, &event);
 
1954
    return 0;
 
1955
}
 
1956
 
 
1957
int TLuaInterpreter::setBorderBottom( lua_State *L )
 
1958
{
 
1959
    int x1;
 
1960
    if( ! lua_isnumber( L, 1 ) )
 
1961
    {
 
1962
        lua_pushstring( L, "setBorderBottom: wrong argument type" );
 
1963
        lua_error( L );
 
1964
        return 1;
 
1965
    }
 
1966
    else
 
1967
    {
 
1968
        x1 = lua_tonumber( L, 1 );
 
1969
    }
 
1970
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1971
    pHost->mBorderBottomHeight = x1;
 
1972
    int x,y;
 
1973
    x = pHost->mpConsole->width();
 
1974
    y = pHost->mpConsole->height();
 
1975
    QSize s = QSize(x,y);
 
1976
    QResizeEvent event(s, s);
 
1977
    QApplication::sendEvent( pHost->mpConsole, &event);
 
1978
    return 0;
 
1979
}
 
1980
 
 
1981
int TLuaInterpreter::setBorderLeft( lua_State *L )
 
1982
{
 
1983
    int x1;
 
1984
    if( ! lua_isnumber( L, 1 ) )
 
1985
    {
 
1986
        lua_pushstring( L, "setBorderLeft: wrong argument type" );
 
1987
        lua_error( L );
 
1988
        return 1;
 
1989
    }
 
1990
    else
 
1991
    {
 
1992
        x1 = lua_tonumber( L, 1 );
 
1993
    }
 
1994
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
1995
    pHost->mBorderLeftWidth = x1;
 
1996
    int x,y;
 
1997
    x = pHost->mpConsole->width();
 
1998
    y = pHost->mpConsole->height();
 
1999
    QSize s = QSize(x,y);
 
2000
    QResizeEvent event(s, s);
 
2001
    QApplication::sendEvent( pHost->mpConsole, &event);
 
2002
    return 0;
 
2003
}
 
2004
 
 
2005
int TLuaInterpreter::setBorderRight( lua_State *L )
 
2006
{
 
2007
    int x1;
 
2008
    if( ! lua_isnumber( L, 1 ) )
 
2009
    {
 
2010
        lua_pushstring( L, "setBorderRight: wrong argument type" );
 
2011
        lua_error( L );
 
2012
        return 1;
 
2013
    }
 
2014
    else
 
2015
    {
 
2016
        x1 = lua_tonumber( L, 1 );
 
2017
    }
 
2018
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2019
    pHost->mBorderRightWidth = x1;
 
2020
    int x,y;
 
2021
    x = pHost->mpConsole->width();
 
2022
    y = pHost->mpConsole->height();
 
2023
    QSize s = QSize(x,y);
 
2024
    QResizeEvent event(s, s);
 
2025
    QApplication::sendEvent( pHost->mpConsole, &event);
 
2026
    return 0;
 
2027
}
 
2028
 
 
2029
int TLuaInterpreter::resizeUserWindow( lua_State *L )
 
2030
{
 
2031
    string luaSendText="";
 
2032
    if( ! lua_isstring( L, 1 ) )
 
2033
    {
 
2034
        lua_pushstring( L, "resizeUserWindow: wrong argument type" );
 
2035
        lua_error( L );
 
2036
        return 1;
 
2037
    }
 
2038
    else
 
2039
    {
 
2040
        luaSendText = lua_tostring( L, 1 );
 
2041
    }
 
2042
    double x1;
 
2043
    if( ! lua_isnumber( L, 2 ) )
 
2044
    {
 
2045
        lua_pushstring( L, "resizeUserWindow: wrong argument type" );
 
2046
        lua_error( L );
 
2047
        return 1;
 
2048
    }
 
2049
    else
 
2050
    {
 
2051
        x1 = lua_tonumber( L, 2 );
 
2052
    }
 
2053
    double y1;
 
2054
    if( ! lua_isnumber( L, 3 ) )
 
2055
    {
 
2056
        lua_pushstring( L, "resizeUserWindow: wrong argument type" );
 
2057
        lua_error( L );
 
2058
        return 1;
 
2059
    }
 
2060
    else
 
2061
    {
 
2062
        y1 = lua_tonumber( L, 3 );
 
2063
    }
 
2064
 
 
2065
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2066
    QString text(luaSendText.c_str());
 
2067
    mudlet::self()->resizeWindow( pHost, text, static_cast<int>(x1), static_cast<int>(y1) );
 
2068
 
 
2069
    return 0;
 
2070
}
 
2071
 
 
2072
int TLuaInterpreter::moveWindow( lua_State *L )
 
2073
{
 
2074
    string luaSendText="";
 
2075
    if( ! lua_isstring( L, 1 ) )
 
2076
    {
 
2077
        lua_pushstring( L, "moveWindow: wrong argument type" );
 
2078
        lua_error( L );
 
2079
        return 1;
 
2080
    }
 
2081
    else
 
2082
    {
 
2083
        luaSendText = lua_tostring( L, 1 );
 
2084
    }
 
2085
    double x1;
 
2086
    if( ! lua_isnumber( L, 2 ) )
 
2087
    {
 
2088
        lua_pushstring( L, "moveWindow: wrong argument type" );
 
2089
        lua_error( L );
 
2090
        return 1;
 
2091
    }
 
2092
    else
 
2093
    {
 
2094
        x1 = lua_tonumber( L, 2 );
 
2095
    }
 
2096
    double y1;
 
2097
    if( ! lua_isnumber( L, 3 ) )
 
2098
    {
 
2099
        lua_pushstring( L, "moveWindow: wrong argument type" );
 
2100
        lua_error( L );
 
2101
        return 1;
 
2102
    }
 
2103
    else
 
2104
    {
 
2105
        y1 = lua_tonumber( L, 3 );
 
2106
    }
 
2107
 
 
2108
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2109
 
 
2110
    QString text(luaSendText.c_str());
 
2111
    mudlet::self()->moveWindow( pHost, text, static_cast<int>(x1), static_cast<int>(y1) );
 
2112
    return 0;
 
2113
}
 
2114
 
 
2115
int TLuaInterpreter::setMainWindowSize( lua_State *L )
 
2116
{
 
2117
    int x1;
 
2118
    if( ! lua_isnumber( L, 1 ) )
 
2119
    {
 
2120
        lua_pushstring( L, "setBackgroundColor: wrong argument type" );
 
2121
        lua_error( L );
 
2122
        return 1;
 
2123
    }
 
2124
    else
 
2125
    {
 
2126
        x1 = lua_tonumber( L, 1 );
 
2127
    }
 
2128
    int y1;
 
2129
    if( ! lua_isnumber( L, 2 ) )
 
2130
    {
 
2131
        lua_pushstring( L, "setBackgroundColor: wrong argument type" );
 
2132
        lua_error( L );
 
2133
        return 1;
 
2134
    }
 
2135
    else
 
2136
    {
 
2137
        y1 = lua_tonumber( L, 2 );
 
2138
    }
 
2139
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2140
 
 
2141
    mudlet::self()->resize( x1, y1 );
 
2142
 
 
2143
    return 0;
 
2144
}
 
2145
 
 
2146
int TLuaInterpreter::setBackgroundColor( lua_State *L )
 
2147
{
 
2148
    string luaSendText="";
 
2149
    if( ! lua_isstring( L, 1 ) )
 
2150
    {
 
2151
        lua_pushstring( L, "setBackgroundColor: wrong argument type" );
 
2152
        lua_error( L );
 
2153
        return 1;
 
2154
    }
 
2155
    else
 
2156
    {
 
2157
        luaSendText = lua_tostring( L, 1 );
 
2158
    }
 
2159
    double x1;
 
2160
    if( ! lua_isnumber( L, 2 ) )
 
2161
    {
 
2162
        lua_pushstring( L, "setBackgroundColor: wrong argument type" );
 
2163
        lua_error( L );
 
2164
        return 1;
 
2165
    }
 
2166
    else
 
2167
    {
 
2168
        x1 = lua_tonumber( L, 2 );
 
2169
    }
 
2170
    double y1;
 
2171
    if( ! lua_isnumber( L, 3 ) )
 
2172
    {
 
2173
        lua_pushstring( L, "setBackgroundColor: wrong argument type" );
 
2174
        lua_error( L );
 
2175
        return 1;
 
2176
    }
 
2177
    else
 
2178
    {
 
2179
        y1 = lua_tonumber( L, 3 );
 
2180
    }
 
2181
    double x2;
 
2182
    if( ! lua_isnumber( L, 4 ) )
 
2183
    {
 
2184
        lua_pushstring( L, "setBackgroundColor: wrong argument type" );
 
2185
        lua_error( L );
 
2186
        return 1;
 
2187
    }
 
2188
    else
 
2189
    {
 
2190
        x2 = lua_tonumber( L, 4 );
 
2191
    }
 
2192
    double y2;
 
2193
    if( ! lua_isnumber( L, 5 ) )
 
2194
    {
 
2195
        lua_pushstring( L, "setBackgroundColor: wrong argument type" );
 
2196
        lua_error( L );
 
2197
        return 1;
 
2198
    }
 
2199
    else
 
2200
    {
 
2201
        y2 = lua_tonumber( L, 5 );
 
2202
    }
 
2203
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2204
    QString text(luaSendText.c_str());
 
2205
    mudlet::self()->setBackgroundColor( pHost, text, static_cast<int>(x1), static_cast<int>(y1), static_cast<int>(x2), static_cast<int>(y2) );
 
2206
 
 
2207
    return 0;
 
2208
}
 
2209
 
 
2210
int TLuaInterpreter::calcFontWidth( int size )
 
2211
{
 
2212
    QFont font = QFont("Bitstream Vera Sans Mono", size, QFont::Courier);
 
2213
    return QFontMetrics( font ).width( QChar('W') );
 
2214
}
 
2215
 
 
2216
int TLuaInterpreter::calcFontHeight( int size )
 
2217
{
 
2218
    QFont font = QFont("Bitstream Vera Sans Mono", size, QFont::Courier);
 
2219
    int fontDescent = QFontMetrics( font ).descent();
 
2220
    int fontAscent = QFontMetrics( font ).ascent();
 
2221
    return fontAscent + fontDescent;
 
2222
}
 
2223
 
 
2224
int TLuaInterpreter::calcFontSize( lua_State *L )
 
2225
{
 
2226
    int x = 0;
 
2227
    if( ! lua_isnumber( L, 1 ) )
 
2228
    {
 
2229
        lua_pushstring( L, "calcFontSize: wrong argument type" );
 
2230
        lua_error( L );
 
2231
        return 1;
 
2232
    }
 
2233
    else
 
2234
    {
 
2235
        x = lua_tonumber( L, 1 );
 
2236
    }
 
2237
 
 
2238
    lua_pushnumber( L, calcFontWidth( x ) );
 
2239
    lua_pushnumber( L, calcFontHeight( x ) );
 
2240
    return 2;
 
2241
}
 
2242
 
 
2243
int TLuaInterpreter::startLogging( lua_State *L )
 
2244
{
 
2245
    bool logOn = true;
 
2246
    if( ! lua_isboolean( L, 1 ) )
 
2247
    {
 
2248
        lua_pushstring( L, "startLogging: wrong argument type" );
 
2249
        lua_error( L );
 
2250
        return 1;
 
2251
    }
 
2252
    else
 
2253
    {
 
2254
        logOn = lua_toboolean( L, 1 );
 
2255
    }
 
2256
 
 
2257
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2258
    pHost->mpConsole->mLogToLogFile = ! logOn;
 
2259
    pHost->mpConsole->slot_toggleLogging();
 
2260
    return 0;
 
2261
}
 
2262
 
 
2263
int TLuaInterpreter::setBackgroundImage( lua_State *L )
 
2264
{
 
2265
    string luaSendText="";
 
2266
    if( ! lua_isstring( L, 1 ) )
 
2267
    {
 
2268
        lua_pushstring( L, "setBackgroundImage: wrong argument type" );
 
2269
        lua_error( L );
 
2270
        return 1;
 
2271
    }
 
2272
    else
 
2273
    {
 
2274
        luaSendText = lua_tostring( L, 1 );
 
2275
    }
 
2276
    string luaName="";
 
2277
    if( ! lua_isstring( L, 2 ) )
 
2278
    {
 
2279
        lua_pushstring( L, "setBackgroundImage: wrong argument type" );
 
2280
        lua_error( L );
 
2281
        return 1;
 
2282
    }
 
2283
    else
 
2284
    {
 
2285
        luaName = lua_tostring( L, 2 );
 
2286
    }
 
2287
 
 
2288
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2289
    QString text(luaSendText.c_str());
 
2290
    QString name(luaName.c_str());
 
2291
    mudlet::self()->setBackgroundImage( pHost, text, name );
 
2292
 
 
2293
    return 0;
 
2294
}
 
2295
 
 
2296
int TLuaInterpreter::setLabelClickCallback( lua_State *L )
 
2297
{
 
2298
    string luaSendText="";
 
2299
    if( ! lua_isstring( L, 1 ) )
 
2300
    {
 
2301
        lua_pushstring( L, "setLabelClickCallback: wrong argument type" );
 
2302
        lua_error( L );
 
2303
        return 1;
 
2304
    }
 
2305
    else
 
2306
    {
 
2307
        luaSendText = lua_tostring( L, 1 );
 
2308
    }
 
2309
    string luaName="";
 
2310
    if( ! lua_isstring( L, 2 ) )
 
2311
    {
 
2312
        lua_pushstring( L, "setLabelClickCallback: wrong argument type" );
 
2313
        lua_error( L );
 
2314
        return 1;
 
2315
    }
 
2316
    else
 
2317
    {
 
2318
        luaName = lua_tostring( L, 2 );
 
2319
    }
 
2320
 
 
2321
    TEvent * pE = new TEvent;
 
2322
 
 
2323
    int n = lua_gettop( L );
 
2324
    for( int i=3; i<=n; i++)
 
2325
    {
 
2326
        if( lua_isnumber( L, i ) )
 
2327
        {
 
2328
            pE->mArgumentList.append( QString::number(lua_tonumber( L, i ) ) );
 
2329
            pE->mArgumentTypeList.append( ARGUMENT_TYPE_NUMBER );
 
2330
        }
 
2331
        else if( lua_isstring( L, i ) )
 
2332
        {
 
2333
            pE->mArgumentList.append( QString(lua_tostring( L, i )) );
 
2334
            pE->mArgumentTypeList.append( ARGUMENT_TYPE_STRING );
 
2335
        }
 
2336
    }
 
2337
 
 
2338
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2339
    QString text(luaSendText.c_str());
 
2340
    QString name(luaName.c_str());
 
2341
    mudlet::self()->setLabelClickCallback( pHost, text, name, pE );
 
2342
 
 
2343
    return 0;
 
2344
}
 
2345
 
 
2346
int TLuaInterpreter::setTextFormat( lua_State *L )
 
2347
{
 
2348
    string luaSendText="";
 
2349
    if( ! lua_isstring( L, 1 ) )
 
2350
    {
 
2351
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2352
        lua_error( L );
 
2353
        return 1;
 
2354
    }
 
2355
    else
 
2356
    {
 
2357
        luaSendText = lua_tostring( L, 1 );
 
2358
    }
 
2359
    double r1;
 
2360
    if( ! lua_isnumber( L, 2 ) )
 
2361
    {
 
2362
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2363
        lua_error( L );
 
2364
        return 1;
 
2365
    }
 
2366
    else
 
2367
    {
 
2368
        r1 = lua_tonumber( L, 2 );
 
2369
    }
 
2370
    double g1;
 
2371
    if( ! lua_isnumber( L, 3 ) )
 
2372
    {
 
2373
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2374
        lua_error( L );
 
2375
        return 1;
 
2376
    }
 
2377
    else
 
2378
    {
 
2379
        g1 = lua_tonumber( L, 3 );
 
2380
    }
 
2381
    double b1;
 
2382
    if( ! lua_isnumber( L, 4 ) )
 
2383
    {
 
2384
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2385
        lua_error( L );
 
2386
        return 1;
 
2387
    }
 
2388
    else
 
2389
    {
 
2390
        b1 = lua_tonumber( L, 4 );
 
2391
    }
 
2392
    double r2;
 
2393
    if( ! lua_isnumber( L, 5 ) )
 
2394
    {
 
2395
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2396
        lua_error( L );
 
2397
        return 1;
 
2398
    }
 
2399
    else
 
2400
    {
 
2401
        r2 = lua_tonumber( L, 5 );
 
2402
    }
 
2403
    double g2;
 
2404
    if( ! lua_isnumber( L, 6 ) )
 
2405
    {
 
2406
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2407
        lua_error( L );
 
2408
        return 1;
 
2409
    }
 
2410
    else
 
2411
    {
 
2412
        g2 = lua_tonumber( L, 6 );
 
2413
    }
 
2414
    double b2;
 
2415
    if( ! lua_isnumber( L, 7 ) )
 
2416
    {
 
2417
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2418
        lua_error( L );
 
2419
        return 1;
 
2420
    }
 
2421
    else
 
2422
    {
 
2423
        b2 = lua_tonumber( L, 7 );
 
2424
    }
 
2425
    double bold;
 
2426
    if( ! lua_isnumber( L, 8 ) )
 
2427
    {
 
2428
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2429
        lua_error( L );
 
2430
        return 1;
 
2431
    }
 
2432
    else
 
2433
    {
 
2434
        bold = lua_tonumber( L, 8 );
 
2435
    }
 
2436
    double underline;
 
2437
    if( ! lua_isnumber( L, 9 ) )
 
2438
    {
 
2439
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2440
        lua_error( L );
 
2441
        return 1;
 
2442
    }
 
2443
    else
 
2444
    {
 
2445
        underline = lua_tonumber( L, 9 );
 
2446
    }
 
2447
    double italics;
 
2448
    if( ! lua_isnumber( L, 10 ) )
 
2449
    {
 
2450
        lua_pushstring( L, "setTextFormat: wrong argument type" );
 
2451
        lua_error( L );
 
2452
        return 1;
 
2453
    }
 
2454
    else
 
2455
    {
 
2456
        italics = lua_tonumber( L, 10 );
 
2457
    }
 
2458
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2459
    QString text(luaSendText.c_str());
 
2460
    if( text == "main" )
 
2461
    {
 
2462
        TConsole * pC = pHost->mpConsole;
 
2463
        pC->mFormatCurrent.bgR = r1;
 
2464
        pC->mFormatCurrent.bgG = g1;
 
2465
        pC->mFormatCurrent.bgB = b1;
 
2466
        pC->mFormatCurrent.fgR = r2;
 
2467
        pC->mFormatCurrent.fgG = g2;
 
2468
        pC->mFormatCurrent.fgB = b2;
 
2469
        pC->mFormatCurrent.bold = bold;
 
2470
        pC->mFormatCurrent.underline = underline;
 
2471
        pC->mFormatCurrent.italics = italics;
 
2472
        return true;
 
2473
    }
 
2474
    else
 
2475
    {
 
2476
        mudlet::self()->setTextFormat( pHost, text, static_cast<int>(r1), static_cast<int>(g1), static_cast<int>(b1), static_cast<int>(r2),static_cast<int>(g2), static_cast<int>(b2), static_cast<bool>(bold), static_cast<bool>(underline), static_cast<bool>(italics) );
 
2477
    }
 
2478
 
 
2479
    return 0;
 
2480
}
 
2481
 
 
2482
int TLuaInterpreter::showUserWindow( lua_State *L )
 
2483
{
 
2484
    string luaSendText="";
 
2485
    if( ! lua_isstring( L, 1 ) )
 
2486
    {
 
2487
        lua_pushstring( L, "showUserWindow: wrong argument type" );
 
2488
        lua_error( L );
 
2489
        return 1;
 
2490
    }
 
2491
    else
 
2492
    {
 
2493
        luaSendText = lua_tostring( L, 1 );
 
2494
    }
 
2495
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2496
    QString text(luaSendText.c_str());
 
2497
    lua_pushboolean( L, pHost->mpConsole->showWindow( text ));
 
2498
    return 1;
 
2499
}
 
2500
 
 
2501
// xRot, yRot, zRot, zoom
 
2502
int TLuaInterpreter::setMapperView( lua_State *L )
 
2503
{
 
2504
    float x, y, z, zoom;
 
2505
 
 
2506
    if( ! lua_isnumber( L, 1 ) )
 
2507
    {
 
2508
        lua_pushstring( L, "setMapperView: wrong argument type" );
 
2509
        lua_error( L );
 
2510
        return 1;
 
2511
    }
 
2512
    else
 
2513
    {
 
2514
        x = lua_tonumber( L, 1 );
 
2515
    }
 
2516
 
 
2517
    if( ! lua_isnumber( L, 2 ) )
 
2518
    {
 
2519
        lua_pushstring( L, "setMapperView: wrong argument type" );
 
2520
        lua_error( L );
 
2521
        return 1;
 
2522
    }
 
2523
    else
 
2524
    {
 
2525
        y = lua_tonumber( L, 2 );
 
2526
    }
 
2527
    if( ! lua_isnumber( L, 3 ) )
 
2528
    {
 
2529
        lua_pushstring( L, "setMapperView: wrong argument type" );
 
2530
        lua_error( L );
 
2531
        return 1;
 
2532
    }
 
2533
    else
 
2534
    {
 
2535
        z = lua_tonumber( L, 3 );
 
2536
    }
 
2537
    if( ! lua_isnumber( L, 4 ) )
 
2538
    {
 
2539
        lua_pushstring( L, "setMapperView: wrong argument type" );
 
2540
        lua_error( L );
 
2541
        return 1;
 
2542
    }
 
2543
    else
 
2544
    {
 
2545
        zoom = lua_tonumber( L, 4 );
 
2546
    }
 
2547
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2548
 
 
2549
    pHost->mpMap->setView( x, y, z, zoom  );
 
2550
    return 0;
 
2551
}
 
2552
 
 
2553
int TLuaInterpreter::setRoomEnv( lua_State *L )
 
2554
{
 
2555
    int id, env;
 
2556
    if( ! lua_isnumber( L, 1 ) )
 
2557
    {
 
2558
        lua_pushstring( L, "setRoomEnv: wrong argument type" );
 
2559
        lua_error( L );
 
2560
        return 1;
 
2561
    }
 
2562
    else
 
2563
    {
 
2564
        id = lua_tonumber( L, 1 );
 
2565
    }
 
2566
    if( ! lua_isnumber( L, 2 ) )
 
2567
    {
 
2568
        lua_pushstring( L, "setRoomEnv: wrong argument type" );
 
2569
        lua_error( L );
 
2570
        return 1;
 
2571
    }
 
2572
    else
 
2573
    {
 
2574
        env = lua_tonumber( L, 2 );
 
2575
    }
 
2576
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2577
    if( pHost->mpMap->rooms.contains( id ) )
 
2578
    {
 
2579
        pHost->mpMap->rooms[id]->environment = env;
 
2580
    }
 
2581
 
 
2582
    return 0;
 
2583
}
 
2584
 
 
2585
int TLuaInterpreter::setRoomName( lua_State *L )
 
2586
{
 
2587
    int id;
 
2588
    string name;
 
2589
    if( ! lua_isnumber( L, 1 ) )
 
2590
    {
 
2591
        lua_pushstring( L, "setRoomName: wrong argument type" );
 
2592
        lua_error( L );
 
2593
        return 1;
 
2594
    }
 
2595
    else
 
2596
    {
 
2597
        id = lua_tonumber( L, 1 );
 
2598
    }
 
2599
    if( ! lua_isstring( L, 2 ) )
 
2600
    {
 
2601
        lua_pushstring( L, "setRoomName: wrong argument type" );
 
2602
        lua_error( L );
 
2603
        return 1;
 
2604
    }
 
2605
    else
 
2606
    {
 
2607
        name = lua_tostring( L, 2 );
 
2608
    }
 
2609
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2610
    QString _name = name.c_str();
 
2611
    if( pHost->mpMap->rooms.contains( id ) )
 
2612
    {
 
2613
        pHost->mpMap->rooms[id]->name = _name;
 
2614
    }
 
2615
 
 
2616
    return 0;
 
2617
}
 
2618
 
 
2619
int TLuaInterpreter::getRoomName( lua_State *L )
 
2620
{
 
2621
    int id;
 
2622
    if( ! lua_isnumber( L, 1 ) )
 
2623
    {
 
2624
        lua_pushstring( L, "getRoomName: wrong argument type" );
 
2625
        lua_error( L );
 
2626
        return 1;
 
2627
    }
 
2628
    else
 
2629
    {
 
2630
        id = lua_tonumber( L, 1 );
 
2631
    }
 
2632
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2633
    if( pHost->mpMap->rooms.contains( id ) )
 
2634
    {
 
2635
        lua_pushstring(L, pHost->mpMap->rooms[id]->name.toLatin1().data() );
 
2636
    }
 
2637
 
 
2638
    return 1;
 
2639
}
 
2640
 
 
2641
int TLuaInterpreter::setRoomWeight( lua_State *L )
 
2642
{
 
2643
    int id;
 
2644
    if( ! lua_isnumber( L, 1 ) )
 
2645
    {
 
2646
        lua_pushstring( L, "setRoomWeight: wrong argument type" );
 
2647
        lua_error( L );
 
2648
        return 1;
 
2649
    }
 
2650
    else
 
2651
    {
 
2652
        id = lua_tonumber( L, 1 );
 
2653
    }
 
2654
    int w;
 
2655
    if( ! lua_isnumber( L, 2 ) )
 
2656
    {
 
2657
        lua_pushstring( L, "setRoomWeight: wrong argument type" );
 
2658
        lua_error( L );
 
2659
        return 1;
 
2660
    }
 
2661
    else
 
2662
    {
 
2663
        w = lua_tonumber( L, 2 );
 
2664
    }
 
2665
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2666
    if( pHost->mpMap->rooms.contains( id ) )
 
2667
    {
 
2668
        pHost->mpMap->rooms[id]->weight = w;
 
2669
        pHost->mpMap->mMapGraphNeedsUpdate = true;
 
2670
    }
 
2671
 
 
2672
    return 0;
 
2673
}
 
2674
 
 
2675
int TLuaInterpreter::connectToServer( lua_State *L )
 
2676
{
 
2677
    int port;
 
2678
    string url;
 
2679
    if( ! lua_isstring( L, 1 ) )
 
2680
    {
 
2681
        lua_pushstring( L, "connectToServer: wrong argument type" );
 
2682
        lua_error( L );
 
2683
        return 1;
 
2684
    }
 
2685
    else
 
2686
    {
 
2687
        url = lua_tostring( L, 1 );
 
2688
    }
 
2689
 
 
2690
    if( ! lua_isnumber( L, 2 ) )
 
2691
    {
 
2692
        lua_pushstring( L, "connectToServer: wrong argument type" );
 
2693
        lua_error( L );
 
2694
        return 1;
 
2695
    }
 
2696
    else
 
2697
    {
 
2698
        port = lua_tonumber( L, 2 );
 
2699
    }
 
2700
    QString _url = url.c_str();
 
2701
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2702
    pHost->mTelnet.connectIt( _url, port );
 
2703
    return 0;
 
2704
}
 
2705
 
 
2706
int TLuaInterpreter::setRoomIDbyHash( lua_State *L )
 
2707
{
 
2708
    int id;
 
2709
    if( ! lua_isnumber( L, 1 ) )
 
2710
    {
 
2711
        lua_pushstring( L, "setRoomIDbyHash: wrong argument type" );
 
2712
        lua_error( L );
 
2713
        return 1;
 
2714
    }
 
2715
    else
 
2716
    {
 
2717
        id = lua_tonumber( L, 1 );
 
2718
    }
 
2719
    string hash;
 
2720
    if( ! lua_isstring( L, 2 ) )
 
2721
    {
 
2722
        lua_pushstring( L, "setRoomIDbyHash: wrong argument type" );
 
2723
        lua_error( L );
 
2724
        return 1;
 
2725
    }
 
2726
    else
 
2727
    {
 
2728
        hash = lua_tostring( L, 2 );
 
2729
    }
 
2730
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2731
    pHost->mpMap->hashTable[QString(hash.c_str())] = id;
 
2732
    return 0;
 
2733
}
 
2734
 
 
2735
int TLuaInterpreter::getRoomIDbyHash( lua_State *L )
 
2736
{
 
2737
    string hash;
 
2738
    if( ! lua_isstring( L, 1 ) )
 
2739
    {
 
2740
        lua_pushstring( L, "getRoomIDbyHash() wrong argument type" );
 
2741
        lua_error( L );
 
2742
        return 1;
 
2743
    }
 
2744
    else
 
2745
    {
 
2746
        hash = lua_tostring( L, 1 );
 
2747
    }
 
2748
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2749
    int retID = -1;
 
2750
    QString _hash = hash.c_str();
 
2751
    if( pHost->mpMap->hashTable.contains( _hash ) )
 
2752
    {
 
2753
        retID = pHost->mpMap->hashTable[_hash];
 
2754
        lua_pushnumber( L, retID );
 
2755
    }
 
2756
    else
 
2757
        lua_pushnumber( L, -1 );
 
2758
 
 
2759
    return 1;
 
2760
}
 
2761
 
 
2762
int TLuaInterpreter::solveRoomCollisions( lua_State *L )
 
2763
{
 
2764
    int id;
 
2765
    if( ! lua_isnumber( L, 1 ) )
 
2766
    {
 
2767
        lua_pushstring( L, "solveRoomCollisions() wrong argument type" );
 
2768
        lua_error( L );
 
2769
        return 1;
 
2770
    }
 
2771
    else
 
2772
    {
 
2773
        id = lua_tonumber( L, 1 );
 
2774
    }
 
2775
    int creationDirection=0;
 
2776
    if( ! lua_isnumber( L, 2 ) )
 
2777
    {
 
2778
        lua_pushstring( L, "solveRoomCollisons() wrong argument type" );
 
2779
        lua_error( L );
 
2780
        return 1;
 
2781
    }
 
2782
    else
 
2783
    {
 
2784
        creationDirection = lua_tonumber( L, 2 );
 
2785
    }
 
2786
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2787
    if( pHost->mpMap->rooms.contains( id ) )
 
2788
    {
 
2789
        pHost->mpMap->solveRoomCollision( id, creationDirection );
 
2790
    }
 
2791
    return 0;
 
2792
}
 
2793
 
 
2794
int TLuaInterpreter::roomLocked( lua_State *L )
 
2795
{
 
2796
    int id;
 
2797
    if( ! lua_isnumber( L, 1 ) )
 
2798
    {
 
2799
        lua_pushstring( L, "roomLocked: wrong argument type" );
 
2800
        lua_error( L );
 
2801
        return 1;
 
2802
    }
 
2803
    else
 
2804
    {
 
2805
        id = lua_tonumber( L, 1 );
 
2806
    }
 
2807
 
 
2808
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2809
    if( pHost->mpMap->rooms.contains( id ) )
 
2810
    {
 
2811
        bool r = pHost->mpMap->rooms[id]->isLocked;
 
2812
        lua_pushboolean( L, r );
 
2813
    }
 
2814
    else
 
2815
    {
 
2816
        lua_pushboolean(L, false);
 
2817
    }
 
2818
    return 1;
 
2819
}
 
2820
 
 
2821
int TLuaInterpreter::lockRoom( lua_State *L )
 
2822
{
 
2823
    bool b = true;
 
2824
    int id;
 
2825
    if( ! lua_isnumber( L, 1 ) )
 
2826
    {
 
2827
        lua_pushstring( L, "lockRoom: wrong argument type" );
 
2828
        lua_error( L );
 
2829
        return 1;
 
2830
    }
 
2831
    else
 
2832
    {
 
2833
        id = lua_tonumber( L, 1 );
 
2834
    }
 
2835
 
 
2836
    if( ! lua_isboolean( L, 2 ) )
 
2837
    {
 
2838
        lua_pushstring( L, "lockRoom: wrong argument type" );
 
2839
        lua_error( L );
 
2840
        return 1;
 
2841
    }
 
2842
    else
 
2843
    {
 
2844
        b = lua_toboolean( L, 2 );
 
2845
    }
 
2846
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2847
    if( pHost->mpMap->rooms.contains( id ) )
 
2848
    {
 
2849
        pHost->mpMap->rooms[id]->isLocked = b;
 
2850
        pHost->mpMap->mMapGraphNeedsUpdate = true;
 
2851
        lua_pushboolean(L, true);
 
2852
    }
 
2853
    else
 
2854
    {
 
2855
        lua_pushboolean(L, false);
 
2856
    }
 
2857
    return 1;
 
2858
}
 
2859
 
 
2860
int TLuaInterpreter::lockExit( lua_State *L )
 
2861
{
 
2862
    bool b = true;
 
2863
    int id;
 
2864
    int dir;
 
2865
    if( ! lua_isnumber( L, 1 ) )
 
2866
    {
 
2867
        lua_pushstring( L, "lockExit: wrong argument type" );
 
2868
        lua_error( L );
 
2869
        return 1;
 
2870
    }
 
2871
    else
 
2872
    {
 
2873
        id = lua_tonumber( L, 1 );
 
2874
    }
 
2875
 
 
2876
 
 
2877
    if( ! lua_isnumber( L, 2 ) )
 
2878
    {
 
2879
        lua_pushstring( L, "lockExit: wrong argument type" );
 
2880
        lua_error( L );
 
2881
        return 1;
 
2882
    }
 
2883
    else
 
2884
    {
 
2885
        dir = lua_tonumber( L, 2 );
 
2886
    }
 
2887
 
 
2888
    if( ! lua_isboolean( L, 3 ) )
 
2889
    {
 
2890
        lua_pushstring( L, "lockRoom: wrong argument type" );
 
2891
        lua_error( L );
 
2892
        return 1;
 
2893
    }
 
2894
    else
 
2895
    {
 
2896
        b = lua_toboolean( L, 3 );
 
2897
    }
 
2898
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2899
    if( pHost->mpMap->rooms.contains( id ) )
 
2900
    {
 
2901
        pHost->mpMap->rooms[id]->setExitLock( dir, b );
 
2902
        pHost->mpMap->mMapGraphNeedsUpdate = true;
 
2903
    }
 
2904
    return 0;
 
2905
}
 
2906
 
 
2907
int TLuaInterpreter::lockSpecialExit( lua_State *L )
 
2908
{
 
2909
    bool b = true;
 
2910
    int id, to;
 
2911
    std::string dir;
 
2912
    if( ! lua_isnumber( L, 1 ) )
 
2913
    {
 
2914
        lua_pushstring( L, "lockSpecialExit: wrong argument type" );
 
2915
        lua_error( L );
 
2916
        return 1;
 
2917
    }
 
2918
    else
 
2919
    {
 
2920
        id = lua_tonumber( L, 1 );
 
2921
    }
 
2922
 
 
2923
    if( ! lua_isnumber( L, 2 ) )
 
2924
    {
 
2925
        lua_pushstring( L, "lockExit: wrong argument type" );
 
2926
        lua_error( L );
 
2927
        return 1;
 
2928
    }
 
2929
    else
 
2930
    {
 
2931
        to = lua_tonumber( L, 2 );
 
2932
    }
 
2933
 
 
2934
    if( ! lua_isstring( L, 3 ) )
 
2935
    {
 
2936
        lua_pushstring( L, "lockSpecialExit: wrong argument type" );
 
2937
        lua_error( L );
 
2938
        return 1;
 
2939
    }
 
2940
    else
 
2941
    {
 
2942
        dir = lua_tostring( L, 3 );
 
2943
    }
 
2944
 
 
2945
    if( ! lua_isboolean( L, 4 ) )
 
2946
    {
 
2947
        lua_pushstring( L, "lockSpecialExit: wrong argument type" );
 
2948
        lua_error( L );
 
2949
        return 1;
 
2950
    }
 
2951
    else
 
2952
    {
 
2953
        b = lua_toboolean( L, 4 );
 
2954
    }
 
2955
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
2956
    if( pHost->mpMap->rooms.contains( id ) )
 
2957
    {
 
2958
        QString _dir = dir.c_str();
 
2959
        pHost->mpMap->rooms[id]->setSpecialExitLock( to, _dir, b );
 
2960
        pHost->mpMap->mMapGraphNeedsUpdate = true;
 
2961
    }
 
2962
    return 0;
 
2963
}
 
2964
 
 
2965
int TLuaInterpreter::hasSpecialExitLock( lua_State *L )
 
2966
{
 
2967
    bool b = true;
 
2968
    int id, to;
 
2969
    std::string dir;
 
2970
    if( ! lua_isnumber( L, 1 ) )
 
2971
    {
 
2972
        lua_pushstring( L, "lockExit: wrong argument type" );
 
2973
        lua_error( L );
 
2974
        return 1;
 
2975
    }
 
2976
    else
 
2977
    {
 
2978
        id = lua_tonumber( L, 1 );
 
2979
    }
 
2980
 
 
2981
 
 
2982
    if( ! lua_isnumber( L, 2 ) )
 
2983
    {
 
2984
        lua_pushstring( L, "lockExit: wrong argument type" );
 
2985
        lua_error( L );
 
2986
        return 1;
 
2987
    }
 
2988
    else
 
2989
    {
 
2990
        to = lua_tonumber( L, 2 );
 
2991
    }
 
2992
    if( ! lua_isstring( L, 3 ) )
 
2993
    {
 
2994
        lua_pushstring( L, "hasSpecialExitLock: wrong argument type" );
 
2995
        lua_error( L );
 
2996
        return 1;
 
2997
    }
 
2998
    else
 
2999
    {
 
3000
        dir = lua_tostring( L, 3 );
 
3001
    }
 
3002
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3003
    if( pHost->mpMap->rooms.contains( id ) )
 
3004
    {
 
3005
        QString _dir = dir.c_str();
 
3006
        lua_pushboolean( L, pHost->mpMap->rooms[id]->hasSpecialExitLock( to, _dir ) );
 
3007
        return 1;
 
3008
    }
 
3009
    return 0;
 
3010
}
 
3011
 
 
3012
int TLuaInterpreter::hasExitLock( lua_State *L )
 
3013
{
 
3014
    int id;
 
3015
    int dir;
 
3016
    if( ! lua_isnumber( L, 1 ) )
 
3017
    {
 
3018
        lua_pushstring( L, "lockExit: wrong argument type" );
 
3019
        lua_error( L );
 
3020
        return 1;
 
3021
    }
 
3022
    else
 
3023
    {
 
3024
        id = lua_tonumber( L, 1 );
 
3025
    }
 
3026
 
 
3027
 
 
3028
    if( ! lua_isnumber( L, 2 ) )
 
3029
    {
 
3030
        lua_pushstring( L, "lockExit: wrong argument type" );
 
3031
        lua_error( L );
 
3032
        return 1;
 
3033
    }
 
3034
    else
 
3035
    {
 
3036
        dir = lua_tonumber( L, 2 );
 
3037
    }
 
3038
 
 
3039
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3040
    if( pHost->mpMap->rooms.contains( id ) )
 
3041
    {
 
3042
        lua_pushboolean( L, pHost->mpMap->rooms[id]->hasExitLock(dir) );
 
3043
        return 1;
 
3044
    }
 
3045
    return 0;
 
3046
}
 
3047
 
 
3048
int TLuaInterpreter::isLockedRoom( lua_State *L )
 
3049
{
 
3050
    int id;
 
3051
    if( ! lua_isnumber( L, 1 ) )
 
3052
    {
 
3053
        lua_pushstring( L, "lockRoom: wrong argument type" );
 
3054
        lua_error( L );
 
3055
        return 1;
 
3056
    }
 
3057
    else
 
3058
    {
 
3059
        id = lua_tonumber( L, 1 );
 
3060
    }
 
3061
 
 
3062
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3063
    if( pHost->mpMap->rooms.contains( id ) )
 
3064
    {
 
3065
        lua_pushboolean( L, pHost->mpMap->rooms[id]->isLocked );
 
3066
    }
 
3067
    else
 
3068
    {
 
3069
        lua_pushboolean(L, false);
 
3070
    }
 
3071
    return 1;
 
3072
}
 
3073
 
 
3074
 
 
3075
int TLuaInterpreter::getRoomExits( lua_State *L )
 
3076
{
 
3077
    int id;
 
3078
    if( ! lua_isnumber( L, 1 ) )
 
3079
    {
 
3080
        lua_pushstring( L, "getRoomExits: wrong argument type" );
 
3081
        lua_error( L );
 
3082
        return 1;
 
3083
    }
 
3084
    else
 
3085
    {
 
3086
        id = lua_tonumber( L, 1 );
 
3087
    }
 
3088
 
 
3089
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3090
    if( pHost->mpMap->rooms.contains( id ) )
 
3091
    {
 
3092
        lua_newtable(L);
 
3093
        if( pHost->mpMap->rooms[id]->north != -1 )
 
3094
        {
 
3095
            lua_pushstring( L, "north" );
 
3096
            lua_pushnumber( L, pHost->mpMap->rooms[id]->north );
 
3097
            lua_settable(L, -3);
 
3098
        }
 
3099
        if( pHost->mpMap->rooms[id]->northwest != -1 )
 
3100
        {
 
3101
            lua_pushstring( L, "northwest" );
 
3102
            lua_pushnumber( L, pHost->mpMap->rooms[id]->northwest );
 
3103
            lua_settable(L, -3);
 
3104
        }
 
3105
        if( pHost->mpMap->rooms[id]->northeast != -1 )
 
3106
        {
 
3107
            lua_pushstring( L, "northeast" );
 
3108
            lua_pushnumber( L, pHost->mpMap->rooms[id]->northeast );
 
3109
            lua_settable(L, -3);
 
3110
        }
 
3111
        if( pHost->mpMap->rooms[id]->south != -1 )
 
3112
        {
 
3113
            lua_pushstring( L, "south" );
 
3114
            lua_pushnumber( L, pHost->mpMap->rooms[id]->south );
 
3115
            lua_settable(L, -3);
 
3116
        }
 
3117
        if( pHost->mpMap->rooms[id]->southwest != -1 )
 
3118
        {
 
3119
            lua_pushstring( L, "southwest" );
 
3120
            lua_pushnumber( L, pHost->mpMap->rooms[id]->southwest );
 
3121
            lua_settable(L, -3);
 
3122
        }
 
3123
        if( pHost->mpMap->rooms[id]->southeast != -1 )
 
3124
        {
 
3125
            lua_pushstring( L, "southeast" );
 
3126
            lua_pushnumber( L, pHost->mpMap->rooms[id]->southeast );
 
3127
            lua_settable(L, -3);
 
3128
        }
 
3129
        if( pHost->mpMap->rooms[id]->west != -1 )
 
3130
        {
 
3131
            lua_pushstring( L, "west" );
 
3132
            lua_pushnumber( L, pHost->mpMap->rooms[id]->west );
 
3133
            lua_settable(L, -3);
 
3134
        }
 
3135
        if( pHost->mpMap->rooms[id]->east != -1 )
 
3136
        {
 
3137
            lua_pushstring( L, "east" );
 
3138
            lua_pushnumber( L, pHost->mpMap->rooms[id]->east );
 
3139
            lua_settable(L, -3);
 
3140
        }
 
3141
        if( pHost->mpMap->rooms[id]->up != -1 )
 
3142
        {
 
3143
            lua_pushstring( L, "up" );
 
3144
            lua_pushnumber( L, pHost->mpMap->rooms[id]->up );
 
3145
            lua_settable(L, -3);
 
3146
        }
 
3147
        if( pHost->mpMap->rooms[id]->down != -1 )
 
3148
        {
 
3149
            lua_pushstring( L, "down" );
 
3150
            lua_pushnumber( L, pHost->mpMap->rooms[id]->down );
 
3151
            lua_settable(L, -3);
 
3152
        }
 
3153
        if( pHost->mpMap->rooms[id]->in != -1 )
 
3154
        {
 
3155
            lua_pushstring( L, "in" );
 
3156
            lua_pushnumber( L, pHost->mpMap->rooms[id]->in );
 
3157
            lua_settable(L, -3);
 
3158
        }
 
3159
        if( pHost->mpMap->rooms[id]->out != -1 )
 
3160
        {
 
3161
            lua_pushstring( L, "out" );
 
3162
            lua_pushnumber( L, pHost->mpMap->rooms[id]->out );
 
3163
            lua_settable(L, -3);
 
3164
        }
 
3165
        return 1;
 
3166
    }
 
3167
    else
 
3168
        return 0;
 
3169
}
 
3170
 
 
3171
int TLuaInterpreter::searchRoom( lua_State *L )
 
3172
{
 
3173
    int room_id = 0;
 
3174
    bool gotRoomID = false;
 
3175
    string room;
 
3176
    if( lua_isnumber( L, 1 ) )
 
3177
    {
 
3178
        room_id = lua_tointeger( L, 1 );
 
3179
        gotRoomID = true;
 
3180
    }
 
3181
    else if( lua_isstring( L, 1 ) )
 
3182
    {
 
3183
        room = lua_tostring( L, 1 );
 
3184
    }
 
3185
    else
 
3186
    {
 
3187
        lua_pushstring( L, "searchRoom: wrong argument type" );
 
3188
        lua_error( L );
 
3189
        return 1;
 
3190
    }
 
3191
 
 
3192
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3193
    if( gotRoomID )
 
3194
    {
 
3195
        if( pHost->mpMap->rooms.contains( room_id ) )
 
3196
        {
 
3197
            lua_pushstring( L, pHost->mpMap->rooms[room_id]->name.toLatin1().data() );
 
3198
            return 1;
 
3199
        }
 
3200
        else
 
3201
        {
 
3202
            lua_pushstring( L, "searchRoom ERROR: no such room" );
 
3203
            return 1;
 
3204
        }
 
3205
    }
 
3206
    else
 
3207
    {
 
3208
        QMapIterator<int, TRoom *> it( pHost->mpMap->rooms );
 
3209
        lua_newtable(L);
 
3210
        while( it.hasNext() )
 
3211
        {
 
3212
            it.next();
 
3213
            int i = it.key();
 
3214
            if( pHost->mpMap->rooms[i]->name.contains( QString(room.c_str()), Qt::CaseInsensitive ) )
 
3215
            {
 
3216
                QString name = pHost->mpMap->rooms[i]->name;
 
3217
                int roomID = pHost->mpMap->rooms[i]->id;
 
3218
                lua_pushstring( L, name.toLatin1().data() );
 
3219
                lua_pushnumber( L, roomID );
 
3220
                lua_settable(L, -3);
 
3221
            }
 
3222
        }
 
3223
        return 1;
 
3224
    }
 
3225
}
 
3226
 
 
3227
int TLuaInterpreter::getAreaTable( lua_State *L )
 
3228
{
 
3229
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3230
    QMapIterator<int, QString> it( pHost->mpMap->areaNamesMap );
 
3231
    lua_newtable(L);
 
3232
    while( it.hasNext() )
 
3233
    {
 
3234
        it.next();
 
3235
        int roomID = it.key();
 
3236
        QString name = it.value();
 
3237
        lua_pushstring( L, name.toLatin1().data() );
 
3238
        lua_pushnumber( L, roomID );
 
3239
        lua_settable(L, -3);
 
3240
    }
 
3241
    return 1;
 
3242
}
 
3243
 
 
3244
int TLuaInterpreter::getAreaRooms( lua_State *L )
 
3245
{
 
3246
    int area;
 
3247
    if( ! lua_isnumber( L, 1 ) )
 
3248
    {
 
3249
        lua_pushstring( L, "getAreaRooms: wrong argument type" );
 
3250
        lua_error( L );
 
3251
        return 1;
 
3252
    }
 
3253
    else
 
3254
    {
 
3255
        area = lua_tonumber( L, 1 );
 
3256
    }
 
3257
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3258
    if( ! pHost->mpMap->areas.contains( area ) )
 
3259
    {
 
3260
        lua_pushnil(L);
 
3261
        return 1;
 
3262
    }
 
3263
    lua_newtable(L);
 
3264
    for( int i=0; i<pHost->mpMap->areas[area]->rooms.size(); i++ )
 
3265
    {
 
3266
        int roomID = pHost->mpMap->areas[area]->rooms.at( i );
 
3267
        lua_pushnumber( L, i );
 
3268
        lua_pushnumber( L, roomID );
 
3269
        lua_settable(L, -3);
 
3270
    }
 
3271
    return 1;
 
3272
}
 
3273
 
 
3274
int TLuaInterpreter::getRooms( lua_State *L )
 
3275
{
 
3276
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3277
    lua_newtable(L);
 
3278
    QMapIterator<int,TRoom*> it(pHost->mpMap->rooms);
 
3279
    while( it.hasNext() )
 
3280
    {
 
3281
        it.next();
 
3282
        lua_pushnumber( L, it.key() );
 
3283
        lua_pushstring( L, it.value()->name.toLatin1().data() );
 
3284
        lua_settable(L, -3);
 
3285
    }
 
3286
    return 1;
 
3287
}
 
3288
 
 
3289
 
 
3290
 
 
3291
int TLuaInterpreter::getRoomWeight( lua_State *L )
 
3292
{
 
3293
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3294
    if( pHost->mpMap->rooms.contains( pHost->mpMap->mRoomId ) )
 
3295
    {
 
3296
        lua_pushnumber( L, pHost->mpMap->rooms[pHost->mpMap->mRoomId]->weight );
 
3297
    }
 
3298
 
 
3299
    return 1;
 
3300
}
 
3301
 
 
3302
int TLuaInterpreter::gotoRoom( lua_State *L )
 
3303
{
 
3304
    int r;
 
3305
    if( ! lua_isnumber( L, 1 ) )
 
3306
    {
 
3307
        lua_pushstring( L, "gotoRoom: wrong argument type" );
 
3308
        lua_error( L );
 
3309
        return 1;
 
3310
    }
 
3311
    else
 
3312
    {
 
3313
        r = lua_tonumber( L, 1 );
 
3314
    }
 
3315
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3316
    bool ret = pHost->mpMap->gotoRoom( r );
 
3317
    pHost->startSpeedWalk();
 
3318
    lua_pushboolean( L, ret );
 
3319
    return 1;
 
3320
}
 
3321
 
 
3322
int TLuaInterpreter::getPath( lua_State *L )
 
3323
{
 
3324
    int r1;
 
3325
    int r2;
 
3326
    if( ! lua_isnumber( L, 1 ) )
 
3327
    {
 
3328
        lua_pushstring( L, "getPath: wrong argument type" );
 
3329
        lua_error( L );
 
3330
        return 1;
 
3331
    }
 
3332
    else
 
3333
    {
 
3334
        r1 = lua_tonumber( L, 1 );
 
3335
    }
 
3336
    if( ! lua_isnumber( L, 2 ) )
 
3337
    {
 
3338
        lua_pushstring( L, "getPath: wrong argument type" );
 
3339
        lua_error( L );
 
3340
        return 1;
 
3341
    }
 
3342
    else
 
3343
    {
 
3344
        r2 = lua_tonumber( L, 2 );
 
3345
    }
 
3346
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3347
    bool ret = pHost->mpMap->gotoRoom( r1, r2 );
 
3348
    pHost->assemblePath();
 
3349
    lua_pushboolean( L, ret );
 
3350
    return 1;
 
3351
}
 
3352
 
 
3353
int TLuaInterpreter::deselect( lua_State *L )
 
3354
{
 
3355
    string luaWindowName="";
 
3356
    if( lua_isstring( L, 1 ) )
 
3357
    {
 
3358
        luaWindowName = lua_tostring( L, 1 );
 
3359
    }
 
3360
 
 
3361
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3362
    QString name = luaWindowName.c_str();
 
3363
    if( luaWindowName.size() < 1 || luaWindowName == "main" )
 
3364
    {
 
3365
        pHost->mpConsole->deselect();
 
3366
    }
 
3367
    else
 
3368
    {
 
3369
        mudlet::self()->deselect( pHost, name );
 
3370
    }
 
3371
    return 0;
 
3372
}
 
3373
 
 
3374
int TLuaInterpreter::reset( lua_State *L )
 
3375
{
 
3376
    string luaWindowName="";
 
3377
    if( lua_isstring( L, 1 ) )
 
3378
    {
 
3379
        luaWindowName = lua_tostring( L, 1 );
 
3380
    }
 
3381
 
 
3382
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3383
    QString name = luaWindowName.c_str();
 
3384
    if( luaWindowName.size() < 1 || luaWindowName == "main" )
 
3385
    {
 
3386
        pHost->mpConsole->reset();
 
3387
    }
 
3388
    else
 
3389
    {
 
3390
        mudlet::self()->resetFormat( pHost, name );
 
3391
    }
 
3392
    return 0;
 
3393
}
 
3394
 
 
3395
int TLuaInterpreter::hasFocus( lua_State *L )
 
3396
{
 
3397
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3398
    lua_pushboolean( L, pHost->mpConsole->hasFocus() );//FIXME
 
3399
    return 1;
 
3400
}
 
3401
 
 
3402
int TLuaInterpreter::echoUserWindow( lua_State *L )
 
3403
{
 
3404
    string luaWindowName="";
 
3405
    if( ! lua_isstring( L, 1 ) )
 
3406
    {
 
3407
        lua_pushstring( L, "echoUserWindow: wrong argument type" );
 
3408
        lua_error( L );
 
3409
        return 1;
 
3410
    }
 
3411
    else
 
3412
    {
 
3413
        luaWindowName = lua_tostring( L, 1 );
 
3414
    }
 
3415
 
 
3416
    string luaSendText="";
 
3417
    if( ! lua_isstring( L, 2 ) )
 
3418
    {
 
3419
        lua_pushstring( L, "echoUserWindow: wrong argument type" );
 
3420
        lua_error( L );
 
3421
        return 1;
 
3422
    }
 
3423
    else
 
3424
    {
 
3425
        luaSendText = lua_tostring( L, 2 );
 
3426
    }
 
3427
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3428
    QString text(luaSendText.c_str());
 
3429
    QString windowName(luaWindowName.c_str());
 
3430
    mudlet::self()->echoWindow( pHost, windowName, text );
 
3431
    return 0;
 
3432
}
 
3433
 
 
3434
//qApp->setStyleSheet("QMainWindow::separator{border: 0px;width: 0px; height: 0px; padding: 0px;} QMainWindow::separator:hover {background: red;}");
 
3435
int TLuaInterpreter::setAppStyleSheet( lua_State *L )
 
3436
{
 
3437
    string luaWindowName="";
 
3438
    if( lua_isstring( L, 1 ) )
 
3439
    {
 
3440
        luaWindowName = lua_tostring( L, 1 );
 
3441
    }
 
3442
    else
 
3443
        luaWindowName = "main";
 
3444
    qApp->setStyleSheet( luaWindowName.c_str() );
 
3445
}
 
3446
 
 
3447
// this is an internal only function used by the package system
 
3448
int TLuaInterpreter::showUnzipProgress( lua_State * L )
 
3449
{
 
3450
    string luaSendText="";
 
3451
    if( ! lua_isstring( L, 1 ) )
 
3452
    {
 
3453
        lua_pushstring( L, "showUnzipProgress: wrong argument type" );
 
3454
        lua_error( L );
 
3455
        return 1;
 
3456
    }
 
3457
    else
 
3458
    {
 
3459
        luaSendText = lua_tostring( L, 1 );
 
3460
    }
 
3461
    QString txt = luaSendText.c_str();
 
3462
    mudlet::self()->showUnzipProgress( txt );
 
3463
    return 0;
 
3464
}
 
3465
 
 
3466
#ifdef Q_OS_LINUX
 
3467
    #include <phonon>
 
3468
#else
 
3469
    #include <Phonon>
 
3470
#endif
 
3471
 
 
3472
int TLuaInterpreter::playSoundFile( lua_State * L )
 
3473
{
 
3474
    string luaSendText="";
 
3475
    if( ! lua_isstring( L, 1 ) )
 
3476
    {
 
3477
        lua_pushstring( L, "playSoundFile: wrong argument type" );
 
3478
        lua_error( L );
 
3479
        return 1;
 
3480
    }
 
3481
    else
 
3482
    {
 
3483
        luaSendText = lua_tostring( L, 1 );
 
3484
    }
 
3485
    QString sound = luaSendText.c_str();
 
3486
    //QSound::play( QString( luaSendText.c_str() ) );
 
3487
    if( QDir::homePath().contains('\\') )
 
3488
    {
 
3489
        sound.replace('/', "\\");
 
3490
    }
 
3491
    else
 
3492
    {
 
3493
        sound.replace('\\', "/");
 
3494
    }
 
3495
    mudlet::self()->playSound( sound );
 
3496
    return 0;
 
3497
}
 
3498
 
 
3499
int TLuaInterpreter::moveCursorEnd( lua_State *L )
 
3500
{
 
3501
    string luaWindowName="";
 
3502
    if( lua_isstring( L, 1 ) )
 
3503
    {
 
3504
        luaWindowName = lua_tostring( L, 1 );
 
3505
    }
 
3506
    else
 
3507
        luaWindowName = "main";
 
3508
 
 
3509
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3510
    QString windowName(luaWindowName.c_str());
 
3511
    if( luaWindowName == "main" )
 
3512
        pHost->mpConsole->moveCursorEnd();
 
3513
    else
 
3514
       mudlet::self()->moveCursorEnd( pHost, windowName );
 
3515
    return 0;
 
3516
}
 
3517
 
 
3518
int TLuaInterpreter::getLastLineNumber( lua_State *L )
 
3519
{
 
3520
    string luaWindowName;
 
3521
    if( ! lua_isstring( L, 1 ) )
 
3522
    {
 
3523
        lua_pushstring( L, "getLastLineNumber: wrong argument type" );
 
3524
        lua_error( L );
 
3525
        return 1;
 
3526
    }
 
3527
    else
 
3528
    {
 
3529
        luaWindowName = lua_tostring( L, 1 );
 
3530
    }
 
3531
 
 
3532
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3533
    QString windowName(luaWindowName.c_str());
 
3534
    int number;
 
3535
    if( luaWindowName == "main" )
 
3536
        number = pHost->mpConsole->getLastLineNumber();
 
3537
    else
 
3538
        number = mudlet::self()->getLastLineNumber( pHost, windowName );
 
3539
    lua_pushnumber( L, number );
 
3540
    return 1;
 
3541
}
 
3542
 
 
3543
 
 
3544
 
 
3545
int TLuaInterpreter::getMudletHomeDir( lua_State * L )
 
3546
{
 
3547
    QString home = QDir::homePath();
 
3548
    home.append( "/.config/mudlet/profiles/" );
 
3549
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3550
    QString name = pHost->getName();
 
3551
    home.append( name );
 
3552
    QString erg = QDir::toNativeSeparators( home );
 
3553
    lua_pushstring( L, erg.toLatin1().data() );
 
3554
    return 1;
 
3555
}
 
3556
 
 
3557
int TLuaInterpreter::disconnect( lua_State * L )
 
3558
{
 
3559
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3560
    pHost->mTelnet.disconnect();
 
3561
    return 0;
 
3562
}
 
3563
 
 
3564
int TLuaInterpreter::reconnect( lua_State * L )
 
3565
{
 
3566
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3567
    pHost->mTelnet.connectIt( pHost->getUrl(), pHost->getPort() );
 
3568
    return 0;
 
3569
}
 
3570
 
 
3571
int TLuaInterpreter::setTriggerStayOpen( lua_State *L )
 
3572
{
 
3573
    string luaWindowName;
 
3574
    double b;
 
3575
    int s = 1;
 
3576
    if( lua_gettop( L ) > 1 )
 
3577
    {
 
3578
        if( ! lua_isstring( L, s ) )
 
3579
        {
 
3580
            lua_pushstring( L, "setTriggerStayOpen: wrong argument type" );
 
3581
            lua_error( L );
 
3582
            return 1;
 
3583
        }
 
3584
        else
 
3585
        {
 
3586
            luaWindowName = lua_tostring( L, s );
 
3587
            s++;
 
3588
        }
 
3589
    }
 
3590
    if( ! lua_isnumber( L, s ) )
 
3591
    {
 
3592
        lua_pushstring( L, "setTriggerStayOpen: wrong argument type" );
 
3593
        lua_error( L );
 
3594
        return 1;
 
3595
    }
 
3596
    else
 
3597
    {
 
3598
        b = lua_tonumber( L, s );
 
3599
    }
 
3600
 
 
3601
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3602
    QString windowName(luaWindowName.c_str());
 
3603
    pHost->getTriggerUnit()->setTriggerStayOpen( QString( luaWindowName.c_str() ), static_cast<int>(b) );
 
3604
    return 0;
 
3605
}
 
3606
 
 
3607
int TLuaInterpreter::setLink( lua_State * L )
 
3608
{
 
3609
    string luaWindowName;
 
3610
    string linkText;
 
3611
    string linkFunction;
 
3612
    string linkHint;
 
3613
    int s = 1;
 
3614
    if( lua_gettop( L ) > 2 )
 
3615
    {
 
3616
        if( ! lua_isstring( L, s ) )
 
3617
        {
 
3618
            lua_pushstring( L, "setLink: wrong argument type" );
 
3619
            lua_error( L );
 
3620
            return 1;
 
3621
        }
 
3622
        else
 
3623
        {
 
3624
            luaWindowName = lua_tostring( L, s );
 
3625
            s++;
 
3626
        }
 
3627
    }
 
3628
    /*if( ! lua_isstring( L, s ) )
 
3629
    {
 
3630
        lua_pushstring( L, "setLink: wrong argument type" );
 
3631
        lua_error( L );
 
3632
        return 1;
 
3633
    }
 
3634
    else
 
3635
    {
 
3636
        linkText = lua_tostring( L, s );
 
3637
        s++;
 
3638
    }*/
 
3639
 
 
3640
    if( ! lua_isstring( L, s ) )
 
3641
    {
 
3642
        lua_pushstring( L, "setLink: wrong argument type" );
 
3643
        lua_error( L );
 
3644
        return 1;
 
3645
    }
 
3646
    else
 
3647
    {
 
3648
        linkFunction = lua_tostring( L, s );
 
3649
        s++;
 
3650
    }
 
3651
    if( ! lua_isstring( L, s ) )
 
3652
    {
 
3653
        lua_pushstring( L, "setLink: wrong argument type" );
 
3654
        lua_error( L );
 
3655
        return 1;
 
3656
    }
 
3657
    else
 
3658
    {
 
3659
        linkHint = lua_tostring( L, s );
 
3660
        s++;
 
3661
    }
 
3662
 
 
3663
 
 
3664
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3665
    QString windowName(luaWindowName.c_str());
 
3666
    QString _linkText = "";//QString(linkText.c_str());
 
3667
    QStringList _linkFunction;
 
3668
    _linkFunction <<  QString(linkFunction.c_str());
 
3669
    QStringList _linkHint;
 
3670
    _linkHint << QString(linkHint.c_str());
 
3671
    if( windowName.size() > 0 )
 
3672
        mudlet::self()->setLink( pHost, windowName, _linkText, _linkFunction, _linkHint );
 
3673
    else
 
3674
        pHost->mpConsole->setLink( _linkText, _linkFunction, _linkHint );
 
3675
    return 0;
 
3676
}
 
3677
 
 
3678
int TLuaInterpreter::setPopup( lua_State *L )
 
3679
{
 
3680
    string a1 = "";
 
3681
    string a2;
 
3682
    QStringList _hintList;
 
3683
    QStringList _commandList;
 
3684
    bool customFormat = false;
 
3685
    int s = 1;
 
3686
    int n = lua_gettop( L );
 
3687
    // console name is an optional first argument
 
3688
    if( n > 4 )
 
3689
    {
 
3690
        if( ! lua_isstring( L, s ) )
 
3691
        {
 
3692
            lua_pushstring( L, "setPopup: wrong argument type" );
 
3693
            lua_error( L );
 
3694
            return 1;
 
3695
        }
 
3696
        else
 
3697
        {
 
3698
            a1 = lua_tostring( L, s );
 
3699
            s++;
 
3700
        }
 
3701
    }
 
3702
    if( ! lua_isstring( L, s ) )
 
3703
    {
 
3704
        lua_pushstring( L, "setPopup: wrong argument type" );
 
3705
        lua_error( L );
 
3706
        return 1;
 
3707
    }
 
3708
    else
 
3709
    {
 
3710
        a2 = lua_tostring( L, s );
 
3711
        s++;
 
3712
    }
 
3713
 
 
3714
    if( ! lua_istable( L, s ) )
 
3715
    {
 
3716
        lua_pushstring( L, "setPopup: wrong argument type" );
 
3717
        lua_error( L );
 
3718
        return 1;
 
3719
    }
 
3720
    else
 
3721
    {
 
3722
        lua_pushnil( L );
 
3723
        while( lua_next( L, s ) != 0 )
 
3724
        {
 
3725
            // key at index -2 and value at index -1
 
3726
            if( lua_type(L, -1) == LUA_TSTRING )
 
3727
            {
 
3728
                QString cmd = lua_tostring( L, -1 );
 
3729
                _commandList << cmd;
 
3730
            }
 
3731
            // removes value, but keeps key for next iteration
 
3732
            lua_pop(L, 1);
 
3733
        }
 
3734
        s++;
 
3735
    }
 
3736
    if( ! lua_istable( L, s ) )
 
3737
    {
 
3738
        lua_pushstring( L, "setPopup: wrong argument type" );
 
3739
        lua_error( L );
 
3740
        return 1;
 
3741
    }
 
3742
    else
 
3743
    {
 
3744
        lua_pushnil( L );
 
3745
        while( lua_next( L, s ) != 0 )
 
3746
        {
 
3747
            // key at index -2 and value at index -1
 
3748
            if( lua_type(L, -1) == LUA_TSTRING )
 
3749
            {
 
3750
                QString hint = lua_tostring( L, -1 );
 
3751
                _hintList << hint;
 
3752
            }
 
3753
            // removes value, but keeps key for next iteration
 
3754
            lua_pop(L, 1);
 
3755
        }
 
3756
        s++;
 
3757
    }
 
3758
    if( n >= s )
 
3759
    {
 
3760
        customFormat = lua_toboolean( L, s );
 
3761
    }
 
3762
 
 
3763
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3764
    QString txt = a2.c_str();
 
3765
    QString name = a1.c_str();
 
3766
    if( _commandList.size() != _hintList.size() )
 
3767
    {
 
3768
        lua_pushstring( L, "Error: command list size and hint list size do not match cannot create popup" );
 
3769
        lua_error( L );
 
3770
        return 1;
 
3771
    }
 
3772
 
 
3773
    if( a1 == "" )
 
3774
    {
 
3775
        pHost->mpConsole->setLink( txt, _commandList, _hintList );
 
3776
    }
 
3777
    else
 
3778
    {
 
3779
        mudlet::self()->setLink( pHost, name, txt, _commandList, _hintList );
 
3780
    }
 
3781
 
 
3782
    return 0;
 
3783
}
 
3784
 
 
3785
 
 
3786
int TLuaInterpreter::setBold( lua_State * L )
 
3787
{
 
3788
    string luaWindowName;
 
3789
    bool b;
 
3790
    int s = 1;
 
3791
    if( lua_gettop( L ) > 1 )
 
3792
    {
 
3793
        if( ! lua_isstring( L, s ) )
 
3794
        {
 
3795
            lua_pushstring( L, "setBold: wrong argument type" );
 
3796
            lua_error( L );
 
3797
            return 1;
 
3798
        }
 
3799
        else
 
3800
        {
 
3801
            luaWindowName = lua_tostring( L, s );
 
3802
            s++;
 
3803
        }
 
3804
    }
 
3805
    if( ! lua_isboolean( L, s ) )
 
3806
    {
 
3807
        lua_pushstring( L, "setBold: wrong argument type" );
 
3808
        lua_error( L );
 
3809
        return 1;
 
3810
    }
 
3811
    else
 
3812
    {
 
3813
        b = lua_toboolean( L, s );
 
3814
    }
 
3815
 
 
3816
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3817
    QString windowName(luaWindowName.c_str());
 
3818
    if( windowName.size() > 0 )
 
3819
        mudlet::self()->setBold( pHost, windowName, b );
 
3820
    else
 
3821
        pHost->mpConsole->setBold( b );
 
3822
    return 0;
 
3823
}
 
3824
 
 
3825
int TLuaInterpreter::setItalics( lua_State *L )
 
3826
{
 
3827
        string luaWindowName;
 
3828
    bool b;
 
3829
    int s = 1;
 
3830
    if( lua_gettop( L ) > 1 )
 
3831
    {
 
3832
        if( ! lua_isstring( L, s ) )
 
3833
        {
 
3834
            lua_pushstring( L, "setItalics: wrong argument type" );
 
3835
            lua_error( L );
 
3836
            return 1;
 
3837
        }
 
3838
        else
 
3839
        {
 
3840
            luaWindowName = lua_tostring( L, s );
 
3841
            s++;
 
3842
        }
 
3843
    }
 
3844
    if( ! lua_isboolean( L, s ) )
 
3845
    {
 
3846
        lua_pushstring( L, "setItalics: wrong argument type" );
 
3847
        lua_error( L );
 
3848
        return 1;
 
3849
    }
 
3850
    else
 
3851
    {
 
3852
        b = lua_toboolean( L, s );
 
3853
    }
 
3854
 
 
3855
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3856
    QString windowName(luaWindowName.c_str());
 
3857
    if( windowName.size() > 0 )
 
3858
        mudlet::self()->setItalics( pHost, windowName, b );
 
3859
    else
 
3860
        pHost->mpConsole->setItalics( b );
 
3861
    return 0;
 
3862
}
 
3863
int TLuaInterpreter::setUnderline( lua_State *L )
 
3864
{
 
3865
    string luaWindowName;
 
3866
    bool b;
 
3867
    int s = 1;
 
3868
    if( lua_gettop( L ) > 1 )
 
3869
    {
 
3870
        if( ! lua_isstring( L, s ) )
 
3871
        {
 
3872
            lua_pushstring( L, "setUnderline: wrong argument type" );
 
3873
            lua_error( L );
 
3874
            return 1;
 
3875
        }
 
3876
        else
 
3877
        {
 
3878
            luaWindowName = lua_tostring( L, s );
 
3879
            s++;
 
3880
        }
 
3881
    }
 
3882
    if( ! lua_isboolean( L, s ) )
 
3883
    {
 
3884
        lua_pushstring( L, "setUnderline: wrong argument type" );
 
3885
        lua_error( L );
 
3886
        return 1;
 
3887
    }
 
3888
    else
 
3889
    {
 
3890
        b = lua_toboolean( L, s );
 
3891
    }
 
3892
 
 
3893
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3894
    QString windowName(luaWindowName.c_str());
 
3895
    if( windowName.size() > 0 )
 
3896
        mudlet::self()->setUnderline( pHost, windowName, b );
 
3897
    else
 
3898
        pHost->mpConsole->setUnderline( b );
 
3899
    return 0;
 
3900
}
 
3901
 
 
3902
int TLuaInterpreter::debug( lua_State *L )
 
3903
{
 
3904
    int nbargs = lua_gettop(L);
 
3905
    QString luaDebugText="";
 
3906
    for (int i=0; i<nbargs; i++)
 
3907
    {
 
3908
        luaDebugText += (nbargs > 1 ? " [" + QString::number(i) + "] " : " ") + lua_tostring( L, i+1 );
 
3909
    }
 
3910
    return 0;
 
3911
}
 
3912
 
 
3913
int TLuaInterpreter::hideToolBar( lua_State *L )
 
3914
{
 
3915
    string luaWindowName;
 
3916
    if( ! lua_isstring( L, 1 ) )
 
3917
    {
 
3918
        lua_pushstring( L, "hideToolBar: wrong argument type" );
 
3919
        lua_error( L );
 
3920
        return 1;
 
3921
    }
 
3922
    else
 
3923
    {
 
3924
        luaWindowName = lua_tostring( L, 1 );
 
3925
    }
 
3926
 
 
3927
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3928
    QString windowName(luaWindowName.c_str());
 
3929
    pHost->getActionUnit()->hideToolBar( windowName );
 
3930
    return 0;
 
3931
}
 
3932
 
 
3933
int TLuaInterpreter::showToolBar( lua_State *L )
 
3934
{
 
3935
    string luaWindowName;
 
3936
    if( ! lua_isstring( L, 1 ) )
 
3937
    {
 
3938
        lua_pushstring( L, "showToolBar: wrong argument type" );
 
3939
        lua_error( L );
 
3940
        return 1;
 
3941
    }
 
3942
    else
 
3943
    {
 
3944
        luaWindowName = lua_tostring( L, 1 );
 
3945
    }
 
3946
 
 
3947
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3948
    QString windowName(luaWindowName.c_str());
 
3949
    pHost->getActionUnit()->showToolBar( windowName );
 
3950
    return 0;
 
3951
}
 
3952
 
 
3953
int TLuaInterpreter::sendATCP( lua_State *L )
 
3954
{
 
3955
    string msg;
 
3956
    if( ! lua_isstring( L, 1 ) )
 
3957
    {
 
3958
        lua_pushstring( L, "sendATCP: what do you want to send?" );
 
3959
        lua_error( L );
 
3960
        return 1;
 
3961
    }
 
3962
    else
 
3963
    {
 
3964
        msg = lua_tostring( L, 1 );
 
3965
    }
 
3966
 
 
3967
    string what;
 
3968
    if( ! lua_isstring( L, 2 ) )
 
3969
    {
 
3970
        lua_pushstring( L, "sendATCP: wrong argument type" );
 
3971
        lua_error( L );
 
3972
        return 1;
 
3973
    }
 
3974
    else
 
3975
    {
 
3976
        what = lua_tostring( L, 2 );
 
3977
    }
 
3978
    string _h;
 
3979
    _h += TN_IAC;
 
3980
    _h += TN_SB;
 
3981
    _h += 200;
 
3982
    _h += msg;
 
3983
    if (what != "") {
 
3984
      _h += " ";
 
3985
      _h += what;
 
3986
    }
 
3987
    _h += TN_IAC;
 
3988
    _h += TN_SE;
 
3989
 
 
3990
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
3991
    pHost->mTelnet.socketOutRaw( _h );
 
3992
    return 0;
 
3993
}
 
3994
 
 
3995
int TLuaInterpreter::sendGMCP( lua_State *L )
 
3996
{
 
3997
    string msg;
 
3998
    if( ! lua_isstring( L, 1 ) )
 
3999
    {
 
4000
        lua_pushstring( L, "sendGMCP: what do you want to send?" );
 
4001
        lua_error( L );
 
4002
        return 1;
 
4003
    }
 
4004
    else
 
4005
    {
 
4006
        msg = lua_tostring( L, 1 );
 
4007
    }
 
4008
 
 
4009
    string what;
 
4010
    if( lua_isstring( L, 2 ) )
 
4011
    {
 
4012
        what = lua_tostring( L, 2 );
 
4013
    }
 
4014
    string _h;
 
4015
    _h += TN_IAC;
 
4016
    _h += TN_SB;
 
4017
    _h += GMCP;
 
4018
    _h += msg;
 
4019
    if( what != "" )
 
4020
    {
 
4021
        _h += " ";
 
4022
        _h += what;
 
4023
    }
 
4024
    _h += TN_IAC;
 
4025
    _h += TN_SE;
 
4026
 
 
4027
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4028
    pHost->mTelnet.socketOutRaw( _h );
 
4029
    return 0;
 
4030
}
 
4031
 
 
4032
int TLuaInterpreter::sendTelnetChannel102( lua_State *L )
 
4033
{
 
4034
    string msg;
 
4035
    if( ! lua_isstring( L, 1 ) )
 
4036
    {
 
4037
        lua_pushstring( L, "sendTelnetChannel102: wrong argument type" );
 
4038
        lua_error( L );
 
4039
        return 1;
 
4040
    }
 
4041
    else
 
4042
    {
 
4043
        msg = lua_tostring( L, 1 );
 
4044
    }
 
4045
    string _h;
 
4046
    _h += TN_IAC;
 
4047
    _h += TN_SB;
 
4048
    _h += 102;
 
4049
    _h += msg;
 
4050
    _h += TN_IAC;
 
4051
    _h += TN_SE;
 
4052
 
 
4053
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4054
    pHost->mTelnet.socketOutRaw( _h );
 
4055
    return 0;
 
4056
}
 
4057
 
 
4058
int TLuaInterpreter::getButtonState( lua_State *L )
 
4059
{
 
4060
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4061
    int state;
 
4062
    state = pHost->mpConsole->getButtonState();
 
4063
    lua_pushnumber( L, state );
 
4064
    return 1;
 
4065
}
 
4066
 
 
4067
int TLuaInterpreter::getNetworkLatency( lua_State *L )
 
4068
{
 
4069
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4070
    double number;
 
4071
    number = pHost->mTelnet.networkLatency;
 
4072
    lua_pushnumber( L, number );
 
4073
    return 1;
 
4074
}
 
4075
 
 
4076
int TLuaInterpreter::getMainConsoleWidth( lua_State * L )
 
4077
{
 
4078
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4079
    int fw = QFontMetrics(pHost->mDisplayFont).width("W");
 
4080
    fw *= pHost->mWrapAt + 1;
 
4081
    lua_pushnumber( L, fw );
 
4082
    return 1;
 
4083
}
 
4084
 
 
4085
int TLuaInterpreter::getMainWindowSize( lua_State *L )
 
4086
{
 
4087
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4088
    QSize size = pHost->mpConsole->mpMainFrame->size();
 
4089
    lua_pushnumber( L, size.width() );
 
4090
    lua_pushnumber( L, size.height()-pHost->mpConsole->mpCommandLine->height() );
 
4091
    return 2;
 
4092
}
 
4093
 
 
4094
// tempTimer(int session, float seconds, string function to call, string name) // one shot timer.
 
4095
int TLuaInterpreter::tempTimer( lua_State *L )
 
4096
{
 
4097
    double luaTimeout;
 
4098
    if( ! lua_isnumber( L, 1 ) )
 
4099
    {
 
4100
        lua_pushstring( L, "tempTimer: wrong argument type" );
 
4101
        lua_error( L );
 
4102
        return 1;
 
4103
    }
 
4104
    else
 
4105
    {
 
4106
        luaTimeout = lua_tonumber( L, 1 );
 
4107
    }
 
4108
 
 
4109
    string luaFunction;
 
4110
    if( lua_isfunction( L, 2 ) )
 
4111
    {
 
4112
        Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4113
        TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4114
        QString _fun;
 
4115
        int timerID = pLuaInterpreter->startTempTimer( luaTimeout, _fun );
 
4116
        TTimer * pT = pHost->getTimerUnit()->getTimer( timerID );
 
4117
        pT->mRegisteredAnonymousLuaFunction = true;
 
4118
        lua_pushlightuserdata( L, pT );
 
4119
        lua_pushvalue( L, 2 );
 
4120
        lua_settable( L, LUA_REGISTRYINDEX );
 
4121
        lua_pushnumber( L, timerID );
 
4122
        return 1;
 
4123
    }
 
4124
    if( ! lua_isstring( L, 2 ) )
 
4125
    {
 
4126
        lua_pushstring( L, "tempTimer: wrong argument type" );
 
4127
        lua_error( L );
 
4128
        return 1;
 
4129
    }
 
4130
    else
 
4131
    {
 
4132
        luaFunction = lua_tostring( L, 2 );
 
4133
    }
 
4134
 
 
4135
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4136
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4137
    QString _fun = luaFunction.c_str();
 
4138
    int timerID = pLuaInterpreter->startTempTimer( luaTimeout, _fun );
 
4139
    lua_pushnumber( L, timerID );
 
4140
    return 1;
 
4141
}
 
4142
 
 
4143
int TLuaInterpreter::tempExactMatchTrigger( lua_State *L )
 
4144
{
 
4145
    string luaRegex;
 
4146
    if( ! lua_isstring( L, 1 ) )
 
4147
    {
 
4148
        lua_pushstring( L, "tempTrigger: wrong argument type" );
 
4149
        lua_error( L );
 
4150
        return 1;
 
4151
    }
 
4152
    else
 
4153
    {
 
4154
        luaRegex = lua_tostring( L, 1 );
 
4155
    }
 
4156
 
 
4157
    string luaFunction;
 
4158
    if( ! lua_isstring( L, 2 ) )
 
4159
    {
 
4160
        lua_pushstring( L, "tempTrigger: wrong argument type" );
 
4161
        lua_error( L );
 
4162
        return 1;
 
4163
    }
 
4164
    else
 
4165
    {
 
4166
        luaFunction = lua_tostring( L, 2 );
 
4167
    }
 
4168
 
 
4169
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4170
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4171
    QString _reg = luaRegex.c_str();
 
4172
    QString _fun = luaFunction.c_str();
 
4173
    int timerID = pLuaInterpreter->startTempExactMatchTrigger( _reg, _fun );
 
4174
    lua_pushnumber( L, timerID );
 
4175
    return 1;
 
4176
}
 
4177
 
 
4178
int TLuaInterpreter::tempBeginOfLineTrigger( lua_State *L )
 
4179
{
 
4180
    string luaRegex;
 
4181
    if( ! lua_isstring( L, 1 ) )
 
4182
    {
 
4183
        lua_pushstring( L, "tempTrigger: wrong argument type" );
 
4184
        lua_error( L );
 
4185
        return 1;
 
4186
    }
 
4187
    else
 
4188
    {
 
4189
        luaRegex = lua_tostring( L, 1 );
 
4190
    }
 
4191
 
 
4192
    string luaFunction;
 
4193
    if( ! lua_isstring( L, 2 ) )
 
4194
    {
 
4195
        lua_pushstring( L, "tempTrigger: wrong argument type" );
 
4196
        lua_error( L );
 
4197
        return 1;
 
4198
    }
 
4199
    else
 
4200
    {
 
4201
        luaFunction = lua_tostring( L, 2 );
 
4202
    }
 
4203
 
 
4204
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4205
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4206
    QString _reg = luaRegex.c_str();
 
4207
    QString _fun = luaFunction.c_str();
 
4208
    int timerID = pLuaInterpreter->startTempBeginOfLineTrigger( _reg, _fun );
 
4209
    lua_pushnumber( L, timerID );
 
4210
    return 1;
 
4211
}
 
4212
 
 
4213
 
 
4214
// tempTrigger( string regex, string function to call ) // one shot timer.
 
4215
int TLuaInterpreter::tempTrigger( lua_State *L )
 
4216
{
 
4217
    string luaRegex;
 
4218
    if( ! lua_isstring( L, 1 ) )
 
4219
    {
 
4220
        lua_pushstring( L, "tempTrigger: wrong argument type" );
 
4221
        lua_error( L );
 
4222
        return 1;
 
4223
    }
 
4224
    else
 
4225
    {
 
4226
        luaRegex = lua_tostring( L, 1 );
 
4227
    }
 
4228
 
 
4229
    string luaFunction;
 
4230
    if( ! lua_isstring( L, 2 ) )
 
4231
    {
 
4232
        lua_pushstring( L, "tempTrigger: wrong argument type" );
 
4233
        lua_error( L );
 
4234
        return 1;
 
4235
    }
 
4236
    else
 
4237
    {
 
4238
        luaFunction = lua_tostring( L, 2 );
 
4239
    }
 
4240
 
 
4241
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4242
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4243
    QString _reg = luaRegex.c_str();
 
4244
    QString _fun = luaFunction.c_str();
 
4245
    int timerID = pLuaInterpreter->startTempTrigger( _reg, _fun );
 
4246
    lua_pushnumber( L, timerID );
 
4247
    return 1;
 
4248
}
 
4249
 
 
4250
 
 
4251
// temporary color trigger. args: ansiFGColorCode, ansiBgColorCode, luaCode
 
4252
int TLuaInterpreter::tempColorTrigger( lua_State *L )
 
4253
{
 
4254
    int luaFrom;
 
4255
    if( ! lua_isnumber( L, 1 ) )
 
4256
    {
 
4257
        lua_pushstring( L, "tempColorTrigger: wrong argument type" );
 
4258
        lua_error( L );
 
4259
        return 1;
 
4260
    }
 
4261
    else
 
4262
    {
 
4263
        luaFrom = lua_tointeger( L, 1 );
 
4264
    }
 
4265
    int luaTo;
 
4266
    if( ! lua_isnumber( L, 2 ) )
 
4267
    {
 
4268
        lua_pushstring( L, "tempColorTrigger: wrong argument type" );
 
4269
        lua_error( L );
 
4270
        return 1;
 
4271
    }
 
4272
    else
 
4273
    {
 
4274
        luaTo = lua_tointeger( L, 2 );
 
4275
    }
 
4276
 
 
4277
    string luaFunction;
 
4278
    if( ! lua_isstring( L, 3 ) )
 
4279
    {
 
4280
        lua_pushstring( L, "tempColorTrigger: wrong argument type" );
 
4281
        lua_error( L );
 
4282
        return 1;
 
4283
    }
 
4284
    else
 
4285
    {
 
4286
        luaFunction = lua_tostring( L, 3 );
 
4287
    }
 
4288
 
 
4289
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4290
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4291
    QString _fun = luaFunction.c_str();
 
4292
    int timerID = pLuaInterpreter->startTempColorTrigger( luaFrom, luaTo, _fun );
 
4293
    lua_pushnumber( L, timerID );
 
4294
    return 1;
 
4295
}
 
4296
 
 
4297
 
 
4298
// triggerID = tempLineTrigger( from, howmany, func )
 
4299
int TLuaInterpreter::tempLineTrigger( lua_State *L )
 
4300
{
 
4301
    int luaFrom;
 
4302
    if( ! lua_isnumber( L, 1 ) )
 
4303
    {
 
4304
        lua_pushstring( L, "tempLineTrigger: wrong argument type" );
 
4305
        lua_error( L );
 
4306
        return 1;
 
4307
    }
 
4308
    else
 
4309
    {
 
4310
        luaFrom = lua_tointeger( L, 1 );
 
4311
    }
 
4312
    int luaTo;
 
4313
    if( ! lua_isnumber( L, 2 ) )
 
4314
    {
 
4315
        lua_pushstring( L, "tempLineTrigger: wrong argument type" );
 
4316
        lua_error( L );
 
4317
        return 1;
 
4318
    }
 
4319
    else
 
4320
    {
 
4321
        luaTo = lua_tointeger( L, 2 );
 
4322
    }
 
4323
 
 
4324
    string luaFunction;
 
4325
    if( ! lua_isstring( L, 3 ) )
 
4326
    {
 
4327
        lua_pushstring( L, "tempLineTrigger: wrong argument type" );
 
4328
        lua_error( L );
 
4329
        return 1;
 
4330
    }
 
4331
    else
 
4332
    {
 
4333
        luaFunction = lua_tostring( L, 3 );
 
4334
    }
 
4335
 
 
4336
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4337
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4338
    QString _fun = luaFunction.c_str();
 
4339
    int timerID = pLuaInterpreter->startTempLineTrigger( luaFrom, luaTo, _fun );
 
4340
    lua_pushnumber( L, timerID );
 
4341
    return 1;
 
4342
}
 
4343
 
 
4344
 
 
4345
// tempTrigger( string regex, string function to call ) // one shot timer.
 
4346
int TLuaInterpreter::tempRegexTrigger( lua_State *L )
 
4347
{
 
4348
    string luaRegex;
 
4349
    if( ! lua_isstring( L, 1 ) )
 
4350
    {
 
4351
        lua_pushstring( L, "tempRegexTrigger: wrong argument type" );
 
4352
        lua_error( L );
 
4353
        return 1;
 
4354
    }
 
4355
    else
 
4356
    {
 
4357
        luaRegex = lua_tostring( L, 1 );
 
4358
    }
 
4359
 
 
4360
    string luaFunction;
 
4361
    if( ! lua_isstring( L, 2 ) )
 
4362
    {
 
4363
        lua_pushstring( L, "tempRegexTrigger: wrong argument type" );
 
4364
        lua_error( L );
 
4365
        return 1;
 
4366
    }
 
4367
    else
 
4368
    {
 
4369
        luaFunction = lua_tostring( L, 2 );
 
4370
    }
 
4371
 
 
4372
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4373
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4374
    QString _luaFunction = luaFunction.c_str();
 
4375
    QString _luaRegex = luaRegex.c_str();
 
4376
    int timerID = pLuaInterpreter->startTempRegexTrigger( _luaRegex, _luaFunction );
 
4377
    lua_pushnumber( L, timerID );
 
4378
    return 1;
 
4379
}
 
4380
 
 
4381
int TLuaInterpreter::tempAlias( lua_State *L )
 
4382
{
 
4383
    string luaRegex;
 
4384
    if( ! lua_isstring( L, 1 ) )
 
4385
    {
 
4386
        lua_pushstring( L, "tempAlias: wrong argument type" );
 
4387
        lua_error( L );
 
4388
        return 1;
 
4389
    }
 
4390
    else
 
4391
    {
 
4392
        luaRegex = lua_tostring( L, 1 );
 
4393
    }
 
4394
 
 
4395
    string luaFunction;
 
4396
    if( ! lua_isstring( L, 2 ) )
 
4397
    {
 
4398
        lua_pushstring( L, "tempAlias: wrong argument type" );
 
4399
        lua_error( L );
 
4400
        return 1;
 
4401
    }
 
4402
    else
 
4403
    {
 
4404
        luaFunction = lua_tostring( L, 2 );
 
4405
    }
 
4406
 
 
4407
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4408
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4409
    QString _luaFunction = luaFunction.c_str();
 
4410
    QString _luaRegex = luaRegex.c_str();
 
4411
    int timerID = pLuaInterpreter->startTempAlias( _luaRegex, _luaFunction );
 
4412
    lua_pushnumber( L, timerID );
 
4413
    return 1;
 
4414
}
 
4415
 
 
4416
int TLuaInterpreter::exists( lua_State * L )
 
4417
{
 
4418
    string _name;
 
4419
    if( ! lua_isstring( L, 1 ) )
 
4420
    {
 
4421
        lua_pushstring( L, "exists: wrong argument type" );
 
4422
        lua_error( L );
 
4423
        return 1;
 
4424
    }
 
4425
    else
 
4426
    {
 
4427
        _name = lua_tostring( L, 1 );
 
4428
    }
 
4429
    string _type;
 
4430
    if( ! lua_isstring( L, 2 ) )
 
4431
    {
 
4432
        lua_pushstring( L, "exists: wrong argument type" );
 
4433
        lua_error( L );
 
4434
        return 1;
 
4435
    }
 
4436
    else
 
4437
    {
 
4438
        _type = lua_tostring( L, 2 );
 
4439
    }
 
4440
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4441
    int cnt = 0;
 
4442
    QString type = _type.c_str();
 
4443
    type = type.toLower();
 
4444
    QString name = _name.c_str();
 
4445
    if( type == "timer")
 
4446
    {
 
4447
        QMap<QString, TTimer *>::const_iterator it1 = pHost->getTimerUnit()->mLookupTable.find( name );
 
4448
        while( it1 != pHost->getTimerUnit()->mLookupTable.end() && it1.key() == name )
 
4449
        {
 
4450
            cnt++;
 
4451
            it1++;
 
4452
        }
 
4453
    }
 
4454
    else if( type == "trigger")
 
4455
    {
 
4456
        QMap<QString, TTrigger *>::const_iterator it1 = pHost->getTriggerUnit()->mLookupTable.find( name );
 
4457
        while( it1 != pHost->getTriggerUnit()->mLookupTable.end() && it1.key() == name )
 
4458
        {
 
4459
            cnt++;
 
4460
            it1++;
 
4461
        }
 
4462
    }
 
4463
    else if( type == "alias")
 
4464
    {
 
4465
        QMap<QString, TAlias *>::const_iterator it1 = pHost->getAliasUnit()->mLookupTable.find( name );
 
4466
        while( it1 != pHost->getAliasUnit()->mLookupTable.end() && it1.key() == name )
 
4467
        {
 
4468
            cnt++;
 
4469
            it1++;
 
4470
        }
 
4471
    }
 
4472
    lua_pushnumber( L, cnt );
 
4473
    return 1;
 
4474
}
 
4475
 
 
4476
int TLuaInterpreter::isActive( lua_State * L )
 
4477
{
 
4478
    string _name;
 
4479
    if( ! lua_isstring( L, 1 ) )
 
4480
    {
 
4481
        lua_pushstring( L, "isActive: wrong argument type" );
 
4482
        lua_error( L );
 
4483
        return 1;
 
4484
    }
 
4485
    else
 
4486
    {
 
4487
        _name = lua_tostring( L, 1 );
 
4488
    }
 
4489
    string _type;
 
4490
    if( ! lua_isstring( L, 2 ) )
 
4491
    {
 
4492
        lua_pushstring( L, "isActive: wrong argument type" );
 
4493
        lua_error( L );
 
4494
        return 1;
 
4495
    }
 
4496
    else
 
4497
    {
 
4498
        _type = lua_tostring( L, 2 );
 
4499
    }
 
4500
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4501
    int cnt = 0;
 
4502
    QString type = _type.c_str();
 
4503
    type = type.toLower();
 
4504
    QString name = _name.c_str();
 
4505
    if( type == "timer")
 
4506
    {
 
4507
        QMap<QString, TTimer *>::const_iterator it1 = pHost->getTimerUnit()->mLookupTable.find( name );
 
4508
        while( it1 != pHost->getTimerUnit()->mLookupTable.end() && it1.key() == name )
 
4509
        {
 
4510
            if( it1.value()->isActive() )
 
4511
            {
 
4512
                cnt++;
 
4513
            }
 
4514
            it1++;
 
4515
        }
 
4516
    }
 
4517
    else if( type == "trigger")
 
4518
    {
 
4519
        QMap<QString, TTrigger *>::const_iterator it1 = pHost->getTriggerUnit()->mLookupTable.find( name );
 
4520
        while( it1 != pHost->getTriggerUnit()->mLookupTable.end() && it1.key() == name )
 
4521
        {
 
4522
            if( it1.value()->isActive() )
 
4523
            {
 
4524
                cnt++;
 
4525
            }
 
4526
            it1++;
 
4527
        }
 
4528
    }
 
4529
    else if( type == "alias")
 
4530
    {
 
4531
        QMap<QString, TAlias *>::const_iterator it1 = pHost->getAliasUnit()->mLookupTable.find( name );
 
4532
        while( it1 != pHost->getAliasUnit()->mLookupTable.end() && it1.key() == name )
 
4533
        {
 
4534
            if( it1.value()->isActive() )
 
4535
            {
 
4536
                cnt++;
 
4537
            }
 
4538
            it1++;
 
4539
        }
 
4540
    }
 
4541
    lua_pushnumber( L, cnt );
 
4542
    return 1;
 
4543
}
 
4544
 
 
4545
 
 
4546
int TLuaInterpreter::permAlias( lua_State *L )
 
4547
{
 
4548
    string luaName;
 
4549
    if( ! lua_isstring( L, 1 ) )
 
4550
    {
 
4551
        lua_pushstring( L, "permAlias: need a name for this alias" );
 
4552
        lua_error( L );
 
4553
        return 1;
 
4554
    }
 
4555
    else
 
4556
    {
 
4557
        luaName = lua_tostring( L, 1 );
 
4558
    }
 
4559
 
 
4560
    string luaParent;
 
4561
    if( ! lua_isstring( L, 2 ) )
 
4562
    {
 
4563
        lua_pushstring( L, "permAlias: need a parent alias/group to add this alias to" );
 
4564
        lua_error( L );
 
4565
        return 1;
 
4566
    }
 
4567
    else
 
4568
    {
 
4569
        luaParent = lua_tostring( L, 2 );
 
4570
    }
 
4571
 
 
4572
    string luaRegex;
 
4573
    if( ! lua_isstring( L, 3 ) )
 
4574
    {
 
4575
        lua_pushstring( L, "permAlias: need the pattern for the alias" );
 
4576
        lua_error( L );
 
4577
        return 1;
 
4578
    }
 
4579
    else
 
4580
    {
 
4581
        luaRegex = lua_tostring( L, 3 );
 
4582
    }
 
4583
 
 
4584
 
 
4585
    string luaFunction;
 
4586
    if( ! lua_isstring( L, 4 ) )
 
4587
    {
 
4588
        lua_pushstring( L, "permAlias: need Lua code for this alias" );
 
4589
        lua_error( L );
 
4590
        return 1;
 
4591
    }
 
4592
    else
 
4593
    {
 
4594
        luaFunction = lua_tostring( L, 4 );
 
4595
    }
 
4596
 
 
4597
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4598
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4599
    QString _luaName = luaName.c_str();
 
4600
    QString _luaParent = luaParent.c_str();
 
4601
    QString _luaFunction = luaFunction.c_str();
 
4602
    QString _luaRegex = luaRegex.c_str();
 
4603
    int aliasID = pLuaInterpreter->startPermAlias( _luaName, _luaParent, _luaRegex, _luaFunction );
 
4604
    lua_pushnumber( L, aliasID );
 
4605
    return 1;
 
4606
}
 
4607
 
 
4608
int TLuaInterpreter::permTimer( lua_State * L )
 
4609
{
 
4610
    string luaName;
 
4611
    if( ! lua_isstring( L, 1 ) )
 
4612
    {
 
4613
        lua_pushstring( L, "permTimer: wrong argument type" );
 
4614
        lua_error( L );
 
4615
        return 1;
 
4616
    }
 
4617
    else
 
4618
    {
 
4619
        luaName = lua_tostring( L, 1 );
 
4620
    }
 
4621
    string luaParent;
 
4622
    if( ! lua_isstring( L, 2 ) )
 
4623
    {
 
4624
        lua_pushstring( L, "permTimer: wrong argument type" );
 
4625
        lua_error( L );
 
4626
        return 1;
 
4627
    }
 
4628
    else
 
4629
    {
 
4630
        luaParent = lua_tostring( L, 2 );
 
4631
    }
 
4632
 
 
4633
    double luaTimeout;
 
4634
    if( ! lua_isnumber( L, 3 ) )
 
4635
    {
 
4636
        lua_pushstring( L, "permTimer: wrong argument type" );
 
4637
        lua_error( L );
 
4638
        return 1;
 
4639
    }
 
4640
    else
 
4641
    {
 
4642
        luaTimeout = lua_tonumber( L, 3 );
 
4643
    }
 
4644
 
 
4645
    string luaFunction;
 
4646
    if( ! lua_isstring( L, 4 ) )
 
4647
    {
 
4648
        lua_pushstring( L, "permTimer: wrong argument type" );
 
4649
        lua_error( L );
 
4650
        return 1;
 
4651
    }
 
4652
    else
 
4653
    {
 
4654
        luaFunction = lua_tostring( L, 4 );
 
4655
    }
 
4656
 
 
4657
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4658
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4659
    QString _name = luaName.c_str();
 
4660
    QString _parent = luaParent.c_str();
 
4661
    QString _fun = luaFunction.c_str();
 
4662
    int timerID = pLuaInterpreter->startPermTimer( _name, _parent, luaTimeout, _fun );
 
4663
    lua_pushnumber( L, timerID );
 
4664
    return 1;
 
4665
}
 
4666
 
 
4667
int TLuaInterpreter::permSubstringTrigger( lua_State * L )
 
4668
{
 
4669
    string name;
 
4670
    if( ! lua_isstring( L, 1 ) )
 
4671
    {
 
4672
        lua_pushstring( L, "permSubstringTrigger: wrong argument type" );
 
4673
        lua_error( L );
 
4674
        return 1;
 
4675
    }
 
4676
    else
 
4677
    {
 
4678
        name = lua_tostring( L, 1 );
 
4679
    }
 
4680
 
 
4681
    string parent;
 
4682
    if( ! lua_isstring( L, 2 ) )
 
4683
    {
 
4684
        lua_pushstring( L, "permSubstringTrigger: wrong argument type" );
 
4685
        lua_error( L );
 
4686
        return 1;
 
4687
    }
 
4688
    else
 
4689
    {
 
4690
        parent = lua_tostring( L, 2 );
 
4691
    }
 
4692
    QStringList _regList;
 
4693
    if( ! lua_istable( L, 3 ) )
 
4694
    {
 
4695
        lua_pushstring( L, "permSubstringTrigger: wrong argument type" );
 
4696
        lua_error( L );
 
4697
        return 1;
 
4698
    }
 
4699
    else
 
4700
    {
 
4701
        lua_pushnil( L );
 
4702
        while( lua_next( L, 3 ) != 0 )
 
4703
        {
 
4704
            // key at index -2 and value at index -1
 
4705
            if( lua_type(L, -1) == LUA_TSTRING )
 
4706
            {
 
4707
                QString regex = lua_tostring( L, -1 );
 
4708
                _regList << regex;
 
4709
            }
 
4710
            // removes value, but keeps key for next iteration
 
4711
            lua_pop(L, 1);
 
4712
        }
 
4713
    }
 
4714
 
 
4715
    string luaFunction;
 
4716
    if( ! lua_isstring( L, 4 ) )
 
4717
    {
 
4718
        lua_pushstring( L, "permSubstringTrigger: wrong argument type" );
 
4719
        lua_error( L );
 
4720
        return 1;
 
4721
    }
 
4722
    else
 
4723
    {
 
4724
        luaFunction = lua_tostring( L, 4 );
 
4725
    }
 
4726
 
 
4727
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4728
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4729
    QString _name = name.c_str();
 
4730
    QString _parent = parent.c_str();
 
4731
    QString _luaFunction = luaFunction.c_str();
 
4732
    int ret = pLuaInterpreter->startPermSubstringTrigger( _name,
 
4733
                                                          _parent,
 
4734
                                                          _regList,
 
4735
                                                          _luaFunction );
 
4736
    lua_pushnumber( L, ret );
 
4737
    return 1;
 
4738
}
 
4739
 
 
4740
int TLuaInterpreter::permBeginOfLineStringTrigger( lua_State * L )
 
4741
{
 
4742
    string name;
 
4743
    if( ! lua_isstring( L, 1 ) )
 
4744
    {
 
4745
        lua_pushstring( L, "permBeginOfLineStringTrigger: wrong argument type" );
 
4746
        lua_error( L );
 
4747
        return 1;
 
4748
    }
 
4749
    else
 
4750
    {
 
4751
        name = lua_tostring( L, 1 );
 
4752
    }
 
4753
 
 
4754
    string parent;
 
4755
    if( ! lua_isstring( L, 2 ) )
 
4756
    {
 
4757
        lua_pushstring( L, "permBeginOfLineStringTrigger: wrong argument type" );
 
4758
        lua_error( L );
 
4759
        return 1;
 
4760
    }
 
4761
    else
 
4762
    {
 
4763
        parent = lua_tostring( L, 2 );
 
4764
    }
 
4765
    QStringList _regList;
 
4766
    if( ! lua_istable( L, 3 ) )
 
4767
    {
 
4768
        lua_pushstring( L, "permBeginOfLineStringTrigger: wrong argument type" );
 
4769
        lua_error( L );
 
4770
        return 1;
 
4771
    }
 
4772
    else
 
4773
    {
 
4774
        lua_pushnil( L );
 
4775
        while( lua_next( L, 3 ) != 0 )
 
4776
        {
 
4777
            // key at index -2 and value at index -1
 
4778
            if( lua_type(L, -1) == LUA_TSTRING )
 
4779
            {
 
4780
                QString regex = lua_tostring( L, -1 );
 
4781
                _regList << regex;
 
4782
            }
 
4783
            // removes value, but keeps key for next iteration
 
4784
            lua_pop(L, 1);
 
4785
        }
 
4786
    }
 
4787
 
 
4788
    string luaFunction;
 
4789
    if( ! lua_isstring( L, 4 ) )
 
4790
    {
 
4791
        lua_pushstring( L, "permBeginOfLineStringTrigger: wrong argument type" );
 
4792
        lua_error( L );
 
4793
        return 1;
 
4794
    }
 
4795
    else
 
4796
    {
 
4797
        luaFunction = lua_tostring( L, 4 );
 
4798
    }
 
4799
 
 
4800
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4801
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4802
    QString _name = name.c_str();
 
4803
    QString _parent = parent.c_str();
 
4804
    QString _luaFunction = luaFunction.c_str();
 
4805
    int ret = pLuaInterpreter->startPermBeginOfLineStringTrigger( _name,
 
4806
                                                                  _parent,
 
4807
                                                                  _regList,
 
4808
                                                                  _luaFunction );
 
4809
    lua_pushnumber( L, ret );
 
4810
    return 1;
 
4811
}
 
4812
 
 
4813
int TLuaInterpreter::permRegexTrigger( lua_State *L )
 
4814
{
 
4815
    string name;
 
4816
    if( ! lua_isstring( L, 1 ) )
 
4817
    {
 
4818
        lua_pushstring( L, "permRegexTrigger: wrong argument type" );
 
4819
        lua_error( L );
 
4820
        return 1;
 
4821
    }
 
4822
    else
 
4823
    {
 
4824
        name = lua_tostring( L, 1 );
 
4825
    }
 
4826
 
 
4827
    string parent;
 
4828
    if( ! lua_isstring( L, 2 ) )
 
4829
    {
 
4830
        lua_pushstring( L, "permRegexTrigger: wrong argument type" );
 
4831
        lua_error( L );
 
4832
        return 1;
 
4833
    }
 
4834
    else
 
4835
    {
 
4836
        parent = lua_tostring( L, 2 );
 
4837
    }
 
4838
    QStringList _regList;
 
4839
    if( ! lua_istable( L, 3 ) )
 
4840
    {
 
4841
        lua_pushstring( L, "permRegexTrigger: wrong argument type" );
 
4842
        lua_error( L );
 
4843
        return 1;
 
4844
    }
 
4845
    else
 
4846
    {
 
4847
        lua_pushnil( L );
 
4848
        while( lua_next( L, 3 ) != 0 )
 
4849
        {
 
4850
            // key at index -2 and value at index -1
 
4851
            if( lua_type(L, -1) == LUA_TSTRING )
 
4852
            {
 
4853
                QString regex = lua_tostring( L, -1 );
 
4854
                _regList << regex;
 
4855
            }
 
4856
            // removes value, but keeps key for next iteration
 
4857
            lua_pop(L, 1);
 
4858
        }
 
4859
    }
 
4860
 
 
4861
    string luaFunction;
 
4862
    if( ! lua_isstring( L, 4 ) )
 
4863
    {
 
4864
        lua_pushstring( L, "permRegexTrigger: wrong argument type" );
 
4865
        lua_error( L );
 
4866
        return 1;
 
4867
    }
 
4868
    else
 
4869
    {
 
4870
        luaFunction = lua_tostring( L, 4 );
 
4871
    }
 
4872
 
 
4873
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4874
    TLuaInterpreter * pLuaInterpreter = pHost->getLuaInterpreter();
 
4875
    QString _name = name.c_str();
 
4876
    QString _parent = parent.c_str();
 
4877
    QString _luaFunction = luaFunction.c_str();
 
4878
    int ret = pLuaInterpreter->startPermRegexTrigger( _name,
 
4879
                                                      _parent,
 
4880
                                                      _regList,
 
4881
                                                      _luaFunction );
 
4882
    lua_pushnumber( L, ret );
 
4883
    return 1;
 
4884
}
 
4885
 
 
4886
#include <QFileDialog>
 
4887
 
 
4888
int TLuaInterpreter::invokeFileDialog( lua_State * L )
 
4889
{
 
4890
    bool luaDir = false; //default is to chose a directory
 
4891
    if( ! lua_isboolean( L, 1 ) )
 
4892
    {
 
4893
        lua_pushstring( L, "invokeFileDialog: wrong argument type" );
 
4894
        lua_error( L );
 
4895
        return 1;
 
4896
    }
 
4897
    else
 
4898
    {
 
4899
        luaDir = lua_toboolean( L, 1 );
 
4900
    }
 
4901
    string luaTitle;
 
4902
    if( ! lua_isstring( L, 2 ) )
 
4903
    {
 
4904
        lua_pushstring( L, "invokeFileDialog: wrong argument type" );
 
4905
        lua_error( L );
 
4906
        return 1;
 
4907
    }
 
4908
    else
 
4909
    {
 
4910
        luaTitle = lua_tostring( L, 2 );
 
4911
    }
 
4912
    if( ! luaDir )
 
4913
    {
 
4914
        QString fileName = QFileDialog::getExistingDirectory(0, QString( luaTitle.c_str()),
 
4915
                                                        QDir::currentPath() );
 
4916
        lua_pushstring( L, fileName.toLatin1().data() );
 
4917
        return 1;
 
4918
    }
 
4919
    else
 
4920
    {
 
4921
        QString fileName = QFileDialog::getOpenFileName(0, QString( luaTitle.c_str()),
 
4922
                                                        QDir::currentPath() );
 
4923
        lua_pushstring( L, fileName.toLatin1().data() );
 
4924
        return 1;
 
4925
    }
 
4926
}
 
4927
 
 
4928
int TLuaInterpreter::getTimestamp( lua_State * L )
 
4929
{
 
4930
    int luaLine;
 
4931
    int args = lua_gettop( L );
 
4932
    int n = 1;
 
4933
    string name = "";
 
4934
    if( args < 1 )
 
4935
    {
 
4936
 
 
4937
        return 0;
 
4938
    }
 
4939
    if( args == 2 )
 
4940
    {
 
4941
        if( lua_isstring( L, n ) )
 
4942
        {
 
4943
            name = lua_tostring( L, n );
 
4944
            n++;
 
4945
        }
 
4946
    }
 
4947
 
 
4948
    if( ! lua_isnumber( L, n ) )
 
4949
    {
 
4950
        lua_pushstring( L, "getTimestamp: wrong argument type" );
 
4951
        lua_error( L );
 
4952
        return 1;
 
4953
    }
 
4954
    else
 
4955
    {
 
4956
        luaLine = lua_tointeger( L, n );
 
4957
    }
 
4958
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
4959
    if( name == "" )
 
4960
    {
 
4961
        if( luaLine > 0 && luaLine < pHost->mpConsole->buffer.timeBuffer.size() )
 
4962
        {
 
4963
            lua_pushstring( L, pHost->mpConsole->buffer.timeBuffer.at(luaLine).toLatin1().data() );
 
4964
        }
 
4965
        else
 
4966
        {
 
4967
            lua_pushstring( L, "getTimestamp: invalid line number");
 
4968
        }
 
4969
        return 1;
 
4970
    }
 
4971
    QString _name = name.c_str();
 
4972
    QMap<QString, TConsole *> & dockWindowConsoleMap = mudlet::self()->mHostConsoleMap[pHost];
 
4973
    if( dockWindowConsoleMap.contains( _name ) )
 
4974
    {
 
4975
        TConsole * pC = dockWindowConsoleMap[_name];
 
4976
        if( luaLine > 0 && luaLine < pC->buffer.timeBuffer.size() )
 
4977
        {
 
4978
            lua_pushstring( L, pC->buffer.timeBuffer.at(luaLine).toLatin1().data() );
 
4979
        }
 
4980
        else
 
4981
        {
 
4982
            lua_pushstring( L, "getTimestamp: invalid line number");
 
4983
        }
 
4984
        return 1;
 
4985
    }
 
4986
    return 0;
 
4987
}
 
4988
 
 
4989
int TLuaInterpreter::setBorderColor( lua_State *L )
 
4990
{
 
4991
    int luaRed;
 
4992
    int luaGreen;
 
4993
    int luaBlue;
 
4994
    if( ! lua_isnumber( L, 1 ) )
 
4995
    {
 
4996
        lua_pushstring( L, "setBorderColor: wrong argument type" );
 
4997
        lua_error( L );
 
4998
        return 1;
 
4999
    }
 
5000
    else
 
5001
    {
 
5002
        luaRed = lua_tointeger( L, 1 );
 
5003
    }
 
5004
 
 
5005
    if( ! lua_isnumber( L, 2 ) )
 
5006
    {
 
5007
        lua_pushstring( L, "setBorderColor: wrong argument type" );
 
5008
        lua_error( L );
 
5009
        return 1;
 
5010
    }
 
5011
    else
 
5012
    {
 
5013
        luaGreen=lua_tointeger( L, 2 );
 
5014
    }
 
5015
 
 
5016
    if( ! lua_isnumber( L, 3 ) )
 
5017
    {
 
5018
        lua_pushstring( L, "setBorderColor: wrong argument type" );
 
5019
        lua_error( L );
 
5020
        return 1;
 
5021
    }
 
5022
    else
 
5023
    {
 
5024
        luaBlue = lua_tointeger( L, 3 );
 
5025
    }
 
5026
 
 
5027
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5028
    QPalette framePalette;
 
5029
    framePalette.setColor( QPalette::Text, QColor(0,0,0) );
 
5030
    framePalette.setColor( QPalette::Highlight, QColor(55,55,255) );
 
5031
    framePalette.setColor( QPalette::Window, QColor( luaRed, luaGreen, luaBlue, 255 ) );
 
5032
    pHost->mpConsole->mpMainFrame->setPalette( framePalette );
 
5033
    return 0;
 
5034
}
 
5035
 
 
5036
 
 
5037
int TLuaInterpreter::setRoomCoordinates( lua_State *L )
 
5038
{
 
5039
    int id;
 
5040
    int x;
 
5041
    int y;
 
5042
    int z;
 
5043
    if( ! lua_isnumber( L, 1 ) )
 
5044
    {
 
5045
        lua_pushstring( L, "setRoomCoordinates: wrong argument type" );
 
5046
        lua_error( L );
 
5047
        return 1;
 
5048
    }
 
5049
    else
 
5050
    {
 
5051
        id = lua_tointeger( L, 1 );
 
5052
    }
 
5053
 
 
5054
    if( ! lua_isnumber( L, 2 ) )
 
5055
    {
 
5056
        lua_pushstring( L, "setRoomCoordinates: wrong argument type" );
 
5057
        lua_error( L );
 
5058
        return 1;
 
5059
    }
 
5060
    else
 
5061
    {
 
5062
        x = lua_tointeger( L, 2 );
 
5063
    }
 
5064
 
 
5065
    if( ! lua_isnumber( L, 3 ) )
 
5066
    {
 
5067
        lua_pushstring( L, "setRoomCoordinates: wrong argument type" );
 
5068
        lua_error( L );
 
5069
        return 1;
 
5070
    }
 
5071
    else
 
5072
    {
 
5073
        y = lua_tointeger( L, 3 );
 
5074
    }
 
5075
 
 
5076
    if( ! lua_isnumber( L, 4 ) )
 
5077
    {
 
5078
        lua_pushstring( L, "setRoomCoordinates: wrong argument type" );
 
5079
        lua_error( L );
 
5080
        return 1;
 
5081
    }
 
5082
    else
 
5083
    {
 
5084
        z = lua_tointeger( L, 4 );
 
5085
    }
 
5086
 
 
5087
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5088
    lua_pushboolean(L, pHost->mpMap->setRoomCoordinates( id, x, y, z ) );
 
5089
    return 1;
 
5090
}
 
5091
 
 
5092
int TLuaInterpreter::setCustomEnvColor( lua_State *L )
 
5093
{
 
5094
    int id;
 
5095
    int x;
 
5096
    int y;
 
5097
    int z;
 
5098
    if( ! lua_isnumber( L, 1 ) )
 
5099
    {
 
5100
        lua_pushstring( L, "setRoomCoordinates: wrong argument type" );
 
5101
        lua_error( L );
 
5102
        return 1;
 
5103
    }
 
5104
    else
 
5105
    {
 
5106
        id = lua_tointeger( L, 1 );
 
5107
    }
 
5108
 
 
5109
    if( ! lua_isnumber( L, 2 ) )
 
5110
    {
 
5111
        lua_pushstring( L, "setRoomCoordinates: wrong argument type" );
 
5112
        lua_error( L );
 
5113
        return 1;
 
5114
    }
 
5115
    else
 
5116
    {
 
5117
        x = lua_tointeger( L, 2 );
 
5118
    }
 
5119
 
 
5120
    if( ! lua_isnumber( L, 3 ) )
 
5121
    {
 
5122
        lua_pushstring( L, "setRoomCoordinates: wrong argument type" );
 
5123
        lua_error( L );
 
5124
        return 1;
 
5125
    }
 
5126
    else
 
5127
    {
 
5128
        y = lua_tointeger( L, 3 );
 
5129
    }
 
5130
 
 
5131
    if( ! lua_isnumber( L, 4 ) )
 
5132
    {
 
5133
        lua_pushstring( L, "setRoomCoordinates: wrong argument type" );
 
5134
        lua_error( L );
 
5135
        return 1;
 
5136
    }
 
5137
    else
 
5138
    {
 
5139
        z = lua_tointeger( L, 4 );
 
5140
    }
 
5141
 
 
5142
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5143
    pHost->mpMap->customEnvColors[id] = QColor( x, y, z );
 
5144
    return 0;
 
5145
}
 
5146
 
 
5147
 
 
5148
int TLuaInterpreter::setAreaName( lua_State *L )
 
5149
{
 
5150
    int id;
 
5151
    string name;
 
5152
    if( ! lua_isnumber( L, 1 ) )
 
5153
    {
 
5154
        lua_pushstring( L, "setAreaName: wrong argument type" );
 
5155
        lua_error( L );
 
5156
        return 1;
 
5157
    }
 
5158
    else
 
5159
    {
 
5160
        id = lua_tointeger( L, 1 );
 
5161
    }
 
5162
 
 
5163
    if( ! lua_isstring( L, 2 ) )
 
5164
    {
 
5165
        lua_pushstring( L, "setAreaName: wrong argument type" );
 
5166
        lua_error( L );
 
5167
        return 1;
 
5168
    }
 
5169
    else
 
5170
    {
 
5171
        name = lua_tostring( L, 2 );
 
5172
    }
 
5173
 
 
5174
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5175
    QString _name = name.c_str();
 
5176
    pHost->mpMap->areaNamesMap[id] = _name;
 
5177
    return 0;
 
5178
}
 
5179
 
 
5180
int TLuaInterpreter::getRoomAreaName( lua_State *L )
 
5181
{
 
5182
    int id;
 
5183
    string name;
 
5184
    bool gotString = false;
 
5185
    if( ! lua_isnumber( L, 1 ) )
 
5186
    {
 
5187
        if( ! lua_isstring( L, 1 ) )
 
5188
        {
 
5189
            lua_pushstring( L, "getRoomAreaName: wrong argument type" );
 
5190
            lua_error( L );
 
5191
            return 1;
 
5192
        }
 
5193
        else
 
5194
        {
 
5195
            name = lua_tostring( L, 1 );
 
5196
            gotString = true;
 
5197
        }
 
5198
    }
 
5199
    else
 
5200
    {
 
5201
        id = lua_tonumber( L, 1 );
 
5202
    }
 
5203
 
 
5204
    QString _name = name.c_str();
 
5205
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5206
    if( gotString )
 
5207
    {
 
5208
        QList<int> idList = pHost->mpMap->areaNamesMap.keys( _name );
 
5209
        if( idList.size() > 0 )
 
5210
        {
 
5211
            lua_pushnumber( L, idList[0] );
 
5212
            return 1;
 
5213
        }
 
5214
        else
 
5215
        {
 
5216
            lua_pushnumber( L, -1 );
 
5217
            return 1;
 
5218
        }
 
5219
    }
 
5220
    else
 
5221
    {
 
5222
        if( pHost->mpMap->areaNamesMap.contains( id ) )
 
5223
        {
 
5224
            lua_pushstring( L, pHost->mpMap->areaNamesMap[id].toLatin1().data() );
 
5225
        }
 
5226
        else
 
5227
            lua_pushnumber( L, -1 );
 
5228
        return 1;
 
5229
    }
 
5230
}
 
5231
 
 
5232
int TLuaInterpreter::addAreaName( lua_State *L )
 
5233
{
 
5234
    int id;
 
5235
    string name;
 
5236
 
 
5237
    if( ! lua_isstring( L, 1 ) )
 
5238
    {
 
5239
        lua_pushstring( L, "addAreaName: wrong argument type" );
 
5240
        lua_error( L );
 
5241
        return 1;
 
5242
    }
 
5243
    else
 
5244
    {
 
5245
        name = lua_tostring( L, 1 );
 
5246
    }
 
5247
 
 
5248
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5249
    QString _name = name.c_str();
 
5250
    id = pHost->mpMap->areaNamesMap.size()+1;
 
5251
 
 
5252
 
 
5253
    if( ! pHost->mpMap->areaNamesMap.values().contains( _name ) )
 
5254
    {
 
5255
        while( pHost->mpMap->areaNamesMap.contains( id ) )
 
5256
        {
 
5257
            id++;
 
5258
        }
 
5259
        pHost->mpMap->areaNamesMap[id] = _name;
 
5260
        lua_pushnumber( L, id );
 
5261
    }
 
5262
    else
 
5263
        lua_pushnumber( L, -1 );
 
5264
    return 1;
 
5265
}
 
5266
 
 
5267
int TLuaInterpreter::deleteArea( lua_State *L )
 
5268
{
 
5269
    int id = -1;
 
5270
    string name;
 
5271
 
 
5272
    if( lua_isnumber( L, 1 ) )
 
5273
    {
 
5274
        id = lua_tonumber( L, 1 );
 
5275
        if( id == -1 ) return 0;
 
5276
    }
 
5277
    else if( lua_isstring( L, 1 ) )
 
5278
    {
 
5279
        name = lua_tostring( L, 1 );
 
5280
    }
 
5281
    else
 
5282
    {
 
5283
        lua_pushstring( L, "deleteArea: wrong argument type" );
 
5284
        lua_error( L );
 
5285
        return 1;
 
5286
    }
 
5287
 
 
5288
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5289
    if( id == -1 )
 
5290
    {
 
5291
        QString _name = name.c_str();
 
5292
        if( pHost->mpMap->areaNamesMap.values().contains( _name ) )
 
5293
        {
 
5294
            pHost->mpMap->deleteArea( id );
 
5295
            pHost->mpMap->mMapGraphNeedsUpdate = false;
 
5296
        }
 
5297
    }
 
5298
    else
 
5299
    {
 
5300
        if( pHost->mpMap->areas.contains( id ) )
 
5301
        {
 
5302
            pHost->mpMap->deleteArea( id );
 
5303
            pHost->mpMap->mMapGraphNeedsUpdate = false;
 
5304
        }
 
5305
    }
 
5306
    return 0;
 
5307
}
 
5308
 
 
5309
int TLuaInterpreter::deleteRoom( lua_State *L )
 
5310
{
 
5311
    int id;
 
5312
 
 
5313
    if( lua_isnumber( L, 1 ) )
 
5314
    {
 
5315
        id = lua_tonumber( L, 1 );
 
5316
        if( id <= 0 ) return 0;
 
5317
    }
 
5318
    else
 
5319
    {
 
5320
        lua_pushstring( L, "addAreaName: wrong argument type" );
 
5321
        lua_error( L );
 
5322
        return 1;
 
5323
    }
 
5324
 
 
5325
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5326
    if( pHost->mpMap->rooms.contains( id ) )
 
5327
    {
 
5328
       pHost->mpMap->deleteRoom( id );
 
5329
       pHost->mpMap->mMapGraphNeedsUpdate = false;
 
5330
    }
 
5331
    return 0;
 
5332
}
 
5333
 
 
5334
 
 
5335
int TLuaInterpreter::setExit( lua_State *L )
 
5336
{
 
5337
    int from, to;
 
5338
    int dir;
 
5339
    if( ! lua_isnumber( L, 1 ) )
 
5340
    {
 
5341
        lua_pushstring( L, "setExit: wrong argument type" );
 
5342
        lua_error( L );
 
5343
        return 1;
 
5344
    }
 
5345
    else
 
5346
    {
 
5347
        from = lua_tointeger( L, 1 );
 
5348
    }
 
5349
 
 
5350
    if( ! lua_isnumber( L, 2 ) )
 
5351
    {
 
5352
        lua_pushstring( L, "setExit: wrong argument type" );
 
5353
        lua_error( L );
 
5354
        return 1;
 
5355
    }
 
5356
    else
 
5357
    {
 
5358
        to = lua_tointeger( L, 2 );
 
5359
    }
 
5360
 
 
5361
    if( ! lua_isnumber( L, 3 ) )
 
5362
    {
 
5363
        lua_pushstring( L, "setExit: wrong argument type" );
 
5364
        lua_error( L );
 
5365
        return 1;
 
5366
    }
 
5367
    else
 
5368
    {
 
5369
        dir = lua_tointeger( L, 3 );
 
5370
    }
 
5371
 
 
5372
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5373
    lua_pushboolean(L, pHost->mpMap->setExit( from, to, dir ) );
 
5374
    pHost->mpMap->mMapGraphNeedsUpdate = true;
 
5375
    return 1;
 
5376
}
 
5377
 
 
5378
int TLuaInterpreter::getRoomCoordinates( lua_State * L )
 
5379
{
 
5380
    int id;
 
5381
    if( ! lua_isnumber( L, 1 ) )
 
5382
    {
 
5383
        lua_pushstring( L, "getRoomCoordinates: wrong argument type" );
 
5384
        lua_error( L );
 
5385
        return 1;
 
5386
    }
 
5387
    else
 
5388
    {
 
5389
        id = lua_tointeger( L, 1 );
 
5390
    }
 
5391
 
 
5392
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5393
    if( ! pHost->mpMap->rooms.contains( id ) )
 
5394
    {
 
5395
        lua_pushstring( L, "getRoomCoordinates: room ID does not exist");
 
5396
        lua_error( L );
 
5397
        return 1;
 
5398
    }
 
5399
    lua_pushnumber( L, pHost->mpMap->rooms[id]->x );
 
5400
    lua_pushnumber( L, pHost->mpMap->rooms[id]->y );
 
5401
    lua_pushnumber( L, pHost->mpMap->rooms[id]->z );
 
5402
    return 3;
 
5403
}
 
5404
 
 
5405
int TLuaInterpreter::getRoomArea( lua_State * L )
 
5406
{
 
5407
    int id;
 
5408
    if( ! lua_isnumber( L, 1 ) )
 
5409
    {
 
5410
        lua_pushstring( L, "getRoomArea: wrong argument type" );
 
5411
        lua_error( L );
 
5412
        return 1;
 
5413
    }
 
5414
    else
 
5415
    {
 
5416
        id = lua_tointeger( L, 1 );
 
5417
    }
 
5418
 
 
5419
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5420
    if( ! pHost->mpMap->rooms.contains( id ) )
 
5421
    {
 
5422
        lua_pushnil(L);
 
5423
        return 1;
 
5424
    }
 
5425
    lua_pushnumber( L, pHost->mpMap->rooms[id]->area );
 
5426
    return 1;
 
5427
}
 
5428
 
 
5429
 
 
5430
int TLuaInterpreter::roomExists( lua_State * L )
 
5431
{
 
5432
    int id;
 
5433
    if( ! lua_isnumber( L, 1 ) || ! lua_isstring( L, 1 ) )
 
5434
    {
 
5435
        lua_pushstring( L, "roomExists: wrong argument type" );
 
5436
        lua_error( L );
 
5437
        return 1;
 
5438
    }
 
5439
    else
 
5440
    {
 
5441
        id = lua_tointeger( L, 1 );
 
5442
    }
 
5443
 
 
5444
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5445
    if( pHost->mpMap->rooms.contains( id ) )
 
5446
    {
 
5447
        lua_pushboolean( L, 1 );
 
5448
        return 1;
 
5449
    }
 
5450
    lua_pushboolean( L, 0 );
 
5451
    return 1;
 
5452
}
 
5453
 
 
5454
int TLuaInterpreter::addRoom( lua_State * L )
 
5455
{
 
5456
    int id;
 
5457
    if( ! lua_isnumber( L, 1 ) )
 
5458
    {
 
5459
        lua_pushstring( L, "getRoomArea: wrong argument type" );
 
5460
        lua_error( L );
 
5461
        return 1;
 
5462
    }
 
5463
    else
 
5464
    {
 
5465
        id = lua_tointeger( L, 1 );
 
5466
    }
 
5467
 
 
5468
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5469
    lua_pushboolean( L, pHost->mpMap->addRoom( id ) );
 
5470
    pHost->mpMap->mMapGraphNeedsUpdate = true;
 
5471
 
 
5472
    return 1;
 
5473
}
 
5474
 
 
5475
int TLuaInterpreter::createRoomID( lua_State * L )
 
5476
{
 
5477
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5478
    lua_pushnumber( L, pHost->mpMap->createNewRoomID() );
 
5479
    return 1;
 
5480
}
 
5481
 
 
5482
int TLuaInterpreter::unHighlightRoom( lua_State * L )
 
5483
{
 
5484
    int id;
 
5485
    if( ! lua_isnumber( L, 1 ) )
 
5486
    {
 
5487
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5488
        lua_error( L );
 
5489
        return 1;
 
5490
    }
 
5491
    else
 
5492
    {
 
5493
        id = lua_tointeger( L, 1 );
 
5494
    }
 
5495
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5496
    if( ! pHost->mpMap->rooms.contains( id ) ) return 0;
 
5497
    pHost->mpMap->rooms[id]->highlight = false;
 
5498
    if( pHost->mpMap )
 
5499
        if( pHost->mpMap->mpMapper )
 
5500
            pHost->mpMap->mpMapper->mp2dMap->update();
 
5501
    return 0;
 
5502
}
 
5503
 
 
5504
// highlightRoom( roomID, colorRed, colorGreen, colorBlue, col2Red, col2Green, col2Blue, (float)highlightRadius, alphaColor1, alphaColor2 )
 
5505
 
 
5506
int TLuaInterpreter::highlightRoom( lua_State * L )
 
5507
{
 
5508
    int id, fgr, fgg, fgb, bgr, bgg, bgb;
 
5509
    float radius;
 
5510
    if( ! lua_isnumber( L, 1 ) )
 
5511
    {
 
5512
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5513
        lua_error( L );
 
5514
        return 1;
 
5515
    }
 
5516
    else
 
5517
    {
 
5518
        id = lua_tointeger( L, 1 );
 
5519
    }
 
5520
 
 
5521
    if( ! lua_isnumber( L, 2 ) )
 
5522
    {
 
5523
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5524
        lua_error( L );
 
5525
        return 1;
 
5526
    }
 
5527
    else
 
5528
    {
 
5529
        fgr = lua_tointeger( L, 2 );
 
5530
    }
 
5531
 
 
5532
    if( ! lua_isnumber( L, 3 ) )
 
5533
    {
 
5534
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5535
        lua_error( L );
 
5536
        return 1;
 
5537
    }
 
5538
    else
 
5539
    {
 
5540
        fgg = lua_tointeger( L, 3 );
 
5541
    }
 
5542
 
 
5543
    if( ! lua_isnumber( L, 4 ) )
 
5544
    {
 
5545
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5546
        lua_error( L );
 
5547
        return 1;
 
5548
    }
 
5549
    else
 
5550
    {
 
5551
        fgb = lua_tointeger( L, 4 );
 
5552
    }
 
5553
    if( ! lua_isnumber( L, 5 ) )
 
5554
    {
 
5555
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5556
        lua_error( L );
 
5557
        return 1;
 
5558
    }
 
5559
    else
 
5560
    {
 
5561
        bgr = lua_tointeger( L, 5 );
 
5562
    }
 
5563
 
 
5564
    if( ! lua_isnumber( L, 6 ) )
 
5565
    {
 
5566
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5567
        lua_error( L );
 
5568
        return 1;
 
5569
    }
 
5570
    else
 
5571
    {
 
5572
        bgg = lua_tointeger( L, 6 );
 
5573
    }
 
5574
 
 
5575
    if( ! lua_isnumber( L, 7 ) )
 
5576
    {
 
5577
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5578
        lua_error( L );
 
5579
        return 1;
 
5580
    }
 
5581
    else
 
5582
    {
 
5583
        bgb = lua_tointeger( L, 7 );
 
5584
    }
 
5585
    if( ! lua_isnumber( L, 8 ) )
 
5586
    {
 
5587
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5588
        lua_error( L );
 
5589
        return 1;
 
5590
    }
 
5591
    else
 
5592
    {
 
5593
        radius = lua_tonumber( L, 8 );
 
5594
    }
 
5595
    int alpha1, alpha2;
 
5596
    if( ! lua_isnumber( L, 9 ) )
 
5597
    {
 
5598
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5599
        lua_error( L );
 
5600
        return 1;
 
5601
    }
 
5602
    else
 
5603
    {
 
5604
        alpha1 = lua_tointeger( L, 9 );
 
5605
    }
 
5606
    if( ! lua_isnumber( L, 10 ) )
 
5607
    {
 
5608
        lua_pushstring( L, "highlightRoom: wrong argument type" );
 
5609
        lua_error( L );
 
5610
        return 1;
 
5611
    }
 
5612
    else
 
5613
    {
 
5614
        alpha2 = lua_tointeger( L, 10 );
 
5615
    }
 
5616
 
 
5617
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5618
    if( !pHost->mpMap ) return 0;
 
5619
    if( ! pHost->mpMap->rooms.contains( id ) ) return 0;
 
5620
    QColor fg = QColor(fgr,fgg,fgb,alpha1);
 
5621
    QColor bg = QColor(bgr,bgg,bgb,alpha2);
 
5622
    pHost->mpMap->rooms[id]->highlight = true;
 
5623
    pHost->mpMap->rooms[id]->highlightColor = fg;
 
5624
    pHost->mpMap->rooms[id]->highlightColor2 = bg;
 
5625
    pHost->mpMap->rooms[id]->highlightRadius = radius;
 
5626
 
 
5627
    if( pHost->mpMap->mpMapper )
 
5628
        if( pHost->mpMap->mpMapper->mp2dMap )
 
5629
            pHost->mpMap->mpMapper->mp2dMap->update();
 
5630
    return 0;
 
5631
}
 
5632
 
 
5633
 
 
5634
// labelID = createMapLabel( area, text, posx, posy, fgRed, fgGreen, fgBlue, bgRed, bgGreen, bgBlue )
 
5635
int TLuaInterpreter::createMapLabel( lua_State * L )
 
5636
{
 
5637
    int area, fgr, fgg, fgb, bgr, bgg, bgb;
 
5638
    float posx, posy;
 
5639
    string text;
 
5640
    if( ! lua_isnumber( L, 1 ) )
 
5641
    {
 
5642
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5643
        lua_error( L );
 
5644
        return 1;
 
5645
    }
 
5646
    else
 
5647
    {
 
5648
        area = lua_tointeger( L, 1 );
 
5649
    }
 
5650
 
 
5651
    if( ! lua_isstring( L, 2 ) )
 
5652
    {
 
5653
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5654
        lua_error( L );
 
5655
        return 1;
 
5656
    }
 
5657
    else
 
5658
    {
 
5659
        text = lua_tostring( L, 2 );
 
5660
    }
 
5661
 
 
5662
    if( ! lua_isnumber( L, 3 ) )
 
5663
    {
 
5664
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5665
        lua_error( L );
 
5666
        return 1;
 
5667
    }
 
5668
    else
 
5669
    {
 
5670
        posx = lua_tonumber( L, 3 );
 
5671
    }
 
5672
 
 
5673
    if( ! lua_isnumber( L, 4 ) )
 
5674
    {
 
5675
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5676
        lua_error( L );
 
5677
        return 1;
 
5678
    }
 
5679
    else
 
5680
    {
 
5681
        posy = lua_tonumber( L, 4 );
 
5682
    }
 
5683
 
 
5684
    if( ! lua_isnumber( L, 5 ) )
 
5685
    {
 
5686
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5687
        lua_error( L );
 
5688
        return 1;
 
5689
    }
 
5690
    else
 
5691
    {
 
5692
        fgr = lua_tointeger( L, 5 );
 
5693
    }
 
5694
 
 
5695
    if( ! lua_isnumber( L, 6 ) )
 
5696
    {
 
5697
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5698
        lua_error( L );
 
5699
        return 1;
 
5700
    }
 
5701
    else
 
5702
    {
 
5703
        fgg = lua_tointeger( L, 6 );
 
5704
    }
 
5705
 
 
5706
    if( ! lua_isnumber( L, 7 ) )
 
5707
    {
 
5708
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5709
        lua_error( L );
 
5710
        return 1;
 
5711
    }
 
5712
    else
 
5713
    {
 
5714
        fgb = lua_tointeger( L, 7 );
 
5715
    }
 
5716
 
 
5717
    if( ! lua_isnumber( L, 8 ) )
 
5718
    {
 
5719
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5720
        lua_error( L );
 
5721
        return 1;
 
5722
    }
 
5723
    else
 
5724
    {
 
5725
        bgr = lua_tointeger( L, 8 );
 
5726
    }
 
5727
 
 
5728
    if( ! lua_isnumber( L, 9 ) )
 
5729
    {
 
5730
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5731
        lua_error( L );
 
5732
        return 1;
 
5733
    }
 
5734
    else
 
5735
    {
 
5736
        bgg = lua_tointeger( L, 9 );
 
5737
    }
 
5738
 
 
5739
    if( ! lua_isnumber( L, 10 ) )
 
5740
    {
 
5741
        lua_pushstring( L, "createMapLabel: wrong argument type" );
 
5742
        lua_error( L );
 
5743
        return 1;
 
5744
    }
 
5745
    else
 
5746
    {
 
5747
        bgb = lua_tointeger( L, 10 );
 
5748
    }
 
5749
 
 
5750
    QString _text = text.c_str();
 
5751
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5752
    QColor fg = QColor(fgr,fgg,fgb);
 
5753
    QColor bg = QColor(bgr, bgg, bgb);
 
5754
    lua_pushinteger( L, pHost->mpMap->createMapLabel( area, _text, posx, posy, fg, bg ) );
 
5755
    return 1;
 
5756
}
 
5757
 
 
5758
int TLuaInterpreter::deleteMapLabel( lua_State * L )
 
5759
{
 
5760
    int area, labelID;
 
5761
    if( ! lua_isnumber( L, 1 ) )
 
5762
    {
 
5763
        lua_pushstring( L, "deleteMapLabel: wrong argument type" );
 
5764
        lua_error( L );
 
5765
        return 1;
 
5766
    }
 
5767
    else
 
5768
    {
 
5769
        area = lua_tointeger( L, 1 );
 
5770
    }
 
5771
    if( ! lua_isnumber( L, 2 ) )
 
5772
    {
 
5773
        lua_pushstring( L, "deleteMapLabel: wrong argument type" );
 
5774
        lua_error( L );
 
5775
        return 1;
 
5776
    }
 
5777
    else
 
5778
    {
 
5779
        labelID = lua_tointeger( L, 2 );
 
5780
    }
 
5781
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5782
    pHost->mpMap->deleteMapLabel( area, labelID );
 
5783
    return 0;
 
5784
}
 
5785
 
 
5786
int TLuaInterpreter::getMapLabels( lua_State * L )
 
5787
{
 
5788
    int area, labelID;
 
5789
    if( ! lua_isnumber( L, 1 ) )
 
5790
    {
 
5791
        lua_pushstring( L, "deleteMapLabel: wrong argument type" );
 
5792
        lua_error( L );
 
5793
        return 1;
 
5794
    }
 
5795
    else
 
5796
    {
 
5797
        area = lua_tointeger( L, 1 );
 
5798
    }
 
5799
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5800
    if( pHost->mpMap->mapLabels.contains( area ) )
 
5801
    {
 
5802
        lua_newtable(L);
 
5803
        QMapIterator<int,TMapLabel> it(pHost->mpMap->mapLabels[area]);
 
5804
        while( it.hasNext() )
 
5805
        {
 
5806
            it.next();
 
5807
            lua_pushnumber( L, it.key() );
 
5808
            lua_pushstring( L, it.value().text.toLatin1().data() );
 
5809
            lua_settable(L, -3);
 
5810
        }
 
5811
    }
 
5812
    return 1;
 
5813
}
 
5814
 
 
5815
int TLuaInterpreter::addSpecialExit( lua_State * L )
 
5816
{
 
5817
    int id_from, id_to;
 
5818
    string dir;
 
5819
    if( ! lua_isnumber( L, 1 ) )
 
5820
    {
 
5821
        lua_pushstring( L, "addSpecialExit: wrong argument type" );
 
5822
        lua_error( L );
 
5823
        return 1;
 
5824
    }
 
5825
    else
 
5826
    {
 
5827
        id_from = lua_tointeger( L, 1 );
 
5828
    }
 
5829
    if( ! lua_isnumber( L, 2 ) )
 
5830
    {
 
5831
        lua_pushstring( L, "addSpecialExit: wrong argument type" );
 
5832
        lua_error( L );
 
5833
        return 1;
 
5834
    }
 
5835
    else
 
5836
    {
 
5837
        id_to = lua_tointeger( L, 2 );
 
5838
    }
 
5839
    if( ! lua_isstring( L, 3 ) )
 
5840
    {
 
5841
        lua_pushstring( L, "addSpecialExit: wrong argument type" );
 
5842
        lua_error( L );
 
5843
        return 1;
 
5844
    }
 
5845
    else
 
5846
    {
 
5847
        dir = lua_tostring( L, 3 );
 
5848
    }
 
5849
    QString _dir = dir.c_str();
 
5850
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5851
    if( pHost->mpMap->rooms.contains( id_from ) )
 
5852
    {
 
5853
        if( pHost->mpMap->rooms.contains( id_to ) )
 
5854
        {
 
5855
            pHost->mpMap->rooms[id_from]->addSpecialExit( id_to, _dir );
 
5856
            pHost->mpMap->mMapGraphNeedsUpdate = true;
 
5857
        }
 
5858
    }
 
5859
    return 0;
 
5860
}
 
5861
 
 
5862
int TLuaInterpreter::clearRoomUserData( lua_State * L )
 
5863
{
 
5864
    int id_from;
 
5865
    if( ! lua_isnumber( L, 1 ) )
 
5866
    {
 
5867
        lua_pushstring( L, "clearRoomUserData: wrong argument type" );
 
5868
        lua_error( L );
 
5869
        return 1;
 
5870
    }
 
5871
    else
 
5872
    {
 
5873
        id_from = lua_tointeger( L, 1 );
 
5874
    }
 
5875
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5876
    if( pHost->mpMap->rooms.contains( id_from ) )
 
5877
    {
 
5878
        pHost->mpMap->rooms[id_from]->userData.clear();
 
5879
    }
 
5880
    return 0;
 
5881
}
 
5882
 
 
5883
int TLuaInterpreter::clearSpecialExits( lua_State * L )
 
5884
{
 
5885
    int id_from;
 
5886
    if( ! lua_isnumber( L, 1 ) )
 
5887
    {
 
5888
        lua_pushstring( L, "clearSpecialExits: wrong argument type" );
 
5889
        lua_error( L );
 
5890
        return 1;
 
5891
    }
 
5892
    else
 
5893
    {
 
5894
        id_from = lua_tointeger( L, 1 );
 
5895
    }
 
5896
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5897
    if( pHost->mpMap->rooms.contains( id_from ) )
 
5898
    {
 
5899
        pHost->mpMap->rooms[id_from]->other.clear();
 
5900
        pHost->mpMap->mMapGraphNeedsUpdate = true;
 
5901
    }
 
5902
    return 0;
 
5903
}
 
5904
 
 
5905
int TLuaInterpreter::getSpecialExits( lua_State * L )
 
5906
{
 
5907
    int id_from;
 
5908
    if( ! lua_isnumber( L, 1 ) )
 
5909
    {
 
5910
        lua_pushstring( L, "getSpecialExits: wrong argument type" );
 
5911
        lua_error( L );
 
5912
        return 1;
 
5913
    }
 
5914
    else
 
5915
    {
 
5916
        id_from = lua_tointeger( L, 1 );
 
5917
    }
 
5918
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5919
    if( pHost->mpMap->rooms.contains( id_from ) )
 
5920
    {
 
5921
        QMapIterator<int, QString> it(pHost->mpMap->rooms[id_from]->other);
 
5922
        lua_newtable(L);
 
5923
        while( it.hasNext() )
 
5924
        {
 
5925
            it.next();
 
5926
            int id_to = it.key();
 
5927
            QString dir = it.value();
 
5928
            lua_pushnumber( L, id_to );
 
5929
            lua_pushstring( L, dir.toLatin1().data() );
 
5930
            lua_settable(L, -3);
 
5931
        }
 
5932
        return 1;
 
5933
    }
 
5934
    return 0;
 
5935
}
 
5936
 
 
5937
int TLuaInterpreter::getSpecialExitsSwap( lua_State * L )
 
5938
{
 
5939
    int id_from;
 
5940
    if( ! lua_isnumber( L, 1 ) )
 
5941
    {
 
5942
        lua_pushstring( L, "getSpecialExitsSwap: wrong argument type" );
 
5943
        lua_error( L );
 
5944
        return 1;
 
5945
    }
 
5946
    else
 
5947
    {
 
5948
        id_from = lua_tointeger( L, 1 );
 
5949
    }
 
5950
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5951
    if( pHost->mpMap->rooms.contains( id_from ) )
 
5952
    {
 
5953
        QMapIterator<int, QString> it(pHost->mpMap->rooms[id_from]->other);
 
5954
        lua_newtable(L);
 
5955
        while( it.hasNext() )
 
5956
        {
 
5957
            it.next();
 
5958
            int id_to = it.key();
 
5959
            QString dir = it.value();
 
5960
            lua_pushstring( L, dir.toLatin1().data() );
 
5961
            lua_pushnumber( L, id_to );
 
5962
            lua_settable(L, -3);
 
5963
        }
 
5964
        return 1;
 
5965
    }
 
5966
    return 0;
 
5967
}
 
5968
 
 
5969
int TLuaInterpreter::getRoomEnv( lua_State * L )
 
5970
{
 
5971
    int roomID;
 
5972
    if( ! lua_isnumber( L, 1 ) )
 
5973
    {
 
5974
        lua_pushstring( L, "getRoomEnv: wrong argument type" );
 
5975
        lua_error( L );
 
5976
        return 1;
 
5977
    }
 
5978
    else
 
5979
    {
 
5980
        roomID = lua_tointeger( L, 1 );
 
5981
    }
 
5982
 
 
5983
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
5984
    if( pHost->mpMap->rooms.contains( roomID ) )
 
5985
    {
 
5986
        lua_pushnumber( L, pHost->mpMap->rooms[roomID]->environment );
 
5987
        return 1;
 
5988
    }
 
5989
    return 0;
 
5990
}
 
5991
 
 
5992
int TLuaInterpreter::getRoomUserData( lua_State * L )
 
5993
{
 
5994
    int roomID;
 
5995
    if( ! lua_isnumber( L, 1 ) )
 
5996
    {
 
5997
        lua_pushstring( L, "getRoomUserData: wrong argument type" );
 
5998
        lua_error( L );
 
5999
        return 1;
 
6000
    }
 
6001
    else
 
6002
    {
 
6003
        roomID = lua_tointeger( L, 1 );
 
6004
    }
 
6005
    string key;
 
6006
    if( ! lua_isstring( L, 2 ) )
 
6007
    {
 
6008
        lua_pushstring( L, "getRoomUserData: wrong argument type" );
 
6009
        lua_error( L );
 
6010
        return 1;
 
6011
    }
 
6012
    else
 
6013
    {
 
6014
        key = lua_tostring( L, 2 );
 
6015
    }
 
6016
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6017
    if( pHost->mpMap->rooms.contains( roomID ) )
 
6018
    {
 
6019
        QString _key = key.c_str();
 
6020
        if( pHost->mpMap->rooms[roomID]->userData.contains( _key ) )
 
6021
        {
 
6022
            lua_pushstring( L, pHost->mpMap->rooms[roomID]->userData[_key].toLatin1().data() );
 
6023
            return 1;
 
6024
        }
 
6025
        else
 
6026
        {
 
6027
            lua_pushstring( L, "" );
 
6028
            return 1;
 
6029
        }
 
6030
    }
 
6031
    else
 
6032
    {
 
6033
        lua_pushstring( L, "" );
 
6034
        return 1;
 
6035
    }
 
6036
}
 
6037
 
 
6038
int TLuaInterpreter::setRoomUserData( lua_State * L )
 
6039
{
 
6040
    int roomID;
 
6041
    if( ! lua_isnumber( L, 1 ) )
 
6042
    {
 
6043
        lua_pushstring( L, "getRoomUserData: wrong argument type" );
 
6044
        lua_error( L );
 
6045
        return 1;
 
6046
    }
 
6047
    else
 
6048
    {
 
6049
        roomID = lua_tointeger( L, 1 );
 
6050
    }
 
6051
    string key;
 
6052
    if( ! lua_isstring( L, 2 ) )
 
6053
    {
 
6054
        lua_pushstring( L, "getRoomUserData: wrong argument type" );
 
6055
        lua_error( L );
 
6056
        return 1;
 
6057
    }
 
6058
    else
 
6059
    {
 
6060
        key = lua_tostring( L, 2 );
 
6061
    }
 
6062
    string value;
 
6063
    if( ! lua_isstring( L, 3 ) )
 
6064
    {
 
6065
        lua_pushstring( L, "getRoomUserData: wrong argument type" );
 
6066
        lua_error( L );
 
6067
        return 1;
 
6068
    }
 
6069
    else
 
6070
    {
 
6071
        value = lua_tostring( L, 3 );
 
6072
    }
 
6073
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6074
    if( pHost->mpMap->rooms.contains( roomID ) )
 
6075
    {
 
6076
        QString _key = key.c_str();
 
6077
        QString _value = value.c_str();
 
6078
        pHost->mpMap->rooms[roomID]->userData[_key] = _value;
 
6079
        lua_pushboolean( L, true );
 
6080
        return 1;
 
6081
    }
 
6082
    else
 
6083
    {
 
6084
        lua_pushboolean( L, false );
 
6085
        return 1;
 
6086
    }
 
6087
}
 
6088
 
 
6089
 
 
6090
 
 
6091
int TLuaInterpreter::downloadFile( lua_State * L )
 
6092
{
 
6093
    string path, url;
 
6094
    if( ! lua_isstring( L, 1 ) )
 
6095
    {
 
6096
        lua_pushstring( L, "downloadFile: wrong argument type" );
 
6097
        lua_error( L );
 
6098
        return 1;
 
6099
    }
 
6100
    else
 
6101
    {
 
6102
        path = lua_tostring( L, 1 );
 
6103
    }
 
6104
    if( ! lua_isstring( L, 2 ) )
 
6105
    {
 
6106
        lua_pushstring( L, "downloadFile: wrong argument type" );
 
6107
        lua_error( L );
 
6108
        return 1;
 
6109
    }
 
6110
    else
 
6111
    {
 
6112
        url = lua_tostring( L, 2 );
 
6113
    }
 
6114
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6115
    QString _url = url.c_str();
 
6116
    QString _path = path.c_str();
 
6117
 
 
6118
    QNetworkReply * reply = pHost->mLuaInterpreter.mpFileDownloader->get( QNetworkRequest( QUrl( _url ) ) );
 
6119
    pHost->mLuaInterpreter.downloadMap[reply] = _path;
 
6120
    return 0;
 
6121
 
 
6122
}
 
6123
 
 
6124
 
 
6125
int TLuaInterpreter::setRoomArea( lua_State * L )
 
6126
{
 
6127
    int id, area;
 
6128
    if( ! lua_isnumber( L, 1 ) )
 
6129
    {
 
6130
        lua_pushstring( L, "setRoomArea: wrong argument type" );
 
6131
        lua_error( L );
 
6132
        return 1;
 
6133
    }
 
6134
    else
 
6135
    {
 
6136
        id = lua_tointeger( L, 1 );
 
6137
    }
 
6138
    if( ! lua_isnumber( L, 2 ) )
 
6139
    {
 
6140
        lua_pushstring( L, "setRoomArea: wrong argument type" );
 
6141
        lua_error( L );
 
6142
        return 1;
 
6143
    }
 
6144
    else
 
6145
    {
 
6146
        area = lua_tointeger( L, 2 );
 
6147
    }
 
6148
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6149
 
 
6150
    pHost->mpMap->setRoomArea( id, area );
 
6151
    return 0;
 
6152
}
 
6153
 
 
6154
int TLuaInterpreter::setRoomChar( lua_State * L )
 
6155
{
 
6156
    int id;
 
6157
    string c;
 
6158
    if( ! lua_isnumber( L, 1 ) )
 
6159
    {
 
6160
        lua_pushstring( L, "setRoomArea: wrong argument type" );
 
6161
        lua_error( L );
 
6162
        return 1;
 
6163
    }
 
6164
    else
 
6165
    {
 
6166
        id = lua_tointeger( L, 1 );
 
6167
    }
 
6168
    if( ! lua_isstring( L, 2 ) )
 
6169
    {
 
6170
        lua_pushstring( L, "setRoomArea: wrong argument type" );
 
6171
        lua_error( L );
 
6172
        return 1;
 
6173
    }
 
6174
    else
 
6175
    {
 
6176
        c = lua_tostring( L, 2 );
 
6177
    }
 
6178
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6179
    if( ! pHost->mpMap->rooms.contains( id ) )
 
6180
    {
 
6181
        lua_pushstring( L, "setRoomArea: room ID does not exist");
 
6182
        lua_error( L );
 
6183
        return 1;
 
6184
    }
 
6185
    else
 
6186
    {
 
6187
        if( c.size() >= 1 )
 
6188
        {
 
6189
            pHost->mpMap->rooms[id]->c = c[0];
 
6190
        }
 
6191
    }
 
6192
    return 0;
 
6193
}
 
6194
 
 
6195
 
 
6196
int TLuaInterpreter::getRoomsByPosition( lua_State * L )
 
6197
{
 
6198
    int area, x, y, z;
 
6199
    if( ! lua_isnumber( L, 1 ) )
 
6200
    {
 
6201
        lua_pushstring( L, "getRoomsByPosition: wrong argument type" );
 
6202
        lua_error( L );
 
6203
        return 1;
 
6204
    }
 
6205
    else
 
6206
    {
 
6207
        area = lua_tointeger( L, 1 );
 
6208
    }
 
6209
    if( ! lua_isnumber( L, 2 ) )
 
6210
    {
 
6211
        lua_pushstring( L, "getRoomsByPosition: wrong argument type" );
 
6212
        lua_error( L );
 
6213
        return 1;
 
6214
    }
 
6215
    else
 
6216
    {
 
6217
        x = lua_tointeger( L, 2 );
 
6218
    }
 
6219
    if( ! lua_isnumber( L, 3 ) )
 
6220
    {
 
6221
        lua_pushstring( L, "getRoomsByPosition: wrong argument type" );
 
6222
        lua_error( L );
 
6223
        return 1;
 
6224
    }
 
6225
    else
 
6226
    {
 
6227
        y = lua_tointeger( L, 3 );
 
6228
    }
 
6229
    if( ! lua_isnumber( L, 4 ) )
 
6230
    {
 
6231
        lua_pushstring( L, "getRoomsByPosition: wrong argument type" );
 
6232
        lua_error( L );
 
6233
        return 1;
 
6234
    }
 
6235
    else
 
6236
    {
 
6237
        z = lua_tointeger( L, 4 );
 
6238
    }
 
6239
 
 
6240
 
 
6241
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6242
    if( ! pHost->mpMap->areas.contains( area ) )
 
6243
    {
 
6244
        lua_pushnil( L );
 
6245
        return 1;
 
6246
    }
 
6247
    QList<int> rL = pHost->mpMap->areas[area]->getRoomsByPosition( x, y, z);
 
6248
    lua_newtable( L );
 
6249
    for( int i=0; i<rL.size(); i++ )
 
6250
    {
 
6251
        lua_pushnumber( L, i );
 
6252
        lua_pushnumber( L, rL[i] );
 
6253
        lua_settable(L, -3);
 
6254
    }
 
6255
    return 1;
 
6256
}
 
6257
 
 
6258
 
 
6259
// returns true if area exits, otherwise false
 
6260
int TLuaInterpreter::setGridMode( lua_State * L )
 
6261
{
 
6262
    int area;
 
6263
    bool gridMode = false;
 
6264
    if( ! lua_isnumber( L, 1 ) )
 
6265
    {
 
6266
        lua_pushstring( L, "setRoomArea: wrong argument type" );
 
6267
        lua_error( L );
 
6268
        return 1;
 
6269
    }
 
6270
    else
 
6271
    {
 
6272
        area = lua_tointeger( L, 1 );
 
6273
    }
 
6274
    if( ! lua_isboolean( L, 2 ) )
 
6275
    {
 
6276
        lua_pushstring( L, "setRoomArea: wrong argument type" );
 
6277
        lua_error( L );
 
6278
        return 1;
 
6279
    }
 
6280
    else
 
6281
    {
 
6282
        gridMode = lua_toboolean( L, 2 );
 
6283
    }
 
6284
 
 
6285
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6286
    if( ! pHost->mpMap->areas.contains( area ) )
 
6287
    {
 
6288
        lua_pushboolean( L, false);
 
6289
        return 1;
 
6290
    }
 
6291
    else
 
6292
    {
 
6293
        pHost->mpMap->areas[area]->gridMode = gridMode;
 
6294
    }
 
6295
    lua_pushboolean( L, true );
 
6296
    return 1;
 
6297
}
 
6298
 
 
6299
 
 
6300
 
 
6301
int TLuaInterpreter::setFgColor( lua_State *L )
 
6302
{
 
6303
    int s = 1;
 
6304
    int n = lua_gettop( L );
 
6305
    string a1;
 
6306
    int luaRed;
 
6307
    int luaGreen;
 
6308
    int luaBlue;
 
6309
    if( n > 3 )
 
6310
    {
 
6311
        if( lua_isstring( L, s ) )
 
6312
        {
 
6313
            a1 = lua_tostring( L, s );
 
6314
            s++;
 
6315
        }
 
6316
    }
 
6317
    if( ! lua_isnumber( L, s ) )
 
6318
    {
 
6319
        lua_pushstring( L, "setFgColor: wrong argument type" );
 
6320
        lua_error( L );
 
6321
        return 1;
 
6322
    }
 
6323
    else
 
6324
    {
 
6325
        luaRed = lua_tointeger( L, s );
 
6326
        s++;
 
6327
    }
 
6328
 
 
6329
    if( ! lua_isnumber( L, s ) )
 
6330
    {
 
6331
        lua_pushstring( L, "setFgColor: wrong argument type" );
 
6332
        lua_error( L );
 
6333
        return 1;
 
6334
    }
 
6335
    else
 
6336
    {
 
6337
        luaGreen=lua_tointeger( L, s );
 
6338
        s++;
 
6339
    }
 
6340
 
 
6341
    if( ! lua_isnumber( L, s ) )
 
6342
    {
 
6343
        lua_pushstring( L, "setFgColor: wrong argument type" );
 
6344
        lua_error( L );
 
6345
        return 1;
 
6346
    }
 
6347
    else
 
6348
    {
 
6349
        luaBlue = lua_tointeger( L, s );
 
6350
    }
 
6351
 
 
6352
    QString _name( a1.c_str() );
 
6353
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6354
    if( n < 4 )
 
6355
        pHost->mpConsole->setFgColor( luaRed, luaGreen, luaBlue );
 
6356
    else
 
6357
        mudlet::self()->setFgColor( pHost, _name, luaRed, luaGreen, luaBlue );
 
6358
    return 0;
 
6359
}
 
6360
 
 
6361
int TLuaInterpreter::setBgColor( lua_State *L )
 
6362
{
 
6363
    int s = 1;
 
6364
    int n = lua_gettop( L );
 
6365
    string a1;
 
6366
    int luaRed;
 
6367
    int luaGreen;
 
6368
    int luaBlue;
 
6369
    if( n > 3 )
 
6370
    {
 
6371
        if( lua_isstring( L, s ) )
 
6372
        {
 
6373
            a1 = lua_tostring( L, s );
 
6374
            s++;
 
6375
        }
 
6376
    }
 
6377
    if( ! lua_isnumber( L, s ) )
 
6378
    {
 
6379
        lua_pushstring( L, "setBgColor: wrong argument type" );
 
6380
        lua_error( L );
 
6381
        return 1;
 
6382
    }
 
6383
    else
 
6384
    {
 
6385
        luaRed = lua_tointeger( L, s );
 
6386
        s++;
 
6387
    }
 
6388
 
 
6389
    if( ! lua_isnumber( L, s ) )
 
6390
    {
 
6391
        lua_pushstring( L, "setBgColor: wrong argument type" );
 
6392
        lua_error( L );
 
6393
        return 1;
 
6394
    }
 
6395
    else
 
6396
    {
 
6397
        luaGreen=lua_tointeger( L, s );
 
6398
        s++;
 
6399
    }
 
6400
 
 
6401
    if( ! lua_isnumber( L, s ) )
 
6402
    {
 
6403
        lua_pushstring( L, "setBgColor: wrong argument type" );
 
6404
        lua_error( L );
 
6405
        return 1;
 
6406
    }
 
6407
    else
 
6408
    {
 
6409
        luaBlue = lua_tointeger( L, s );
 
6410
    }
 
6411
 
 
6412
    QString _name( a1.c_str() );
 
6413
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6414
    if( n < 4 )
 
6415
        pHost->mpConsole->setBgColor( luaRed, luaGreen, luaBlue );
 
6416
    else
 
6417
        mudlet::self()->setBgColor( pHost, _name, luaRed, luaGreen, luaBlue );
 
6418
    return 0;
 
6419
}
 
6420
 
 
6421
int TLuaInterpreter::insertLink( lua_State *L )
 
6422
{
 
6423
    string a1;
 
6424
    string a2;
 
6425
    string a3;
 
6426
    string a4;
 
6427
    int n = lua_gettop( L );
 
6428
    int s = 1;
 
6429
    if( ! lua_isstring( L, s ) )
 
6430
    {
 
6431
        lua_pushstring( L, "insertLink: wrong argument type" );
 
6432
        lua_error( L );
 
6433
        return 1;
 
6434
    }
 
6435
    else
 
6436
    {
 
6437
        a1 = lua_tostring( L, s );
 
6438
        s++;
 
6439
    }
 
6440
    QString _name( a1.c_str() );
 
6441
 
 
6442
    if( n > 1 )
 
6443
    {
 
6444
        if( ! lua_isstring( L, s ) )
 
6445
        {
 
6446
            lua_pushstring( L, "insertLink: wrong argument type" );
 
6447
            lua_error( L );
 
6448
            return 1;
 
6449
        }
 
6450
        else
 
6451
        {
 
6452
            a2 = lua_tostring( L, s );
 
6453
            s++;
 
6454
        }
 
6455
    }
 
6456
    if( n > 2 )
 
6457
    {
 
6458
        if( ! lua_isstring( L, s ) )
 
6459
        {
 
6460
            lua_pushstring( L, "insertLink: wrong argument type" );
 
6461
            lua_error( L );
 
6462
            return 1;
 
6463
        }
 
6464
        else
 
6465
        {
 
6466
            a3 = lua_tostring( L, s );
 
6467
        }
 
6468
    }
 
6469
    if( n > 3 )
 
6470
    {
 
6471
        if( ! lua_isstring( L, s ) )
 
6472
        {
 
6473
            lua_pushstring( L, "insertLink: wrong argument type" );
 
6474
            lua_error( L );
 
6475
            return 1;
 
6476
        }
 
6477
        else
 
6478
        {
 
6479
            a4 = lua_tostring( L, s );
 
6480
        }
 
6481
    }
 
6482
    QStringList command;
 
6483
    QStringList hint;
 
6484
 
 
6485
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6486
    if( n == 3 )
 
6487
    {
 
6488
        command << a2.c_str();
 
6489
        hint << a3.c_str();
 
6490
        pHost->mpConsole->insertLink( QString(a1.c_str()), command, hint );
 
6491
    }
 
6492
    else
 
6493
    {
 
6494
        command << a3.c_str();
 
6495
        hint << a4.c_str();
 
6496
        mudlet::self()->insertLink( pHost, _name, QString( a2.c_str() ), command, hint );
 
6497
    }
 
6498
 
 
6499
    return 0;
 
6500
}
 
6501
 
 
6502
int TLuaInterpreter::insertPopup( lua_State *L )
 
6503
{
 
6504
    string a1 = "";
 
6505
    string a2;
 
6506
    QStringList _hintList;
 
6507
    QStringList _commandList;
 
6508
    bool customFormat = false;
 
6509
    int s = 1;
 
6510
    int n = lua_gettop( L );
 
6511
    // console name is an optional first argument
 
6512
    if( n > 4 )
 
6513
    {
 
6514
        if( ! lua_isstring( L, s ) )
 
6515
        {
 
6516
            lua_pushstring( L, "insertPopup: wrong argument type" );
 
6517
            lua_error( L );
 
6518
            return 1;
 
6519
        }
 
6520
        else
 
6521
        {
 
6522
            a1 = lua_tostring( L, s );
 
6523
            s++;
 
6524
        }
 
6525
    }
 
6526
    if( ! lua_isstring( L, s ) )
 
6527
    {
 
6528
        lua_pushstring( L, "insertPopup: wrong argument type" );
 
6529
        lua_error( L );
 
6530
        return 1;
 
6531
    }
 
6532
    else
 
6533
    {
 
6534
        a2 = lua_tostring( L, s );
 
6535
        s++;
 
6536
    }
 
6537
 
 
6538
    if( ! lua_istable( L, s ) )
 
6539
    {
 
6540
        lua_pushstring( L, "insertPopup: wrong argument type" );
 
6541
        lua_error( L );
 
6542
        return 1;
 
6543
    }
 
6544
    else
 
6545
    {
 
6546
        lua_pushnil( L );
 
6547
        while( lua_next( L, s ) != 0 )
 
6548
        {
 
6549
            // key at index -2 and value at index -1
 
6550
            if( lua_type(L, -1) == LUA_TSTRING )
 
6551
            {
 
6552
                QString cmd = lua_tostring( L, -1 );
 
6553
                _commandList << cmd;
 
6554
            }
 
6555
            // removes value, but keeps key for next iteration
 
6556
            lua_pop(L, 1);
 
6557
        }
 
6558
        s++;
 
6559
    }
 
6560
    if( ! lua_istable( L, s ) )
 
6561
    {
 
6562
        lua_pushstring( L, "insertPopup: wrong argument type" );
 
6563
        lua_error( L );
 
6564
        return 1;
 
6565
    }
 
6566
    else
 
6567
    {
 
6568
        lua_pushnil( L );
 
6569
        while( lua_next( L, s ) != 0 )
 
6570
        {
 
6571
            // key at index -2 and value at index -1
 
6572
            if( lua_type(L, -1) == LUA_TSTRING )
 
6573
            {
 
6574
                QString hint = lua_tostring( L, -1 );
 
6575
                _hintList << hint;
 
6576
            }
 
6577
            // removes value, but keeps key for next iteration
 
6578
            lua_pop(L, 1);
 
6579
        }
 
6580
        s++;
 
6581
    }
 
6582
    if( n >= s )
 
6583
    {
 
6584
        customFormat = lua_toboolean( L, s );
 
6585
    }
 
6586
 
 
6587
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6588
    QString txt = a2.c_str();
 
6589
    QString name = a1.c_str();
 
6590
    if( _commandList.size() != _hintList.size() )
 
6591
    {
 
6592
        lua_pushstring( L, "Error: command list size and hint list size do not match cannot create popup" );
 
6593
        lua_error( L );
 
6594
        return 1;
 
6595
    }
 
6596
 
 
6597
    if( a1 == "" )
 
6598
    {
 
6599
        pHost->mpConsole->insertLink( txt, _commandList, _hintList, customFormat );
 
6600
    }
 
6601
    else
 
6602
    {
 
6603
        mudlet::self()->insertLink( pHost, name, txt, _commandList, _hintList, customFormat );
 
6604
    }
 
6605
 
 
6606
    return 0;
 
6607
}
 
6608
 
 
6609
int TLuaInterpreter::insertText( lua_State *L )
 
6610
{
 
6611
    string a1;
 
6612
    string a2;
 
6613
    int n = lua_gettop( L );
 
6614
    int s = 1;
 
6615
    if( ! lua_isstring( L, s ) )
 
6616
    {
 
6617
        lua_pushstring( L, "insertText: wrong argument type" );
 
6618
        lua_error( L );
 
6619
        return 1;
 
6620
    }
 
6621
    else
 
6622
    {
 
6623
        a1 = lua_tostring( L, s );
 
6624
        s++;
 
6625
    }
 
6626
    QString _name( a1.c_str() );
 
6627
 
 
6628
    if( n > 1 )
 
6629
    {
 
6630
        if( ! lua_isstring( L, s ) )
 
6631
        {
 
6632
            lua_pushstring( L, "insertText: wrong argument type" );
 
6633
            lua_error( L );
 
6634
            return 1;
 
6635
        }
 
6636
        else
 
6637
        {
 
6638
            a2 = lua_tostring( L, s );
 
6639
        }
 
6640
    }
 
6641
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6642
    if( n == 1 )
 
6643
        pHost->mpConsole->insertText( QString(a1.c_str()) );
 
6644
    else
 
6645
        mudlet::self()->insertText( pHost, _name, QString( a2.c_str() ) );
 
6646
    return 0;
 
6647
}
 
6648
 
 
6649
int TLuaInterpreter::insertHTML( lua_State *L )
 
6650
{
 
6651
    string luaSendText;
 
6652
    if( ! lua_isstring( L, 1 ) )
 
6653
    {
 
6654
        lua_pushstring( L, "insertHTML: wrong argument type" );
 
6655
        lua_error( L );
 
6656
        return 1;
 
6657
    }
 
6658
    else
 
6659
    {
 
6660
        luaSendText = lua_tostring( L, 1 );
 
6661
    }
 
6662
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6663
    pHost->mpConsole->insertHTML( QString(luaSendText.c_str()) );
 
6664
    return 0;
 
6665
}
 
6666
 
 
6667
int TLuaInterpreter::Echo( lua_State *L )
 
6668
{
 
6669
    string a1;
 
6670
    string a2;
 
6671
    int s = 1;
 
6672
    int n = lua_gettop( L );
 
6673
 
 
6674
    if( ! lua_isstring( L, s ) )
 
6675
    {
 
6676
        lua_pushstring( L, "Echo: wrong argument type" );
 
6677
        lua_error( L );
 
6678
        return 1;
 
6679
    }
 
6680
    else
 
6681
    {
 
6682
        a1 = lua_tostring( L, s );
 
6683
        s++;
 
6684
    }
 
6685
    if( n > 1 )
 
6686
    {
 
6687
        if( ! lua_isstring( L, s ) )
 
6688
        {
 
6689
            lua_pushstring( L, "Echo: wrong argument type" );
 
6690
            lua_error( L );
 
6691
            return 1;
 
6692
        }
 
6693
        else
 
6694
        {
 
6695
            a2 = lua_tostring( L, s );
 
6696
        }
 
6697
    }
 
6698
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6699
    QString txt;
 
6700
    QString name;
 
6701
    if( n == 1 )
 
6702
    {
 
6703
        txt = a1.c_str();
 
6704
        pHost->mpConsole->echo( txt );
 
6705
    }
 
6706
    else
 
6707
    {
 
6708
        txt = a2.c_str();
 
6709
        name = a1.c_str();
 
6710
        mudlet::self()->echoWindow( pHost, name, txt );
 
6711
    }
 
6712
 
 
6713
    return 0;
 
6714
}
 
6715
 
 
6716
int TLuaInterpreter::echoPopup( lua_State *L )
 
6717
{
 
6718
    string a1 = "";
 
6719
    string a2;
 
6720
    QStringList _hintList;
 
6721
    QStringList _commandList;
 
6722
    bool customFormat = false;
 
6723
    int s = 1;
 
6724
    int n = lua_gettop( L );
 
6725
    // console name is an optional first argument
 
6726
    if( n > 4 )
 
6727
    {
 
6728
        if( ! lua_isstring( L, s ) )
 
6729
        {
 
6730
            lua_pushstring( L, "echoPopup: wrong argument type" );
 
6731
            lua_error( L );
 
6732
            return 1;
 
6733
        }
 
6734
        else
 
6735
        {
 
6736
            a1 = lua_tostring( L, s );
 
6737
            s++;
 
6738
        }
 
6739
    }
 
6740
    if( ! lua_isstring( L, s ) )
 
6741
    {
 
6742
        lua_pushstring( L, "echoPopup: wrong argument type" );
 
6743
        lua_error( L );
 
6744
        return 1;
 
6745
    }
 
6746
    else
 
6747
    {
 
6748
        a2 = lua_tostring( L, s );
 
6749
        s++;
 
6750
    }
 
6751
 
 
6752
    if( ! lua_istable( L, s ) )
 
6753
    {
 
6754
        lua_pushstring( L, "echoPopup: wrong argument type" );
 
6755
        lua_error( L );
 
6756
        return 1;
 
6757
    }
 
6758
    else
 
6759
    {
 
6760
        lua_pushnil( L );
 
6761
        while( lua_next( L, s ) != 0 )
 
6762
        {
 
6763
            // key at index -2 and value at index -1
 
6764
            if( lua_type(L, -1) == LUA_TSTRING )
 
6765
            {
 
6766
                QString cmd = lua_tostring( L, -1 );
 
6767
                _commandList << cmd;
 
6768
            }
 
6769
            // removes value, but keeps key for next iteration
 
6770
            lua_pop(L, 1);
 
6771
        }
 
6772
        s++;
 
6773
    }
 
6774
    if( ! lua_istable( L, s ) )
 
6775
    {
 
6776
        lua_pushstring( L, "echoPopup: wrong argument type" );
 
6777
        lua_error( L );
 
6778
        return 1;
 
6779
    }
 
6780
    else
 
6781
    {
 
6782
        lua_pushnil( L );
 
6783
        while( lua_next( L, s ) != 0 )
 
6784
        {
 
6785
            // key at index -2 and value at index -1
 
6786
            if( lua_type(L, -1) == LUA_TSTRING )
 
6787
            {
 
6788
                QString hint = lua_tostring( L, -1 );
 
6789
                _hintList << hint;
 
6790
            }
 
6791
            // removes value, but keeps key for next iteration
 
6792
            lua_pop(L, 1);
 
6793
        }
 
6794
        s++;
 
6795
    }
 
6796
    if( n >= s )
 
6797
    {
 
6798
        customFormat = lua_toboolean( L, s );
 
6799
    }
 
6800
 
 
6801
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6802
    QString txt = a2.c_str();
 
6803
    QString name = a1.c_str();
 
6804
    if( _commandList.size() != _hintList.size() )
 
6805
    {
 
6806
        lua_pushstring( L, "Error: command list size and hint list size do not match cannot create popup" );
 
6807
        lua_error( L );
 
6808
        return 1;
 
6809
    }
 
6810
 
 
6811
    if( a1 == "" )
 
6812
    {
 
6813
        pHost->mpConsole->echoLink( txt, _commandList, _hintList, customFormat );
 
6814
    }
 
6815
    else
 
6816
    {
 
6817
        mudlet::self()->echoLink( pHost, name, txt, _commandList, _hintList, customFormat );
 
6818
    }
 
6819
 
 
6820
    return 0;
 
6821
}
 
6822
 
 
6823
 
 
6824
int TLuaInterpreter::echoLink( lua_State *L )
 
6825
{
 
6826
    string a1;
 
6827
    string a2;
 
6828
    string a3;
 
6829
    string a4;
 
6830
    bool a5 = false;
 
6831
    bool gotBool = false;
 
6832
 
 
6833
    int s = 1;
 
6834
    int n = lua_gettop( L );
 
6835
 
 
6836
    if( ! lua_isstring( L, s ) )
 
6837
    {
 
6838
        lua_pushstring( L, "echoLink: wrong argument type" );
 
6839
        lua_error( L );
 
6840
        return 1;
 
6841
    }
 
6842
    else
 
6843
    {
 
6844
        a1 = lua_tostring( L, s );
 
6845
        s++;
 
6846
    }
 
6847
    if( n > 1 )
 
6848
    {
 
6849
        if( ! lua_isstring( L, s ) )
 
6850
        {
 
6851
            lua_pushstring( L, "echoLink: wrong argument type" );
 
6852
            lua_error( L );
 
6853
            return 1;
 
6854
        }
 
6855
        else
 
6856
        {
 
6857
            a2 = lua_tostring( L, s );
 
6858
            s++;
 
6859
        }
 
6860
    }
 
6861
    if( n > 2 )
 
6862
    {
 
6863
        if( ! lua_isstring( L, s ) )
 
6864
        {
 
6865
            lua_pushstring( L, "echoLink: wrong argument type" );
 
6866
            lua_error( L );
 
6867
            return 1;
 
6868
        }
 
6869
        else
 
6870
        {
 
6871
            a3 = lua_tostring( L, s );
 
6872
            s++;
 
6873
        }
 
6874
    }
 
6875
    if( n > 3 )
 
6876
    {
 
6877
        if( lua_isstring( L, s ) )
 
6878
        {
 
6879
            a4 = lua_tostring( L, s );
 
6880
            s++;
 
6881
        }
 
6882
        else if( lua_isboolean( L, s ) )
 
6883
        {
 
6884
            gotBool = true;
 
6885
            a5 = lua_toboolean( L, s );
 
6886
            s++;
 
6887
        }
 
6888
    }
 
6889
    if( n > 4 )
 
6890
    {
 
6891
        if( ! lua_isboolean( L, s ) )
 
6892
        {
 
6893
            lua_pushstring( L, "echoLink: wrong argument type" );
 
6894
            lua_error( L );
 
6895
            return 1;
 
6896
        }
 
6897
        else
 
6898
        {
 
6899
            a5 = lua_toboolean( L, s );
 
6900
            gotBool = true;
 
6901
            s++;
 
6902
        }
 
6903
    }
 
6904
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6905
    QString txt;
 
6906
    QString name;
 
6907
    QStringList func;
 
6908
    QStringList hint;
 
6909
    if( n == 3 || ( n == 4 && gotBool ) )
 
6910
    {
 
6911
        txt = a1.c_str();
 
6912
        func << a2.c_str();
 
6913
        hint << a3.c_str();
 
6914
        pHost->mpConsole->echoLink( txt, func, hint, a5 );
 
6915
    }
 
6916
    else
 
6917
    {
 
6918
        txt = a2.c_str();
 
6919
        func << a3.c_str();
 
6920
        hint << a4.c_str();
 
6921
        name = a1.c_str();
 
6922
        mudlet::self()->echoLink( pHost, name, txt, func, hint, a5 );
 
6923
    }
 
6924
 
 
6925
    return 0;
 
6926
}
 
6927
 
 
6928
int TLuaInterpreter::pasteWindow( lua_State *L )
 
6929
{
 
6930
    string luaName;
 
6931
    if( ! lua_isstring( L, 1 ) )
 
6932
    {
 
6933
        lua_pushstring( L, "pasteWindow: wrong argument type" );
 
6934
        lua_error( L );
 
6935
        return 1;
 
6936
    }
 
6937
    else
 
6938
    {
 
6939
        luaName = lua_tostring( L, 1 );
 
6940
    }
 
6941
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6942
    QString name( luaName.c_str());
 
6943
    mudlet::self()->pasteWindow( pHost, name );
 
6944
    return 0;
 
6945
}
 
6946
 
 
6947
int TLuaInterpreter::openUrl( lua_State *L )
 
6948
{
 
6949
    string luaName;
 
6950
    if( ! lua_isstring( L, 1 ) )
 
6951
    {
 
6952
        lua_pushstring( L, "openUrl: wrong argument type" );
 
6953
        lua_error( L );
 
6954
        return 1;
 
6955
    }
 
6956
    else
 
6957
    {
 
6958
        luaName = lua_tostring( L, 1 );
 
6959
    }
 
6960
    QString url( luaName.c_str());
 
6961
    QDesktopServices::openUrl(url);
 
6962
    return 0;
 
6963
}
 
6964
 
 
6965
int TLuaInterpreter::setLabelStyleSheet( lua_State * L )
 
6966
{
 
6967
    string luaSendText="";
 
6968
    if( ! lua_isstring( L, 1 ) )
 
6969
    {
 
6970
        lua_pushstring( L, "setLabelStyleSheet: wrong argument type" );
 
6971
        lua_error( L );
 
6972
        return 1;
 
6973
    }
 
6974
    else
 
6975
    {
 
6976
        luaSendText = lua_tostring( L, 1 );
 
6977
    }
 
6978
    string a2;
 
6979
    if( ! lua_isstring( L, 2 ) )
 
6980
    {
 
6981
        lua_pushstring( L, "setLabelStyleSheet: wrong argument type" );
 
6982
        lua_error( L );
 
6983
        return 1;
 
6984
    }
 
6985
    else
 
6986
    {
 
6987
        a2 = lua_tostring( L, 2 );
 
6988
    }
 
6989
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6990
    pHost->mpConsole->setLabelStyleSheet( luaSendText, a2 );
 
6991
    return 0;
 
6992
}
 
6993
 
 
6994
int TLuaInterpreter::getCustomEnvColorTable( lua_State * L )
 
6995
{
 
6996
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
6997
    if( pHost->mpMap->customEnvColors.size() > 0 )
 
6998
    {
 
6999
        lua_newtable( L );
 
7000
        QList<int> colorList = pHost->mpMap->customEnvColors.keys();
 
7001
        for( int idx=0; idx<colorList.size(); idx++ )
 
7002
        {
 
7003
            lua_pushnumber( L, colorList[idx] );
 
7004
            lua_newtable( L );
 
7005
            // red component
 
7006
            {
 
7007
                lua_pushnumber( L, 1 );
 
7008
                lua_pushnumber( L, pHost->mpMap->customEnvColors[colorList[idx]].red() );
 
7009
                lua_settable( L, -3 );//match in matches
 
7010
            }
 
7011
            // green component
 
7012
            {
 
7013
                lua_pushnumber( L, 2 );
 
7014
                lua_pushnumber( L, pHost->mpMap->customEnvColors[colorList[idx]].green() );
 
7015
                lua_settable( L, -3 );//match in matches
 
7016
            }
 
7017
            // blue component
 
7018
            {
 
7019
                lua_pushnumber( L, 3 );
 
7020
                lua_pushnumber( L, pHost->mpMap->customEnvColors[colorList[idx]].blue() );
 
7021
                lua_settable( L, -3 );//match in matches
 
7022
            }
 
7023
            lua_settable( L, -3 );//matches in regex
 
7024
        }
 
7025
    }
 
7026
    else
 
7027
    {
 
7028
        lua_newtable( L );
 
7029
    }
 
7030
    return 1;
 
7031
}
 
7032
 
 
7033
//syntax: getTime( bool return_string, string time_format ) with return_string == false -> return table
 
7034
int TLuaInterpreter::getTime( lua_State * L )
 
7035
{
 
7036
    int n = lua_gettop( L );
 
7037
    bool return_string = false;
 
7038
    QString fmt = "yyyy.MM.dd hh:mm:ss.zzz";
 
7039
    QString tm;
 
7040
    if( n > 0 )
 
7041
    {
 
7042
        return_string = lua_toboolean( L, 1 );
 
7043
        if( n > 1 )
 
7044
        {
 
7045
            if( ! lua_isstring( L, 2 ) )
 
7046
            {
 
7047
                lua_pushstring( L, "getTime: wrong argument type" );
 
7048
                lua_error( L );
 
7049
                return 1;
 
7050
            }
 
7051
            else
 
7052
            {
 
7053
                fmt = lua_tostring( L, 2 );
 
7054
            }
 
7055
        }
 
7056
    }
 
7057
    QDateTime time = QDateTime::currentDateTime();
 
7058
    if( return_string )
 
7059
    {
 
7060
        tm = time.toString( fmt );
 
7061
        lua_pushstring( L, tm.toLatin1().data() );
 
7062
    }
 
7063
    else
 
7064
    {
 
7065
        QDate dt = time.date();
 
7066
        QTime tm = time.time();
 
7067
        lua_createtable( L, 0, 4 );
 
7068
        lua_pushstring( L, "hour" );
 
7069
        lua_pushinteger( L, tm.hour() );
 
7070
        lua_rawset( L, n+1 );
 
7071
        lua_pushstring( L, "min" );
 
7072
        lua_pushinteger( L, tm.minute() );
 
7073
        lua_rawset( L, n+1 );
 
7074
        lua_pushstring( L, "sec" );
 
7075
        lua_pushinteger( L, tm.second() );
 
7076
        lua_rawset( L, n+1 );
 
7077
        lua_pushstring( L, "msec" );
 
7078
        lua_pushinteger( L, tm.msec() );
 
7079
        lua_rawset( L, n+1 );
 
7080
        lua_pushstring( L, "year" );
 
7081
        lua_pushinteger( L, dt.year() );
 
7082
        lua_rawset( L, n+1 );
 
7083
        lua_pushstring( L, "month" );
 
7084
        lua_pushinteger( L, dt.month() );
 
7085
        lua_rawset( L, n+1 );
 
7086
        lua_pushstring( L, "day" );
 
7087
        lua_pushinteger( L, dt.day() );
 
7088
        lua_rawset( L, n+1 );
 
7089
    }
 
7090
    return 1;
 
7091
}
 
7092
 
 
7093
 
 
7094
int TLuaInterpreter::appendBuffer( lua_State *L )
 
7095
{
 
7096
    string a1;
 
7097
    string a2;
 
7098
    int s = 1;
 
7099
    int n = lua_gettop( L );
 
7100
    if( n > 0 )
 
7101
    {
 
7102
        if( ! lua_isstring( L, s ) )
 
7103
        {
 
7104
            lua_pushstring( L, "appendBuffer: wrong argument type" );
 
7105
            lua_error( L );
 
7106
            return 1;
 
7107
        }
 
7108
        else
 
7109
        {
 
7110
            a1 = lua_tostring( L, s );
 
7111
            s++;
 
7112
        }
 
7113
    }
 
7114
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7115
 
 
7116
    if( s == 1 )
 
7117
    {
 
7118
        pHost->mpConsole->appendBuffer();
 
7119
    }
 
7120
    else
 
7121
    {
 
7122
        QString name = a1.c_str();
 
7123
        mudlet::self()->appendBuffer( pHost, name );
 
7124
    }
 
7125
 
 
7126
    return 0;
 
7127
}
 
7128
 
 
7129
int TLuaInterpreter::appendCmdLine( lua_State * L )
 
7130
{
 
7131
    string luaSendText;
 
7132
    if( ! lua_isstring( L, 1 ) )
 
7133
    {
 
7134
        lua_pushstring( L, "appendCmdLine(): wrong argument type" );
 
7135
        lua_error( L );
 
7136
        return 1;
 
7137
    }
 
7138
    else
 
7139
    {
 
7140
        luaSendText = lua_tostring( L, 1 );
 
7141
    }
 
7142
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7143
    QString curText = pHost->mpConsole->mpCommandLine->toPlainText();
 
7144
    pHost->mpConsole->mpCommandLine->setPlainText( curText + QString( luaSendText.c_str() ) );
 
7145
    QTextCursor cur = pHost->mpConsole->mpCommandLine->textCursor();
 
7146
    cur.clearSelection();
 
7147
    cur.movePosition(QTextCursor::EndOfLine);
 
7148
    pHost->mpConsole->mpCommandLine->setTextCursor(cur);
 
7149
    return 0;
 
7150
}
 
7151
 
 
7152
int TLuaInterpreter::registerAnonymousEventHandler( lua_State * L )
 
7153
{
 
7154
    string event;
 
7155
    if( ! lua_isstring( L, 1 ) )
 
7156
    {
 
7157
        lua_pushstring( L, "appendCmdLine(): wrong argument type" );
 
7158
        lua_error( L );
 
7159
        return 1;
 
7160
    }
 
7161
    else
 
7162
    {
 
7163
        event = lua_tostring( L, 1 );
 
7164
    }
 
7165
    string func;
 
7166
    if( ! lua_isstring( L, 2 ) )
 
7167
    {
 
7168
        lua_pushstring( L, "appendCmdLine(): wrong argument type" );
 
7169
        lua_error( L );
 
7170
        return 1;
 
7171
    }
 
7172
    else
 
7173
    {
 
7174
        func = lua_tostring( L, 2 );
 
7175
    }
 
7176
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7177
    QString e = event.c_str();
 
7178
    QString f = func.c_str();
 
7179
    pHost->registerAnonymousEventHandler( e, f );
 
7180
    return 0;
 
7181
}
 
7182
 
 
7183
 
 
7184
int TLuaInterpreter::Send( lua_State * L )
 
7185
{
 
7186
    string luaSendText;
 
7187
    if( ! lua_isstring( L, 1 ) )
 
7188
    {
 
7189
        lua_pushstring( L, "Send: wrong argument type" );
 
7190
        lua_error( L );
 
7191
        return 1;
 
7192
    }
 
7193
    else
 
7194
    {
 
7195
        luaSendText = lua_tostring( L, 1 );
 
7196
    }
 
7197
    bool wantPrint = true;
 
7198
    if( lua_gettop( L ) > 1 )
 
7199
    {
 
7200
        if( ! lua_isboolean( L, 2 ) )
 
7201
        {
 
7202
            lua_pushstring( L, "Send: wrong argument type" );
 
7203
            lua_error( L );
 
7204
            return 1;
 
7205
        }
 
7206
        else
 
7207
        {
 
7208
            wantPrint = lua_toboolean( L, 2 );
 
7209
        }
 
7210
    }
 
7211
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7212
    pHost->send( QString( luaSendText.c_str() ), wantPrint, false );
 
7213
    return 0;
 
7214
}
 
7215
 
 
7216
int TLuaInterpreter::printCmdLine( lua_State * L )
 
7217
{
 
7218
    string luaSendText;
 
7219
    if( ! lua_isstring( L, 1 ) )
 
7220
    {
 
7221
        lua_pushstring( L, "printCmdLine: wrong argument type" );
 
7222
        lua_error( L );
 
7223
        return 1;
 
7224
    }
 
7225
    else
 
7226
    {
 
7227
        luaSendText = lua_tostring( L, 1 );
 
7228
    }
 
7229
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7230
    pHost->mpConsole->mpCommandLine->setPlainText( QString( luaSendText.c_str() ) );
 
7231
    QTextCursor cur = pHost->mpConsole->mpCommandLine->textCursor();
 
7232
    cur.clearSelection();
 
7233
    cur.movePosition(QTextCursor::EndOfLine);
 
7234
    pHost->mpConsole->mpCommandLine->setTextCursor(cur);
 
7235
    return 0;
 
7236
}
 
7237
 
 
7238
int TLuaInterpreter::clearCmdLine( lua_State * L )
 
7239
{
 
7240
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7241
    pHost->mpConsole->mpCommandLine->clear();
 
7242
    return 0;
 
7243
}
 
7244
 
 
7245
int TLuaInterpreter::sendRaw( lua_State * L )
 
7246
{
 
7247
    string luaSendText;
 
7248
    if( ! lua_isstring( L, 1 ) )
 
7249
    {
 
7250
        lua_pushstring( L, "sendRaw: wrong argument type" );
 
7251
        lua_error( L );
 
7252
        return 1;
 
7253
    }
 
7254
    else
 
7255
    {
 
7256
        luaSendText = lua_tostring( L, 1 );
 
7257
    }
 
7258
    bool wantPrint = true;
 
7259
    if( lua_gettop( L ) > 1 )
 
7260
    {
 
7261
        if( ! lua_isboolean( L, 2 ) )
 
7262
        {
 
7263
            lua_pushstring( L, "sendRaw: wrong argument type" );
 
7264
            lua_error( L );
 
7265
            return 1;
 
7266
        }
 
7267
        else
 
7268
        {
 
7269
            wantPrint = lua_toboolean( L, 2 );
 
7270
        }
 
7271
    }
 
7272
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7273
    pHost->send( QString(luaSendText.c_str()), wantPrint, true );
 
7274
    return 0;
 
7275
}
 
7276
 
 
7277
int TLuaInterpreter::sendSocket( lua_State * L )
 
7278
{
 
7279
    string luaSendText;
 
7280
    if( ! lua_isstring( L, 1 ) )
 
7281
    {
 
7282
        lua_pushstring( L, "sendSocket: wrong argument type" );
 
7283
        lua_error( L );
 
7284
        return 1;
 
7285
    }
 
7286
    else
 
7287
    {
 
7288
        luaSendText = lua_tostring( L, 1 );
 
7289
    }
 
7290
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7291
    pHost->mTelnet.socketOutRaw( luaSendText );
 
7292
    return 0;
 
7293
}
 
7294
 
 
7295
#include "dlgIRC.h"
 
7296
int TLuaInterpreter::sendIrc( lua_State * L )
 
7297
{
 
7298
    string who;
 
7299
    if( ! lua_isstring( L, 1 ) )
 
7300
    {
 
7301
        lua_pushstring( L, "sendSocket: wrong argument type" );
 
7302
        lua_error( L );
 
7303
        return 1;
 
7304
    }
 
7305
    else
 
7306
    {
 
7307
        who = lua_tostring( L, 1 );
 
7308
    }
 
7309
    string text;
 
7310
    if( ! lua_isstring( L, 2 ) )
 
7311
    {
 
7312
        lua_pushstring( L, "sendSocket: wrong argument type" );
 
7313
        lua_error( L );
 
7314
        return 1;
 
7315
    }
 
7316
    else
 
7317
    {
 
7318
        text = lua_tostring( L, 2 );
 
7319
    }
 
7320
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7321
    QString chan = who.c_str();
 
7322
    QString txt = text.c_str();
 
7323
    if( ! mudlet::self()->mpIRC ) return 0;
 
7324
    mudlet::self()->mpIRC->session->cmdMessage( chan, txt );
 
7325
    return 0;
 
7326
}
 
7327
 
 
7328
 
 
7329
bool TLuaInterpreter::compileAndExecuteScript( QString & code )
 
7330
{
 
7331
    if( code.size() < 1 ) return false;
 
7332
    lua_State * L = pGlobalLua;
 
7333
    if( ! L )
 
7334
    {
 
7335
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7336
        return false;
 
7337
    }
 
7338
 
 
7339
    int error = luaL_dostring( L, code.toLatin1().data() );
 
7340
    QString n;
 
7341
    if( error != 0 )
 
7342
    {
 
7343
        string e = "no error message available from Lua";
 
7344
        if( lua_isstring( L, 1 ) )
 
7345
        {
 
7346
            e = "Lua error:";
 
7347
            e+=lua_tostring( L, 1 );
 
7348
        }
 
7349
        if( mudlet::debugMode ) qDebug()<<"LUA ERROR: code did not compile: ERROR:"<<e.c_str();
 
7350
        emit signalEchoMessage( mHostID, QString( e.c_str() ) );
 
7351
    }
 
7352
 
 
7353
    lua_pop( L, lua_gettop( L ) );
 
7354
 
 
7355
    if( error == 0 ) return true;
 
7356
    else return false;
 
7357
}
 
7358
 
 
7359
bool TLuaInterpreter::compileScript( QString & code )
 
7360
{
 
7361
    lua_State * L = pGlobalLua;
 
7362
    if( ! L )
 
7363
    {
 
7364
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7365
        return false;
 
7366
    }
 
7367
 
 
7368
    /*lua_newtable( L );
 
7369
 
 
7370
    // set values
 
7371
    for( int i=0; i<matches.size(); i++ )
 
7372
    {
 
7373
        lua_pushnumber( L, i+1 ); // Lua indexes start with 1
 
7374
        lua_pushstring( L, matches[i].toLatin1().data() );
 
7375
        lua_settable( L, -3 );
 
7376
    }
 
7377
    lua_setglobal( L, "matches" );*/
 
7378
 
 
7379
    int error = luaL_dostring( L, code.toLatin1().data() );
 
7380
    QString n;
 
7381
    if( error != 0 )
 
7382
    {
 
7383
        string e = "no error message available from Lua";
 
7384
        if( lua_isstring( L, 1 ) )
 
7385
        {
 
7386
            e = "Lua error:";
 
7387
            e+=lua_tostring( L, 1 );
 
7388
        }
 
7389
        if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::red))<<"LUA: code did not compile: ERROR:"<<e.c_str()<<"\n">>0;}
 
7390
    }
 
7391
    else
 
7392
    {
 
7393
        if( mudlet::debugMode ) {TDebug(QColor(Qt::white),QColor(Qt::darkGreen))<<"LUA: code compiled without errors. OK\n">>0;}
 
7394
    }
 
7395
    lua_pop( L, lua_gettop( L ) );
 
7396
 
 
7397
    if( error == 0 ) return true;
 
7398
    else return false;
 
7399
}
 
7400
 
 
7401
bool TLuaInterpreter::compile( QString & code )
 
7402
{
 
7403
    lua_State * L = pGlobalLua;
 
7404
    if( ! L )
 
7405
    {
 
7406
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7407
        return false;
 
7408
    }
 
7409
 
 
7410
    int error = luaL_dostring( L, code.toLatin1().data() );
 
7411
    QString n;
 
7412
    if( error != 0 )
 
7413
    {
 
7414
        string e = "no error message available from Lua";
 
7415
        if( lua_isstring( L, 1 ) )
 
7416
        {
 
7417
            e = "Lua error:";
 
7418
            e+=lua_tostring( L, 1 );
 
7419
        }
 
7420
        if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::red))<<"LUA: code did not compile: ERROR:"<<e.c_str()<<"\n">>0;}
 
7421
    }
 
7422
    else
 
7423
    {
 
7424
        if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::darkGreen))<<"LUA: code compiled without errors. OK\n" >> 0;}
 
7425
    }
 
7426
    lua_pop( L, lua_gettop( L ) );
 
7427
 
 
7428
    if( error == 0 ) return true;
 
7429
    else return false;
 
7430
}
 
7431
 
 
7432
bool TLuaInterpreter::compile( QString & code, QString & errorMsg )
 
7433
{
 
7434
    lua_State * L = pGlobalLua;
 
7435
    if( ! L )
 
7436
    {
 
7437
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7438
        return false;
 
7439
    }
 
7440
 
 
7441
    int error = luaL_dostring( L, code.toLatin1().data() );
 
7442
    QString n;
 
7443
    if( error != 0 )
 
7444
    {
 
7445
        string e = "Lua syntax error:";
 
7446
        if( lua_isstring( L, 1 ) )
 
7447
        {
 
7448
            e.append( lua_tostring( L, 1 ) );
 
7449
        }
 
7450
        errorMsg = "<b><font color='blue'>";
 
7451
        errorMsg.append( e.c_str() );
 
7452
        errorMsg.append("</b></font>");
 
7453
        if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::red))<<"\n "<<e.c_str()<<"\n">>0;}
 
7454
    }
 
7455
    else
 
7456
    {
 
7457
        if( mudlet::debugMode ) {TDebug(QColor(Qt::white),QColor(Qt::red))<<"\nLUA: code compiled without errors. OK\n" >> 0;}
 
7458
    }
 
7459
    lua_pop( L, lua_gettop( L ) );
 
7460
 
 
7461
    if( error == 0 ) return true;
 
7462
    else return false;
 
7463
}
 
7464
 
 
7465
void TLuaInterpreter::setMultiCaptureGroups( const std::list< std::list<std::string> > & captureList,
 
7466
                                             const std::list< std::list<int> > & posList )
 
7467
{
 
7468
    mMultiCaptureGroupList = captureList;
 
7469
    mMultiCaptureGroupPosList = posList;
 
7470
 
 
7471
    /*std::list< std::list<string> >::const_iterator mit = mMultiCaptureGroupList.begin();
 
7472
 
 
7473
    int k=1;
 
7474
    for( ; mit!=mMultiCaptureGroupList.end(); mit++, k++ )
 
7475
    {
 
7476
        cout << "regex#"<<k<<" got:"<<endl;
 
7477
        std::list<string>::const_iterator it = (*mit).begin();
 
7478
        for( int i=1; it!=(*mit).end(); it++, i++ )
 
7479
        {
 
7480
            cout << i<<"#"<<"<"<<*it<<">"<<endl;
 
7481
        }
 
7482
        cout << "-----------------------------"<<endl;
 
7483
    }*/
 
7484
}
 
7485
 
 
7486
void TLuaInterpreter::setCaptureGroups( const std::list<std::string> & captureList, const std::list<int> & posList )
 
7487
{
 
7488
    mCaptureGroupList = captureList;
 
7489
    mCaptureGroupPosList = posList;
 
7490
 
 
7491
    /*std::list<string>::iterator it2 = mCaptureGroupList.begin();
 
7492
    std::list<int>::iterator it1 = mCaptureGroupPosList.begin();
 
7493
    int i=0;
 
7494
    for( ; it1!=mCaptureGroupPosList.end(); it1++, it2++, i++ )
 
7495
    {
 
7496
        cout << "group#"<<i<<" begin="<<*it1<<" len="<<(*it2).size()<<"word="<<*it2<<endl;
 
7497
    } */
 
7498
}
 
7499
 
 
7500
void TLuaInterpreter::clearCaptureGroups()
 
7501
{
 
7502
    mCaptureGroupList.clear();
 
7503
    mCaptureGroupPosList.clear();
 
7504
    mMultiCaptureGroupList.clear();
 
7505
    mMultiCaptureGroupPosList.clear();
 
7506
 
 
7507
    lua_State * L = pGlobalLua;
 
7508
    if( ! L )
 
7509
    {
 
7510
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7511
    }
 
7512
 
 
7513
    lua_newtable( L );
 
7514
    lua_setglobal( L, "matches" );
 
7515
    lua_newtable( L );
 
7516
    lua_setglobal( L, "multimatches" );
 
7517
 
 
7518
    lua_pop( L, lua_gettop( L ) );
 
7519
}
 
7520
 
 
7521
 
 
7522
void TLuaInterpreter::adjustCaptureGroups( int x, int a )
 
7523
{
 
7524
    // adjust all capture group positions in line if data has been inserted by the user
 
7525
    typedef std::list<int>::iterator I;
 
7526
    for( I it=mCaptureGroupPosList.begin(); it!=mCaptureGroupPosList.end(); it++ )
 
7527
    {
 
7528
        if( *it >= x )
 
7529
        {
 
7530
            *it += a;
 
7531
        }
 
7532
    }
 
7533
}
 
7534
 
 
7535
void TLuaInterpreter::setAtcpTable( QString & var, QString & arg )
 
7536
{
 
7537
    lua_State * L = pGlobalLua;
 
7538
    lua_getglobal( L, "atcp" ); //defined in LuaGlobal.lua
 
7539
    lua_pushstring( L, var.toLatin1().data() );
 
7540
    lua_pushstring( L, arg.toLatin1().data() );
 
7541
    lua_rawset( L, -3 );
 
7542
    lua_pop( L, 1 );
 
7543
 
 
7544
    TEvent event;
 
7545
    event.mArgumentList.append( var );
 
7546
    event.mArgumentTypeList.append( ARGUMENT_TYPE_STRING );
 
7547
    event.mArgumentList.append( arg );
 
7548
    event.mArgumentTypeList.append( ARGUMENT_TYPE_STRING );
 
7549
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7550
    pHost->raiseEvent( & event );
 
7551
}
 
7552
 
 
7553
 
 
7554
void TLuaInterpreter::setGMCPTable(QString & key, QString & string_data)
 
7555
{
 
7556
    lua_State * L = pGlobalLua;
 
7557
    lua_getglobal(L, "gmcp");   //defined in Lua init
 
7558
    if( !lua_istable(L, -1) )
 
7559
    {
 
7560
        qDebug()<<"ERROR: gmcp not defined -> error in LuaGlobal.lua";
 
7561
        return;
 
7562
    }
 
7563
    // key is in format of Blah.Blah or Blah.Blah.Bleh - we want to push & pre-create the tables as appriate
 
7564
 
 
7565
    QStringList tokenList = key.split(".");
 
7566
    if( ! lua_checkstack( L, tokenList.size()+5 ) ) return;
 
7567
    int i = 0;
 
7568
    for( ; i<tokenList.size()-1; i++ )
 
7569
    {
 
7570
        lua_getfield(L, -1, tokenList[i].toLatin1().data());
 
7571
        if( !lua_istable(L, -1) )
 
7572
        {
 
7573
            lua_pop(L, 1);
 
7574
            lua_pushstring(L, tokenList[i].toLatin1().data());
 
7575
            lua_newtable(L);
 
7576
            lua_rawset(L, -3);
 
7577
            lua_getfield(L, -1, tokenList[i].toLatin1().data());
 
7578
        }
 
7579
        lua_remove(L, -2);
 
7580
    }
 
7581
    bool __needMerge = false;
 
7582
    lua_getfield(L, -1, tokenList[i].toLatin1().data());
 
7583
    if( lua_istable(L, -1) )
 
7584
    {
 
7585
        // only merge tables (instead of replacing them) if the key has been registered as a need to merge key by the user default is Char.Status only
 
7586
        if( mpHost->mGMCP_merge_table_keys.contains( key ) )
 
7587
        {
 
7588
            __needMerge = true;
 
7589
        }
 
7590
    }
 
7591
    lua_pop( L, 1 );
 
7592
    if( ! __needMerge )
 
7593
        lua_pushstring(L, tokenList[i].toLatin1().data());
 
7594
    else
 
7595
        lua_pushstring(L, "__needMerge");
 
7596
 
 
7597
    lua_getglobal(L, "json_to_value");
 
7598
 
 
7599
    if( !lua_isfunction(L, -1) )
 
7600
    {
 
7601
        lua_settop(L, 0);
 
7602
        qDebug()<<"CRITICAL ERROR: json_to_value not defined";
 
7603
        return;
 
7604
    }
 
7605
    lua_pushlstring(L, string_data.toLatin1().data(), string_data.size());
 
7606
    int error = lua_pcall(L, 1, 1, 0);
 
7607
    if( error == 0 )
 
7608
    {
 
7609
        // Top of stack should now contain the lua representation of json.
 
7610
        lua_rawset(L, -3);
 
7611
        if( __needMerge )
 
7612
        {
 
7613
            lua_settop(L, 0);
 
7614
            lua_getglobal(L, "__gmcp_merge_gmcp_sub_tables");
 
7615
            if( !lua_isfunction(L, -1) )
 
7616
            {
 
7617
               lua_settop(L, 0);
 
7618
               qDebug()<<"CRITICAL ERROR: __gmcp_merge_gmcp_sub_tables is not defined in lua_LuaGlobal.lua";
 
7619
               return;
 
7620
            }
 
7621
            lua_getglobal(L, "gmcp");
 
7622
            i = 0;
 
7623
            for( ; i<tokenList.size()-1; i++ )
 
7624
            {
 
7625
                lua_getfield(L, -1, tokenList[i].toLatin1().data());
 
7626
                lua_remove(L, -2);
 
7627
            }
 
7628
            lua_pushstring( L, tokenList[i].toLatin1().data());
 
7629
            lua_pcall(L, 2, 0, 0);
 
7630
        }
 
7631
    }
 
7632
    lua_settop(L, 0);
 
7633
 
 
7634
    // events: for key "foo.bar.top" we raise: gmcp.foo, gmcp.foo.bar and gmcp.foo.bar.top
 
7635
    // with the actual key given as parameter e.g. event=gmcp.foo, param="gmcp.foo.bar"
 
7636
 
 
7637
    QString token = "gmcp";
 
7638
    key.prepend("gmcp.");
 
7639
    for( int k=0; k<tokenList.size(); k++ )
 
7640
    {
 
7641
        TEvent event;
 
7642
        token.append( "." );
 
7643
        token.append( tokenList[k] );
 
7644
        event.mArgumentList.append( token );
 
7645
        event.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
 
7646
        event.mArgumentList.append( key );
 
7647
        event.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
 
7648
        Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7649
        if( mudlet::debugMode )
 
7650
        {
 
7651
            QString msg = "\nGMCP event <";
 
7652
            msg.append( token );
 
7653
            msg.append("> display(gmcp) to see the full content\n");
 
7654
            pHost->mpConsole->printSystemMessage(msg);
 
7655
        }
 
7656
        pHost->raiseEvent( &event );
 
7657
    }
 
7658
    // auto-detect IRE composer
 
7659
    if( tokenList.size() == 3 && tokenList.at(0) == "IRE" && tokenList.at(1) == "Composer" && tokenList.at(2) == "Edit")
 
7660
    {
 
7661
        QRegExp rx("\\{ \"title\": \"(.*)\", \"text\": \"(.*)\" \\}");
 
7662
        if( rx.indexIn(string_data) != -1 )
 
7663
        {
 
7664
            QString title = rx.cap(1);
 
7665
            QString initialText = rx.cap(2);
 
7666
            initialText.replace(QString("\\n"), QString("\n"));
 
7667
            Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7668
            if( pHost->mTelnet.mpComposer ) return;
 
7669
            pHost->mTelnet.mpComposer = new dlgComposer( pHost );
 
7670
            pHost->mTelnet.mpComposer->init( title, initialText );
 
7671
            pHost->mTelnet.mpComposer->raise();
 
7672
            pHost->mTelnet.mpComposer->show();
 
7673
        }
 
7674
    }
 
7675
    lua_pop( L, lua_gettop( L ) );
 
7676
}
 
7677
 
 
7678
void TLuaInterpreter::setChannel102Table( int & var, int & arg )
 
7679
{
 
7680
    lua_State * L = pGlobalLua;
 
7681
    lua_getglobal( L, "channel102" ); //defined in LuaGlobal.lua
 
7682
    lua_pushnumber( L, var );
 
7683
    lua_pushnumber( L, arg );
 
7684
    lua_rawset( L, -3 );
 
7685
    lua_pop( L, 1 );
 
7686
 
 
7687
    TEvent event;
 
7688
    QString e = "channel102Message";
 
7689
    event.mArgumentList.append( e );
 
7690
    event.mArgumentTypeList.append( ARGUMENT_TYPE_STRING );
 
7691
    event.mArgumentList.append( QString::number(var) );
 
7692
    event.mArgumentTypeList.append( ARGUMENT_TYPE_NUMBER );
 
7693
    event.mArgumentList.append( QString::number(arg) );
 
7694
    event.mArgumentTypeList.append( ARGUMENT_TYPE_NUMBER );
 
7695
    Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
 
7696
    pHost->raiseEvent( & event );
 
7697
}
 
7698
 
 
7699
bool TLuaInterpreter::call_luafunction( void * pT )
 
7700
{
 
7701
    lua_State * L = pGlobalLua;
 
7702
    lua_pushlightuserdata( L, pT );
 
7703
    lua_gettable( L, LUA_REGISTRYINDEX );
 
7704
    if( lua_isfunction( L, -1 ) )
 
7705
    {
 
7706
        int error = lua_pcall( L, 0, LUA_MULTRET, 0 );
 
7707
        if( error != 0 )
 
7708
        {
 
7709
            int nbpossible_errors = lua_gettop(L);
 
7710
            for (int i=1; i<=nbpossible_errors; i++)
 
7711
            {
 
7712
                string e = "";
 
7713
                if(lua_isstring( L, i) )
 
7714
                {
 
7715
                    e = "Lua error:";
 
7716
                    e+=lua_tostring( L, i );
 
7717
                    QString _n = "error in anonymous Lua function";
 
7718
                    QString _n2 = "no debug data available";
 
7719
                    logError( e, _n, _n2 );
 
7720
                    if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::red))<<"LUA: ERROR running anonymous Lua function ERROR:"<<e.c_str()>>0;}
 
7721
                }
 
7722
            }
 
7723
        }
 
7724
        else
 
7725
        {
 
7726
            if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::darkGreen))<<"LUA OK anonymous Lua function ran without errors\n">>0;}
 
7727
        }
 
7728
        lua_pop( L, lua_gettop( L ) );
 
7729
        //lua_settop(L, 0);
 
7730
        if( error == 0 )
 
7731
            return true;
 
7732
        else
 
7733
            return false;
 
7734
    }
 
7735
    else
 
7736
    {
 
7737
        QString _n = "error in anonymous Lua function";
 
7738
        QString _n2 = "func reference not found by Lua, func can not be called";
 
7739
        string e = "Lua error:";
 
7740
        logError( e, _n, _n2 );
 
7741
    }
 
7742
 
 
7743
    return false;
 
7744
}
 
7745
 
 
7746
 
 
7747
bool TLuaInterpreter::call( QString & function, QString & mName )
 
7748
{
 
7749
    lua_State * L = pGlobalLua;
 
7750
    if( ! L )
 
7751
    {
 
7752
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7753
        return false;
 
7754
    }
 
7755
 
 
7756
    if( mCaptureGroupList.size() > 0 )
 
7757
    {
 
7758
        lua_newtable( L );
 
7759
 
 
7760
        // set values
 
7761
        int i=1; // Lua indexes start with 1 as a general convention
 
7762
        std::list<std::string>::iterator it = mCaptureGroupList.begin();
 
7763
        for( ; it!=mCaptureGroupList.end(); it++, i++ )
 
7764
        {
 
7765
            //if( (*it).length() < 1 ) continue; //have empty capture groups to be undefined keys i.e. machts[emptyCapGroupNumber] = nil otherwise it's = "" i.e. an empty string
 
7766
            lua_pushnumber( L, i );
 
7767
            lua_pushstring( L, (*it).c_str() );
 
7768
            lua_settable( L, -3 );
 
7769
        }
 
7770
        lua_setglobal( L, "matches" );
 
7771
    }
 
7772
 
 
7773
    lua_getglobal( L, function.toLatin1().data() );
 
7774
    lua_getfield( L, LUA_GLOBALSINDEX, function.toLatin1().data() );
 
7775
    int error = lua_pcall( L, 0, LUA_MULTRET, 0 );
 
7776
    if( error != 0 )
 
7777
    {
 
7778
        int nbpossible_errors = lua_gettop(L);
 
7779
        for (int i=1; i<=nbpossible_errors; i++)
 
7780
        {
 
7781
            string e = "";
 
7782
            if(lua_isstring( L, i) )
 
7783
            {
 
7784
                e += lua_tostring( L, i );
 
7785
                logError( e, mName, function );
 
7786
                if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::red))<<"LUA: ERROR running script "<< mName << " (" << function <<") ERROR:"<<e.c_str()>>0;}
 
7787
            }
 
7788
        }
 
7789
    }
 
7790
    else
 
7791
    {
 
7792
        if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::darkGreen))<<"LUA OK script "<<mName << " (" << function <<") ran without errors\n">>0;}
 
7793
    }
 
7794
    lua_pop( L, lua_gettop( L ) );
 
7795
    if( error == 0 ) return true;
 
7796
    else return false;
 
7797
}
 
7798
 
 
7799
void TLuaInterpreter::logError( std::string & e, QString & name, QString & function )
 
7800
{
 
7801
    //QDateTime time = QDateTime::currentDateTime();
 
7802
    // QString entry = QString("[%1]object:<%2> function:<%3> error:<%4>").arg(time.toString("MMM:dd:yyyy hh-mm-ss")).arg(name).arg(function).arg(e.c_str());
 
7803
    //mpHost->mErrorLogStream << entry << endl;
 
7804
    QColor blue = QColor(0,0,255);
 
7805
    QColor green = QColor(0,255,0);
 
7806
    QColor red = QColor(255,0,0);
 
7807
    QColor black = QColor(0,0,0);
 
7808
    QString s1 = QString("[ERROR:]");
 
7809
    QString s2 = QString(" object:<%1> function:<%2>\n").arg(name).arg(function);
 
7810
    QString s3 = QString("         <%1>\n").arg(e.c_str());
 
7811
    if( mpHost->mpEditorDialog )
 
7812
    {
 
7813
        mpHost->mpEditorDialog->mpErrorConsole->printDebug(blue, black, s1 );
 
7814
        mpHost->mpEditorDialog->mpErrorConsole->printDebug(green, black, s2 );
 
7815
        mpHost->mpEditorDialog->mpErrorConsole->printDebug(red, black, s3 );
 
7816
    }
 
7817
 
 
7818
}
 
7819
 
 
7820
bool TLuaInterpreter::callConditionFunction( std::string & function, QString & mName )
 
7821
{
 
7822
    lua_State * L = pGlobalLua;
 
7823
    if( ! L )
 
7824
    {
 
7825
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7826
        return false;
 
7827
    }
 
7828
 
 
7829
    lua_getfield( L, LUA_GLOBALSINDEX, function.c_str() );
 
7830
    int error = lua_pcall( L, 0, 1, 0 );
 
7831
    if( error != 0 )
 
7832
    {
 
7833
        int nbpossible_errors = lua_gettop(L);
 
7834
        for (int i=1; i<=nbpossible_errors; i++)
 
7835
        {
 
7836
            string e = "";
 
7837
            if(lua_isstring( L, i) )
 
7838
            {
 
7839
                e+=lua_tostring( L, i );
 
7840
                QString _f = function.c_str();
 
7841
                logError( e, mName, _f );
 
7842
                if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::red))<<"LUA: ERROR running script "<< mName << " (" << function.c_str() <<") ERROR:"<<e.c_str()<<"\n">>0;}
 
7843
            }
 
7844
        }
 
7845
    }
 
7846
    else
 
7847
    {
 
7848
        if( mudlet::debugMode ){ TDebug(QColor(Qt::white),QColor(Qt::darkGreen))<<"LUA OK script "<<mName << " (" << function.c_str() <<") ran without errors\n">>0;}
 
7849
    }
 
7850
 
 
7851
    int ret = 0;
 
7852
    int returnValues = lua_gettop( L );
 
7853
    if( returnValues > 0 )
 
7854
    {
 
7855
        if( lua_isboolean( L, 1 ) )
 
7856
        {
 
7857
            ret = lua_toboolean( L, 1 );
 
7858
        }
 
7859
    }
 
7860
    lua_pop( L, returnValues );
 
7861
    if( (error == 0) && (ret>0) )
 
7862
    {
 
7863
        return true;
 
7864
    }
 
7865
    else
 
7866
    {
 
7867
        return false;
 
7868
    }
 
7869
}
 
7870
 
 
7871
bool TLuaInterpreter::callMulti( QString & function, QString & mName )
 
7872
{
 
7873
    lua_State * L = pGlobalLua;
 
7874
    if( ! L )
 
7875
    {
 
7876
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7877
        return false;
 
7878
    }
 
7879
 
 
7880
    if( mMultiCaptureGroupList.size() > 0 )
 
7881
    {
 
7882
        int k=1; // Lua indexes start with 1 as a general convention
 
7883
        std::list< std::list<std::string> >::iterator mit = mMultiCaptureGroupList.begin();
 
7884
        lua_newtable( L );//multimatches
 
7885
        for( ; mit!=mMultiCaptureGroupList.end(); mit++, k++ )
 
7886
        {
 
7887
            // multimatches{ trigger_idx{ table_matches{ ... } } }
 
7888
            lua_pushnumber( L, k );
 
7889
            lua_newtable( L );//regex-value => table matches
 
7890
            int i=1; // Lua indexes start with 1 as a general convention
 
7891
            std::list<std::string>::iterator it = (*mit).begin();
 
7892
            for( ; it!=(*mit).end(); it++, i++ )
 
7893
            {
 
7894
                lua_pushnumber( L, i );
 
7895
                lua_pushstring( L, (*it).c_str() );
 
7896
                lua_settable( L, -3 );//match in matches
 
7897
            }
 
7898
            lua_settable( L, -3 );//matches in regex
 
7899
        }
 
7900
        lua_setglobal( L, "multimatches" );
 
7901
    }
 
7902
 
 
7903
    lua_getglobal( L, function.toLatin1().data() );
 
7904
    lua_getfield( L, LUA_GLOBALSINDEX, function.toLatin1().data() );
 
7905
    int error = lua_pcall( L, 0, LUA_MULTRET, 0 );
 
7906
    if( error != 0 )
 
7907
    {
 
7908
        int nbpossible_errors = lua_gettop(L);
 
7909
        for (int i=1; i<=nbpossible_errors; i++)
 
7910
        {
 
7911
            string e = "";
 
7912
            if(lua_isstring( L, i) )
 
7913
            {
 
7914
                e += lua_tostring( L, i );
 
7915
                logError( e, mName, function );
 
7916
                if( mudlet::debugMode ) {TDebug(QColor(Qt::white),QColor(Qt::red))<<"LUA: ERROR running script "<< mName << " (" << function <<") ERROR:"<<e.c_str()<<"\n">>0;}
 
7917
            }
 
7918
        }
 
7919
    }
 
7920
    else
 
7921
    {
 
7922
        if( mudlet::debugMode ) {TDebug(QColor(Qt::white),QColor(Qt::darkGreen))<<"LUA OK script "<<mName << " (" << function <<") ran without errors\n">>0;}
 
7923
    }
 
7924
    lua_pop( L, lua_gettop( L ) );
 
7925
    if( error == 0 ) return true;
 
7926
    else return false;
 
7927
}
 
7928
 
 
7929
 
 
7930
bool TLuaInterpreter::callEventHandler( QString & function, TEvent * pE )
 
7931
{
 
7932
    lua_State * L = pGlobalLua;
 
7933
    lua_getglobal( L, function.toLatin1().data() );
 
7934
    lua_getfield( L, LUA_GLOBALSINDEX, function.toLatin1().data() );
 
7935
    for( int i=0; i<pE->mArgumentList.size(); i++ )
 
7936
    {
 
7937
        if( pE->mArgumentTypeList[i] == ARGUMENT_TYPE_NUMBER )
 
7938
        {
 
7939
            lua_pushnumber( L, pE->mArgumentList[i].toInt() );
 
7940
        }
 
7941
        else
 
7942
        {
 
7943
            lua_pushstring( L, pE->mArgumentList[i].toLatin1().data() );
 
7944
        }
 
7945
    }
 
7946
    int error = lua_pcall( L, pE->mArgumentList.size(), LUA_MULTRET, 0 );
 
7947
    if( error != 0 )
 
7948
    {
 
7949
        string e = "";
 
7950
        if(lua_isstring( L, 1) )
 
7951
        {
 
7952
            e+=lua_tostring( L, 1 );
 
7953
        }
 
7954
        QString _n = "event handler function";
 
7955
        logError( e, _n, function );
 
7956
        if( mudlet::debugMode ) {TDebug(QColor(Qt::white),QColor(Qt::red))<<"LUA: ERROR running script "<< function << " (" << function <<") ERROR:"<<e.c_str()<<"\n">>0;}
 
7957
    }
 
7958
    lua_pop( L, lua_gettop( L ) );
 
7959
    if( error == 0 ) return true;
 
7960
    else return false;
 
7961
}
 
7962
 
 
7963
 
 
7964
void TLuaInterpreter::set_lua_table( QString & tableName, QStringList & variableList )
 
7965
{
 
7966
    lua_State * L = pGlobalLua;
 
7967
    if( ! L )
 
7968
    {
 
7969
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7970
        return;
 
7971
    }
 
7972
    lua_newtable(L);
 
7973
    for( int i=0; i<variableList.size(); i++ )
 
7974
    {
 
7975
        lua_pushnumber( L, i+1 ); // Lua indexes start with 1
 
7976
        lua_pushstring( L, variableList[i].toLatin1().data() );
 
7977
        lua_settable( L, -3 );
 
7978
    }
 
7979
    lua_setglobal( L, tableName.toLatin1().data() );
 
7980
    lua_pop( pGlobalLua, lua_gettop( pGlobalLua ) );
 
7981
}
 
7982
 
 
7983
void TLuaInterpreter::set_lua_string( const QString & varName, QString & varValue )
 
7984
{
 
7985
    lua_State * L = pGlobalLua;
 
7986
    if( ! L )
 
7987
    {
 
7988
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
7989
        return;
 
7990
    }
 
7991
 
 
7992
    //lua_pushstring( L, varName.toLatin1().data() );
 
7993
    lua_pushstring( L, varValue.toLatin1().data() );
 
7994
    lua_setglobal( L, varName.toLatin1().data() );
 
7995
    //lua_setfield( L, LUA_GLOBALSINDEX, s )
 
7996
    lua_pop( pGlobalLua, lua_gettop( pGlobalLua ) );
 
7997
 
 
7998
//lua_settable( L, LUA_GLOBALSINDEX );
 
7999
}
 
8000
 
 
8001
QString TLuaInterpreter::get_lua_string( QString & stringName )
 
8002
{
 
8003
    lua_State * L = pGlobalLua;
 
8004
    if( ! L )
 
8005
    {
 
8006
        qDebug()<< "LUA CRITICAL ERROR: no suitable Lua execution unit found.";
 
8007
        return QString( "LUA CRITICAL ERROR" );
 
8008
    }
 
8009
 
 
8010
    lua_getglobal( L, stringName.toLatin1().data() );
 
8011
    lua_getfield( L, LUA_GLOBALSINDEX, stringName.toLatin1().data() );
 
8012
    return QString( lua_tostring( L, 1 ) );
 
8013
}
 
8014
 
 
8015
int TLuaInterpreter::check_for_mappingscript()
 
8016
{
 
8017
    lua_State * L = pGlobalLua;
 
8018
    lua_getglobal(L, "mudlet");
 
8019
    if( !lua_istable(L, -1) ) {
 
8020
        lua_pop( L, 1 );
 
8021
        return 0;
 
8022
    }
 
8023
 
 
8024
    lua_getfield(L, -1, "mapper_script");
 
8025
    if( !lua_isboolean(L, -1) ) {
 
8026
        lua_pop( L, 2 );
 
8027
        return 0;
 
8028
    }
 
8029
 
 
8030
    int r = lua_toboolean(L, -1);
 
8031
    lua_pop( L, 2 );
 
8032
    return r;
 
8033
}
 
8034
 
 
8035
void TLuaInterpreter::threadLuaInterpreterExec( string code )
 
8036
{
 
8037
    /* cout << "TLuaMainThread::threadLuaInterpreterExec(code) executing following code:" << endl;
 
8038
     cout << "--------------------------------------------snip<" <<endl;
 
8039
     cout << code << endl;
 
8040
     cout << ">snip--------------------------------------------" <<endl;*/
 
8041
     lua_State * L = pGlobalLua;
 
8042
     int error = luaL_dostring(L,code.c_str());
 
8043
     QString n;
 
8044
     if( error != 0 )
 
8045
     {
 
8046
        string e = "no error message available from Lua";
 
8047
        if( lua_isstring( L, 1 ) )
 
8048
        {
 
8049
            e = "Lua error:";
 
8050
            e += lua_tostring( L, 1 );
 
8051
        }
 
8052
        emit signalEchoMessage( mHostID, QString( e.c_str() ) );
 
8053
        qDebug()<< "LUA_ERROR:"<<e.c_str();
 
8054
     }
 
8055
 
 
8056
     cout << "cRunningScript::threadLuaInterpreterExec() done" << endl;
 
8057
}
 
8058
 
 
8059
 
 
8060
 
 
8061
void TLuaInterpreter::startLuaSessionInterpreter()
 
8062
{
 
8063
    //connect(this,SIGNAL(signalOpenWindow(int,QString)), this,SLOT(slotOpenWindow(int,QString)));
 
8064
    //connect(this,SIGNAL(signalEchoWindow(int,QString,QString)), this,SLOT(slotEchoWindow(int,QString,QString)));
 
8065
    //connect(this,SIGNAL(signalClearWindow(int,QString)), this,SLOT(slotClearWindow(int,QString)));
 
8066
    //connect(this,SIGNAL(signalNewTrigger(QString,QString, int, QString)), this,SLOT(slotNewTrigger(QString,QString, int, QString)));
 
8067
    //connect(this,SIGNAL(signalAddTimer(int,int,QString,QString)),this,SLOT(slotAddTimer(int,int,QString,QString)));
 
8068
    //connect(this,SIGNAL(signalDeleteTrigger(int,QString)), this,SLOT(slotDeleteTrigger(int,QString)));
 
8069
 
 
8070
 
 
8071
    //connect(this,SIGNAL(signalEchoMessage(int,QString)), this,SLOT(slotEchoMessage(int,QString)));//,Qt::DirectConnection);
 
8072
    //connect(this,SIGNAL(signalNewEcho(int,QString)), this,SLOT(slotNewEcho(int,QString)));
 
8073
    //connect(this,SIGNAL(signalNewCommand(int,QString)), this,SLOT(slotNewCommand(int,QString)));//,Qt::QueuedConnection);
 
8074
 
 
8075
    mpLuaSessionThread = new TLuaMainThread(this);
 
8076
    mpLuaSessionThread->start(); //calls initLuaGlobals() to initialize the interpreter for this session
 
8077
}
 
8078
 
 
8079
#ifdef Q_OS_MAC
 
8080
    #include "luazip.c"
 
8081
#endif
 
8082
 
 
8083
// this function initializes the Lua Session interpreter.
 
8084
// on initialization of a new session *or* in case of an interpreter reset by the user.
 
8085
void TLuaInterpreter::initLuaGlobals()
 
8086
{
 
8087
    pGlobalLua = luaL_newstate();
 
8088
    TLuaInterpreter::luaInterpreterMap[pGlobalLua]=mpHost;
 
8089
 
 
8090
    luaL_openlibs( pGlobalLua );
 
8091
 
 
8092
    lua_pushstring( pGlobalLua, "SESSION" );
 
8093
    lua_pushnumber( pGlobalLua, mHostID );
 
8094
    lua_settable( pGlobalLua, LUA_GLOBALSINDEX );
 
8095
 
 
8096
    lua_pushstring( pGlobalLua, "SCRIPT_NAME" );
 
8097
    lua_pushstring( pGlobalLua, "Global Lua Session Interpreter" );
 
8098
    lua_settable( pGlobalLua, LUA_GLOBALSINDEX );
 
8099
 
 
8100
    lua_pushstring( pGlobalLua, "SCRIPT_ID" );
 
8101
    lua_pushnumber( pGlobalLua, -1 ); // ID 1 is used to indicate that this is the global Lua interpreter
 
8102
    lua_settable( pGlobalLua, LUA_GLOBALSINDEX );
 
8103
    lua_register( pGlobalLua, "showUnzipProgress", TLuaInterpreter::showUnzipProgress );//internal function used by the package system NOT FOR USERS
 
8104
    lua_register( pGlobalLua, "wait", TLuaInterpreter::Wait );
 
8105
    lua_register( pGlobalLua, "expandAlias", TLuaInterpreter::Send );
 
8106
    lua_register( pGlobalLua, "echo", TLuaInterpreter::Echo );
 
8107
    lua_register( pGlobalLua, "selectString", TLuaInterpreter::select );
 
8108
    lua_register( pGlobalLua, "selectSection", TLuaInterpreter::selectSection );
 
8109
    lua_register( pGlobalLua, "replace", TLuaInterpreter::replace );
 
8110
    lua_register( pGlobalLua, "setBgColor", TLuaInterpreter::setBgColor );
 
8111
    lua_register( pGlobalLua, "setFgColor", TLuaInterpreter::setFgColor );
 
8112
    lua_register( pGlobalLua, "tempTimer", TLuaInterpreter::tempTimer );
 
8113
    lua_register( pGlobalLua, "tempTrigger", TLuaInterpreter::tempTrigger );
 
8114
    lua_register( pGlobalLua, "tempRegexTrigger", TLuaInterpreter::tempRegexTrigger );
 
8115
    lua_register( pGlobalLua, "openUserWindow", TLuaInterpreter::openUserWindow );
 
8116
    lua_register( pGlobalLua, "echoUserWindow", TLuaInterpreter::echoUserWindow );
 
8117
    lua_register( pGlobalLua, "enableTimer", TLuaInterpreter::enableTimer );
 
8118
    lua_register( pGlobalLua, "disableTimer", TLuaInterpreter::disableTimer );
 
8119
    lua_register( pGlobalLua, "enableKey", TLuaInterpreter::enableKey );
 
8120
    lua_register( pGlobalLua, "disableKey", TLuaInterpreter::disableKey );
 
8121
    lua_register( pGlobalLua, "clearUserWindow", TLuaInterpreter::clearUserWindow );
 
8122
    lua_register( pGlobalLua, "clearWindow", TLuaInterpreter::clearUserWindow );
 
8123
    lua_register( pGlobalLua, "killTimer", TLuaInterpreter::killTimer );
 
8124
    lua_register( pGlobalLua, "moveCursor", TLuaInterpreter::moveCursor );
 
8125
    lua_register( pGlobalLua, "getLines", TLuaInterpreter::getLines );
 
8126
    lua_register( pGlobalLua, "getLineNumber", TLuaInterpreter::getLineNumber );
 
8127
    lua_register( pGlobalLua, "insertHTML", TLuaInterpreter::insertHTML );
 
8128
    lua_register( pGlobalLua, "insertText", TLuaInterpreter::insertText );
 
8129
    lua_register( pGlobalLua, "enableTrigger", TLuaInterpreter::enableTrigger );
 
8130
    lua_register( pGlobalLua, "disableTrigger", TLuaInterpreter::disableTrigger );
 
8131
    lua_register( pGlobalLua, "killTrigger", TLuaInterpreter::killTrigger );
 
8132
    lua_register( pGlobalLua, "getLineCount", TLuaInterpreter::getLineCount );
 
8133
    lua_register( pGlobalLua, "getColumnNumber", TLuaInterpreter::getColumnNumber );
 
8134
    //lua_register( pGlobalLua, "getBufferTable", TLuaInterpreter::getBufferTable );
 
8135
    //lua_register( pGlobalLua, "getBufferLine", TLuaInterpreter::getBufferLine );
 
8136
    lua_register( pGlobalLua, "send", TLuaInterpreter::sendRaw );
 
8137
    lua_register( pGlobalLua, "selectCaptureGroup", TLuaInterpreter::selectCaptureGroup );
 
8138
    lua_register( pGlobalLua, "tempLineTrigger", TLuaInterpreter::tempLineTrigger );
 
8139
    lua_register( pGlobalLua, "raiseEvent", TLuaInterpreter::raiseEvent );
 
8140
    lua_register( pGlobalLua, "deleteLine", TLuaInterpreter::deleteLine );
 
8141
    lua_register( pGlobalLua, "copy", TLuaInterpreter::copy );
 
8142
    lua_register( pGlobalLua, "cut", TLuaInterpreter::cut );
 
8143
    lua_register( pGlobalLua, "paste", TLuaInterpreter::paste );
 
8144
    lua_register( pGlobalLua, "pasteWindow", TLuaInterpreter::pasteWindow );
 
8145
    //lua_register( pGlobalLua, "userWindowLineWrap", TLuaInterpreter::userWindowLineWrap );
 
8146
    lua_register( pGlobalLua, "debugc", TLuaInterpreter::debug );
 
8147
    lua_register( pGlobalLua, "setWindowWrap", TLuaInterpreter::setWindowWrap );
 
8148
    lua_register( pGlobalLua, "setWindowWrapIndent", TLuaInterpreter::setWindowWrapIndent );
 
8149
    lua_register( pGlobalLua, "resetFormat", TLuaInterpreter::reset );
 
8150
    lua_register( pGlobalLua, "moveCursorEnd", TLuaInterpreter::moveCursorEnd );
 
8151
    lua_register( pGlobalLua, "getLastLineNumber", TLuaInterpreter::getLastLineNumber );
 
8152
    lua_register( pGlobalLua, "getNetworkLatency", TLuaInterpreter::getNetworkLatency );
 
8153
    lua_register( pGlobalLua, "createMiniConsole", TLuaInterpreter::createMiniConsole );
 
8154
    lua_register( pGlobalLua, "createLabel", TLuaInterpreter::createLabel );
 
8155
    lua_register( pGlobalLua, "hideWindow", TLuaInterpreter::hideUserWindow );
 
8156
    lua_register( pGlobalLua, "showWindow", TLuaInterpreter::showUserWindow );
 
8157
    lua_register( pGlobalLua, "createBuffer", TLuaInterpreter::createBuffer );
 
8158
    lua_register( pGlobalLua, "createStopWatch", TLuaInterpreter::createStopWatch );
 
8159
    lua_register( pGlobalLua, "getStopWatchTime", TLuaInterpreter::getStopWatchTime );
 
8160
    lua_register( pGlobalLua, "stopStopWatch", TLuaInterpreter::stopStopWatch );
 
8161
    lua_register( pGlobalLua, "startStopWatch", TLuaInterpreter::startStopWatch );
 
8162
    lua_register( pGlobalLua, "resetStopWatch", TLuaInterpreter::resetStopWatch );
 
8163
    lua_register( pGlobalLua, "closeUserWindow", TLuaInterpreter::closeUserWindow );
 
8164
    lua_register( pGlobalLua, "resizeWindow", TLuaInterpreter::resizeUserWindow );
 
8165
    lua_register( pGlobalLua, "appendBuffer", TLuaInterpreter::appendBuffer );
 
8166
    lua_register( pGlobalLua, "setBackgroundImage", TLuaInterpreter::setBackgroundImage );
 
8167
    lua_register( pGlobalLua, "setBackgroundColor", TLuaInterpreter::setBackgroundColor );
 
8168
    lua_register( pGlobalLua, "createButton", TLuaInterpreter::createButton );
 
8169
    lua_register( pGlobalLua, "setLabelClickCallback", TLuaInterpreter::setLabelClickCallback );
 
8170
    lua_register( pGlobalLua, "moveWindow", TLuaInterpreter::moveWindow );
 
8171
    lua_register( pGlobalLua, "setTextFormat", TLuaInterpreter::setTextFormat );
 
8172
    lua_register( pGlobalLua, "getMainWindowSize", TLuaInterpreter::getMainWindowSize );
 
8173
    lua_register( pGlobalLua, "getCurrentLine", TLuaInterpreter::getCurrentLine );
 
8174
    lua_register( pGlobalLua, "setMiniConsoleFontSize", TLuaInterpreter::setMiniConsoleFontSize );
 
8175
    lua_register( pGlobalLua, "selectCurrentLine", TLuaInterpreter::selectCurrentLine );
 
8176
    lua_register( pGlobalLua, "spawn", TLuaInterpreter::spawn );
 
8177
    lua_register( pGlobalLua, "getButtonState", TLuaInterpreter::getButtonState );
 
8178
    lua_register( pGlobalLua, "showToolBar", TLuaInterpreter::showToolBar );
 
8179
    lua_register( pGlobalLua, "hideToolBar", TLuaInterpreter::hideToolBar );
 
8180
    lua_register( pGlobalLua, "loadRawFile", TLuaInterpreter::loadRawFile );
 
8181
    lua_register( pGlobalLua, "setBold", TLuaInterpreter::setBold );
 
8182
    lua_register( pGlobalLua, "setItalics", TLuaInterpreter::setItalics );
 
8183
    lua_register( pGlobalLua, "setUnderline", TLuaInterpreter::setUnderline );
 
8184
    lua_register( pGlobalLua, "disconnect", TLuaInterpreter::disconnect );
 
8185
    lua_register( pGlobalLua, "reconnect", TLuaInterpreter::reconnect );
 
8186
    lua_register( pGlobalLua, "getMudletHomeDir", TLuaInterpreter::getMudletHomeDir );
 
8187
    lua_register( pGlobalLua, "setTriggerStayOpen", TLuaInterpreter::setTriggerStayOpen );
 
8188
    lua_register( pGlobalLua, "wrapLine", TLuaInterpreter::wrapLine );
 
8189
    lua_register( pGlobalLua, "getFgColor", TLuaInterpreter::getFgColor );
 
8190
    lua_register( pGlobalLua, "getBgColor", TLuaInterpreter::getBgColor );
 
8191
    lua_register( pGlobalLua, "tempColorTrigger", TLuaInterpreter::tempColorTrigger );
 
8192
    lua_register( pGlobalLua, "isAnsiFgColor", TLuaInterpreter::isAnsiFgColor );
 
8193
    lua_register( pGlobalLua, "isAnsiBgColor", TLuaInterpreter::isAnsiBgColor );
 
8194
    lua_register( pGlobalLua, "playSoundFile", TLuaInterpreter::playSoundFile );
 
8195
    lua_register( pGlobalLua, "setBorderTop", TLuaInterpreter::setBorderTop );
 
8196
    lua_register( pGlobalLua, "setBorderBottom", TLuaInterpreter::setBorderBottom );
 
8197
    lua_register( pGlobalLua, "setBorderLeft", TLuaInterpreter::setBorderLeft );
 
8198
    lua_register( pGlobalLua, "setBorderRight", TLuaInterpreter::setBorderRight );
 
8199
    lua_register( pGlobalLua, "setBorderColor", TLuaInterpreter::setBorderColor );
 
8200
    lua_register( pGlobalLua, "setConsoleBufferSize", TLuaInterpreter::setConsoleBufferSize );
 
8201
    lua_register( pGlobalLua, "startLogging", TLuaInterpreter::startLogging );
 
8202
    lua_register( pGlobalLua, "calcFontSize", TLuaInterpreter::calcFontSize );
 
8203
    lua_register( pGlobalLua, "permRegexTrigger", TLuaInterpreter::permRegexTrigger );
 
8204
    lua_register( pGlobalLua, "permSubstringTrigger", TLuaInterpreter::permSubstringTrigger );
 
8205
    lua_register( pGlobalLua, "permBeginOfLineStringTrigger", TLuaInterpreter::permBeginOfLineStringTrigger );
 
8206
    lua_register( pGlobalLua, "permTimer", TLuaInterpreter::permTimer );
 
8207
    lua_register( pGlobalLua, "permAlias", TLuaInterpreter::permAlias );
 
8208
    lua_register( pGlobalLua, "exists", TLuaInterpreter::exists );
 
8209
    lua_register( pGlobalLua, "isActive", TLuaInterpreter::isActive );
 
8210
    lua_register( pGlobalLua, "enableAlias", TLuaInterpreter::enableAlias );
 
8211
    lua_register( pGlobalLua, "tempAlias", TLuaInterpreter::tempAlias );
 
8212
    lua_register( pGlobalLua, "disableAlias", TLuaInterpreter::disableAlias );
 
8213
    lua_register( pGlobalLua, "killAlias", TLuaInterpreter::killAlias );
 
8214
    lua_register( pGlobalLua, "setLabelStyleSheet", TLuaInterpreter::setLabelStyleSheet );
 
8215
    lua_register( pGlobalLua, "getTime", TLuaInterpreter::getTime );
 
8216
    lua_register( pGlobalLua, "invokeFileDialog", TLuaInterpreter::invokeFileDialog );
 
8217
    lua_register( pGlobalLua, "getTimestamp", TLuaInterpreter::getTimestamp );
 
8218
    lua_register( pGlobalLua, "setLink", TLuaInterpreter::setLink );
 
8219
    lua_register( pGlobalLua, "deselect", TLuaInterpreter::deselect );
 
8220
    lua_register( pGlobalLua, "insertLink", TLuaInterpreter::insertLink );
 
8221
    lua_register( pGlobalLua, "echoLink", TLuaInterpreter::echoLink );
 
8222
    lua_register( pGlobalLua, "echoPopup", TLuaInterpreter::echoPopup );
 
8223
    lua_register( pGlobalLua, "insertPopup", TLuaInterpreter::insertPopup );
 
8224
    lua_register( pGlobalLua, "setPopup", TLuaInterpreter::setPopup );
 
8225
    lua_register( pGlobalLua, "sendATCP", TLuaInterpreter::sendATCP );
 
8226
    lua_register( pGlobalLua, "hasFocus", TLuaInterpreter::hasFocus );
 
8227
    lua_register( pGlobalLua, "isPrompt", TLuaInterpreter::isPrompt );
 
8228
    lua_register( pGlobalLua, "feedTriggers", TLuaInterpreter::feedTriggers );
 
8229
    lua_register( pGlobalLua, "sendTelnetChannel102", TLuaInterpreter::sendTelnetChannel102 );
 
8230
    lua_register( pGlobalLua, "setRoomWeight", TLuaInterpreter::setRoomWeight );
 
8231
    lua_register( pGlobalLua, "getRoomWeight", TLuaInterpreter::getRoomWeight );
 
8232
    lua_register( pGlobalLua, "gotoRoom", TLuaInterpreter::gotoRoom );
 
8233
    lua_register( pGlobalLua, "setMapperView", TLuaInterpreter::setMapperView );
 
8234
    lua_register( pGlobalLua, "getRoomExits", TLuaInterpreter::getRoomExits );
 
8235
    lua_register( pGlobalLua, "lockRoom", TLuaInterpreter::lockRoom );
 
8236
    lua_register( pGlobalLua, "createMapper", TLuaInterpreter::createMapper );
 
8237
    lua_register( pGlobalLua, "getMainConsoleWidth", TLuaInterpreter::getMainConsoleWidth );
 
8238
    lua_register( pGlobalLua, "resetProfile", TLuaInterpreter::resetProfile );
 
8239
    lua_register( pGlobalLua, "printCmdLine", TLuaInterpreter::printCmdLine );
 
8240
    lua_register( pGlobalLua, "searchRoom", TLuaInterpreter::searchRoom );
 
8241
    lua_register( pGlobalLua, "clearCmdLine", TLuaInterpreter::clearCmdLine );
 
8242
    lua_register( pGlobalLua, "getAreaTable", TLuaInterpreter::getAreaTable );
 
8243
    lua_register( pGlobalLua, "getAreaRooms", TLuaInterpreter::getAreaRooms );
 
8244
    lua_register( pGlobalLua, "getPath", TLuaInterpreter::getPath );
 
8245
    lua_register( pGlobalLua, "centerview", TLuaInterpreter::centerview );
 
8246
    lua_register( pGlobalLua, "denyCurrentSend", TLuaInterpreter::denyCurrentSend );
 
8247
    lua_register( pGlobalLua, "tempBeginOfLineTrigger", TLuaInterpreter::tempBeginOfLineTrigger );
 
8248
    lua_register( pGlobalLua, "tempExactMatchTrigger", TLuaInterpreter::tempExactMatchTrigger );
 
8249
    lua_register( pGlobalLua, "sendGMCP", TLuaInterpreter::sendGMCP );
 
8250
    lua_register( pGlobalLua, "roomExists", TLuaInterpreter::roomExists );
 
8251
    lua_register( pGlobalLua, "addRoom", TLuaInterpreter::addRoom );
 
8252
    lua_register( pGlobalLua, "setExit", TLuaInterpreter::setExit );
 
8253
    lua_register( pGlobalLua, "setRoomCoordinates", TLuaInterpreter::setRoomCoordinates );
 
8254
    lua_register( pGlobalLua, "getRoomCoordinates", TLuaInterpreter::getRoomCoordinates );
 
8255
    lua_register( pGlobalLua, "createRoomID", TLuaInterpreter::createRoomID );
 
8256
    lua_register( pGlobalLua, "getRoomArea", TLuaInterpreter::getRoomArea );
 
8257
    lua_register( pGlobalLua, "setRoomArea", TLuaInterpreter::setRoomArea );
 
8258
    lua_register( pGlobalLua, "setAreaName", TLuaInterpreter::setAreaName );
 
8259
    lua_register( pGlobalLua, "roomLocked", TLuaInterpreter::roomLocked );
 
8260
    lua_register( pGlobalLua, "setCustomEnvColor", TLuaInterpreter::setCustomEnvColor );
 
8261
    lua_register( pGlobalLua, "getCustomEnvColorTable", TLuaInterpreter::getCustomEnvColorTable );
 
8262
    //lua_register( pGlobalLua, "setLevelColor", TLuaInterpreter::setLevelColor );
 
8263
    //lua_register( pGlobalLua, "getLevelColorTable", TLuaInterpreter::getLevelColorTable );
 
8264
    lua_register( pGlobalLua, "setRoomEnv", TLuaInterpreter::setRoomEnv );
 
8265
    lua_register( pGlobalLua, "setRoomName", TLuaInterpreter::setRoomName );
 
8266
    lua_register( pGlobalLua, "getRoomName", TLuaInterpreter::getRoomName );
 
8267
    lua_register( pGlobalLua, "setGridMode", TLuaInterpreter::setGridMode );
 
8268
    lua_register( pGlobalLua, "solveRoomCollisions", TLuaInterpreter::solveRoomCollisions );
 
8269
    lua_register( pGlobalLua, "addSpecialExit", TLuaInterpreter::addSpecialExit );
 
8270
    lua_register( pGlobalLua, "getSpecialExits", TLuaInterpreter::getSpecialExits );
 
8271
    lua_register( pGlobalLua, "getSpecialExitsSwap", TLuaInterpreter::getSpecialExitsSwap );
 
8272
    lua_register( pGlobalLua, "clearSpecialExits", TLuaInterpreter::clearSpecialExits );
 
8273
    lua_register( pGlobalLua, "getRoomEnv", TLuaInterpreter::getRoomEnv );
 
8274
    lua_register( pGlobalLua, "getRoomUserData", TLuaInterpreter::getRoomUserData );
 
8275
    lua_register( pGlobalLua, "setRoomUserData", TLuaInterpreter::setRoomUserData );
 
8276
    lua_register( pGlobalLua, "getRoomsByPosition", TLuaInterpreter::getRoomsByPosition );
 
8277
    //lua_register( pGlobalLua, "dumpRoomUserData", TLuaInterpreter::dumpRoomUserData );
 
8278
    lua_register( pGlobalLua, "clearRoomUserData", TLuaInterpreter::clearRoomUserData );
 
8279
    lua_register( pGlobalLua, "downloadFile", TLuaInterpreter::downloadFile );
 
8280
    lua_register( pGlobalLua, "appendCmdLine", TLuaInterpreter::appendCmdLine );
 
8281
    lua_register( pGlobalLua, "openUrl", TLuaInterpreter::openUrl );
 
8282
    lua_register( pGlobalLua, "sendSocket", TLuaInterpreter::sendSocket );
 
8283
    lua_register( pGlobalLua, "setRoomIDbyHash", TLuaInterpreter::setRoomIDbyHash );
 
8284
    lua_register( pGlobalLua, "getRoomIDbyHash", TLuaInterpreter::getRoomIDbyHash );
 
8285
    lua_register( pGlobalLua, "addAreaName", TLuaInterpreter::addAreaName );
 
8286
    lua_register( pGlobalLua, "getRoomAreaName", TLuaInterpreter::getRoomAreaName );
 
8287
    lua_register( pGlobalLua, "deleteArea", TLuaInterpreter::deleteArea );
 
8288
    lua_register( pGlobalLua, "deleteRoom", TLuaInterpreter::deleteRoom );
 
8289
    lua_register( pGlobalLua, "setRoomChar", TLuaInterpreter::setRoomChar );
 
8290
    lua_register( pGlobalLua, "registerAnonymousEventHandler", TLuaInterpreter::registerAnonymousEventHandler );
 
8291
    lua_register( pGlobalLua, "saveMap", TLuaInterpreter::saveMap );
 
8292
    lua_register( pGlobalLua, "setMainWindowSize", TLuaInterpreter::setMainWindowSize );
 
8293
    // removed because of various Qt crashes
 
8294
    //lua_register( pGlobalLua, "setAppStyleSheet", TLuaInterpreter::setAppStyleSheet );
 
8295
    lua_register( pGlobalLua, "sendIrc", TLuaInterpreter::sendIrc );
 
8296
    lua_register( pGlobalLua, "connectToServer", TLuaInterpreter::connectToServer );
 
8297
    lua_register( pGlobalLua, "getRooms", TLuaInterpreter::getRooms );
 
8298
    lua_register( pGlobalLua, "createMapLabel", TLuaInterpreter::createMapLabel );
 
8299
    lua_register( pGlobalLua, "deleteMapLabel", TLuaInterpreter::deleteMapLabel );
 
8300
    lua_register( pGlobalLua, "highlightRoom", TLuaInterpreter::highlightRoom );
 
8301
    lua_register( pGlobalLua, "unHighlightRoom", TLuaInterpreter::unHighlightRoom );
 
8302
    lua_register( pGlobalLua, "getMapLabels", TLuaInterpreter::getMapLabels );
 
8303
    lua_register( pGlobalLua, "lockExit", TLuaInterpreter::lockExit );
 
8304
    lua_register( pGlobalLua, "hasExitLock", TLuaInterpreter::hasExitLock );
 
8305
    lua_register( pGlobalLua, "lockSpecialExit", TLuaInterpreter::lockSpecialExit );
 
8306
    lua_register( pGlobalLua, "hasSpecialExitLock", TLuaInterpreter::hasSpecialExitLock );
 
8307
 
 
8308
    luaopen_yajl(pGlobalLua);
 
8309
    lua_setglobal( pGlobalLua, "yajl" );
 
8310
 
 
8311
#ifdef Q_OS_MAC
 
8312
    luaopen_zip( pGlobalLua );
 
8313
    lua_setglobal( pGlobalLua, "zip" );
 
8314
#endif
 
8315
    QString n;
 
8316
    int error;
 
8317
 
 
8318
    // if using LuaJIT, adjust the cpath to look in /usr/lib as well - it doesn't by default
 
8319
    luaL_dostring (pGlobalLua, "if jit then package.cpath = package.cpath .. ';/usr/lib/lua/5.1/?.so;' end");
 
8320
 
 
8321
    error = luaL_dostring( pGlobalLua, "require \"rex_pcre\"" );
 
8322
 
 
8323
    if( error != 0 )
 
8324
    {
 
8325
        string e = "no error message available from Lua";
 
8326
        if( lua_isstring( pGlobalLua, 1 ) )
 
8327
        {
 
8328
            e = "Lua error:";
 
8329
            e+=lua_tostring( pGlobalLua, 1 );
 
8330
        }
 
8331
        QString msg = "[ ERROR ] cannot find Lua module rex_pcre. Some functions may not be available.";
 
8332
        msg.append( e.c_str() );
 
8333
        gSysErrors << msg;
 
8334
    }
 
8335
    else
 
8336
    {
 
8337
        QString msg = "[  OK  ]  -  Lua module rex_pcre loaded";
 
8338
        gSysErrors << msg;
 
8339
    }
 
8340
 
 
8341
    error = luaL_dostring( pGlobalLua, "require \"zip\"" );
 
8342
 
 
8343
    if( error != 0 )
 
8344
    {
 
8345
        string e = "no error message available from Lua";
 
8346
        if( lua_isstring( pGlobalLua, 1 ) )
 
8347
        {
 
8348
            e = "Lua error:";
 
8349
            e+=lua_tostring( pGlobalLua, 1 );
 
8350
        }
 
8351
        QString msg = "[ ERROR ] cannot find Lua module zip";
 
8352
        msg.append( e.c_str() );
 
8353
        gSysErrors << msg;
 
8354
    }
 
8355
    else
 
8356
    {
 
8357
        QString msg = "[  OK  ]  -  Lua module zip loaded";
 
8358
        gSysErrors << msg;
 
8359
    }
 
8360
    error = luaL_dostring( pGlobalLua, "require \"lfs\"" );
 
8361
 
 
8362
    if( error != 0 )
 
8363
    {
 
8364
        string e = "no error message available from Lua";
 
8365
        if( lua_isstring( pGlobalLua, 1 ) )
 
8366
        {
 
8367
            e = "Lua error:";
 
8368
            e+=lua_tostring( pGlobalLua, 1 );
 
8369
        }
 
8370
        QString msg = "[ ERROR ] cannot find Lua module lfs (Lua File System).";
 
8371
        msg.append( e.c_str() );
 
8372
        gSysErrors << msg;
 
8373
    }
 
8374
    else
 
8375
    {
 
8376
        QString msg = "[  OK  ]  -  Lua module lfs loaded";
 
8377
        gSysErrors << msg;
 
8378
    }
 
8379
 
 
8380
    error = luaL_dostring( pGlobalLua, "require \"luasql.sqlite3\"" );
 
8381
 
 
8382
    if( error != 0 )
 
8383
    {
 
8384
        string e = "no error message available from Lua";
 
8385
        if( lua_isstring( pGlobalLua, 1 ) )
 
8386
        {
 
8387
            e = "Lua error:";
 
8388
            e+=lua_tostring( pGlobalLua, 1 );
 
8389
        }
 
8390
        QString msg = "[ ERROR ] cannot find Lua module luasql.sqlite3. Database support will not be available.";
 
8391
        msg.append( e.c_str() );
 
8392
        gSysErrors << msg;
 
8393
    }
 
8394
    else
 
8395
    {
 
8396
        QString msg = "[  OK  ]  -  Lua module sqlite3 loaded";
 
8397
        gSysErrors << msg;
 
8398
    }
 
8399
 
 
8400
//    QString path = QDir::homePath()+"/.config/mudlet/mudlet-lua/lua/LuaGlobal.lua";
 
8401
//    error = luaL_dofile( pGlobalLua, path.toLatin1().data() );
 
8402
//    if( error != 0 )
 
8403
//    {
 
8404
//        string e = "no error message available from Lua";
 
8405
//        if( lua_isstring( pGlobalLua, 1 ) )
 
8406
//        {
 
8407
//            e = "[CRITICAL ERROR] LuaGlobal.lua compile error - please report";
 
8408
//            e += lua_tostring( pGlobalLua, 1 );
 
8409
//        }
 
8410
//        gSysErrors << e.c_str();
 
8411
//    }
 
8412
//    else
 
8413
//    {
 
8414
//        gSysErrors << "[INFO] LuaGlobal.lua loaded successfully.";
 
8415
//    }
 
8416
 
 
8417
    /*path = QDir::homePath()+"/.config/mudlet/db.lua";
 
8418
    error = luaL_dofile( pGlobalLua, path.toLatin1().data() );
 
8419
    if( error != 0 )
 
8420
    {
 
8421
        string e = "no error message available from Lua";
 
8422
        if( lua_isstring( pGlobalLua, 1 ) )
 
8423
        {
 
8424
            e = "[CRITICAL ERROR] db.lua compile error - please report";
 
8425
            e += lua_tostring( pGlobalLua, 1 );
 
8426
        }
 
8427
        gSysErrors << e.c_str();
 
8428
    }
 
8429
    else
 
8430
    {
 
8431
        gSysErrors << "[INFO] db.lua loaded successfully.";
 
8432
    }*/
 
8433
 
 
8434
 
 
8435
    QString tn = "atcp";
 
8436
    QStringList args;
 
8437
    set_lua_table( tn, args );
 
8438
 
 
8439
    tn = "channel102";
 
8440
    set_lua_table( tn, args );
 
8441
 
 
8442
    lua_pop( pGlobalLua, lua_gettop( pGlobalLua ) );
 
8443
 
 
8444
    //FIXME make function call in destructor lua_close(L);
 
8445
}
 
8446
 
 
8447
void TLuaInterpreter::loadGlobal()
 
8448
{
 
8449
    //QString path = QDir::homePath()+"/.config/mudlet/mudlet-lua/lua/LuaGlobal.lua";
 
8450
 
 
8451
    QString path = "/usr/share/games/mudlet/lua/LuaGlobal.lua";
 
8452
    int error = luaL_dofile( pGlobalLua, path.toLatin1().data() );
 
8453
    if( error != 0 )
 
8454
    {
 
8455
        string e = "no error message available from Lua";
 
8456
        if( lua_isstring( pGlobalLua, 1 ) )
 
8457
        {
 
8458
            e = "[ ERROR ]  -  LuaGlobal.lua compile error - please report!";
 
8459
            e += lua_tostring( pGlobalLua, 1 );
 
8460
        }
 
8461
        gSysErrors << e.c_str();
 
8462
    }
 
8463
    else
 
8464
    {
 
8465
        gSysErrors << "[  OK  ]  -  mudlet-lua API & Geyser Layout manager loaded.";
 
8466
    }
 
8467
}
 
8468
 
 
8469
void TLuaInterpreter::slotEchoMessage(int hostID, QString msg)
 
8470
{
 
8471
    Host * pHost = HostManager::self()->getHostFromHostID( hostID );
 
8472
    mudlet::self()->print( pHost, msg );
 
8473
}
 
8474
 
 
8475
 
 
8476
void TLuaInterpreter::slotNewCommand(int hostID, QString cmd)
 
8477
{
 
8478
    Host * pHost = HostManager::self()->getHostFromHostID( hostID );
 
8479
    pHost->send( cmd );
 
8480
}
 
8481
 
 
8482
void TLuaInterpreter::slotOpenUserWindow(int hostID, QString windowName )
 
8483
{
 
8484
}
 
8485
 
 
8486
void TLuaInterpreter::slotClearUserWindow(int hostID, QString windowName )
 
8487
{
 
8488
}
 
8489
 
 
8490
void TLuaInterpreter::slotEnableTimer(int hostID, QString windowName )
 
8491
{
 
8492
    Host * pHost = HostManager::self()->getHostFromHostID( hostID );
 
8493
    pHost->enableTimer( windowName );
 
8494
}
 
8495
 
 
8496
void TLuaInterpreter::slotDisableTimer(int hostID, QString windowName )
 
8497
{
 
8498
    Host * pHost = HostManager::self()->getHostFromHostID( hostID );
 
8499
    pHost->disableTimer( windowName );
 
8500
}
 
8501
 
 
8502
void TLuaInterpreter::slotReplace(int hostID, QString text)
 
8503
{
 
8504
}
 
8505
 
 
8506
void TLuaInterpreter::slotEchoUserWindow(int hostID, QString windowName, QString text )
 
8507
{
 
8508
}
 
8509
 
 
8510
void TLuaInterpreter::slotTempTimer( int hostID, double timeout, QString function, QString timerName )
 
8511
{
 
8512
    Host * pHost = HostManager::self()->getHostFromHostID( hostID );
 
8513
    QTime time(0,0,0,0);
 
8514
    int msec = static_cast<int>(timeout * 1000);
 
8515
    QTime time2 = time.addMSecs( msec );
 
8516
    TTimer * pT;
 
8517
    pT = new TTimer( timerName, time2, pHost );
 
8518
    pT->setName( timerName );
 
8519
    pT->setTime( time2 );
 
8520
    //qDebug()<<"setting time of tempTimer to "<<time2.minute()<<":"<<time2.second()<<":"<<time2.msec()<<" timeout="<<timeout;
 
8521
    pT->setScript( function );
 
8522
    pT->setIsFolder( false );
 
8523
    pT->setIsActive( true );
 
8524
    pT->setIsTempTimer( true );
 
8525
    pT->registerTimer();
 
8526
}
 
8527
 
 
8528
int TLuaInterpreter::startPermTimer( QString & name, QString & parent, double timeout, QString & function )
 
8529
{
 
8530
    QTime time( 0, 0, 0, 0 );
 
8531
    int msec = static_cast<int>(timeout * 1000);
 
8532
    QTime time2 = time.addMSecs( msec );
 
8533
    TTimer * pT;
 
8534
    if( parent.isEmpty() )
 
8535
    {
 
8536
        pT = new TTimer( "a", time2, mpHost );
 
8537
    }
 
8538
    else
 
8539
    {
 
8540
        TTimer * pP = mpHost->getTimerUnit()->findTimer( parent );
 
8541
        if( !pP )
 
8542
        {
 
8543
            return -1;//parent not found
 
8544
        }
 
8545
        pT = new TTimer( pP, mpHost );
 
8546
    }
 
8547
 
 
8548
    pT->setTime( time2 );
 
8549
    pT->setIsFolder( false );
 
8550
    pT->setIsTempTimer( false );
 
8551
    pT->registerTimer();
 
8552
    pT->setScript( function );
 
8553
    int id = pT->getID();
 
8554
    pT->setName( name );//darf erst nach isTempTimer gesetzt werde, damit setName() schneller ist
 
8555
    pT->setIsActive( false );
 
8556
    mpHost->mpEditorDialog->mNeedUpdateData = true;
 
8557
    return id;
 
8558
}
 
8559
 
 
8560
int TLuaInterpreter::startTempTimer( double timeout, QString & function )
 
8561
{
 
8562
    QTime time( 0, 0, 0, 0 );
 
8563
    int msec = static_cast<int>(timeout * 1000);
 
8564
    QTime time2 = time.addMSecs( msec );
 
8565
    TTimer * pT;
 
8566
    pT = new TTimer( "a", time2, mpHost );
 
8567
    pT->setTime( time2 );
 
8568
    pT->setIsFolder( false );
 
8569
    pT->setIsTempTimer( true );
 
8570
    pT->registerTimer();
 
8571
    pT->setScript( function );
 
8572
    int id = pT->getID();
 
8573
    pT->setName( QString::number( id ) );//darf erst nach isTempTimer gesetzt werde, damit setName() schneller ist
 
8574
    pT->setIsActive( true );
 
8575
    pT->enableTimer( id );
 
8576
    return id;
 
8577
}
 
8578
 
 
8579
int TLuaInterpreter::startPermAlias( QString & name, QString & parent, QString & regex, QString & function )
 
8580
{
 
8581
    TAlias * pT;
 
8582
 
 
8583
    if( parent.isEmpty() )
 
8584
    {
 
8585
        pT = new TAlias("a", mpHost );
 
8586
    }
 
8587
    else
 
8588
    {
 
8589
        TAlias * pP = mpHost->getAliasUnit()->findAlias( parent );
 
8590
        if( !pP )
 
8591
        {
 
8592
            return -1;//parent not found
 
8593
        }
 
8594
        pT = new TAlias( pP, mpHost );
 
8595
    }
 
8596
    pT->setRegexCode( regex );
 
8597
    pT->setIsFolder( false );
 
8598
    pT->setIsActive( true );
 
8599
    pT->setIsTempAlias( false );
 
8600
    pT->registerAlias();
 
8601
    pT->setScript( function );
 
8602
    int id = pT->getID();
 
8603
    pT->setName( name );
 
8604
    mpHost->mpEditorDialog->mNeedUpdateData = true;
 
8605
    return id;
 
8606
}
 
8607
 
 
8608
int TLuaInterpreter::startTempAlias( QString & regex, QString & function )
 
8609
{
 
8610
    TAlias * pT;
 
8611
    pT = new TAlias("a", mpHost );
 
8612
    pT->setRegexCode( regex );
 
8613
    pT->setIsFolder( false );
 
8614
    pT->setIsActive( true );
 
8615
    pT->setIsTempAlias( true );
 
8616
    pT->registerAlias();
 
8617
    pT->setScript( function );
 
8618
    int id = pT->getID();
 
8619
    pT->setName( QString::number( id ) );
 
8620
    return id;
 
8621
}
 
8622
 
 
8623
int TLuaInterpreter::startTempExactMatchTrigger( QString & regex, QString & function )
 
8624
{
 
8625
    TTrigger * pT;
 
8626
    QStringList sList;
 
8627
    sList<<regex;
 
8628
    QList<int> propertyList;
 
8629
    propertyList << REGEX_EXACT_MATCH;
 
8630
    pT = new TTrigger("a", sList, propertyList, false, mpHost );
 
8631
    pT->setIsFolder( false );
 
8632
    pT->setIsActive( true );
 
8633
    pT->setIsTempTrigger( true );
 
8634
    pT->registerTrigger();
 
8635
    pT->setScript( function );
 
8636
    int id = pT->getID();
 
8637
    pT->setName( QString::number( id ) );
 
8638
    return id;
 
8639
}
 
8640
 
 
8641
int TLuaInterpreter::startTempBeginOfLineTrigger( QString & regex, QString & function )
 
8642
{
 
8643
    TTrigger * pT;
 
8644
    QStringList sList;
 
8645
    sList<<regex;
 
8646
    QList<int> propertyList;
 
8647
    propertyList << REGEX_BEGIN_OF_LINE_SUBSTRING;
 
8648
    pT = new TTrigger("a", sList, propertyList, false, mpHost );
 
8649
    pT->setIsFolder( false );
 
8650
    pT->setIsActive( true );
 
8651
    pT->setIsTempTrigger( true );
 
8652
    pT->registerTrigger();
 
8653
    pT->setScript( function );
 
8654
    int id = pT->getID();
 
8655
    pT->setName( QString::number( id ) );
 
8656
    return id;
 
8657
}
 
8658
 
 
8659
 
 
8660
int TLuaInterpreter::startTempTrigger( QString & regex, QString & function )
 
8661
{
 
8662
    TTrigger * pT;
 
8663
    QStringList sList;
 
8664
    sList<<regex;
 
8665
    QList<int> propertyList;
 
8666
    propertyList << REGEX_SUBSTRING;// substring trigger is default
 
8667
    pT = new TTrigger("a", sList, propertyList, false, mpHost );
 
8668
    pT->setIsFolder( false );
 
8669
    pT->setIsActive( true );
 
8670
    pT->setIsTempTrigger( true );
 
8671
    pT->registerTrigger();
 
8672
    pT->setScript( function );
 
8673
    int id = pT->getID();
 
8674
    pT->setName( QString::number( id ) );
 
8675
    return id;
 
8676
}
 
8677
 
 
8678
int TLuaInterpreter::startTempLineTrigger( int from, int howmany, QString & function )
 
8679
{
 
8680
    TTrigger * pT;
 
8681
//    QStringList sList;
 
8682
//    QList<int> propertyList;
 
8683
//    propertyList << REGEX_SUBSTRING;// substring trigger is default
 
8684
//    pT = new TTrigger("a", sList, propertyList, false, mpHost );
 
8685
    pT = new TTrigger( 0, mpHost );
 
8686
    pT->setIsFolder( false );
 
8687
    pT->setIsActive( true );
 
8688
    pT->setIsTempTrigger( true );
 
8689
    pT->setIsLineTrigger( true );
 
8690
    pT->setStartOfLineDelta( from );
 
8691
    pT->setLineDelta( howmany );
 
8692
    pT->registerTrigger();
 
8693
    pT->setScript( function );
 
8694
    int id = pT->getID();
 
8695
    pT->setName( QString::number( id ) );
 
8696
    return id;
 
8697
}
 
8698
 
 
8699
int TLuaInterpreter::startTempColorTrigger( int fg, int bg, QString & function )
 
8700
{
 
8701
    TTrigger * pT;
 
8702
//    QStringList sList;
 
8703
//    QList<int> propertyList;
 
8704
//    propertyList << REGEX_SUBSTRING;// substring trigger is default
 
8705
//    pT = new TTrigger("a", sList, propertyList, false, mpHost );
 
8706
    pT = new TTrigger( 0, mpHost );
 
8707
    pT->setIsFolder( false );
 
8708
    pT->setIsActive( true );
 
8709
    pT->setIsTempTrigger( true );
 
8710
    pT->setupTmpColorTrigger( fg, bg );
 
8711
 
 
8712
    pT->registerTrigger();
 
8713
    pT->setScript( function );
 
8714
    int id = pT->getID();
 
8715
    pT->setName( QString::number( id ) );
 
8716
    return id;
 
8717
}
 
8718
 
 
8719
int TLuaInterpreter::startTempRegexTrigger( QString & regex, QString & function )
 
8720
{
 
8721
    TTrigger * pT;
 
8722
    QStringList sList;
 
8723
    sList<<regex;
 
8724
 
 
8725
    QList<int> propertyList;
 
8726
    propertyList << REGEX_PERL;// substring trigger is default
 
8727
    pT = new TTrigger("a", sList, propertyList, false, mpHost );
 
8728
    pT->setIsFolder( false );
 
8729
    pT->setIsActive( true );
 
8730
    pT->setIsTempTrigger( true );
 
8731
    pT->registerTrigger();
 
8732
    pT->setScript( function );
 
8733
    int id = pT->getID();
 
8734
    pT->setName( QString::number( id ) );
 
8735
    return id;
 
8736
}
 
8737
 
 
8738
int TLuaInterpreter::startPermRegexTrigger( QString & name, QString & parent, QStringList & regexList, QString & function )
 
8739
{
 
8740
    TTrigger * pT;
 
8741
    QList<int> propertyList;
 
8742
    for( int i=0; i<regexList.size(); i++ )
 
8743
    {
 
8744
        propertyList << REGEX_PERL;
 
8745
    }
 
8746
    if( parent.isEmpty() )
 
8747
    {
 
8748
        pT = new TTrigger( "a", regexList, propertyList, (regexList.size()>1), mpHost );
 
8749
    }
 
8750
    else
 
8751
    {
 
8752
        TTrigger * pP = mpHost->getTriggerUnit()->findTrigger( parent );
 
8753
        if( !pP )
 
8754
        {
 
8755
            return -1;//parent not found
 
8756
        }
 
8757
        pT = new TTrigger( pP, mpHost );
 
8758
        pT->setRegexCodeList( regexList, propertyList );
 
8759
    }
 
8760
    pT->setIsFolder( (regexList.size()==0) );
 
8761
    pT->setIsActive( true );
 
8762
    pT->setIsTempTrigger( false );
 
8763
    pT->registerTrigger();
 
8764
    pT->setScript( function );
 
8765
    pT->setName( name );
 
8766
    mpHost->mpEditorDialog->mNeedUpdateData = true;
 
8767
    return 1;
 
8768
 
 
8769
}
 
8770
 
 
8771
int TLuaInterpreter::startPermBeginOfLineStringTrigger( QString & name, QString & parent, QStringList & regexList, QString & function )
 
8772
{
 
8773
    TTrigger * pT;
 
8774
    QList<int> propertyList;
 
8775
    for( int i=0; i<regexList.size(); i++ )
 
8776
    {
 
8777
        propertyList << REGEX_BEGIN_OF_LINE_SUBSTRING;
 
8778
    }
 
8779
    if( parent.isEmpty() )
 
8780
    {
 
8781
        pT = new TTrigger( "a", regexList, propertyList, (regexList.size()>1), mpHost );
 
8782
    }
 
8783
    else
 
8784
    {
 
8785
        TTrigger * pP = mpHost->getTriggerUnit()->findTrigger( parent );
 
8786
        if( !pP )
 
8787
        {
 
8788
            return -1;//parent not found
 
8789
        }
 
8790
        pT = new TTrigger( pP, mpHost );
 
8791
        pT->setRegexCodeList( regexList, propertyList );
 
8792
    }
 
8793
    pT->setIsFolder( (regexList.size()==0) );
 
8794
    pT->setIsActive( true );
 
8795
    pT->setIsTempTrigger( false );
 
8796
    pT->registerTrigger();
 
8797
    pT->setScript( function );
 
8798
    pT->setName( name );
 
8799
    mpHost->mpEditorDialog->mNeedUpdateData = true;
 
8800
    return 1;
 
8801
 
 
8802
}
 
8803
 
 
8804
int TLuaInterpreter::startPermSubstringTrigger( QString & name, QString & parent, QStringList & regexList, QString & function )
 
8805
{
 
8806
    TTrigger * pT;
 
8807
    QList<int> propertyList;
 
8808
    for( int i=0; i<regexList.size(); i++ )
 
8809
    {
 
8810
        propertyList << REGEX_SUBSTRING;
 
8811
    }
 
8812
    if( parent.isEmpty() )
 
8813
    {
 
8814
        pT = new TTrigger( "a", regexList, propertyList, (regexList.size()>1), mpHost );
 
8815
    }
 
8816
    else
 
8817
    {
 
8818
        TTrigger * pP = mpHost->getTriggerUnit()->findTrigger( parent );
 
8819
        if( !pP )
 
8820
        {
 
8821
            return -1;//parent not found
 
8822
        }
 
8823
        pT = new TTrigger( pP, mpHost );
 
8824
        pT->setRegexCodeList( regexList, propertyList );
 
8825
    }
 
8826
    pT->setIsFolder( (regexList.size()==0) );
 
8827
    pT->setIsActive( true );
 
8828
    pT->setIsTempTrigger( false );
 
8829
    pT->registerTrigger();
 
8830
    pT->setScript( function );
 
8831
    pT->setName( name );
 
8832
    mpHost->mpEditorDialog->mNeedUpdateData = true;
 
8833
    return 1;
 
8834
 
 
8835
}
 
8836
 
 
8837
 
 
8838
 
 
8839
 
 
8840
 
 
8841
 
 
8842