~ubuntu-branches/ubuntu/saucy/lordsawar/saucy

« back to all changes in this revision

Viewing changes to src/editor/editorbigmap.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese
  • Date: 2008-12-20 13:52:12 UTC
  • mfrom: (1.1.6 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081220135212-noeb2w3y98ebo7o9
Tags: 0.1.4-1
[ Barry deFreese ]
* New upstream release.
* Move 0.0.8-2.1 changelog entry to correct point in changelog.
* Make lordsawar-data suggest lordsawar.
* Update my e-mail address.
* Add build-depends on intltool, uuid-dev, and libboost-dev.
* Don't install locales since there are no translations currently.
* Add simple man page for new lordsawar-pbm binary.
* Drop gcc4.3 patches as they have been fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "editorbigmap.h"
26
26
 
27
 
#include "../army.h"
28
 
#include "../stacklist.h"
29
 
#include "../stack.h"
30
 
#include "../citylist.h"
31
 
#include "../ruinlist.h"
32
 
#include "../signpostlist.h"
33
 
#include "../templelist.h"
34
 
#include "../bridgelist.h"
35
 
#include "../portlist.h"
36
 
#include "../roadlist.h"
37
 
#include "../ruin.h"
38
 
#include "../signpost.h"
39
 
#include "../temple.h"
40
 
#include "../road.h"
41
 
#include "../playerlist.h"
42
 
#include "../defs.h"
43
 
#include "../File.h"
44
 
#include "../GameMap.h"
45
 
#include "../Configuration.h"
46
 
#include "../rewardlist.h"
47
 
#include "../GraphicsCache.h"
48
 
#include "../armysetlist.h"
49
 
#include "../sdl-draw.h"
50
 
#include "../MapRenderer.h"
 
27
#include "army.h"
 
28
#include "stacklist.h"
 
29
#include "stack.h"
 
30
#include "citylist.h"
 
31
#include "ruinlist.h"
 
32
#include "signpostlist.h"
 
33
#include "templelist.h"
 
34
#include "bridgelist.h"
 
35
#include "portlist.h"
 
36
#include "roadlist.h"
 
37
#include "ruin.h"
 
38
#include "signpost.h"
 
39
#include "temple.h"
 
40
#include "road.h"
 
41
#include "playerlist.h"
 
42
#include "defs.h"
 
43
#include "File.h"
 
44
#include "GameMap.h"
 
45
#include "Configuration.h"
 
46
#include "rewardlist.h"
 
47
#include "GraphicsCache.h"
 
48
#include "armysetlist.h"
 
49
#include "sdl-draw.h"
 
50
#include "MapRenderer.h"
 
51
#include "CreateScenario.h"
51
52
 
52
53
 
53
54
EditorBigMap::EditorBigMap()
60
61
    pointer_size = 1;
61
62
    pointer_terrain = Tile::GRASS;
62
63
    pointer_tile_style_id = -1;
 
64
    show_tile_types_instead_of_tile_styles = false;
63
65
}
64
66
 
65
67
EditorBigMap::~EditorBigMap()
189
191
    draw();
190
192
}
191
193
 
 
194
std::vector<Vector<int> > EditorBigMap::get_screen_tiles()
 
195
{
 
196
    // find out which tiles are within bounds
 
197
    std::vector<Vector<int> > tiles;
 
198
 
 
199
    for (int y = buffer_view.y; y < buffer_view.y + buffer_view.h; y++)
 
200
      for (int x = buffer_view.x; x < buffer_view.x + buffer_view.w; x++)
 
201
        {
 
202
            Vector<int> tile(x, y);
 
203
            if (tile.x >= 0 && tile.x < GameMap::getWidth() &&
 
204
                tile.y >= 0 && tile.y < GameMap::getHeight())
 
205
                tiles.push_back(tile);
 
206
        }
 
207
    
 
208
    return tiles;
 
209
}
 
210
 
