QTfrontend/net/hwmap.cpp
branchsdl2transition
changeset 11362 ed5a6478e710
parent 11046 47a8c19ecb60
child 12897 fc47fc4af6bd
equal deleted inserted replaced
11361:31570b766315 11362:ed5a6478e710
     1 /*
     1 /*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2006-2007 Ulyanov Igor <iulyanov@gmail.com>
     3  * Copyright (c) 2006-2007 Ulyanov Igor <iulyanov@gmail.com>
     4  * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
     4  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
     5  *
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License as published by
     7  * it under the terms of the GNU General Public License as published by
     8  * the Free Software Foundation; version 2 of the License
     8  * the Free Software Foundation; version 2 of the License
     9  *
     9  *
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    13  * GNU General Public License for more details.
    14  *
    14  *
    15  * You should have received a copy of the GNU General Public License
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program; if not, write to the Free Software
    16  * along with this program; if not, write to the Free Software
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18  */
    18  */
       
    19 
       
    20 #include <QPainter>
       
    21 #include <QBitmap>
       
    22 #include <QLinearGradient>
    19 
    23 
    20 #include "hwconsts.h"
    24 #include "hwconsts.h"
    21 #include "hwmap.h"
    25 #include "hwmap.h"
    22 
    26 
    23 HWMap::HWMap(QObject * parent) :
    27 HWMap::HWMap(QObject * parent) :
    24     TCPBase(false, parent)
    28     TCPBase(false, parent)
    25 {
    29 {
       
    30     templateFilter = 0;
       
    31     m_mapgen = MAPGEN_REGULAR;
       
    32     m_maze_size = 0;
       
    33     m_feature_size = 50;
    26 }
    34 }
    27 
    35 
    28 HWMap::~HWMap()
    36 HWMap::~HWMap()
    29 {
    37 {
    30 }
    38 }
    32 bool HWMap::couldBeRemoved()
    40 bool HWMap::couldBeRemoved()
    33 {
    41 {
    34     return !m_hasStarted;
    42     return !m_hasStarted;
    35 }
    43 }
    36 
    44 
    37 void HWMap::getImage(const QString & seed, int filter, MapGenerator mapgen, int maze_size, const QByteArray & drawMapData)
    45 void HWMap::getImage(const QString & seed, int filter, MapGenerator mapgen, int maze_size, const QByteArray & drawMapData, QString & script, QString & scriptparam, int feature_size)
    38 {
    46 {
    39     m_seed = seed;
    47     m_seed = seed;
       
    48     m_script = script;
       
    49     m_scriptparam = scriptparam;
    40     templateFilter = filter;
    50     templateFilter = filter;
    41     m_mapgen = mapgen;
    51     m_mapgen = mapgen;
    42     m_maze_size = maze_size;
    52     m_maze_size = maze_size;
       
    53     m_feature_size = feature_size;
    43     if(mapgen == MAPGEN_DRAWN) m_drawMapData = drawMapData;
    54     if(mapgen == MAPGEN_DRAWN) m_drawMapData = drawMapData;
    44     Start(true);
    55     Start(true);
    45 }
    56 }
    46 
    57 
    47 QStringList HWMap::getArguments()
    58 QStringList HWMap::getArguments()
    50     arguments << "--internal";
    61     arguments << "--internal";
    51     arguments << "--port";
    62     arguments << "--port";
    52     arguments << QString("%1").arg(ipc_port);
    63     arguments << QString("%1").arg(ipc_port);
    53     arguments << "--user-prefix";
    64     arguments << "--user-prefix";
    54     arguments << cfgdir->absolutePath();
    65     arguments << cfgdir->absolutePath();
       
    66     arguments << "--prefix";
       
    67     arguments << datadir->absolutePath();
    55     arguments << "--landpreview";
    68     arguments << "--landpreview";
    56     return arguments;
    69     return arguments;
    57 }
    70 }
    58 
    71 
    59 void HWMap::onClientDisconnect()
    72 void HWMap::onClientDisconnect()
    60 {
    73 {    
       
    74     QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128));
       
    75     linearGrad.setColorAt(1, QColor(0, 0, 192));
       
    76     linearGrad.setColorAt(0, QColor(66, 115, 225));
       
    77 
    61     if (readbuffer.size() == 128 * 32 + 1)
    78     if (readbuffer.size() == 128 * 32 + 1)
    62     {
    79     {
    63         quint8 *buf = (quint8*) readbuffer.constData();
    80         quint8 *buf = (quint8*) readbuffer.constData();
    64         QImage im(buf, 256, 128, QImage::Format_Mono);
    81         QImage im(buf, 256, 128, QImage::Format_Mono);
    65         im.setNumColors(2);
    82         im.setNumColors(2);
       
    83 
       
    84         QPixmap px(QSize(256, 128));
       
    85         QPixmap pxres(px.size());
       
    86         QPainter p(&pxres);
       
    87 
       
    88         px.fill(Qt::yellow);
       
    89         QBitmap bm = QBitmap::fromImage(im);
       
    90         px.setMask(bm);
       
    91 
       
    92         p.fillRect(pxres.rect(), linearGrad);
       
    93         p.drawPixmap(0, 0, px);
       
    94 
    66         emit HHLimitReceived(buf[128 * 32]);
    95         emit HHLimitReceived(buf[128 * 32]);
    67         emit ImageReceived(im);
    96         emit ImageReceived(px);
       
    97     } else if (readbuffer.size() == 128 * 256 + 1)
       
    98     {
       
    99         QVector<QRgb> colorTable;
       
   100         colorTable.resize(256);
       
   101         for(int i = 0; i < 256; ++i)
       
   102             colorTable[i] = qRgba(255, 255, 0, i);
       
   103 
       
   104         const quint8 *buf = (const quint8*) readbuffer.constData();
       
   105         QImage im(buf, 256, 128, QImage::Format_Indexed8);
       
   106         im.setColorTable(colorTable);
       
   107 
       
   108         QPixmap px = QPixmap::fromImage(im, Qt::ColorOnly);
       
   109         QPixmap pxres(px.size());
       
   110         QPainter p(&pxres);
       
   111 
       
   112         p.fillRect(pxres.rect(), linearGrad);
       
   113         p.drawPixmap(0, 0, px);
       
   114 
       
   115         emit HHLimitReceived(buf[128 * 256]);
       
   116         emit ImageReceived(px);
    68     }
   117     }
    69 }
   118 }
    70 
   119 
    71 void HWMap::SendToClientFirst()
   120 void HWMap::SendToClientFirst()
    72 {
   121 {
    73     SendIPC(QString("eseed %1").arg(m_seed).toUtf8());
   122     SendIPC(QString("eseed %1").arg(m_seed).toUtf8());
    74     SendIPC(QString("e$template_filter %1").arg(templateFilter).toUtf8());
   123     SendIPC(QString("e$template_filter %1").arg(templateFilter).toUtf8());
    75     SendIPC(QString("e$mapgen %1").arg(m_mapgen).toUtf8());
   124     SendIPC(QString("e$mapgen %1").arg(m_mapgen).toUtf8());
       
   125     SendIPC(QString("e$feature_size %1").arg(m_feature_size).toUtf8());
       
   126     if (!m_script.isEmpty())
       
   127     {
       
   128         SendIPC(QString("escript Scripts/Multiplayer/%1.lua").arg(m_script).toUtf8());
       
   129         SendIPC(QString("e$scriptparam %1").arg(m_scriptparam).toUtf8());
       
   130     }
    76 
   131 
    77     switch (m_mapgen)
   132     switch (m_mapgen)
    78     {
   133     {
    79         case MAPGEN_MAZE:
   134         case MAPGEN_MAZE:
       
   135         case MAPGEN_PERLIN:
    80             SendIPC(QString("e$maze_size %1").arg(m_maze_size).toUtf8());
   136             SendIPC(QString("e$maze_size %1").arg(m_maze_size).toUtf8());
    81             break;
   137             break;
    82 
   138 
    83         case MAPGEN_DRAWN:
   139         case MAPGEN_DRAWN:
    84         {
   140         {