summaryrefslogtreecommitdiff
path: root/src/char/int_homun.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/char/int_homun.c')
-rw-r--r--src/char/int_homun.c131
1 files changed, 75 insertions, 56 deletions
diff --git a/src/char/int_homun.c b/src/char/int_homun.c
index d633dc7e0..e5fde2330 100644
--- a/src/char/int_homun.c
+++ b/src/char/int_homun.c
@@ -49,7 +49,7 @@ void inter_homunculus_sql_final(void)
return;
}
-void mapif_homunculus_created(int fd, int account_id, struct s_homunculus *sh, unsigned char flag)
+void mapif_homunculus_created(int fd, int account_id, const struct s_homunculus *sh, unsigned char flag)
{
nullpo_retv(sh);
WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
@@ -97,7 +97,7 @@ void mapif_homunculus_saved(int fd, int account_id, bool flag)
WFIFOSET(fd, 7);
}
-void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, char* name)
+void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, const char *name)
{
nullpo_retv(name);
WFIFOHEAD(fd, NAME_LENGTH+12);
@@ -105,68 +105,83 @@ void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char
WFIFOL(fd, 2) = account_id;
WFIFOL(fd, 6) = char_id;
WFIFOB(fd,10) = flag;
- safestrncpy((char*)WFIFOP(fd,11), name, NAME_LENGTH);
+ safestrncpy(WFIFOP(fd,11), name, NAME_LENGTH);
WFIFOSET(fd, NAME_LENGTH+12);
}
-bool mapif_homunculus_save(struct s_homunculus* hd)
+/**
+ * Creates a new homunculus with the given data.
+ *
+ * @remark
+ * The homunculus ID is expected to be 0, and will be filled with the newly
+ * assigned ID.
+ *
+ * @param[in,out] hd The new homunculus' data.
+ * @retval false in case of errors.
+ */
+bool mapif_homunculus_create(struct s_homunculus *hd)
{
- bool flag = true;
char esc_name[NAME_LENGTH*2+1];
- nullpo_ret(hd);
+ nullpo_retr(false, hd);
+ Assert_retr(false, hd->hom_id == 0);
+
SQL->EscapeStringLen(inter->sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH));
- if( hd->hom_id == 0 )
- {// new homunculus
- if( SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` "
+ if (SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` "
"(`char_id`, `class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`, `rename_flag`, `vaporize`) "
"VALUES ('%d', '%d', '%d', '%s', '%d', '%u', '%u', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
- hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize) )
- {
- Sql_ShowDebug(inter->sql_handle);
- flag = false;
- }
- else
- {
- hd->hom_id = (int)SQL->LastInsertId(inter->sql_handle);
- }
+ hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize)) {
+ Sql_ShowDebug(inter->sql_handle);
+ return false;
}
- else
- {
- if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `char_id`='%d', `class`='%d',`prev_class`='%d',`name`='%s',`level`='%d',`exp`='%u',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'",
+ hd->hom_id = (int)SQL->LastInsertId(inter->sql_handle);
+ return true;
+}
+
+/**
+ * Saves an existing homunculus.
+ *
+ * @param hd The homunculus' data.
+ * @retval false in case of errors.
+ */
+bool mapif_homunculus_save(const struct s_homunculus *hd)
+{
+ bool flag = true;
+ char esc_name[NAME_LENGTH*2+1];
+
+ nullpo_retr(false, hd);
+ Assert_retr(false, hd->hom_id > 0);
+
+ SQL->EscapeStringLen(inter->sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH));
+
+ if (SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `char_id`='%d', `class`='%d',`prev_class`='%d',`name`='%s',`level`='%d',`exp`='%u',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'",
homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
- hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id) )
- {
- Sql_ShowDebug(inter->sql_handle);
+ hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id)) {
+ Sql_ShowDebug(inter->sql_handle);
+ flag = false;
+ } else {
+ int i;
+ SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle);
+
+ if (SQL_ERROR == SQL->StmtPrepare(stmt, "REPLACE INTO `%s` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", skill_homunculus_db, hd->hom_id)) {
+ SqlStmt_ShowDebug(stmt);
flag = false;
- }
- else
- {
- SqlStmt* stmt;
- int i;
-
- stmt = SQL->StmtMalloc(inter->sql_handle);
- if( SQL_ERROR == SQL->StmtPrepare(stmt, "REPLACE INTO `%s` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", skill_homunculus_db, hd->hom_id) )
- SqlStmt_ShowDebug(stmt);
- for( i = 0; i < MAX_HOMUNSKILL; ++i )
- {
- if( hd->hskill[i].id > 0 && hd->hskill[i].lv != 0 )
- {
- SQL->StmtBindParam(stmt, 0, SQLDT_USHORT, &hd->hskill[i].id, 0);
- SQL->StmtBindParam(stmt, 1, SQLDT_USHORT, &hd->hskill[i].lv, 0);
- if( SQL_ERROR == SQL->StmtExecute(stmt) )
- {
+ } else {
+ for (i = 0; i < MAX_HOMUNSKILL; ++i) {
+ if (hd->hskill[i].id > 0 && hd->hskill[i].lv != 0) {
+ SQL->StmtBindParam(stmt, 0, SQLDT_USHORT, (void*)&hd->hskill[i].id, 0); // FIXME: StmtBindParam should take const void
+ SQL->StmtBindParam(stmt, 1, SQLDT_USHORT, (void*)&hd->hskill[i].lv, 0); // FIXME: StmtBindParam should take const void
+ if (SQL_ERROR == SQL->StmtExecute(stmt)) {
SqlStmt_ShowDebug(stmt);
- SQL->StmtFree(stmt);
flag = false;
break;
}
}
}
- SQL->StmtFree(stmt);
}
+ SQL->StmtFree(stmt);
}
return flag;
@@ -264,7 +279,7 @@ bool mapif_homunculus_delete(int homun_id)
return true;
}
-bool mapif_homunculus_rename(char *name)
+bool mapif_homunculus_rename(const char *name)
{
int i;
@@ -287,10 +302,15 @@ bool mapif_homunculus_rename(char *name)
}
-void mapif_parse_homunculus_create(int fd, int len, int account_id, struct s_homunculus* phd)
+void mapif_parse_homunculus_create(int fd, int len, int account_id, const struct s_homunculus *phd)
{
- bool result = mapif->homunculus_save(phd);
- mapif->homunculus_created(fd, account_id, phd, result);
+ struct s_homunculus shd;
+ bool result;
+
+ memcpy(&shd, phd, sizeof(shd));
+
+ result = mapif->homunculus_create(&shd);
+ mapif->homunculus_created(fd, account_id, &shd, result);
}
void mapif_parse_homunculus_delete(int fd, int homun_id)
@@ -306,13 +326,13 @@ void mapif_parse_homunculus_load(int fd, int account_id, int homun_id)
mapif->homunculus_loaded(fd, account_id, ( result ? &hd : NULL ));
}
-void mapif_parse_homunculus_save(int fd, int len, int account_id, struct s_homunculus* phd)
+void mapif_parse_homunculus_save(int fd, int len, int account_id, const struct s_homunculus *phd)
{
bool result = mapif->homunculus_save(phd);
mapif->homunculus_saved(fd, account_id, result);
}
-void mapif_parse_homunculus_rename(int fd, int account_id, int char_id, char* name)
+void mapif_parse_homunculus_rename(int fd, int account_id, int char_id, const char *name)
{
bool result = mapif->homunculus_rename(name);
mapif->homunculus_renamed(fd, account_id, char_id, result, name);
@@ -325,13 +345,12 @@ int inter_homunculus_parse_frommap(int fd)
{
unsigned short cmd = RFIFOW(fd,0);
- switch( cmd )
- {
- case 0x3090: mapif->parse_homunculus_create(fd, (int)RFIFOW(fd,2), (int)RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,8)); break;
- case 0x3091: mapif->parse_homunculus_load (fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6)); break;
- case 0x3092: mapif->parse_homunculus_save (fd, (int)RFIFOW(fd,2), (int)RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,8)); break;
- case 0x3093: mapif->parse_homunculus_delete(fd, (int)RFIFOL(fd,2)); break;
- case 0x3094: mapif->parse_homunculus_rename(fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6), (char*)RFIFOP(fd,10)); break;
+ switch (cmd) {
+ case 0x3090: mapif->parse_homunculus_create(fd, RFIFOW(fd,2), RFIFOL(fd,4), RFIFOP(fd,8)); break;
+ case 0x3091: mapif->parse_homunculus_load (fd, RFIFOL(fd,2), RFIFOL(fd,6)); break;
+ case 0x3092: mapif->parse_homunculus_save (fd, RFIFOW(fd,2), RFIFOL(fd,4), RFIFOP(fd,8)); break;
+ case 0x3093: mapif->parse_homunculus_delete(fd, RFIFOL(fd,2)); break;
+ case 0x3094: mapif->parse_homunculus_rename(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOP(fd,10)); break;
default:
return 0;
}