Ver código fonte

Write files on add map functionality. Still need to address gMapGroups

Marcus Huderle 6 anos atrás
pai
commit
f6cb002592
1 arquivos alterados com 149 adições e 0 exclusões
  1. 149
    0
      project.cpp

+ 149
- 0
project.cpp Ver arquivo

@@ -6,6 +6,7 @@
6 6
 #include "event.h"
7 7
 
8 8
 #include <QDebug>
9
+#include <QDir>
9 10
 #include <QFile>
10 11
 #include <QTextStream>
11 12
 #include <QStandardItem>
@@ -542,6 +543,154 @@ void Project::readMapGroups() {
542 543
 }
543 544
 
544 545
 void Project::addNewMapToGroup(QString mapName, int groupNum) {
546
+    int mapIndex = 0;// TODO: need to calculate the new map index.
547
+
548
+    // Write new map to project files.
549
+    // 1. Create directory data/maps/<map_name>/
550
+    // 2. Create file data/maps/<map_name>/border.bin
551
+    // 3. Create file data/maps/<map_name>/header.inc
552
+    // 4. Create file data/maps/<map_name>/map.bin
553
+    // 5. Create file data/maps/events/<map_name>.inc
554
+    // 6. Create file data/scripts/maps/<map_name>.inc
555
+    // 7. Create file data/text/maps/<map_name>.inc
556
+    // 8. Modify data/event_scripts.s:
557
+    //     .include "data/scripts/maps/<map_name>.inc"
558
+    //     .include "data/text/maps/<map_name>.inc"
559
+    // 9. Modify data/map_events.s:
560
+    //     .include "data/maps/events/<map_name>.inc"
561
+    // 10. Modify data/maps/_assets.inc
562
+    // 11. Modify data/maps/_groups.inc
563
+    // 12. Modify data/maps/attributes_table.inc
564
+    // 13. Modify data/maps/headers.inc
565
+
566
+    // 1. Create directory data/maps/<map_name>/
567
+    QString dataDir = QString("%1/data/").arg(root);
568
+    QString dataMapsDir = QString("%1maps/").arg(dataDir);
569
+    QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName);
570
+    if (!QDir::root().mkdir(newMapDataDir)) {
571
+        qDebug() << "Error: failed to create directory for new map. " << newMapDataDir;
572
+        return;
573
+    }
574
+
575
+    // 2. Create file data/maps/<map_name>/border.bin
576
+    QFile borderFile(newMapDataDir + "border.bin");
577
+    borderFile.open(QIODevice::WriteOnly);
578
+    QDataStream borderStream(&borderFile);
579
+    borderStream.setByteOrder(QDataStream::LittleEndian);
580
+    borderStream << qint16(0x01D4) << qint16(0x01D5) << qint16(0x01DC) << qint16(0x01DD);
581
+    borderFile.close();
582
+
583
+    // 3. Create file data/maps/<map_name>/header.inc
584
+    QFile headerFile(newMapDataDir + "header.inc");
585
+    headerFile.open(QIODevice::WriteOnly);
586
+    QTextStream headerStream(&headerFile);
587
+    headerStream << mapName << "::" << endl
588
+                 << "\t.4byte " << mapName << "_MapAttributes" << endl
589
+                 << "\t.4byte " << mapName << "_MapEvents" << endl
590
+                 << "\t.4byte " << mapName << "_MapScripts" << endl
591
+                 << "\t.4byte 0x0" << endl
592
+                 << "\t.2byte BGM_DAN02" << endl
593
+                 << "\t.2byte " << mapIndex << endl
594
+                 << "\t.byte 0" << endl
595
+                 << "\t.byte 0" << endl
596
+                 << "\t.byte 11" << endl
597
+                 << "\t.byte 4" << endl
598
+                 << "\t.2byte 0" << endl
599
+                 << "\t.byte 1" << endl
600
+                 << "\t.byte 0" << endl;
601
+    headerFile.close();
602
+
603
+    // 4. Create file data/maps/<map_name>/map.bin
604
+    QFile mapFile(newMapDataDir + "map.bin");
605
+    mapFile.open(QIODevice::WriteOnly);
606
+    QDataStream mapStream(&mapFile);
607
+    mapStream.setByteOrder(QDataStream::LittleEndian);
608
+    for (int i = 0; i < 20 * 20; i++) {
609
+        mapStream << qint16(0x3001);
610
+    }
611
+    mapFile.close();
612
+
613
+    // 5. Create file data/maps/events/<map_name>.inc
614
+    QFile eventsFile(dataMapsDir + "events/" + mapName + ".inc");
615
+    eventsFile.open(QIODevice::WriteOnly);
616
+    QTextStream eventsStream(&eventsFile);
617
+    eventsStream << mapName << "_MapEvents::" << endl
618
+                 << "\tmap_events 0x0, 0x0, 0x0, 0x0" << endl;
619
+    eventsFile.close();
620
+
621
+    // 6. Create file data/scripts/maps/<map_name>.inc
622
+    QFile scriptsFile(dataDir + "scripts/maps/" + mapName + ".inc");
623
+    scriptsFile.open(QIODevice::WriteOnly);
624
+    QTextStream scriptsStream(&scriptsFile);
625
+    scriptsStream << mapName << "_MapScripts::" << endl
626
+                  << "\t.byte 0" << endl;
627
+    scriptsFile.close();
628
+
629
+    // 7. Create file data/text/maps/<map_name>.inc
630
+    QFile textFile(dataDir + "text/maps/" + mapName + ".inc");
631
+    textFile.open(QIODevice::WriteOnly);
632
+    QTextStream textStream(&textFile);
633
+    textStream << endl;
634
+    textFile.close();
635
+
636
+    // 8. Modify data/event_scripts.s:
637
+    QFile eventScriptsFile(dataDir + "event_scripts.s");
638
+    eventScriptsFile.open(QIODevice::Append);
639
+    QTextStream eventScriptsStream(&eventScriptsFile);
640
+    eventScriptsStream << endl
641
+                       << "\t.include \"data/scripts/maps/" << mapName << ".inc\"" << endl
642
+                       << "\t.include \"data/text/maps/" << mapName << ".inc\"" << endl;
643
+    eventScriptsFile.close();
644
+
645
+    // 9. Modify data/map_events.s:
646
+    QFile mapEventsFile(dataDir + "map_events.s");
647
+    mapEventsFile.open(QIODevice::Append);
648
+    QTextStream mapEventsStream(&mapEventsFile);
649
+    mapEventsStream << endl
650
+                    << "\t.include \"data/maps/events/" << mapName << ".inc\"" << endl;
651
+    mapEventsFile.close();
652
+
653
+    // 10. Modify data/maps/_assets.inc
654
+    QFile assetsFile(dataMapsDir + "_assets.inc");
655
+    assetsFile.open(QIODevice::Append);
656
+    QTextStream assetsStream(&assetsFile);
657
+    assetsStream << endl
658
+                 << mapName << "_MapBorder::" << endl
659
+                 << "\t.incbin \"data/maps/" << mapName << "/border.bin\"" << endl
660
+                 << endl
661
+                 << mapName << "_MapBlockdata::" << endl
662
+                 << "\t.incbin \"data/maps/" << mapName << "/map.bin\"" << endl
663
+                 << endl
664
+                 << "\t.align 2" << endl
665
+                 << mapName << "_MapAttributes::" << endl
666
+                 << "\t.4byte 0x14" << endl
667
+                 << "\t.4byte 0x14" << endl
668
+                 << "\t.4byte " << mapName << "_MapBorder" << endl
669
+                 << "\t.4byte " << mapName << "_MapBlockdata" << endl
670
+                 << "\t.4byte gTileset_General" << endl
671
+                 << "\t.4byte gTileset_Pacifidlog" << endl
672
+                 << endl;
673
+    assetsFile.close();
674
+
675
+    // 11. Modify data/maps/_groups.inc
676
+    // TODO:
677
+
678
+    // 12. Modify data/maps/attributes_table.inc
679
+    QFile attributesFile(dataMapsDir + "attributes_table.inc");
680
+    attributesFile.open(QIODevice::Append);
681
+    QTextStream attributesStream(&attributesFile);
682
+    attributesStream << endl
683
+                    << "\t.4byte " << mapName << "_MapAttributes" << endl;
684
+    attributesFile.close();
685
+
686
+    // 13. Modify data/maps/headers.inc
687
+    QFile headersFile(dataMapsDir + "headers.inc");
688
+    headersFile.open(QIODevice::Append);
689
+    QTextStream headersStream(&headersFile);
690
+    headersStream << endl
691
+                    << "\t.include \"data/maps/" << mapName << "/header.inc\"" << endl;
692
+    headersFile.close();
693
+
545 694
     mapNames->append(mapName);
546 695
     map_groups->insert(mapName, groupNum);
547 696
     groupedMapNames->value(groupNum)->append(mapName);