summaryrefslogtreecommitdiff
path: root/src/char/int_mercenary.c
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-02-24 23:40:24 +0300
committerAndrei Karas <akaras@inbox.ru>2016-02-24 23:40:24 +0300
commitff314da1740080a6cf1920f5ab2583ec3c60e3d2 (patch)
tree629046feb2d98a8a8fe0bc9e3674b3952a529ecc /src/char/int_mercenary.c
parent9ee06831aec11a8ffea77f3b5c81465579f993e9 (diff)
parent027dfa3a608bb2986f4c495f20afdeba4e2ad459 (diff)
downloadhercules-ff314da1740080a6cf1920f5ab2583ec3c60e3d2.tar.gz
hercules-ff314da1740080a6cf1920f5ab2583ec3c60e3d2.tar.bz2
hercules-ff314da1740080a6cf1920f5ab2583ec3c60e3d2.tar.xz
hercules-ff314da1740080a6cf1920f5ab2583ec3c60e3d2.zip
Merge pull request #1165 from HerculesWS/const_rfifo
Const correctness (and typecast removal) for [RW]{FIFO,BUF}[PBWLQ] functions
Diffstat (limited to 'src/char/int_mercenary.c')
-rw-r--r--src/char/int_mercenary.c79
1 files changed, 51 insertions, 28 deletions
diff --git a/src/char/int_mercenary.c b/src/char/int_mercenary.c
index 2ed45305c..0bc3c0909 100644
--- a/src/char/int_mercenary.c
+++ b/src/char/int_mercenary.c
@@ -94,32 +94,51 @@ bool inter_mercenary_owner_delete(int char_id)
return true;
}
-bool mapif_mercenary_save(struct s_mercenary* merc)
+/**
+ * Creates a new mercenary with the given data.
+ *
+ * @remark
+ * The mercenary ID is expected to be 0, and will be filled with the newly
+ * assigned ID.
+ *
+ * @param[in,out] merc The new mercenary's data.
+ * @retval false in case of errors.
+ */
+bool mapif_mercenary_create(struct s_mercenary *merc)
{
- bool flag = true;
+ nullpo_retr(false, merc);
+ Assert_retr(false, merc->mercenary_id == 0);
- nullpo_ret(merc);
- if( merc->mercenary_id == 0 )
- { // Create new DB entry
- if( SQL_ERROR == SQL->Query(inter->sql_handle,
+ if (SQL_ERROR == SQL->Query(inter->sql_handle,
"INSERT INTO `%s` (`char_id`,`class`,`hp`,`sp`,`kill_counter`,`life_time`) VALUES ('%d','%d','%d','%d','%u','%u')",
- mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time) )
- {
- Sql_ShowDebug(inter->sql_handle);
- flag = false;
- }
- else
- merc->mercenary_id = (int)SQL->LastInsertId(inter->sql_handle);
+ mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time)) {
+ Sql_ShowDebug(inter->sql_handle);
+ return false;
}
- else if( SQL_ERROR == SQL->Query(inter->sql_handle,
- "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `hp` = '%d', `sp` = '%d', `kill_counter` = '%u', `life_time` = '%u' WHERE `mer_id` = '%d'",
- mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time, merc->mercenary_id) )
- { // Update DB entry
+ merc->mercenary_id = (int)SQL->LastInsertId(inter->sql_handle);
+
+ return true;
+}
+
+/**
+ * Saves an existing mercenary.
+ *
+ * @param merc The mercenary's data.
+ * @retval false in case of errors.
+ */
+bool mapif_mercenary_save(const struct s_mercenary *merc)
+{
+ nullpo_retr(false, merc);
+ Assert_retr(false, merc->mercenary_id > 0);
+
+ if (SQL_ERROR == SQL->Query(inter->sql_handle,
+ "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `hp` = '%d', `sp` = '%d', `kill_counter` = '%u', `life_time` = '%u' WHERE `mer_id` = '%d'",
+ mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time, merc->mercenary_id)) {
Sql_ShowDebug(inter->sql_handle);
- flag = false;
+ return false;
}
- return flag;
+ return true;
}
bool mapif_mercenary_load(int merc_id, int char_id, struct s_mercenary *merc)
@@ -179,10 +198,15 @@ void mapif_mercenary_send(int fd, struct s_mercenary *merc, unsigned char flag)
WFIFOSET(fd,size);
}
-void mapif_parse_mercenary_create(int fd, struct s_mercenary* merc)
+void mapif_parse_mercenary_create(int fd, const struct s_mercenary *merc)
{
- bool result = mapif->mercenary_save(merc);
- mapif->mercenary_send(fd, merc, result);
+ struct s_mercenary merc_;
+ bool result;
+
+ memcpy(&merc_, merc, sizeof(merc_));
+
+ result = mapif->mercenary_create(&merc_);
+ mapif->mercenary_send(fd, &merc_, result);
}
void mapif_parse_mercenary_load(int fd, int merc_id, int char_id)
@@ -236,12 +260,11 @@ int inter_mercenary_parse_frommap(int fd)
{
unsigned short cmd = RFIFOW(fd,0);
- switch( cmd )
- {
- case 0x3070: mapif->parse_mercenary_create(fd, (struct s_mercenary*)RFIFOP(fd,4)); break;
- case 0x3071: mapif->parse_mercenary_load(fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6)); break;
- case 0x3072: mapif->parse_mercenary_delete(fd, (int)RFIFOL(fd,2)); break;
- case 0x3073: mapif->parse_mercenary_save(fd, (struct s_mercenary*)RFIFOP(fd,4)); break;
+ switch (cmd) {
+ case 0x3070: mapif->parse_mercenary_create(fd, RFIFOP(fd,4)); break;
+ case 0x3071: mapif->parse_mercenary_load(fd, RFIFOL(fd,2), RFIFOL(fd,6)); break;
+ case 0x3072: mapif->parse_mercenary_delete(fd, RFIFOL(fd,2)); break;
+ case 0x3073: mapif->parse_mercenary_save(fd, RFIFOP(fd,4)); break;
default:
return 0;
}