192
211
std::vector<Vector<int> > EditorBigMap::get_cursor_tiles()
193
212
{
194
213
    // find out which cursor tiles are within bounds
214
233
 
215
234
void EditorBigMap::after_draw()
216
235
{
217
 
    if (pointer == POINTER)
218
 
        return;
219
 
    
 
236
    int tilesize = GameMap::getInstance()->getTileset()->getTileSize();
 
237
    std::vector<Vector<int> > tiles;
 
238
 
 
239
    if (show_tile_types_instead_of_tile_styles)
 
240
      {
 
241
        tiles = get_screen_tiles();
 
242
        for (std::vector<Vector<int> >::iterator i = tiles.begin(),
 
243
             end = tiles.end(); i != end; ++i)
 
244
          {
 
245
            Vector<int> pos = tile_to_buffer_pos(*i);
 
246
            SDL_Color tc = GameMap::getInstance()->getTile(*i)->getColor();
 
247
            const Uint32 color = SDL_MapRGB(buffer->format, tc.r, tc.g, tc.b);
 
248
            draw_filled_rect(buffer, pos.x, pos.y,
 
249
                             pos.x + tilesize, pos.y + tilesize, color);
 
250
          }
 
251
      }
 
252
 
220
253
    // we need to draw a drawing cursor on the map
221
 
    
222
 
    std::vector<Vector<int> > tiles = get_cursor_tiles();
223
 
 
 
254
    tiles = get_cursor_tiles();
224
255
    // draw each tile
225
 
    int tilesize = GameMap::getInstance()->getTileset()->getTileSize();
226
256
    for (std::vector<Vector<int> >::iterator i = tiles.begin(),
227
257
             end = tiles.end(); i != end; ++i)
228
258
    {
284
314
            break;
285
315
            
286
316
        case ROAD:
287
 
            pic = GraphicsCache::getInstance()->getRoadPic(calculateRoadType(*i));
 
317
            pic = GraphicsCache::getInstance()->getRoadPic(CreateScenario::calculateRoadType(*i));
288
318
            SDL_BlitSurface(pic, 0, buffer, &r);
289
319
            break;
290
320
        case PORT:
352
382
    void remove_from_map(T *l, Vector<int> tile)
353
383
    {
354
384
        for (typename T::iterator i = l->begin(), end = l->end(); i != end; ++i)
355
 
            if (i->contains(tile))
 
385
            if ((*i)->contains(tile))
356
386
            {
357
387
                // erase from map
358
388
                GameMap *gamemap = GameMap::getInstance();
359
 
                Rectangle r = i->get_area();
 
389
                Rectangle r = (*i)->get_area();
360
390
                for (int x = r.x; x < r.x + r.w; ++x)
361
391
                    for (int y = r.y; y < r.y + r.h; ++y)
362
392
                        gamemap->getTile(Vector<int>(x, y))->setBuilding(Maptile::NONE);
409
439
            maptile->setType(ts->getIndex(pointer_terrain));
410
440
            if (pointer_tile_style_id == -1)
411
441
              {
412
 
                GameMap::getInstance()->applyTileStyles(tile.y-1, tile.x-1, 
413
 
                                                        tile.y+2, tile.x+2,
414
 
                                                        false);
 
442
                if (pointer_terrain == Tile::MOUNTAIN)
 
443
                  {
 
444
                    GameMap *gm = GameMap::getInstance();
 
445
                    gm->surroundMountains (tile.y-1, tile.x-1, 
 
446
                                           tile.y+2, tile.x+2);
 
447
                    gm->applyTileStyles(tile.y-2, tile.x-2, 
 
448
                                        tile.y+3, tile.x+3,
 
449
                                        false);
 
450
                  }
 
451
                else
 
452
                  GameMap::getInstance()->applyTileStyles(tile.y-1, tile.x-1, 
 
453
                                                          tile.y+2, tile.x+2,
 
454
                                                          false);
415
455
              }
416
456
            else
417
457
              {
421
461
                maptile->setTileStyle (tile_style);
422
462
              }
423
463
            changed_tiles.dim = Vector<int>(1, 1);
 
464
            //change the boatedness of stacks that might be here.
 
465
            if (Stacklist::getObjectAt(tile))
 
466
              {
 
467
                Stack* s = Stacklist::getObjectAt(tile);
 
468
                Port *port = Portlist::getInstance()->getObjectAt(tile);
 
469
                Bridge *bridge = Bridgelist::getInstance()->getObjectAt(tile);
 
470
                if (pointer_terrain == Tile::WATER && s->hasShip() == false && 
 
471
                    !port && !bridge)
 
472
                  {
 
473
                    for (Stack::iterator it = s->begin(); it != s->end(); it++)
 
474
                      (*it)->setInShip(true);
 
475
                  }
 
476
                else if (pointer_terrain != Tile::WATER && s->hasShip() == true)
 
477
                  {
 
478
                    for (Stack::iterator it = s->begin(); it != s->end(); it++)
 
479
                      (*it)->setInShip(false);
 
480
                  }
 
481
              }
424
482
            break;
425
483
            
426
484
        case ERASE:
479
537
                Stack* s = new Stack(p, tile);
480
538
                const Armysetlist* al = Armysetlist::getInstance();
481
539
                Army* a = new Army(*al->getArmy(p->getArmyset(), 0), p);
 
540
                GameMap *gm = GameMap::getInstance();
 
541
                if (gm->getTile(s->getPos())->getBuilding() == Maptile::PORT ||
 
542
                    gm->getTile(s->getPos())->getBuilding() == Maptile::BRIDGE)
 
543
                  a->setInShip(false);
 
544
                else if (gm->getTile(s->getPos())->getMaptileType() == 
 
545
                         Tile::WATER)
 
546
                  a->setInShip(true);
 
547
                else
 
548
                  a->setInShip(false);
482
549
                s->push_back(a);
 
550
                s->group();
483
551
                p->addStack(s);
484
552
                //if we're on a city, change the allegiance of the stack
485
553
                //and it's armies to that of the city
486
 
                GameMap *gm = GameMap::getInstance();
487
554
                if (gm->getTile(s->getPos())->getBuilding() == Maptile::CITY)
488
555
                  {
489
556
                    Citylist *clist = Citylist::getInstance();
527
594
                break;
528
595
            
529
596
            // create the city
530
 
            City c(tile);
531
 
            c.setOwner(Playerlist::getInstance()->getNeutral());
 
597
            City *c = new City(tile);
 
598
            c->setOwner(Playerlist::getInstance()->getNeutral());
532
599
            Citylist::getInstance()->push_back(c);
533
600
 
534
601
            bool replaced_grass = false;
535
602
            // notify the maptiles that a city has been placed here
536
 
            Rectangle r = c.get_area();
 
603
            Rectangle r = c->get_area();
537
604
            for (int x = r.x; x < r.x + r.w; ++x)
538
605
                for (int y = r.y; y < r.y + r.h; ++y)
539
606
                {
545
612
                }
546
613
 
547
614
            //change allegiance of stacks under this city
548
 
            for (unsigned int x = 0; x < c.getSize(); x++)
 
615
            for (unsigned int x = 0; x < c->getSize(); x++)
549
616
              {
550
 
                for (unsigned int y = 0; y < c.getSize(); y++)
 
617
                for (unsigned int y = 0; y < c->getSize(); y++)
551
618
                  {
552
 
                    Stack *s = Stacklist::getObjectAt(c.getPos().x + x, 
553
 
                                                      c.getPos().y + y);
 
619
                    Stack *s = Stacklist::getObjectAt(c->getPos().x + x, 
 
620
                                                      c->getPos().y + y);
554
621
                    if (s)
555
622
                      {
556
 
                        if (s->getOwner() != c.getOwner())
 
623
                        if (s->getOwner() != c->getOwner())
557
624
                          {
558
625
                            //remove it from the old player's list of stacks
559
626
                            s->getOwner()->getStacklist()->remove(s);
560
627
                            //and give it to the new player list of stacks
561
 
                            c.getOwner()->getStacklist()->push_back(s);
 
628
                            c->getOwner()->getStacklist()->push_back(s);
562
629
                            //change the ownership of the stack
563
 
                            s->setOwner(c.getOwner());
 
630
                            s->setOwner(c->getOwner());
564
631
                            //and all of it's armies
565
632
                            for (Stack::iterator it = s->begin(); 
566
633
                                 it != s->end(); it++)
567
 
                              (*it)->setOwner(c.getOwner());
 
634
                              (*it)->setOwner(c->getOwner());
568
635
                          }
569
636
                      }
570
637
                  }
588
655
                  replaced_grass = true;
589
656
                maptile->setType(grass_index);
590
657
                Ruin *ruin = new Ruin(tile);
591
 
                Ruinlist::getInstance()->push_back(*ruin);
 
658
                Ruinlist::getInstance()->push_back(ruin);
592
659
                if (replaced_grass)
593
660
                  GameMap::getInstance()->applyTileStyles
594
661
                    (0, 0, GameMap::getHeight(), GameMap::getWidth(), true);
604
671
                if (maptile->getMaptileType() != Tile::GRASS)
605
672
                  replaced_grass = true;
606
673
                maptile->setType(grass_index);
607
 
                Templelist::getInstance()->push_back(Temple(tile));
 
674
                Templelist::getInstance()->push_back(new Temple(tile));
608
675
                if (replaced_grass)
609
676
                  GameMap::getInstance()->applyTileStyles
610
677
                    (0, 0, GameMap::getHeight(), GameMap::getWidth(), true);
616
683
                && maptile->getMaptileType() == Tile::GRASS)
617
684
            {
618
685
                maptile->setBuilding(Maptile::SIGNPOST);
619
 
                Signpostlist::getInstance()->push_back(Signpost(tile));
 
686
                Signpostlist::getInstance()->push_back(new Signpost(tile));
620
687
            }
621
688
            break;
622
689
            
626
693
                maptile->getTileStyle()->getType() != TileStyle::INNERMIDDLECENTER)
627
694
            {
628
695
                maptile->setBuilding(Maptile::PORT);
629
 
                Portlist::getInstance()->push_back(Port(tile));
 
696
                Portlist::getInstance()->push_back(new Port(tile));
 
697
                if (Stacklist::getObjectAt(tile))
 
698
                  {
 
699
                    Stack* s = Stacklist::getObjectAt(tile);
 
700
                    if (s->hasShip() == true)
 
701
                      {
 
702
                        for (Stack::iterator it = s->begin(); it != s->end(); 
 
703
                             it++)
 
704
                          (*it)->setInShip(false);
 
705
                      }
 
706
                  }
630
707
            }
631
708
            break;
632
709
            
641
718
                else
642
719
                {
643
720
                    maptile->setBuilding(Maptile::BRIDGE);
644
 
                    Bridgelist::getInstance()->push_back(Bridge(tile, type));
 
721
                    Bridgelist::getInstance()->push_back(new Bridge(tile, type));
 
722
                    if (Stacklist::getObjectAt(tile))
 
723
                      {
 
724
                        Stack* s = Stacklist::getObjectAt(tile);
 
725
                        if (s->hasShip() == true)
 
726
                          {
 
727
                            for (Stack::iterator it = s->begin(); it != s->end(); 
 
728
                                 it++)
 
729
                              (*it)->setInShip(false);
 
730
                          }
 
731
                      }
645
732
                }
646
733
                changed_tiles.dim = Vector<int>(1, 1);
647
734
            }
652
739
                 || maptile->getBuilding() == Maptile::ROAD)
653
740
                && maptile->getMaptileType() != Tile::WATER)
654
741
            {
655
 
                int type = calculateRoadType(tile);
 
742
                int type = CreateScenario::calculateRoadType(tile);
656
743
                if (maptile->getBuilding() == Maptile::NONE)
657
744
                {
658
745
                    maptile->setBuilding(Maptile::ROAD);
659
 
                    Roadlist::getInstance()->push_back(Road(tile, type));
 
746
                    Roadlist::getInstance()->push_back(new Road(tile, type));
660
747
                }
661
748
 
662
749
                // now reconfigure all roads in the surroundings
663
750
                for (int x = tile.x - 1; x <= tile.x + 1; ++x)
664
751
                    for (int y = tile.y - 1; y <= tile.y + 1; ++y)
665
752
                    {
666
 
                        if (x < 0 || x >= GameMap::getWidth() &&
667
 
                            y < 0 || y >= GameMap::getHeight())
 
753
                        if ((x < 0 || x >= GameMap::getWidth()) &&
 
754
                            (y < 0 || y >= GameMap::getHeight()))
668
755
                            continue;
669
756
 
670
757
                        Vector<int> pos(x, y);
671
758
                        if (Road *r = Roadlist::getInstance()->getObjectAt(pos))
672
759
                        {
673
 
                            int newtype = calculateRoadType(pos);
 
760
                            int newtype = CreateScenario::calculateRoadType(pos);
674
761
                            r->setType(newtype);
675
762
                        }
676
763
                    }
694
781
                                          view.x+view.w, true);
695
782
  draw();
696
783
}
697
 
 
698
 
int EditorBigMap::calculateRoadType (Vector<int> t)
699
 
{
700
 
    Roadlist *rl = Roadlist::getInstance();
701
 
    Bridgelist *bl = Bridgelist::getInstance();
702
 
 
703
 
    // examine neighbour tiles to discover whether there's a road or
704
 
    // bridge on them
705
 
    bool u = false; //up
706
 
    bool b = false; //bottom
707
 
    bool l = false; //left
708
 
    bool r = false; //right
709
 
 
710
 
    if (t.y > 0)
711
 
      u = rl->getObjectAt(t + Vector<int>(0, -1));
712
 
    if (t.y < GameMap::getHeight() - 1)
713
 
      b = rl->getObjectAt(t + Vector<int>(0, 1));
714
 
    if (t.x > 0)
715
 
      l = rl->getObjectAt(t + Vector<int>(-1, 0));
716
 
    if (t.x < GameMap::getWidth() - 1)
717
 
      r = rl->getObjectAt(t + Vector<int>(1, 0));
718
 
 
719
 
    if (!u && t.y > 0)
720
 
      u = bl->getObjectAt(t + Vector<int>(0, -1));
721
 
    if (!b && t.y < GameMap::getHeight() - 1)
722
 
      b = bl->getObjectAt(t + Vector<int>(0, 1));
723
 
    if (!l && t.x > 0)
724
 
      l = bl->getObjectAt(t + Vector<int>(-1, 0));
725
 
    if (!r && t.x < GameMap::getWidth() - 1)
726
 
      r = bl->getObjectAt(t + Vector<int>(1, 0));
727
 
 
728
 
    // then translate this to the type
729
 
    int type = 2; 
730
 
    //show road type 2 when no other road tiles are around
731
 
    if (!u && !b && !l && !r)
732
 
        type = 2;
733
 
    else if (u && b && l && r)
734
 
        type = 2;
735
 
    else if (!u && b && l && r)
736
 
        type = 9;
737
 
    else if (u && !b && l && r)
738
 
        type = 8;
739
 
    else if (u && b && !l && r)
740
 
        type = 7;
741
 
    else if (u && b && l && !r)
742
 
        type = 10;
743
 
    else if (u && b && !l && !r)
744
 
        type = 1;
745
 
    else if (!u && !b && l && r)
746
 
        type = 0;
747
 
    else if (u && !b && l && !r)
748
 
        type = 3;
749
 
    else if (u && !b && !l && r)
750
 
        type = 4;
751
 
    else if (!u && b && l && !r)
752
 
        type = 6;
753
 
    else if (!u && b && !l && r)
754
 
        type = 5;
755
 
    else if (u && !b && !l && !r)
756
 
        type = 1;
757
 
    else if (!u && b && !l && !r)
758
 
        type = 1;
759
 
    else if (!u && !b && l && !r)
760
 
        type = 0;
761
 
    else if (!u && !b && !l && r)
762
 
        type = 0;
763
 
    return type;
764
 
}
765