view libmlk-rpg/mlk/rpg/quest.c @ 445:773a082f0b91

misc: update copyright years
author David Demelier <markand@malikania.fr>
date Wed, 01 Feb 2023 13:08:54 +0100
parents 4e78f045e8c0
children e1fa1b867281
line wrap: on
line source

/*
 * quest.c -- in game quests
 *
 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <assert.h>

#include <assets/sql/quest-remove.h>
#include <assets/sql/quest-save.h>
#include <assets/sql/quest-step-load.h>
#include <assets/sql/quest-step-save.h>

#include "quest.h"
#include "save.h"

int
quest_save(struct quest *q, struct save *s)
{
	assert(q);
	assert(s);

	const struct quest_step *step;

	if (save_tx_begin(s) < 0)
		return -1;

	if (save_exec(s, (const char *)assets_quest_save, "s", q->name) < 0) {
		save_tx_rollback(s);
		return -1;
	}

	for (size_t i = 0; i < q->stepsz; ++i) {
		step = &q->steps[i];

		if (save_exec(s, (const char *)assets_quest_step_save, "ssi", q->name, step->name, step->percent) < 0) {
			save_tx_rollback(s);
			return -1;
		}
	}

	save_tx_commit(s);

	return 0;
}

int
quest_load(struct quest *q, struct save *s)
{
	assert(q);
	assert(s);

	struct save_stmt stmt;
	struct quest_step *step;

	for (size_t i = 0; i < q->stepsz; ++i) {
		step = &q->steps[i];

		if (save_stmt_init(&stmt, s, (const char *)assets_quest_step_load, "s", step->name))
			return -1;

		if (save_stmt_next(&stmt, "i", &step->percent) < 0) {
			save_stmt_finish(&stmt);
			return -1;
		}

		save_stmt_finish(&stmt);
	}

	return 0;
}