简体   繁体   English

存储文本冒险区域数据的最佳方法是什么?

[英]What is the best way to store area data for a text adventure?

I'm developing a "Zork" style text adventure in C#, and it's going to have a fairly large number of different areas with descriptions and environmental modifiers. 我正在用C#开发一个“Zork”风格的文本冒险,它将有相当多的不同领域,包括描述和环境修饰符。 I don't want to have a database, ideally, unless it really is the best way of doing it. 理想情况下,我不想拥有一个数据库,除非它确实是最好的方法。

I need advice on the best way to store/load this data. 我需要有关存储/加载此数据的最佳方法的建议。

It will include: 它将包括:

  • Area description 区域描述
  • Environmental modifiers (windows open/broken, door closed) 环境改良剂(窗户打开/折断,门关闭)
  • Items present by default 默认情况下存在的项目

I would solve your problem by abandoning C# and writing your program in Inform7 . 我会通过放弃C#并在Inform7中编写程序来解决您的问题。 Inform7 is just about the most awesome programming language I have ever seen and it is specifically designed to solve your problem. Inform7是我见过的最棒的编程语言,它专门用于解决您的问题。

The awesome thing about Inform7 is that you write your text adventure in a language that resembles text adventures. 关于Inform7的一个很棒的事情就是你用类似文本冒险的语言编写文本冒险。 For example, here's a fragment of one of the sample adventures' source code: 例如,这是一个示例冒险源代码的片段:

The iron-barred gate is a door. 
"An iron-barred gate leads [gate direction]." 
It is north of the Drawbridge and south of the Entrance Hall. 
It is closed and openable. 
Before entering the castle, try entering the gate instead. 
Before going inside in the Drawbridge, try going north instead. 
Understand "door" as the gate.

This adds an object to the game - the object is a door, it is called "the iron-barred gate". 这为游戏添加了一个对象 - 对象是一扇门,它被称为“铁栅栏”。 A door is understood to be between two rooms, in this case, the drawbridge and the entrance hall. 门被理解为在两个房间之间,在这种情况下,是吊桥和入口大厅。 If the player tries to "enter the drawbridge" then the game logic will know that this is the same as "go north", and then the door logic will determine whether the door is closed or not. 如果玩家试图“进入吊桥”,则游戏逻辑将知道这与“向北”相同,然后门逻辑将确定门是否关闭。 And so on. 等等。 It makes writing text adventures extremely easy. 它使写文本冒险非常容易。

Is there some particular reason why you want to use C# instead of a domain-specific language like Inform7? 您是否有一些特殊原因要使用C#而不是像Inform7这样的特定于域的语言? If your goal is to learn how to write C# code or how to build a parser or whatever, then by all means do it yourself. 如果你的目标是学习如何编写C#代码或如何构建解析器或其他什么,那么一定要自己动手。 If your goal is to write a text adventure, then I'd use a language designed for that. 如果您的目标是编写文本冒险,那么我将使用专为此设计的语言。

Serialize all the data to file. 将所有数据序列化为文件。 It will ensure the smallest footprint when the user installs the game, without any real disadvantage. 当用户安装游戏时,它将确保最小的占地面积,没有任何真正的缺点。 A database is great when you have a lot of data, but you are talking about a text adventure in which you will load the entire game contents into memory. 当您拥有大量数据时,数据库非常棒,但您正在谈论文本冒险,您可以在其中将整个游戏内容加载到内存中。 A simple file will work for this very nicely. 一个简单的文件可以非常好地工作。

Note, I'm not talking about xml but binary serialization. 注意,我不是在讨论xml而是二进制序列化。 Any kind of text serialization will allow users to peek at your data, and either cheat or hack up the game. 任何类型的文本序列化都将允许用户查看您的数据,并欺骗或破解游戏。 And you can just as easily swap in/out the serialized data file whether it's text or binary. 您可以轻松地交换/输出序列化数据文件,无论是文本还是二进制文件。 Remember, your whole 'text' is likely to be just a few hundred kilobytes at most. 请记住,您的整个“文本”最多可能只有几百KB。

There are many interactive fiction engines already. 已经有很多交互式小说引擎。 I would take a look at their data formats, that way you can re-use existing content and tools for editing the content. 我会看一下他们的数据格式,这样你就可以重用现有的内容和工具来编辑内容。

The most popular engines currently are Glulx http://eblong.com/zarf/glulx/ and Z-Machine http://en.wikipedia.org/wiki/Z-machine 目前最流行的引擎是Glulx http://eblong.com/zarf/glulx/和Z-Machine http://en.wikipedia.org/wiki/Z-machine

