The task for this part of the lab is to draw out the memory as a result of the a
ID: 3795916 • Letter: T
Question
The task for this part of the lab is to draw out the memory as a result of the avatar and enemy code as performed in the Initialize or start method of a game. Specifically, draw a hypothetical view of memory as a result of these commands:
p = new Player (graphics, pTex, new Vector3 (500, 250, 0), 544, 960);
mg = new M_Gun (graphics, mgunTex, new Vector3 (300, 300, 0), 0, 50);
rifl = new Rifle (graphics, rifleTex, new Vector3 (300, 300, 0), 0, 20);
pistl = new Pistol (graphics, pistolTex, new Vector3 (300, 300, 0), 0, 15);
w_list.Add (mg);
w_list.Add (rifl);
w_list.Add (pistl);
eList.Add (new Random_Enemy (graphics, eTex, new Vector3 (gen.Next (960), gen.Next (544), 0),
new Vector3 ((float)gen.NextDouble (), (float)gen.NextDouble (), 0), 960, 544));
eList.Add (new Neutral_En (graphics, eTex, new Vector3 (gen.Next (960), gen.Next (544), 0),
new Vector3 ((float)gen.NextDouble (), (float)gen.NextDouble (), 0)));
eList.Add (new SmartEn (graphics, eTex, new Vector3 (gen.Next (960), gen.Next (544), 0),
new Vector3 ((float)gen.NextDouble (), (float)gen.NextDouble (), 0)));
Be sure to make clear what each element refers to and the type of each space in memory (similar to what we did in class).
(UPDATE: I think my teacher wants a memory diagram to be created from this set of code)
Explanation / Answer
#include "../common/cbasetypes.h" #include "../common/timer.h" #include "../common/nullpo.h" #include "../common/malloc.h" #include "../common/showmsg.h" #include "../common/ers.h" #include "../common/random.h" #include "../common/socket.h" #include "../common/strlib.h" #include "../common/utils.h" #include "map.h" #include "path.h" #include "pc.h" #include "homunculus.h" #include "mercenary.h" #include "elemental.h" #include "pet.h" #include "party.h" #include "battleground.h" #include "chrif.h" #include #include int attr_fix_table[4][ELE_MAX][ELE_MAX]; struct Battle_Config battle_config; static struct eri *delay_damage_ers; //For battle delay damage structures. /** * Returns the current/list skill used by the bl * @param bl * @return skill_id */ uint16 battle_getcurrentskill(struct block_list *bl) { struct unit_data *ud; if( bl->type == BL_SKILL ) { struct skill_unit *su = (struct skill_unit*)bl; return (su && su->group?su->group->skill_id:0); } ud = unit_bl2ud(bl); return (ud?ud->skill_id:0); } /** * Get random targeting enemy * @param bl * @param ap * @return Found target (1) or not found (0) */ static int battle_gettargeted_sub(struct block_list *bl, va_list ap) { struct block_list **bl_list; struct unit_data *ud; int target_id; int *c; bl_list = va_arg(ap, struct block_list **); c = va_arg(ap, int *); target_id = va_arg(ap, int); if (bl->id == target_id) return 0; if (*c >= 24) return 0; if ( !(ud = unit_bl2ud(bl)) ) return 0; if (ud->target == target_id || ud->skilltarget == target_id) { bl_list[(*c)++] = bl; return 1; } return 0; } /** * Returns list of targets * @param target * @return Target list */ struct block_list* battle_gettargeted(struct block_list *target) { struct block_list *bl_list[24]; int c = 0; nullpo_retr(NULL, target); memset(bl_list, 0, sizeof(bl_list)); map_foreachinrange(battle_gettargeted_sub, target, AREA_SIZE, BL_CHAR, bl_list, &c, target->id); if ( c == 0 ) return NULL; if( c > 24 ) c = 24; return bl_list[rnd()%c]; } /** * Returns the ID of the current targeted character of the passed bl * @param bl * @return Target Unit ID * @author [Skotlex] */ int battle_gettarget(struct block_list* bl) { switch (bl->type) { case BL_PC: return ((struct map_session_data*)bl)->ud.target; case BL_MOB: return ((struct mob_data*)bl)->target_id; case BL_PET: return ((struct pet_data*)bl)->target_id; case BL_HOM: return ((struct homun_data*)bl)->ud.target; case BL_MER: return ((struct mercenary_data*)bl)->ud.target; case BL_ELEM: return ((struct elemental_data*)bl)->ud.target; } return 0; } /** * Get random enemy * @param bl * @param ap * @return Found target (1) or not found (0) */ static int battle_getenemy_sub(struct block_list *bl, va_list ap) { struct block_list **bl_list; struct block_list *target; int *c; bl_list = va_arg(ap, struct block_list **); c = va_arg(ap, int *); target = va_arg(ap, struct block_list *); if (bl->id == target->id) return 0; if (*c >= 24) return 0; if (status_isdead(bl)) return 0; if (battle_check_target(target, bl, BCT_ENEMY) > 0) { bl_list[(*c)++] = bl; return 1; } return 0; } /** * Returns list of enemies within given range * @param target * @param type * @param range * @return Target list * @author [Skotlex] */ struct block_list* battle_getenemy(struct block_list *target, int type, int range) { struct block_list *bl_list[24]; int c = 0; memset(bl_list, 0, sizeof(bl_list)); map_foreachinrange(battle_getenemy_sub, target, range, type, bl_list, &c, target); if ( c == 0 ) return NULL; if( c > 24 ) c = 24; return bl_list[rnd()%c]; } /** * Get random enemy within area * @param bl * @param ap * @return Found target (1) or not found (0) */ static int battle_getenemyarea_sub(struct block_list *bl, va_list ap) { struct block_list **bl_list, *src; int *c, ignore_id; bl_list = va_arg(ap, struct block_list **); c = va_arg(ap, int *); src = va_arg(ap, struct block_list *); ignore_id = va_arg(ap, int); if( bl->id == src->id || bl->id == ignore_id ) return 0; // Ignores Caster and a possible pre-target if( *c >= 23 ) return 0; if( status_isdead(bl) ) return 0; if( battle_check_target(src, bl, BCT_ENEMY) > 0 ) {// Is Enemy!... bl_list[(*c)++] = bl; return 1; } return 0; } /** * Returns list of enemies within an area * @param src * @param x * @param y * @param range * @param type * @param ignore_id * @return Target list */ struct block_list* battle_getenemyarea(struct block_list *src, int x, int y, int range, int type, int ignore_id) { struct block_list *bl_list[24]; int c = 0; memset(bl_list, 0, sizeof(bl_list)); map_foreachinarea(battle_getenemyarea_sub, src->m, x - range, y - range, x + range, y + range, type, bl_list, &c, src, ignore_id); if( c == 0 ) return NULL; if( c >= 24 ) c = 23; return bl_list[rnd()%c]; } /// Damage Delayed Structure struct delay_damage { int src_id; int target_id; int64 damage; int delay; unsigned short distance; uint16 skill_lv; uint16 skill_id; enum damage_lv dmg_lv; unsigned short attack_type; bool additional_effects; enum bl_type src_type; bool isspdamage; }; int battle_delay_damage_sub(int tid, unsigned int tick, int id, intptr_t data) { struct delay_damage *dat = (struct delay_damage *)data; if ( dat ) { struct block_list* src = NULL; struct block_list* target = map_id2bl(dat->target_id); if( !target || status_isdead(target) ) { /* Nothing we can do */ if( dat->src_type == BL_PC && (src = map_id2bl(dat->src_id)) && --((TBL_PC*)src)->delayed_damage == 0 && ((TBL_PC*)src)->state.hold_recalc ) { ((TBL_PC*)src)->state.hold_recalc = 0; status_calc_pc(((TBL_PC*)src), SCO_FORCE); } ers_free(delay_damage_ers, dat); return 0; } src = map_id2bl(dat->src_id); if( src && target->m == src->m && (target->type != BL_PC || ((TBL_PC*)target)->invincible_timer == INVALID_TIMER) && check_distance_bl(src, target, dat->distance) ) //Check to see if you haven't teleported. [Skotlex] { //Deal damage battle_damage(src, target, dat->damage, dat->delay, dat->skill_lv, dat->skill_id, dat->dmg_lv, dat->attack_type, dat->additional_effects, tick, dat->isspdamage); } else if( !src && dat->skill_id == CR_REFLECTSHIELD ) { // it was monster reflected damage, and the monster died, we pass the damage to the character as expected map_freeblock_lock(); status_fix_damage(target, target, dat->damage, dat->delay); map_freeblock_unlock(); } if( src && src->type == BL_PC && --((TBL_PC*)src)->delayed_damage == 0 && ((TBL_PC*)src)->state.hold_recalc ) { ((TBL_PC*)src)->state.hold_recalc = 0; status_calc_pc(((TBL_PC*)src), SCO_FORCE); } } ers_free(delay_damage_ers, dat); return 0; } int battle_delay_damage(unsigned int tick, int amotion, struct block_list *src, struct block_list *target, int attack_type, uint16 skill_id, uint16 skill_lv, int64 damage, enum damage_lv dmg_lv, int ddelay, bool additional_effects, bool isspdamage) { struct delay_damage *dat; struct status_change *sc; struct block_list *d_tbl = NULL; struct block_list *e_tbl = NULL; nullpo_ret(src); nullpo_ret(target); sc = status_get_sc(target); if (sc) { if (sc->data[SC_DEVOTION] && sc->data[SC_DEVOTION]->val1) d_tbl = map_id2bl(sc->data[SC_DEVOTION]->val1); if (sc->data[SC_WATER_SCREEN_OPTION] && sc->data[SC_WATER_SCREEN_OPTION]->val1) e_tbl = map_id2bl(sc->data[SC_WATER_SCREEN_OPTION]->val1); } if( ((d_tbl && check_distance_bl(target, d_tbl, sc->data[SC_DEVOTION]->val3)) || e_tbl) && damage > 0 && skill_id != PA_PRESSURE && skill_id != CR_REFLECTSHIELD ) damage = 0; if ( !battle_config.delay_battle_damage || amotion src_id = src->id; dat->target_id = target->id; dat->skill_id = skill_id; dat->skill_lv = skill_lv; dat->attack_type = attack_type; dat->damage = damage; dat->dmg_lv = dmg_lv; dat->delay = ddelay; dat->distance = distance_bl(src, target) + (battle_config.snap_dodge ? 10 : AREA_SIZE); dat->additional_effects = additional_effects; dat->src_type = src->type; dat->isspdamage = isspdamage; if (src->type != BL_PC && amotion > 1000) amotion = 1000; //Aegis places a damage-delay cap of 1 sec to non player attacks. [Skotlex] if( src->type == BL_PC ) ((TBL_PC*)src)->delayed_damage++; add_timer(tick+amotion, battle_delay_damage_sub, 0, (intptr_t)dat); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.