File: script_machine.cc

package info (click to toggle)
rlvm 0.14-2.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 16,964 kB
  • ctags: 33,744
  • sloc: cpp: 91,571; ansic: 39,346; perl: 768; python: 181; sh: 170; makefile: 8
file content (146 lines) | stat: -rw-r--r-- 5,005 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi:tw=80:et:ts=2:sts=2
//
// -----------------------------------------------------------------------
//
// This file is part of RLVM, a RealLive virtual machine clone.
//
// -----------------------------------------------------------------------
//
// Copyright (C) 2008 Elliot Glaysher
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------

#include "script_machine/script_machine.h"

#include <iostream>
#include <map>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

#include "libreallive/intmemref.h"
#include "long_operations/button_object_select_long_operation.h"
#include "long_operations/select_long_operation.h"
#include "machine/serialization.h"
#include "script_machine/script_world.h"

using namespace std;
using libreallive::IntMemRef;

// -----------------------------------------------------------------------
// ScriptMachine
// -----------------------------------------------------------------------
ScriptMachine::ScriptMachine(ScriptWorld& world,
                             System& in_system,
                             libreallive::Archive& in_archive)
    : RLMachine(in_system, in_archive),
      world_(world),
      current_decision_(0),
      save_on_decision_slot_(-1),
      increment_on_save_(false) {}

ScriptMachine::~ScriptMachine() {}

void ScriptMachine::SetDecisionList(const std::vector<std::string>& decisions) {
  decisions_ = decisions;
  current_decision_ = 0;
}

void ScriptMachine::PushLongOperation(LongOperation* long_operation) {
  // Intercept various LongOperations and modify them.
  if (SelectLongOperation* sel =
          dynamic_cast<SelectLongOperation*>(long_operation)) {
    // Try our optional, script provided matching behaviour.
    std::string choice = world_.MakeDecision(sel->GetOptions());
    bool optionFound = false;
    if (choice != "") {
      optionFound = sel->SelectByText(choice);
      if (!optionFound) {
        cerr << "WTF? Script decision handler returned invalid choice" << endl;
      } else {
        cerr << "Selected '" << choice << "' (Chosen by decision handler)"
             << endl;
      }
    }

    // Try our normal matching behaviour.
    if (!optionFound) {
      int offset = 0;
      for (; offset < 4; ++offset) {
        if (current_decision_ + offset >= decisions_.size()) {
          cerr << "WARNING! Ran out of options on decision list." << endl;
          break;
        }

        std::string to_select = decisions_.at(current_decision_ + offset);
        optionFound = sel->SelectByText(to_select);

        if (optionFound) {
          cerr << "Selected '" << to_select << "'";
          if (offset > 0)
            cerr << "(Skipped " << offset << " selections)";
          cerr << endl;

          if (save_on_decision_slot_ != -1) {
            cerr << "(Automatically saving to slot " << save_on_decision_slot_
                 << ")" << endl;
            Serialization::saveGameForSlot(*this, save_on_decision_slot_);

            if (increment_on_save_) {
              save_on_decision_slot_++;
              if (save_on_decision_slot_ > 98) {
                cerr << "Warning: Overflowing save count." << endl;
                save_on_decision_slot_ = 0;
              }
            }
          }

          break;
        }
      }

      if (!optionFound) {
        cerr << "WARNING! Couldn't call option " << current_decision_
             << ". Options are: " << endl;

        for (const std::string& option : sel->GetOptions())
          cerr << "- \"" << option << "\"" << endl;

        current_decision_++;
      } else {
        current_decision_ += offset + 1;
      }
    }
  } else if (ButtonObjectSelectLongOperation* sel =
                 dynamic_cast<ButtonObjectSelectLongOperation*>(
                     long_operation)) {
    // We don't deal with these yet. Return 1 for the first option every time,
    // which corresponds with selecting "NO" for battle missions.
    set_store_register(1);
    delete sel;
    return;
  }

  RLMachine::PushLongOperation(long_operation);
}

int ScriptMachine::GetInt(const std::string& bank, int position) {
  char bchar = bank[0];

  return GetIntValue(IntMemRef(bchar, position));
}