Here is a technical reference for the Glulx format: http://eblong.com/zarf/glulx/technical.txt 以下是Glulx格式的技术参考: http ://eblong.com/zarf/glulx/technical.txt

I know you didn't want a DB, but have you looked at SQL Server Compact Edition ? 我知道你不想要数据库,但是你看过SQL Server Compact Edition吗? It might just do what you want. 它可能只是做你想要的。

I'd argue C# offers you precisely the right tools for this. 我认为C#为您提供了恰当的工具。 Just encapsulate your structures into classes. 只需将结构封装到类中。 Our first OOP project at university was exactly this problem! 我们在大学的第一个OOP项目就是这个问题! It is the perfect case study for OOP. 这是OOP的完美案例研究。

You can then use C#'s many serialization methods to store it persistently (load/save) however you see fit. 然后,您可以使用C#的许多序列化方法来持久存储它(加载/保存),但您认为合适。

How would 'scripting' your adventure as one large text file sound? 如何将您的冒险作为一个大型文本文件进行“脚本编写”? Then have your application parse this file, build the adventure in classes and run from there? 然后让您的应用程序解析此文件,在类中构建冒险并从那里运行?

This would mean you could edit the adventure using a simple text editor. 这意味着您可以使用简单的文本编辑器编辑冒险。 I would imagine that when multiple decisions can be made from a single source it may become tricky visualising the links. 我可以想象,当可以从单一来源做出多个决策时,可能会变得难以想象链接。 However this would also be tricky in a DB without some specialist front-end. 然而,如果没有一些专业的前端,这在数据库中也会很棘手。

UPDATE: 更新:

Or have you considered XML eg... 或者您考虑过XML,例如......

<area id="DarkRoom1">
  <description>Dark Room</description>
  <item>Bucket</item>
  <item>Spade</item>
</area>

Then use this to populate your classes in memory. 然后使用它来填充内存中的类。

You could store the data in a file system (a zip file or a folder). 您可以将数据存储在文件系统(zip文件或文件夹)中。

Each choice could be stored as a folder, while all descriptions, modifiers and other data could be stored as a text file (xml?). 每个选项都可以存储为文件夹,而所有描述,修饰符和其他数据都可以存储为文本文件(xml?)。 When user makes a decision you go to the appropriate folder and follow the plot. 当用户做出决定时,您将转到相应的文件夹并按照图表进行操作。

Example: 例:

Do you want to: 你想要_____吗:

  • open the door (door) 打开门(门)
  • leave (leave) 离开(离开)

If user choses to open the door you go the folder door and reads data from data file in this folder. 如果用户选择打开门,则转到文件夹门并从此文件夹中的数据文件中读取数据。

Pros: 优点:

  • simple 简单
  • do not require database 不需要数据库
  • easy to add new adventures 很容易添加新的冒险

Cons: 缺点:

  • problem with rollback decision (getting back to start or to certain point in plot) 回滚决定的问题(回到开始或在图中的某个点)

Personally, I'd avoid a database in this case, and go with a text-based file format (probably two distinct files, one for the initial state (like terrain and such), which never gets modified, and one for state that is to be modified during the course of the game (the broken windows etc.); or split the entire thing up into one pair of static/dynamic data per area. 就个人而言,在这种情况下,我会避免使用数据库,并使用基于文本的文件格式(可能是两个不同的文件,一个用于初始状态(如地形等),永远不会被修改,一个用于状态,即在游戏过程中被修改(破窗等);或者将每个区域的整个事物分成一对静态/动态数据。

A few reasons: 原因如下:

  • A text file is human-readable; 文本文件是人类可读的; hence, you can create content without a dedicated editor, while with a database approach, you'd either have to enter data through queries, or code a level editor 因此,您可以在没有专用编辑器的情况下创建内容,而使用数据库方法,您必须通过查询输入数据,或者编写级别编辑器
  • Assuming a single-player scenario, concurrency is not an issue 假设单人游戏场景,并发性不是问题
  • Savegames are a matter of copying the modified-state files into a savegame folder, or packing them into a single file Savegames是将修改后的状态文件复制到savegame文件夹或将它们打包到单个文件中的问题
  • You can easily embed scripts 您可以轻松嵌入脚本
  • The data structures you're dealing with are probably simple enough for data integrity not to be a serious issue 您正在处理的数据结构可能非常简单,数据完整性不是一个严重的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM