597 :(NSString *)fileToOpen asNew:(BOOL)asNew
598{
599
600
601
602
603
604
605
606
607
608 BOOL loadedOK = YES;
609 NSDictionary *fileDic =
nil;
610 NSString *fail_reason =
nil;
611
612 if (fileToOpen ==
nil)
613 {
614 fail_reason =
DESC(
@"loadfailed-no-file-specified");
615 loadedOK = NO;
616 }
617
618 if (loadedOK)
619 {
620 OOLog(
@"load.progress",
@"%@",
@"Reading file");
623 {
624 fail_reason =
DESC(
@"loadfailed-could-not-load-file");
625 loadedOK = NO;
626 }
627 }
628
629 if (loadedOK)
630 {
631 OOLog(
@"load.progress",
@"%@",
@"Restricting scenario");
632 NSString *scenarioRestrict = [fileDic oo_stringForKey:@"scenario_restriction" defaultValue:nil];
633 if (scenarioRestrict ==
nil)
634 {
635
636 BOOL strict = [fileDic oo_boolForKey:@"strict" defaultValue:NO];
637 if (strict)
638 {
640 }
641 else
642 {
644 }
645 }
646
647 if (![
UNIVERSE setUseAddOns:scenarioRestrict fromSaveGame:YES forceReinit:YES])
648 {
649 fail_reason =
DESC(
@"loadfailed-saved-game-failed-to-load");
650 loadedOK = NO;
651 }
652 }
653
654
655 if (loadedOK)
656 {
657 OOLog(
@"load.progress",
@"%@",
@"Creating player ship");
658
659 NSString *shipKey =
nil;
660 NSDictionary *shipDict =
nil;
661
662 shipKey = [fileDic oo_stringForKey:@"ship_desc"];
664
666 {
667 loadedOK = NO;
668 if (shipKey !=
nil) fail_reason = [NSString stringWithFormat:DESC(@"loadfailed-could-not-find-ship-type-@-please-reinstall-the-appropriate-OXP"), shipKey];
669 else fail_reason =
DESC(
@"loadfailed-invalid-saved-game-no-ship-specified");
670 }
671 }
672
673 if (loadedOK)
674 {
675 OOLog(
@"load.progress",
@"%@",
@"Initialising player entity");
676 if (![self setUpAndConfirmOK:YES saveGame:YES])
677 {
678 fail_reason =
DESC(
@"loadfailed-could-not-reset-javascript");
679 loadedOK = NO;
680 }
681 }
682
683 if (loadedOK)
684 {
685 OOLog(
@"load.progress",
@"%@",
@"Loading commander data");
686 if (![self setCommanderDataFromDictionary:fileDic])
687 {
688
689
690 fail_reason =
DESC(
@"loadfailed-could-not-set-up-player-ship");
691 loadedOK = NO;
692 }
693 }
694
695 if (loadedOK)
696 {
697 OOLog(
@"load.progress",
@"%@",
@"Recording save path");
698 if (!asNew)
699 {
700 [save_path autorelease];
701 save_path = [fileToOpen retain];
702
703 [[[UNIVERSE gameView] gameController] setPlayerFileToLoad:fileToOpen];
704 [[[UNIVERSE gameView] gameController] setPlayerFileDirectory:fileToOpen];
705 }
706 }
707 else
708 {
709 OOLog(
@"load.failed",
@"***** Failed to load saved game \"%@\
": %@", [fileToOpen lastPathComponent], fail_reason ? fail_reason : (NSString *)
@"unknown error");
710 [[UNIVERSE gameController] setPlayerFileToLoad:nil];
711 [UNIVERSE handleGameOver];
712 [UNIVERSE clearPreviousMessage];
713 [UNIVERSE addMessage:DESC(@"loadfailed-saved-game-failed-to-load") forCount: 9.0];
714 if (fail_reason !=
nil) [UNIVERSE addMessage: fail_reason forCount: 9.0];
715 return NO;
716 }
717
718 OOLog(
@"load.progress",
@"%@",
@"Creating system");
719 [UNIVERSE setTimeAccelerationFactor:TIME_ACCELERATION_FACTOR_DEFAULT];
720 [UNIVERSE setSystemTo:system_id];
721 [UNIVERSE removeAllEntitiesExceptPlayer];
722 [UNIVERSE setGalaxyTo: galaxy_number andReinit:YES];
723 [UNIVERSE setUpSpace];
724 [UNIVERSE setAutoSaveNow:NO];
725
726 OOLog(
@"load.progress",
@"%@",
@"Resetting player flight variables");
727 [self setDockedAtMainStation];
729
730 [UNIVERSE enterGUIViewModeWithMouseInteraction:NO];
731
732 if (dockedStation)
733 {
734 position = [dockedStation
position];
735 [self setOrientation: kIdentityQuaternion];
739 }
740
741 flightRoll = 0.0;
742 flightPitch = 0.0;
743 flightYaw = 0.0;
744 flightSpeed = 0.0;
745
746 [self setEntityPersonalityInt:PersonalityForCommanderDict(fileDic)];
747
748 OOLog(
@"load.progress",
@"%@",
@"Loading system market");
749
750
751 NSArray *market = [fileDic oo_arrayForKey:@"localMarket"];
753 {
754 [dockedStation setLocalMarket:market];
755 }
756 else
757 {
758 [dockedStation initialiseLocalMarket];
759 }
760
761 [self calculateCurrentCargo];
762
763 OOLog(
@"load.progress",
@"%@",
@"Setting scenario key");
764
765 NSString *scenario = [fileDic oo_stringForKey:@"scenario_key" defaultValue:nil];
768 {
769 scenarioKey = [scenario retain];
770 }
771
772 OOLog(
@"load.progress",
@"%@",
@"Starting JS engine");
773
774 [self completeSetUpAndSetTarget:NO];
775
776 OOLog(
@"load.progress",
@"%@",
@"Populating initial system");
777 [UNIVERSE populateNormalSpace];
778
779
781
782
783
784
785 HPVector dockedPos = [fileDic oo_hpvectorForKey:@"docked_station_position"];
786 NSString *dockedRole = [fileDic oo_stringForKey:@"docked_station_role" defaultValue:@""];
787 StationEntity *saveStation = [UNIVERSE stationWithRole:dockedRole andPosition:dockedPos];
788 if (saveStation !=
nil && [saveStation allowsSaving])
789 {
790 [self setDockedStation:saveStation];
792 }
793
794 [UNIVERSE loadStationMarkets:[fileDic oo_arrayForKey:@"station_markets"]];
795
796 OOLog(
@"load.progress",
@"%@",
@"Completing JS startup");
797 [self startUpComplete];
798
799 [[UNIVERSE gameView] suppressKeysUntilKeyUp];
800 gui_screen = GUI_SCREEN_LOAD;
801 [self setGuiToStatusScreen];
802 if (loadedOK) [self doWorldEventUntilMissionScreen:OOJSID("missionScreenOpportunity")];
803 OOLog(
@"load.progress",
@"%@",
@"Loading complete");
804 return loadedOK;
805}
#define OOLog(class, format,...)
NSDictionary * OODictionaryFromFile(NSString *path)
Vector vector_up_from_quaternion(Quaternion quat)
Vector vector_right_from_quaternion(Quaternion quat)
Vector vector_forward_from_quaternion(Quaternion quat)
#define SCENARIO_OXP_DEFINITION_NONE
#define SCENARIO_OXP_DEFINITION_ALL
OOJavaScriptEngine * sharedEngine()
void garbageCollectionOpportunity:(BOOL force)
OOShipRegistry * sharedRegistry()
NSDictionary * shipInfoForKey:(NSString *key)